Skip to content

Commit

Permalink
Adds SynchronousObjectManager and AsynchronousObjectManager
Browse files Browse the repository at this point in the history
  • Loading branch information
anjoismysign committed Sep 12, 2024
1 parent f9680bb commit 9b66956
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package us.mytheria.bloblib.entities;

import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;
import us.mytheria.bloblib.managers.ManagerDirector;
import us.mytheria.bloblib.utilities.HandyDirectory;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.logging.Level;

public class AsynchronousObjectManager<T extends BlobObject> extends ObjectManager<T> {
private final Function<File, T> readFunction;

/**
* Constructor for ObjectManager
*
* @param managerDirector The manager director
* @param loadFilesDirectory The directory to load files from
* @param supplier The supplier
* @param fileSupplier The file supplier
* @param parent The ObjectDirector parent
*/
public AsynchronousObjectManager(ManagerDirector managerDirector, File loadFilesDirectory, Supplier<Map<String, T>> supplier, Supplier<Map<String, File>> fileSupplier, ObjectDirector<T> parent, Function<File, T> readFunction) {
super(managerDirector, loadFilesDirectory, supplier, fileSupplier, parent);
this.readFunction = readFunction;
}

public void loadFiles(File path, CompletableFuture<Void> mainFuture) {
if (!path.exists())
path.mkdir();
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), () -> {
HandyDirectory handyDirectory = HandyDirectory.of(path);
Collection<File> files = handyDirectory.listRecursively("yml");
List<CompletableFuture<Void>> futures = new ArrayList<>();
files.forEach(file -> {
CompletableFuture<Void> fileFuture = CompletableFuture.runAsync(() -> {
loadFile(file, mainFuture::completeExceptionally);
});
futures.add(fileFuture);
});
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).thenAccept(v -> mainFuture.complete(null));
});
}

@Override
public void loadFile(@NotNull File file,
Consumer<Throwable> ifFail) {
T blobObject;
try {
blobObject = readFunction.apply(file);
if (blobObject != null) {
if (blobObject.edit() != null)
getParent().setObjectIsEditable(true);
this.addObject(blobObject.getKey(), blobObject, file);
}
} catch (Throwable throwable) {
Bukkit.getLogger().log(Level.SEVERE, throwable.getMessage() + " \n " +
"At: " + file.getPath(), throwable);
ifFail.accept(throwable);
}
}
}
66 changes: 23 additions & 43 deletions src/main/java/us/mytheria/bloblib/entities/ObjectDirector.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
import us.mytheria.bloblib.itemstack.ItemStackBuilder;
import us.mytheria.bloblib.managers.Manager;
import us.mytheria.bloblib.managers.ManagerDirector;
import us.mytheria.bloblib.utilities.HandyDirectory;
import us.mytheria.bloblib.utilities.ItemStackUtil;

import java.io.File;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.logging.Level;

public class ObjectDirector<T extends BlobObject> extends Manager
implements Listener, RunnableReloadable {
Expand All @@ -36,12 +36,21 @@ public class ObjectDirector<T extends BlobObject> extends Manager
public ObjectDirector(ManagerDirector managerDirector,
ObjectDirectorData objectDirectorData,
Function<File, T> readFunction) {
this(managerDirector, objectDirectorData, readFunction, true);
this(managerDirector, objectDirectorData, readFunction, true, true);
}

public ObjectDirector(ManagerDirector managerDirector,
ObjectDirectorData objectDirectorData,
Function<File, T> readFunction, boolean hasObjectBuilderManager) {
Function<File, T> readFunction,
boolean hasObjectBuilderManager) {
this(managerDirector, objectDirectorData, readFunction, hasObjectBuilderManager, true);
}

public ObjectDirector(ManagerDirector managerDirector,
ObjectDirectorData objectDirectorData,
Function<File, T> readFunction,
boolean hasObjectBuilderManager,
boolean isAsynchronous) {
super(managerDirector);
this.commandDirector = new CommandDirector(managerDirector.getPlugin(), objectDirectorData.objectName());
objectIsEditable = false;
Expand All @@ -57,43 +66,10 @@ public ObjectDirector(ManagerDirector managerDirector,
Bukkit.getLogger().info("The loadFilesPathKey is not valid");
throw new IllegalArgumentException("The loadFilesPathKey is not valid");
}
this.objectManager = new ObjectManager<>(managerDirector, loadFilesDirectory.get(),
ConcurrentHashMap::new, ConcurrentHashMap::new, this) {
public void loadFiles(File path, CompletableFuture<Void> mainFuture) {
if (!path.exists())
path.mkdir();
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), () -> {
HandyDirectory handyDirectory = HandyDirectory.of(path);
Collection<File> files = handyDirectory.listRecursively("yml");
List<CompletableFuture<Void>> futures = new ArrayList<>();
files.forEach(file -> {
CompletableFuture<Void> fileFuture = CompletableFuture.runAsync(() -> {
loadFile(file, mainFuture::completeExceptionally);
});
futures.add(fileFuture);
});
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).thenAccept(v -> mainFuture.complete(null));
});
}

