diff --git a/local-pom.xml b/local-pom.xml index 3af471b..258fad7 100644 --- a/local-pom.xml +++ b/local-pom.xml @@ -5,7 +5,7 @@ us.mytheria BlobLib - 1.698.13 + 1.698.15 pom.xml bloblib diff --git a/pom.xml b/pom.xml index eac467b..ab78a19 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 us.mytheria BlobLib - 1.698.14 + 1.698.15 pom diff --git a/src/main/java/us/mytheria/bloblib/entities/ObjectDirector.java b/src/main/java/us/mytheria/bloblib/entities/ObjectDirector.java index 22e0810..e83d413 100644 --- a/src/main/java/us/mytheria/bloblib/entities/ObjectDirector.java +++ b/src/main/java/us/mytheria/bloblib/entities/ObjectDirector.java @@ -24,7 +24,8 @@ import java.util.function.Function; import java.util.logging.Level; -public class ObjectDirector extends Manager implements Listener { +public class ObjectDirector extends Manager + implements Listener, RunnableReloadable { private final ObjectBuilderManager objectBuilderManager; private final ObjectManager objectManager; private final CommandDirector commandDirector; @@ -365,4 +366,14 @@ public ObjectBuilder getOrDefaultBuilder(UUID uuid) { "Implement it in constructor."); return getObjectBuilderManager().getOrDefault(uuid); } + + @Override + public boolean isReloading() { + return getObjectManager().isReloading(); + } + + @Override + public void whenReloaded(Runnable runnable) { + getObjectManager().whenReloaded(runnable); + } } diff --git a/src/main/java/us/mytheria/bloblib/entities/ObjectManager.java b/src/main/java/us/mytheria/bloblib/entities/ObjectManager.java index 35001f8..6913575 100644 --- a/src/main/java/us/mytheria/bloblib/entities/ObjectManager.java +++ b/src/main/java/us/mytheria/bloblib/entities/ObjectManager.java @@ -22,12 +22,14 @@ * random access memory and tracked by a key. */ public abstract class ObjectManager extends Manager - implements CommandTarget { + implements CommandTarget, + RunnableReloadable { private final File loadFilesDirectory; private final Supplier> objectsSupplier; private final Supplier> fileSupplier; private CompletableFuture loadFiles; private final ObjectDirector parent; + private final List runnableReloadables; /** * The objects that are loaded in random access memory. * Should be initialized in loadInConstructor() method. @@ -35,6 +37,8 @@ public abstract class ObjectManager extends Manager private Map objects; private Map objectFiles; + private boolean isReloading; + /** * Constructor for ObjectManager * @@ -46,6 +50,7 @@ public ObjectManager(ManagerDirector managerDirector, File loadFilesDirectory, Supplier> fileSupplier, ObjectDirector parent) { super(managerDirector); + this.runnableReloadables = new ArrayList<>(); this.loadFilesDirectory = loadFilesDirectory; this.objectsSupplier = supplier; this.fileSupplier = fileSupplier; @@ -219,6 +224,10 @@ public CompletableFuture getLoadFiles() { public void updateLoadFiles(File path) { this.loadFiles = new CompletableFuture<>(); loadFiles(path, loadFiles); + this.loadFiles.thenRun(() -> { + isReloading = false; + runnableReloadables.forEach(Runnable::run); + }); } public void whenFilesLoad(Consumer> consumer) { @@ -240,4 +249,13 @@ public List get() { public T parse(String key) { return getObject(key); } + + @Override + public boolean isReloading() { + return isReloading; + } + + public void whenReloaded(Runnable runnable) { + runnableReloadables.add(runnable); + } } \ No newline at end of file diff --git a/src/main/java/us/mytheria/bloblib/entities/Reloadable.java b/src/main/java/us/mytheria/bloblib/entities/Reloadable.java new file mode 100644 index 0000000..781c7ff --- /dev/null +++ b/src/main/java/us/mytheria/bloblib/entities/Reloadable.java @@ -0,0 +1,16 @@ +package us.mytheria.bloblib.entities; + +public interface Reloadable { + + /** + * Returns whether the Reloadable is reloading. + * + * @return {@code true} if the Reloadable is reloading, {@code false} otherwise. + */ + boolean isReloading(); + + /** + * Reloads the Reloadable. + */ + void reload(); +} diff --git a/src/main/java/us/mytheria/bloblib/entities/RunnableReloadable.java b/src/main/java/us/mytheria/bloblib/entities/RunnableReloadable.java new file mode 100644 index 0000000..4654037 --- /dev/null +++ b/src/main/java/us/mytheria/bloblib/entities/RunnableReloadable.java @@ -0,0 +1,11 @@ +package us.mytheria.bloblib.entities; + +public interface RunnableReloadable extends Reloadable { + + /** + * Adds a Runnable to be run when the RunnableReloadable is reloaded. + * + * @param runnable The Runnable to run when the RunnableReloadable is reloaded. + */ + void whenReloaded(Runnable runnable); +} diff --git a/src/main/java/us/mytheria/bloblib/entities/proxy/IManagerDirectorProxy.java b/src/main/java/us/mytheria/bloblib/entities/proxy/IManagerDirectorProxy.java index 6ceff64..f6919ee 100644 --- a/src/main/java/us/mytheria/bloblib/entities/proxy/IManagerDirectorProxy.java +++ b/src/main/java/us/mytheria/bloblib/entities/proxy/IManagerDirectorProxy.java @@ -22,6 +22,10 @@ public void reloadAll() { managerDirector.reloadAll(); } + public boolean isReloading() { + return managerDirector.isReloading(); + } + public IFileManager getFileManager() { return managerDirector.getFileManager(); } diff --git a/src/main/java/us/mytheria/bloblib/managers/BlobPlugin.java b/src/main/java/us/mytheria/bloblib/managers/BlobPlugin.java index ce4d904..51457c6 100644 --- a/src/main/java/us/mytheria/bloblib/managers/BlobPlugin.java +++ b/src/main/java/us/mytheria/bloblib/managers/BlobPlugin.java @@ -1,5 +1,6 @@ package us.mytheria.bloblib.managers; +import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -33,6 +34,11 @@ public void onDisable() { * NEVER call this method yourself, it SHOULD be called by BlobLib. */ protected void blobLibReload() { + IManagerDirector director = getManagerDirector(); + if (director.isReloading()) { + Bukkit.getLogger().severe("BlobLib tried to reload " + getName() + " while it was reloading!"); + return; + } /* In case of being loaded, it will unload them backwards since some assets can depend @@ -41,7 +47,7 @@ protected void blobLibReload() { PluginManager.unloadAssets(this); //Loads assets PluginManager.loadAssets(this); - getManagerDirector().reloadAll(); + director.reloadAll(); } /** diff --git a/src/main/java/us/mytheria/bloblib/managers/IManagerDirector.java b/src/main/java/us/mytheria/bloblib/managers/IManagerDirector.java index 20f8977..e7d5065 100644 --- a/src/main/java/us/mytheria/bloblib/managers/IManagerDirector.java +++ b/src/main/java/us/mytheria/bloblib/managers/IManagerDirector.java @@ -28,4 +28,11 @@ public interface IManagerDirector { * @return the file manager. */ IFileManager getFileManager(); + + /** + * Whether the manager director is reloading. + * + * @return {@code true} if reloading, {@code false} otherwise. + */ + boolean isReloading(); } diff --git a/src/main/java/us/mytheria/bloblib/managers/Manager.java b/src/main/java/us/mytheria/bloblib/managers/Manager.java index fde3e5c..54bd7fb 100644 --- a/src/main/java/us/mytheria/bloblib/managers/Manager.java +++ b/src/main/java/us/mytheria/bloblib/managers/Manager.java @@ -1,6 +1,8 @@ package us.mytheria.bloblib.managers; -public abstract class Manager { +import us.mytheria.bloblib.entities.Reloadable; + +public abstract class Manager implements Reloadable { private final ManagerDirector managerDirector; public Manager(ManagerDirector managerDirector) { @@ -19,6 +21,15 @@ public void unload() { public void postWorld() { } + /** + * Can be overridden to return whether the manager is reloading. + * + * @return {@code true} if the manager is reloading, {@code false} otherwise. + */ + public boolean isReloading() { + return false; + } + public ManagerDirector getManagerDirector() { return managerDirector; } diff --git a/src/main/java/us/mytheria/bloblib/managers/ManagerDirector.java b/src/main/java/us/mytheria/bloblib/managers/ManagerDirector.java index 8e0aa16..dfbdcf7 100644 --- a/src/main/java/us/mytheria/bloblib/managers/ManagerDirector.java +++ b/src/main/java/us/mytheria/bloblib/managers/ManagerDirector.java @@ -967,6 +967,11 @@ public boolean loadBlobLibExpansion(@NotNull File expansion) { return true; } + // a ManagerDirector can override this method and do their own logic + public boolean isReloading() { + return false; + } + protected Set> getManagerEntry() { return managers.entrySet(); }