Skip to content

Commit

Permalink
Manager implements Reloadable
Browse files Browse the repository at this point in the history
ObjectManager implements RunnableReloadable
ObjectDirector implements RunnableReloadable
Added IManagerDirector#isReloading
BlobPlugin#blobLibReload won't proceed if BlobPlugin's IManagerDirector is reloading
  • Loading branch information
anjoismysign committed Apr 20, 2024
1 parent df0aa72 commit 231775c
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 6 deletions.
2 changes: 1 addition & 1 deletion local-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.698.13</version>
<version>1.698.15</version>
<relativePath>pom.xml</relativePath>
</parent>
<artifactId>bloblib</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.698.14</version>
<version>1.698.15</version>
<packaging>pom</packaging>

<properties>
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/us/mytheria/bloblib/entities/ObjectDirector.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
import java.util.function.Function;
import java.util.logging.Level;

public class ObjectDirector<T extends BlobObject> extends Manager implements Listener {
public class ObjectDirector<T extends BlobObject> extends Manager
implements Listener, RunnableReloadable {
private final ObjectBuilderManager<T> objectBuilderManager;
private final ObjectManager<T> objectManager;
private final CommandDirector commandDirector;
Expand Down Expand Up @@ -365,4 +366,14 @@ public ObjectBuilder<T> 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);
}
}
20 changes: 19 additions & 1 deletion src/main/java/us/mytheria/bloblib/entities/ObjectManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,23 @@
* random access memory and tracked by a key.
*/
public abstract class ObjectManager<T extends BlobObject> extends Manager
implements CommandTarget<T> {
implements CommandTarget<T>,
RunnableReloadable {
private final File loadFilesDirectory;
private final Supplier<Map<String, T>> objectsSupplier;
private final Supplier<Map<String, File>> fileSupplier;
private CompletableFuture<Void> loadFiles;
private final ObjectDirector<T> parent;
private final List<Runnable> runnableReloadables;
/**
* The objects that are loaded in random access memory.
* Should be initialized in loadInConstructor() method.
*/
private Map<String, T> objects;
private Map<String, File> objectFiles;

private boolean isReloading;

/**
* Constructor for ObjectManager
*
Expand All @@ -46,6 +50,7 @@ public ObjectManager(ManagerDirector managerDirector, File loadFilesDirectory,
Supplier<Map<String, File>> fileSupplier,
ObjectDirector<T> parent) {
super(managerDirector);
this.runnableReloadables = new ArrayList<>();
this.loadFilesDirectory = loadFilesDirectory;
this.objectsSupplier = supplier;
this.fileSupplier = fileSupplier;
Expand Down Expand Up @@ -219,6 +224,10 @@ public CompletableFuture<Void> 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<ObjectManager<T>> consumer) {
Expand All @@ -240,4 +249,13 @@ public List<String> get() {
public T parse(String key) {
return getObject(key);
}

@Override
public boolean isReloading() {
return isReloading;
}

public void whenReloaded(Runnable runnable) {
runnableReloadables.add(runnable);
}
}
16 changes: 16 additions & 0 deletions src/main/java/us/mytheria/bloblib/entities/Reloadable.java
Original file line number Diff line number Diff line change
@@ -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();
}
11 changes: 11 additions & 0 deletions src/main/java/us/mytheria/bloblib/entities/RunnableReloadable.java
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public void reloadAll() {
managerDirector.reloadAll();
}

public boolean isReloading() {
return managerDirector.isReloading();
}

public IFileManager getFileManager() {
return managerDirector.getFileManager();
}
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/us/mytheria/bloblib/managers/BlobPlugin.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -41,7 +47,7 @@ protected void blobLibReload() {
PluginManager.unloadAssets(this);
//Loads assets
PluginManager.loadAssets(this);
getManagerDirector().reloadAll();
director.reloadAll();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
13 changes: 12 additions & 1 deletion src/main/java/us/mytheria/bloblib/managers/Manager.java
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Map.Entry<String, Manager>> getManagerEntry() {
return managers.entrySet();
}
Expand Down

0 comments on commit 231775c

Please sign in to comment.