WIP - attempt at implementing GameModeAddon API

https://github.com/BentoBoxWorld/BentoBox/pull/415
This commit is contained in:
tastybento 2018-12-24 20:18:33 -08:00
parent e6293cb480
commit 17c087469b
5 changed files with 100 additions and 38 deletions

View File

@ -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
*/

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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());
}

View File

@ -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());
}
});
}