WIP - reloads addons

This commit is contained in:
tastybento 2019-05-07 07:47:38 -07:00
parent 91034f956e
commit 9ba62e1711
3 changed files with 126 additions and 70 deletions

View File

@ -189,12 +189,12 @@ public abstract class Addon {
} }
/** /**
* Register a listener for this addon * Register a listener for this addon. This MUST be used in order for the addon to be reloadable
* *
* @param listener - listener * @param listener - listener
*/ */
public void registerListener(Listener listener) { public void registerListener(Listener listener) {
Bukkit.getPluginManager().registerEvents(listener, BentoBox.getInstance()); BentoBox.getInstance().getAddonsManager().registerListener(this, listener);
} }
/** /**

View File

@ -29,6 +29,7 @@ public class BentoBoxReloadCommand extends ConfirmableCommand {
@Override @Override
public boolean execute(User user, String label, List<String> args) { public boolean execute(User user, String label, List<String> args) {
if (args.isEmpty()) {
this.askConfirmation(user, () -> { this.askConfirmation(user, () -> {
// Reload settings // Reload settings
getPlugin().loadSettings(); getPlugin().loadSettings();
@ -42,6 +43,15 @@ public class BentoBoxReloadCommand extends ConfirmableCommand {
getPlugin().getLocalesManager().reloadLanguages(); getPlugin().getLocalesManager().reloadLanguages();
user.sendMessage("commands.bentobox.reload.locales-reloaded"); user.sendMessage("commands.bentobox.reload.locales-reloaded");
}); });
} else if (args.size() == 1) {
if (!getPlugin().getAddonsManager().getAddonByName(args.get(0)).isPresent()) {
user.sendRawMessage("Unknown addon");
return false;
}
this.askConfirmation(user, () -> getPlugin().getAddonsManager().getAddonByName(args.get(0)).ifPresent(getPlugin().getAddonsManager()::reloadAddon));
} else {
showHelp(this, user);
}
return true; return true;
} }
} }

View File

@ -3,6 +3,8 @@ package world.bentobox.bentobox.managers;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.generator.ChunkGenerator; import org.bukkit.generator.ChunkGenerator;
import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
@ -17,6 +19,7 @@ import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
@ -43,12 +46,14 @@ public class AddonsManager {
private final Map<String, Class<?>> classes; private final Map<String, Class<?>> classes;
private BentoBox plugin; private BentoBox plugin;
private @NonNull Map<@NonNull String, @Nullable GameModeAddon> worldNames; private @NonNull Map<@NonNull String, @Nullable GameModeAddon> worldNames;
private @NonNull Map<Addon, List<Listener>> listeners;
public AddonsManager(@NonNull BentoBox plugin) { public AddonsManager(@NonNull BentoBox plugin) {
this.plugin = plugin; this.plugin = plugin;
addons = new ArrayList<>(); addons = new ArrayList<>();
loaders = new HashMap<>(); loaders = new HashMap<>();
classes = new HashMap<>(); classes = new HashMap<>();
listeners = new HashMap<>();
worldNames = new HashMap<>(); worldNames = new HashMap<>();
} }
@ -101,6 +106,9 @@ public class AddonsManager {
Bukkit.getPluginManager().callEvent(new AddonEvent().builder().addon(addon).reason(AddonEvent.Reason.LOAD).build()); Bukkit.getPluginManager().callEvent(new AddonEvent().builder().addon(addon).reason(AddonEvent.Reason.LOAD).build());
// Add it to the list of addons // Add it to the list of addons
if (addons.contains(addon)) {
addons.remove(addon);
}
addons.add(addon); addons.add(addon);
// Add to the list of loaders // Add to the list of loaders
@ -133,8 +141,16 @@ public class AddonsManager {
public void enableAddons() { public void enableAddons() {
if (!getLoadedAddons().isEmpty()) { if (!getLoadedAddons().isEmpty()) {
plugin.log("Enabling addons..."); plugin.log("Enabling addons...");
getLoadedAddons().forEach(this::enableAddon);
plugin.log("Addons successfully enabled.");
}
}
getLoadedAddons().forEach(addon -> { /**
* Enables an addon
* @param addon addon
*/
private void enableAddon(Addon addon) {
try { try {
// If this is a GameModeAddon create the worlds, register it and load the schems // If this is a GameModeAddon create the worlds, register it and load the schems
if (addon instanceof GameModeAddon) { if (addon instanceof GameModeAddon) {
@ -165,9 +181,6 @@ public class AddonsManager {
// Unhandled exception. We'll give a bit of debug here. // Unhandled exception. We'll give a bit of debug here.
handleAddonError(addon, e); handleAddonError(addon, e);
} }
});
plugin.log("Addons successfully enabled.");
}
} }
/** /**
@ -201,14 +214,19 @@ public class AddonsManager {
* Reloads all the enabled addons * Reloads all the enabled addons
*/ */
public void reloadAddons() { public void reloadAddons() {
if (!getEnabledAddons().isEmpty()) { disableAddons();
plugin.log("Reloading addons..."); loadAddons();
getEnabledAddons().stream().filter(Addon::isEnabled).forEach(addon -> { enableAddons();
plugin.log("Reloading " + addon.getDescription().getName() + "...");
addon.onReload();
});
plugin.log("Addons successfully reloaded.");
} }
/**
* Reloads one addon
* @param addon - addon
*/
public void reloadAddon(Addon addon) {
Path p = addon.getFile().toPath();
disable(addon);
loadAddon(p.toFile());
} }
/** /**
@ -218,7 +236,7 @@ public class AddonsManager {
*/ */
@NonNull @NonNull
public Optional<Addon> getAddonByName(@NonNull String name){ public Optional<Addon> getAddonByName(@NonNull String name){
return addons.stream().filter(a -> a.getDescription().getName().contains(name)).findFirst(); return addons.stream().filter(a -> a.getDescription().getName().equalsIgnoreCase(name)).findFirst();
} }
@NonNull @NonNull
@ -243,23 +261,14 @@ public class AddonsManager {
if (!getEnabledAddons().isEmpty()) { if (!getEnabledAddons().isEmpty()) {
plugin.log("Disabling addons..."); plugin.log("Disabling addons...");
// Disable addons // Disable addons
getEnabledAddons().forEach(addon -> { getEnabledAddons().forEach(this::disable);
if (addon.isEnabled()) {
addon.onDisable();
Bukkit.getPluginManager().callEvent(new AddonEvent().builder().addon(addon).reason(AddonEvent.Reason.DISABLE).build());
plugin.log("Disabling " + addon.getDescription().getName() + "...");
}
});
loaders.values().forEach(l -> {
try {
l.close();
} catch (IOException ignore) {
// Ignore
}
});
plugin.log("Addons successfully disabled."); plugin.log("Addons successfully disabled.");
} }
// Clear all maps
listeners.clear();
addons.clear();
loaders.clear();
classes.clear();
} }
@NonNull @NonNull
@ -383,4 +392,41 @@ public class AddonsManager {
} }
return null; return null;
} }
/**
* Register a listener
* @param addon - the addon registering
* @param listener - listener
*/
public void registerListener(Addon addon, Listener listener) {
Bukkit.getPluginManager().registerEvents(listener, BentoBox.getInstance());
listeners.computeIfAbsent(addon, k -> new ArrayList<>()).add(listener);
}
/**
* Disables an addon
* @param addon - addon
*/
private void disable(Addon addon) {
// Clear listeners
if (listeners.containsKey(addon)) {
listeners.get(addon).forEach(HandlerList::unregisterAll);
listeners.remove(addon);
}
// Disable
if (addon.isEnabled()) {
addon.onDisable();
Bukkit.getPluginManager().callEvent(new AddonEvent().builder().addon(addon).reason(AddonEvent.Reason.DISABLE).build());
plugin.log("Disabling " + addon.getDescription().getName() + "...");
}
// Clear loaders
if (loaders.containsKey(addon)) {
try {
loaders.get(addon).close();
} catch (IOException ignore) {
// Nothing
}
}
}
} }