Skip to content

Commit 9b66956

Browse files
committed
Adds SynchronousObjectManager and AsynchronousObjectManager
1 parent f9680bb commit 9b66956

File tree

5 files changed

+168
-44
lines changed

5 files changed

+168
-44
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package us.mytheria.bloblib.entities;
2+
3+
import org.bukkit.Bukkit;
4+
import org.jetbrains.annotations.NotNull;
5+
import us.mytheria.bloblib.managers.ManagerDirector;
6+
import us.mytheria.bloblib.utilities.HandyDirectory;
7+
8+
import java.io.File;
9+
import java.util.ArrayList;
10+
import java.util.Collection;
11+
import java.util.List;
12+
import java.util.Map;
13+
import java.util.concurrent.CompletableFuture;
14+
import java.util.function.Consumer;
15+
import java.util.function.Function;
16+
import java.util.function.Supplier;
17+
import java.util.logging.Level;
18+
19+
public class AsynchronousObjectManager<T extends BlobObject> extends ObjectManager<T> {
20+
private final Function<File, T> readFunction;
21+
22+
/**
23+
* Constructor for ObjectManager
24+
*
25+
* @param managerDirector The manager director
26+
* @param loadFilesDirectory The directory to load files from
27+
* @param supplier The supplier
28+
* @param fileSupplier The file supplier
29+
* @param parent The ObjectDirector parent
30+
*/
31+
public AsynchronousObjectManager(ManagerDirector managerDirector, File loadFilesDirectory, Supplier<Map<String, T>> supplier, Supplier<Map<String, File>> fileSupplier, ObjectDirector<T> parent, Function<File, T> readFunction) {
32+
super(managerDirector, loadFilesDirectory, supplier, fileSupplier, parent);
33+
this.readFunction = readFunction;
34+
}
35+
36+
public void loadFiles(File path, CompletableFuture<Void> mainFuture) {
37+
if (!path.exists())
38+
path.mkdir();
39+
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), () -> {
40+
HandyDirectory handyDirectory = HandyDirectory.of(path);
41+
Collection<File> files = handyDirectory.listRecursively("yml");
42+
List<CompletableFuture<Void>> futures = new ArrayList<>();
43+
files.forEach(file -> {
44+
CompletableFuture<Void> fileFuture = CompletableFuture.runAsync(() -> {
45+
loadFile(file, mainFuture::completeExceptionally);
46+
});
47+
futures.add(fileFuture);
48+
});
49+
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).thenAccept(v -> mainFuture.complete(null));
50+
});
51+
}
52+
53+
@Override
54+
public void loadFile(@NotNull File file,
55+
Consumer<Throwable> ifFail) {
56+
T blobObject;
57+
try {
58+
blobObject = readFunction.apply(file);
59+
if (blobObject != null) {
60+
if (blobObject.edit() != null)
61+
getParent().setObjectIsEditable(true);
62+
this.addObject(blobObject.getKey(), blobObject, file);
63+
}
64+
} catch (Throwable throwable) {
65+
Bukkit.getLogger().log(Level.SEVERE, throwable.getMessage() + " \n " +
66+
"At: " + file.getPath(), throwable);
67+
ifFail.accept(throwable);
68+
}
69+
}
70+
}

src/main/java/us/mytheria/bloblib/entities/ObjectDirector.java

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@
1313
import us.mytheria.bloblib.itemstack.ItemStackBuilder;
1414
import us.mytheria.bloblib.managers.Manager;
1515
import us.mytheria.bloblib.managers.ManagerDirector;
16-
import us.mytheria.bloblib.utilities.HandyDirectory;
1716
import us.mytheria.bloblib.utilities.ItemStackUtil;
1817

1918
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;
2223
import java.util.concurrent.ConcurrentHashMap;
2324
import java.util.function.Consumer;
2425
import java.util.function.Function;
25-
import java.util.logging.Level;
2626

