From 17c087469bd5729f260508a72aba8bcde9a1e093 Mon Sep 17 00:00:00 2001 From: tastybento Date: Mon, 24 Dec 2018 20:18:33 -0800 Subject: [PATCH] WIP - attempt at implementing GameModeAddon API https://github.com/BentoBoxWorld/BentoBox/pull/415 --- .../world/bentobox/bentobox/BentoBox.java | 11 ---- .../bentobox/api/addons/GameModeAddon.java | 58 +++++++++++++++++++ .../api/commands/CompositeCommand.java | 5 ++ .../bentobox/managers/AddonsManager.java | 12 ++++ .../bentobox/managers/SchemsManager.java | 52 ++++++++--------- 5 files changed, 100 insertions(+), 38 deletions(-) create mode 100644 src/main/java/world/bentobox/bentobox/api/addons/GameModeAddon.java diff --git a/src/main/java/world/bentobox/bentobox/BentoBox.java b/src/main/java/world/bentobox/bentobox/BentoBox.java index 10f3098fd..ec242204b 100644 --- a/src/main/java/world/bentobox/bentobox/BentoBox.java +++ b/src/main/java/world/bentobox/bentobox/BentoBox.java @@ -3,12 +3,10 @@ package world.bentobox.bentobox; import java.util.Optional; import org.bukkit.Bukkit; -import org.bukkit.World; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import world.bentobox.bentobox.api.configuration.Config; -import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.events.BentoBoxReadyEvent; import world.bentobox.bentobox.api.user.Notifier; import world.bentobox.bentobox.commands.BentoBoxCommand; @@ -312,15 +310,6 @@ public class BentoBox extends JavaPlugin { getLogger().warning(warning); } - /** - * Registers a world as a world to be covered by this plugin - * @param world - Bukkit overworld - * @param worldSettings - settings for this world - */ - public void registerWorld(World world, WorldSettings worldSettings) { - islandWorldManager.addWorld(world, worldSettings); - } - /** * @return the schemsManager */ diff --git a/src/main/java/world/bentobox/bentobox/api/addons/GameModeAddon.java b/src/main/java/world/bentobox/bentobox/api/addons/GameModeAddon.java new file mode 100644 index 000000000..5572a7a88 --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/api/addons/GameModeAddon.java @@ -0,0 +1,58 @@ +/** + * + */ +package world.bentobox.bentobox.api.addons; + +import org.bukkit.Location; +import org.bukkit.World; + +import world.bentobox.bentobox.api.configuration.WorldSettings; +import world.bentobox.bentobox.util.Util; + +/** + * Defines the addon as a game mode. A game mode creates worlds, registers world settings and schems. + * @author tastybento + * + */ +public abstract class GameModeAddon extends Addon { + + protected World islandWorld; + protected World netherWorld; + protected World endWorld; + + /** + * Make the worlds for this GameMode in this method. BentoBox will call it + * after onLoad() and before onEnable(). + * {@link #islandWorld} must be created, + * {@link #netherWorld} and {@link #endWorld} are optional and may be null. + */ + public abstract void createWorlds(); + + /** + * @return WorldSettings for this GameMode + */ + public abstract WorldSettings getWorldSettings(); + + /** + * Checks if a player is in any of the island worlds + * @param loc - player to check + * @return true if in a world or false if not + */ + public boolean inWorld(Location loc) { + return Util.sameWorld(loc.getWorld(), islandWorld); + } + + public World getOverWorld() { + return islandWorld; + } + + public World getNetherWorld() { + return netherWorld; + } + + public World getEndWorld() { + return endWorld; + } + + +} diff --git a/src/main/java/world/bentobox/bentobox/api/commands/CompositeCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/CompositeCommand.java index 00459c661..d0daf681f 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/CompositeCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/CompositeCommand.java @@ -22,6 +22,7 @@ import org.bukkit.entity.Player; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.api.addons.Addon; +import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.api.events.command.CommandEvent; import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; @@ -133,6 +134,10 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi setParametersHelp(COMMANDS + label + ".parameters"); permissionPrefix = (addon != null) ? addon.getPermissionPrefix() : ""; setup(); + // Set up world if this is an AddonGameMode + if (addon instanceof GameModeAddon) { + this.world = ((GameModeAddon)addon).getOverWorld(); + } if (!getSubCommand("help").isPresent() && !label.equals("help")) { new DefaultHelpCommand(this); } diff --git a/src/main/java/world/bentobox/bentobox/managers/AddonsManager.java b/src/main/java/world/bentobox/bentobox/managers/AddonsManager.java index 2a3c24777..eee328675 100644 --- a/src/main/java/world/bentobox/bentobox/managers/AddonsManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/AddonsManager.java @@ -25,6 +25,7 @@ import org.bukkit.configuration.file.YamlConfiguration; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.addons.Addon; import world.bentobox.bentobox.api.addons.AddonClassLoader; +import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.api.addons.exceptions.InvalidAddonFormatException; import world.bentobox.bentobox.api.events.addon.AddonEvent; @@ -147,6 +148,17 @@ public class AddonsManager { loaders.put(addon, addonClassLoader); // Run the onLoad. addon.onLoad(); + // If this is a GameMode, get the GameWorld + if (addon instanceof GameModeAddon) { + GameModeAddon gameMode = (GameModeAddon)addon; + // Create the gameWorlds + gameMode.createWorlds(); + plugin.logDebug("GameModeAddon found! overWorld = " + gameMode.getOverWorld()); + plugin.getIWM().addWorld(gameMode.getOverWorld(), gameMode.getWorldSettings()); + // Register the schems + plugin.logDebug("Trying to register schems"); + plugin.getSchemsManager().loadIslands(gameMode); + } } catch (Exception e) { plugin.logError(e.getMessage()); } diff --git a/src/main/java/world/bentobox/bentobox/managers/SchemsManager.java b/src/main/java/world/bentobox/bentobox/managers/SchemsManager.java index 2171a3b4e..960c8f004 100644 --- a/src/main/java/world/bentobox/bentobox/managers/SchemsManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/SchemsManager.java @@ -14,6 +14,7 @@ import org.bukkit.configuration.InvalidConfigurationException; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.addons.Addon; +import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.schems.Clipboard; @@ -43,7 +44,8 @@ public class SchemsManager { // Save any schems that try (JarFile jar = new JarFile(addon.getFile())) { plugin.getAddonsManager().listJarFiles(jar, "schems", ".schem").forEach(name -> { - addon.saveResource("schems/" + name, false); + plugin.logDebug("Found " + name); + addon.saveResource(name, false); }); } catch (IOException e) { plugin.logError("Could not load schem files from addon jar " + e.getMessage()); @@ -61,33 +63,29 @@ public class SchemsManager { /** * Load schems for addon. Will try and load nether and end schems too if settings are set. - * @param world - world + * @param addon - GameModeAddon */ - public void loadIslands(World world) { - plugin.getIWM().getAddon(world).ifPresent(addon -> { - File schems = new File(addon.getDataFolder(), "schems"); - // Copy any schems fould in the jar - copySchems(addon, schems); - // Load all schems in folder - // Look through the folder - FilenameFilter schemFilter = (File dir, String name) -> name.toLowerCase(java.util.Locale.ENGLISH).endsWith(".schem") - && !name.toLowerCase(java.util.Locale.ENGLISH).startsWith("nether-") - && !name.toLowerCase(java.util.Locale.ENGLISH).startsWith("end-"); - Arrays.stream(Objects.requireNonNull(schems.list(schemFilter))).map(name -> name.substring(0, name.length() - 6)).forEach(name -> { - if (!plugin.getSchemsManager().loadSchem(world, schems, name)) { - plugin.logError("Could not load " + name + ".schem for " + plugin.getIWM().getFriendlyName(world)); - } - if (plugin.getIWM().isNetherGenerate(world) && plugin.getIWM().isNetherIslands(world) - && !plugin.getSchemsManager().loadSchem(plugin.getIWM().getNetherWorld(world), schems, "nether-" + name)) { - plugin.logError("Could not load nether-" + name + ".schem for " + plugin.getIWM().getFriendlyName(world)); - } - if (plugin.getIWM().isEndGenerate(world) && plugin.getIWM().isEndIslands(world) - && !plugin.getSchemsManager().loadSchem(plugin.getIWM().getEndWorld(world), schems, "end-" + name)) { - plugin.logError("Could not load end-" + name + ".schem for " + plugin.getIWM().getFriendlyName(world)); - } - }); - - + public void loadIslands(GameModeAddon addon) { + File schems = new File(addon.getDataFolder(), "schems"); + // Copy any schems fould in the jar + copySchems(addon, schems); + // Load all schems in folder + // Look through the folder + FilenameFilter schemFilter = (File dir, String name) -> name.toLowerCase(java.util.Locale.ENGLISH).endsWith(".schem") + && !name.toLowerCase(java.util.Locale.ENGLISH).startsWith("nether-") + && !name.toLowerCase(java.util.Locale.ENGLISH).startsWith("end-"); + Arrays.stream(Objects.requireNonNull(schems.list(schemFilter))).map(name -> name.substring(0, name.length() - 6)).forEach(name -> { + if (!plugin.getSchemsManager().loadSchem(addon.getOverWorld(), schems, name)) { + plugin.logError("Could not load " + name + ".schem for " + addon.getWorldSettings().getFriendlyName()); + } + if (addon.getWorldSettings().isNetherGenerate() && addon.getWorldSettings().isNetherIslands() + && !plugin.getSchemsManager().loadSchem(addon.getNetherWorld(), schems, "nether-" + name)) { + plugin.logError("Could not load nether-" + name + ".schem for " + addon.getWorldSettings().getFriendlyName()); + } + if (addon.getWorldSettings().isEndGenerate() && addon.getWorldSettings().isEndIslands() + && !plugin.getSchemsManager().loadSchem(addon.getEndWorld(), schems, "end-" + name)) { + plugin.logError("Could not load end-" + name + ".schem for " + addon.getWorldSettings().getFriendlyName()); + } }); }