13
13
import us .mytheria .bloblib .itemstack .ItemStackBuilder ;
14
14
import us .mytheria .bloblib .managers .Manager ;
15
15
import us .mytheria .bloblib .managers .ManagerDirector ;
16
- import us .mytheria .bloblib .utilities .HandyDirectory ;
17
16
import us .mytheria .bloblib .utilities .ItemStackUtil ;
18
17
19
18
import java .io .File ;
20
- import java .util .*;
21
- import java .util .concurrent .CompletableFuture ;
19
+ import java .util .ArrayList ;
20
+ import java .util .List ;
21
+ import java .util .Optional ;
22
+ import java .util .UUID ;
22
23
import java .util .concurrent .ConcurrentHashMap ;
23
24
import java .util .function .Consumer ;
24
25
import java .util .function .Function ;
25
- import java .util .logging .Level ;
26
26
27
27
public class ObjectDirector <T extends BlobObject > extends Manager
28
28
implements Listener , RunnableReloadable {
@@ -36,12 +36,21 @@ public class ObjectDirector<T extends BlobObject> extends Manager
36
36
public ObjectDirector (ManagerDirector managerDirector ,
37
37
ObjectDirectorData objectDirectorData ,
38
38
Function <File , T > readFunction ) {
39
- this (managerDirector , objectDirectorData , readFunction , true );
39
+ this (managerDirector , objectDirectorData , readFunction , true , true );
40
40
}
41
41
42
42
public ObjectDirector (ManagerDirector managerDirector ,
43
43
ObjectDirectorData objectDirectorData ,
44
- Function <File , T > readFunction , boolean hasObjectBuilderManager ) {
44
+ Function <File , T > readFunction ,
45
+ boolean hasObjectBuilderManager ) {
46
+ this (managerDirector , objectDirectorData , readFunction , hasObjectBuilderManager , true );
47
+ }
48
+
49
+ public ObjectDirector (ManagerDirector managerDirector ,
50
+ ObjectDirectorData objectDirectorData ,
51
+ Function <File , T > readFunction ,
52
+ boolean hasObjectBuilderManager ,
53
+ boolean isAsynchronous ) {
45
54
super (managerDirector );
46
55
this .commandDirector = new CommandDirector (managerDirector .getPlugin (), objectDirectorData .objectName ());
47
56
objectIsEditable = false ;
@@ -57,43 +66,10 @@ public ObjectDirector(ManagerDirector managerDirector,
57
66
Bukkit .getLogger ().info ("The loadFilesPathKey is not valid" );
58
67
throw new IllegalArgumentException ("The loadFilesPathKey is not valid" );
59
68
}
60
- this .objectManager = new ObjectManager <>(managerDirector , loadFilesDirectory .get (),
61
- ConcurrentHashMap ::new , ConcurrentHashMap ::new , this ) {
62
- public void loadFiles (File path , CompletableFuture <Void > mainFuture ) {
63
- if (!path .exists ())
64
- path .mkdir ();
65
- Bukkit .getScheduler ().runTaskAsynchronously (getPlugin (), () -> {
66
- HandyDirectory handyDirectory = HandyDirectory .of (path );
67
- Collection <File > files = handyDirectory .listRecursively ("yml" );
68
- List <CompletableFuture <Void >> futures = new ArrayList <>();
69
- files .forEach (file -> {
70
- CompletableFuture <Void > fileFuture = CompletableFuture .runAsync (() -> {
71
- loadFile (file , mainFuture ::completeExceptionally );
72
- });
73
- futures .add (fileFuture );
74
- });
75
- CompletableFuture .allOf (futures .toArray (new CompletableFuture [0 ])).thenAccept (v -> mainFuture .complete (null ));
76
- });
77
- }
78
-
79
- @ Override
80
- public void loadFile (@ NotNull File file ,
81
- Consumer <Throwable > ifFail ) {
82
- T blobObject ;
83
- try {
84
- blobObject = readFunction .apply (file );
85
- if (blobObject != null ) {
86
- if (blobObject .edit () != null )
87
- objectIsEditable = true ;
88
- this .addObject (blobObject .getKey (), blobObject , file );
89
- }
90
- } catch (Throwable throwable ) {
91
- Bukkit .getLogger ().log (Level .SEVERE , throwable .getMessage () + " \n " +
92
- "At: " + file .getPath (), throwable );
93
- ifFail .accept (throwable );
94
- }
95
- }
96
- };
69
+ this .objectManager = isAsynchronous ? new AsynchronousObjectManager <>(managerDirector , loadFilesDirectory .get (),
70
+ ConcurrentHashMap ::new , ConcurrentHashMap ::new , this , readFunction ) :
71
+ new SynchronousObjectManager <>(managerDirector , loadFilesDirectory .get (),
72
+ ConcurrentHashMap ::new , ConcurrentHashMap ::new , this , readFunction );
97
73
Bukkit .getPluginManager ().registerEvents (this , managerDirector .getPlugin ());
98
74
objectName = objectDirectorData .objectName ();
99
75
setDefaultCommands ().setDefaultTabCompleter ();
@@ -336,6 +312,10 @@ public boolean objectIsEditable() {
336
312
return objectIsEditable ;
337
313
}
338
314
315
+ public void setObjectIsEditable (boolean objectIsEditable ) {
316
+ this .objectIsEditable = objectIsEditable ;
317
+ }
318
+
339
319
@ SuppressWarnings ("unchecked" )
340
320
public void editObject (Player player , String key ) {
341
321
if (!objectIsEditable ) {
0 commit comments