2727
public class ObjectDirector<T extends BlobObject> extends Manager
2828
implements Listener, RunnableReloadable {
@@ -36,12 +36,21 @@ public class ObjectDirector<T extends BlobObject> extends Manager
3636
public ObjectDirector(ManagerDirector managerDirector,
3737
ObjectDirectorData objectDirectorData,
3838
Function<File, T> readFunction) {
39-
this(managerDirector, objectDirectorData, readFunction, true);
39+
this(managerDirector, objectDirectorData, readFunction, true, true);
4040
}
4141

4242
public ObjectDirector(ManagerDirector managerDirector,
4343
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) {
4554
super(managerDirector);
4655
this.commandDirector = new CommandDirector(managerDirector.getPlugin(), objectDirectorData.objectName());
4756
objectIsEditable = false;
@@ -57,43 +66,10 @@ public ObjectDirector(ManagerDirector managerDirector,
5766
Bukkit.getLogger().info("The loadFilesPathKey is not valid");
5867
throw new IllegalArgumentException("The loadFilesPathKey is not valid");
5968
}
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);
9773
Bukkit.getPluginManager().registerEvents(this, managerDirector.getPlugin());
9874
objectName = objectDirectorData.objectName();
9975
setDefaultCommands().setDefaultTabCompleter();
@@ -336,6 +312,10 @@ public boolean objectIsEditable() {
336312
return objectIsEditable;
337313
}
338314

315+
public void setObjectIsEditable(boolean objectIsEditable) {
316+
this.objectIsEditable = objectIsEditable;
317+
}
318+
339319
@SuppressWarnings("unchecked")
340320
public void editObject(Player player, String key) {
341321
if (!objectIsEditable) {

src/main/java/us/mytheria/bloblib/entities/ObjectManager.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ public void whenFilesLoad(Consumer<ObjectManager<T>> consumer) {
241241
});
242242
}
243243

244+
public ObjectDirector<T> getParent() {
245+
return parent;
246+
}
247+
244248
public List<String> get() {
245249
return new ArrayList<>(objects.keySet());
246250
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package us.mytheria.bloblib.entities;
2+
3+
import org.bukkit.Bukkit;
4+
import org.jetbrains.annotations.NotNull;
5+
import us.mytheria.bloblib.managers.ManagerDirector;
6+
import us.mytheria.bloblib.utilities.HandyDirectory;
7+
8+
import java.io.File;
9+
import java.util.Collection;
10+
import java.util.Map;
11+
import java.util.concurrent.CompletableFuture;
12+
import java.util.function.Consumer;
13+
import java.util.function.Function;
14+
import java.util.function.Supplier;
15+
import java.util.logging.Level;
16+
17+
public class SynchronousObjectManager<T extends BlobObject> extends ObjectManager<T> {
18+
private final Function<File, T> readFunction;
19+
20+
/**
21+
* Constructor for ObjectManager
22+
*
23+
* @param managerDirector The manager director
24+
* @param loadFilesDirectory The directory to load files from
25+
* @param supplier The supplier
26+
* @param fileSupplier The file supplier
27+
* @param parent The ObjectDirector parent
28+
*/
29+
public SynchronousObjectManager(ManagerDirector managerDirector, File loadFilesDirectory, Supplier<Map<String, T>> supplier, Supplier<Map<String, File>> fileSupplier, ObjectDirector<T> parent, Function<File, T> readFunction) {
30+
super(managerDirector, loadFilesDirectory, supplier, fileSupplier, parent);
31+
this.readFunction = readFunction;
32+
reload();
33+
}
34+
35+
@Override
36+
public void reload() {
37+
if (readFunction == null)
38+
return;
39+
super.reload();
40+
}
41+
42+
public void loadFiles(File path, CompletableFuture<Void> mainFuture) {
43+
if (!path.exists())
44+
path.mkdir();
45+
HandyDirectory handyDirectory = HandyDirectory.of(path);
46+
Collection<File> files = handyDirectory.listRecursively("yml");
47+
files.forEach(file -> {
48+
loadFile(file, mainFuture::completeExceptionally);
49+
});
50+
mainFuture.complete(null);
51+
}
52+
53+
@Override
54+
public void loadFile(@NotNull File file,
55+
Consumer<Throwable> ifFail) {
56+
T blobObject;
57+
try {
58+
blobObject = readFunction.apply(file);
59+
if (blobObject != null) {
60+
if (blobObject.edit() != null)
61+
getParent().setObjectIsEditable(true);
62+
this.addObject(blobObject.getKey(), blobObject, file);
63+
}
64+
} catch (Throwable throwable) {
65+
Bukkit.getLogger().log(Level.SEVERE, throwable.getMessage() + " \n " +
66+
"At: " + file.getPath(), throwable);
67+
ifFail.accept(throwable);
68+
}
69+
}
70+
}

src/main/java/us/mytheria/bloblib/entities/currency/EconomyFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class EconomyFactory {
2222
public static ObjectDirector<Currency> CURRENCY_DIRECTOR(ManagerDirector managerDirector,
2323
String objectName) {
2424
ObjectDirector<Currency> director = new ObjectDirector<>(managerDirector, ObjectDirectorData.simple(managerDirector.getRealFileManager(),
25-
objectName), file -> Currency.fromFile(file, managerDirector));
25+
objectName), file -> Currency.fromFile(file, managerDirector), true, false);
2626
director.getBuilderManager().setBuilderBiFunction(
2727
CurrencyBuilder::build);
2828
return director;

0 commit comments

Comments
 (0)