From e2f4a8fe847bc9dce34fb54fc18fa5ac7c3dfd30 Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Sun, 26 Jun 2016 00:56:30 +0200 Subject: [PATCH] Add worlds automatically on object initialization --- .../dre2n/dungeonsxl/world/EditWorld.java | 2 ++ .../dre2n/dungeonsxl/world/GameWorld.java | 9 ++--- .../dre2n/dungeonsxl/world/InstanceWorld.java | 3 ++ .../dre2n/dungeonsxl/world/ResourceWorld.java | 4 +++ .../github/dre2n/dungeonsxl/world/Worlds.java | 34 ++++++++++++++++++- 5 files changed, 45 insertions(+), 7 deletions(-) diff --git a/src/main/java/io/github/dre2n/dungeonsxl/world/EditWorld.java b/src/main/java/io/github/dre2n/dungeonsxl/world/EditWorld.java index 76fff176..458e9ce6 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/world/EditWorld.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/world/EditWorld.java @@ -135,6 +135,8 @@ public class EditWorld extends InstanceWorld { } FileUtil.removeDirectory(getFolder()); + + worlds.removeInstance(this); } /* Statics */ diff --git a/src/main/java/io/github/dre2n/dungeonsxl/world/GameWorld.java b/src/main/java/io/github/dre2n/dungeonsxl/world/GameWorld.java index 10ae4032..d5b45328 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/world/GameWorld.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/world/GameWorld.java @@ -17,8 +17,6 @@ package io.github.dre2n.dungeonsxl.world; import io.github.dre2n.commons.util.FileUtil; -import io.github.dre2n.dungeonsxl.DungeonsXL; -import io.github.dre2n.dungeonsxl.config.DungeonConfig; import io.github.dre2n.dungeonsxl.dungeon.Dungeon; import io.github.dre2n.dungeonsxl.event.gameworld.GameWorldStartGameEvent; import io.github.dre2n.dungeonsxl.event.gameworld.GameWorldUnloadEvent; @@ -56,8 +54,6 @@ import org.bukkit.inventory.ItemStack; */ public class GameWorld extends InstanceWorld { - static DungeonsXL plugin = DungeonsXL.getInstance(); - // Variables private boolean tutorial; private boolean isPlaying = false; @@ -343,8 +339,7 @@ public class GameWorld extends InstanceWorld { */ public Dungeon getDungeon() { for (Dungeon dungeon : plugin.getDungeons().getDungeons()) { - DungeonConfig dungeonConfig = dungeon.getConfig(); - if (dungeonConfig.getFloors().contains(getName()) || dungeonConfig.getStartFloor().equals(getName()) || dungeonConfig.getEndFloor().equals(getName())) { + if (dungeon.getConfig().containsFloor(getResource())) { return dungeon; } } @@ -401,6 +396,8 @@ public class GameWorld extends InstanceWorld { plugin.getWorlds().getInstances().remove(this); plugin.getServer().unloadWorld(getWorld(), true); FileUtil.removeDirectory(getFolder()); + + worlds.removeInstance(this); } /** diff --git a/src/main/java/io/github/dre2n/dungeonsxl/world/InstanceWorld.java b/src/main/java/io/github/dre2n/dungeonsxl/world/InstanceWorld.java index 3baa7301..d271706f 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/world/InstanceWorld.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/world/InstanceWorld.java @@ -31,6 +31,7 @@ import org.bukkit.World; public abstract class InstanceWorld { protected static DungeonsXL plugin = DungeonsXL.getInstance(); + protected static Worlds worlds = plugin.getWorlds(); public static String ID_FILE_PREFIX = ".id_"; @@ -46,6 +47,8 @@ public abstract class InstanceWorld { this.folder = folder; this.world = world; this.id = id; + + worlds.addInstance(this); } /* Getters and setters */ diff --git a/src/main/java/io/github/dre2n/dungeonsxl/world/ResourceWorld.java b/src/main/java/io/github/dre2n/dungeonsxl/world/ResourceWorld.java index c6d7a046..1cc2aea9 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/world/ResourceWorld.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/world/ResourceWorld.java @@ -60,6 +60,8 @@ public class ResourceWorld { } signData = new SignData(signDataFile); + + worlds.addResource(this); } public ResourceWorld(File folder) { @@ -74,6 +76,8 @@ public class ResourceWorld { if (signData.exists()) { this.signData = new SignData(signData); } + + worlds.addResource(this); } /* Getters and setters */ diff --git a/src/main/java/io/github/dre2n/dungeonsxl/world/Worlds.java b/src/main/java/io/github/dre2n/dungeonsxl/world/Worlds.java index 341dcfd5..9ea1833d 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/world/Worlds.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/world/Worlds.java @@ -35,7 +35,7 @@ public class Worlds { public Worlds(File folder) { for (File file : folder.listFiles()) { if (file.isDirectory()) { - resources.add(new ResourceWorld(file)); + new ResourceWorld(file); } } } @@ -86,6 +86,22 @@ public class Worlds { return resources; } + /** + * @param resource + * the ResourceWorld to add + */ + public void addResource(ResourceWorld resource) { + resources.add(resource); + } + + /** + * @param resource + * the ResourceWorld to remove + */ + public void removeResource(ResourceWorld resource) { + resources.remove(resource); + } + /** * @return the loaded InstanceWorlds in the world container */ @@ -93,6 +109,22 @@ public class Worlds { return instances; } + /** + * @param instance + * the InstanceWorld to add + */ + public void addInstance(InstanceWorld instance) { + instances.add(instance); + } + + /** + * @param instance + * the InstanceWorld to remove + */ + public void removeInstance(InstanceWorld instance) { + instances.remove(instance); + } + /** * @return the loaded GameWorlds */