@Override
public void loadFile(@NotNull File file,
Consumer<Throwable> ifFail) {
T blobObject;
try {
blobObject = readFunction.apply(file);
if (blobObject != null) {
if (blobObject.edit() != null)
objectIsEditable = true;
this.addObject(blobObject.getKey(), blobObject, file);
}
} catch (Throwable throwable) {
Bukkit.getLogger().log(Level.SEVERE, throwable.getMessage() + " \n " +
"At: " + file.getPath(), throwable);
ifFail.accept(throwable);
}
}
};
this.objectManager = isAsynchronous ? new AsynchronousObjectManager<>(managerDirector, loadFilesDirectory.get(),
ConcurrentHashMap::new, ConcurrentHashMap::new, this, readFunction) :
new SynchronousObjectManager<>(managerDirector, loadFilesDirectory.get(),
ConcurrentHashMap::new, ConcurrentHashMap::new, this, readFunction);
Bukkit.getPluginManager().registerEvents(this, managerDirector.getPlugin());
objectName = objectDirectorData.objectName();
setDefaultCommands().setDefaultTabCompleter();
Expand Down Expand Up @@ -336,6 +312,10 @@ public boolean objectIsEditable() {
return objectIsEditable;
}

public void setObjectIsEditable(boolean objectIsEditable) {
this.objectIsEditable = objectIsEditable;
}

@SuppressWarnings("unchecked")
public void editObject(Player player, String key) {
if (!objectIsEditable) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/us/mytheria/bloblib/entities/ObjectManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ public void whenFilesLoad(Consumer<ObjectManager<T>> consumer) {
});
}

public ObjectDirector<T> getParent() {
return parent;
}

public List<String> get() {
return new ArrayList<>(objects.keySet());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package us.mytheria.bloblib.entities;

import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;
import us.mytheria.bloblib.managers.ManagerDirector;
import us.mytheria.bloblib.utilities.HandyDirectory;

import java.io.File;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.logging.Level;

public class SynchronousObjectManager<T extends BlobObject> extends ObjectManager<T> {
private final Function<File, T> readFunction;

/**
* Constructor for ObjectManager
*
* @param managerDirector The manager director
* @param loadFilesDirectory The directory to load files from
* @param supplier The supplier
* @param fileSupplier The file supplier
* @param parent The ObjectDirector parent
*/
public SynchronousObjectManager(ManagerDirector managerDirector, File loadFilesDirectory, Supplier<Map<String, T>> supplier, Supplier<Map<String, File>> fileSupplier, ObjectDirector<T> parent, Function<File, T> readFunction) {
super(managerDirector, loadFilesDirectory, supplier, fileSupplier, parent);
this.readFunction = readFunction;
reload();
}

@Override
public void reload() {
if (readFunction == null)
return;
super.reload();
}

public void loadFiles(File path, CompletableFuture<Void> mainFuture) {
if (!path.exists())
path.mkdir();
HandyDirectory handyDirectory = HandyDirectory.of(path);
Collection<File> files = handyDirectory.listRecursively("yml");
files.forEach(file -> {
loadFile(file, mainFuture::completeExceptionally);
});
mainFuture.complete(null);
}

@Override
public void loadFile(@NotNull File file,
Consumer<Throwable> ifFail) {
T blobObject;
try {
blobObject = readFunction.apply(file);
if (blobObject != null) {
if (blobObject.edit() != null)
getParent().setObjectIsEditable(true);
this.addObject(blobObject.getKey(), blobObject, file);
}
} catch (Throwable throwable) {
Bukkit.getLogger().log(Level.SEVERE, throwable.getMessage() + " \n " +
"At: " + file.getPath(), throwable);
ifFail.accept(throwable);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class EconomyFactory {
public static ObjectDirector<Currency> CURRENCY_DIRECTOR(ManagerDirector managerDirector,
String objectName) {
ObjectDirector<Currency> director = new ObjectDirector<>(managerDirector, ObjectDirectorData.simple(managerDirector.getRealFileManager(),
objectName), file -> Currency.fromFile(file, managerDirector));
objectName), file -> Currency.fromFile(file, managerDirector), true, false);
director.getBuilderManager().setBuilderBiFunction(
CurrencyBuilder::build);
return director;
Expand Down

0 comments on commit 9b66956

Please sign in to comment.