From 2b1c09d0d771de021177c54dad181ba369cbd04a Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Sat, 2 May 2020 00:46:20 +0200 Subject: [PATCH] Use static counter for dungeon world IDs --- .../erethon/dungeonsxl/world/DEditWorld.java | 4 ++-- .../erethon/dungeonsxl/world/DGameWorld.java | 5 ++--- .../dungeonsxl/world/DInstanceWorld.java | 21 ++++--------------- .../dungeonsxl/world/DResourceWorld.java | 8 +++---- 4 files changed, 12 insertions(+), 26 deletions(-) diff --git a/core/src/main/java/de/erethon/dungeonsxl/world/DEditWorld.java b/core/src/main/java/de/erethon/dungeonsxl/world/DEditWorld.java index ac44d81b..8818b794 100644 --- a/core/src/main/java/de/erethon/dungeonsxl/world/DEditWorld.java +++ b/core/src/main/java/de/erethon/dungeonsxl/world/DEditWorld.java @@ -44,8 +44,8 @@ public class DEditWorld extends DInstanceWorld implements EditWorld { private File idFile; - DEditWorld(DungeonsXL plugin, DResourceWorld resourceWorld, File folder, int id) { - super(plugin, resourceWorld, folder, id); + DEditWorld(DungeonsXL plugin, DResourceWorld resourceWorld, File folder) { + super(plugin, resourceWorld, folder); } /* Getters and setters */ diff --git a/core/src/main/java/de/erethon/dungeonsxl/world/DGameWorld.java b/core/src/main/java/de/erethon/dungeonsxl/world/DGameWorld.java index fcf5bab9..3ea6970b 100644 --- a/core/src/main/java/de/erethon/dungeonsxl/world/DGameWorld.java +++ b/core/src/main/java/de/erethon/dungeonsxl/world/DGameWorld.java @@ -59,7 +59,6 @@ import java.util.Map; import java.util.Set; import org.bukkit.Bukkit; import org.bukkit.Location; -import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.Sign; import org.bukkit.entity.Entity; @@ -96,8 +95,8 @@ public class DGameWorld extends DInstanceWorld implements GameWorld { private boolean readySign; - DGameWorld(DungeonsXL plugin, DResourceWorld resourceWorld, File folder, int id) { - super(plugin, resourceWorld, folder, id); + DGameWorld(DungeonsXL plugin, DResourceWorld resourceWorld, File folder) { + super(plugin, resourceWorld, folder); caliburn = plugin.getCaliburn(); } diff --git a/core/src/main/java/de/erethon/dungeonsxl/world/DInstanceWorld.java b/core/src/main/java/de/erethon/dungeonsxl/world/DInstanceWorld.java index 35dfaf53..bb8b4e99 100644 --- a/core/src/main/java/de/erethon/dungeonsxl/world/DInstanceWorld.java +++ b/core/src/main/java/de/erethon/dungeonsxl/world/DInstanceWorld.java @@ -17,7 +17,6 @@ package de.erethon.dungeonsxl.world; import de.erethon.commons.chat.MessageUtil; -import de.erethon.commons.misc.Registry; import de.erethon.commons.player.PlayerUtil; import de.erethon.dungeonsxl.DungeonsXL; import de.erethon.dungeonsxl.api.DungeonsAPI; @@ -47,6 +46,8 @@ public abstract class DInstanceWorld implements InstanceWorld { protected DungeonsXL plugin; protected PlayerCache dPlayers; + static int counter; + protected Map signs = new HashMap<>(); private DResourceWorld resourceWorld; private File folder; @@ -54,13 +55,13 @@ public abstract class DInstanceWorld implements InstanceWorld { private int id; private Location lobby; - DInstanceWorld(DungeonsXL plugin, DResourceWorld resourceWorld, File folder, int id) { + DInstanceWorld(DungeonsXL plugin, DResourceWorld resourceWorld, File folder) { this.plugin = plugin; dPlayers = plugin.getPlayerCache(); this.resourceWorld = resourceWorld; this.folder = folder; - this.id = id; + id = counter++; plugin.getInstanceCache().add(id, this); } @@ -196,20 +197,6 @@ public abstract class DInstanceWorld implements InstanceWorld { } } - /** - * @param instanceCache the used instance cache - * @return an ID for the instance - */ - public static int generateId(Registry instanceCache) { - int id = 0; - for (InstanceWorld instance : instanceCache) { - if (instance.getId() >= id) { - id = instance.getId() + 1; - } - } - return id; - } - /** * @return a name for the instance * @param game whether the instance is a GameWorld diff --git a/core/src/main/java/de/erethon/dungeonsxl/world/DResourceWorld.java b/core/src/main/java/de/erethon/dungeonsxl/world/DResourceWorld.java index c596e31e..9560276a 100644 --- a/core/src/main/java/de/erethon/dungeonsxl/world/DResourceWorld.java +++ b/core/src/main/java/de/erethon/dungeonsxl/world/DResourceWorld.java @@ -176,7 +176,7 @@ public class DResourceWorld implements ResourceWorld { public DInstanceWorld instantiate(boolean game) { plugin.setLoadingWorld(true); - int id = DInstanceWorld.generateId(plugin.getInstanceCache()); + int id = DInstanceWorld.counter; String name = DInstanceWorld.generateName(game, id); File instanceFolder = new File(Bukkit.getWorldContainer(), name); @@ -197,7 +197,7 @@ public class DResourceWorld implements ResourceWorld { } } - DInstanceWorld instance = game ? new DGameWorld(plugin, this, instanceFolder, id) : new DEditWorld(plugin, this, instanceFolder, id); + DInstanceWorld instance = game ? new DGameWorld(plugin, this, instanceFolder) : new DEditWorld(plugin, this, instanceFolder); FileUtil.copyDir(folder, instanceFolder, DungeonsXL.EXCLUDED_FILES); instance.world = Bukkit.createWorld(WorldCreator.name(name).environment(getWorldEnvironment())).getName(); @@ -271,14 +271,14 @@ public class DResourceWorld implements ResourceWorld { * @return the automatically created DEditWorld instance */ public DEditWorld generate() { - int id = DInstanceWorld.generateId(plugin.getInstanceCache()); + int id = DInstanceWorld.counter; String name = DInstanceWorld.generateName(false, id); File folder = new File(Bukkit.getWorldContainer(), name); WorldCreator creator = new WorldCreator(name); creator.type(WorldType.FLAT); creator.generateStructures(false); - DEditWorld editWorld = new DEditWorld(plugin, this, folder, id); + DEditWorld editWorld = new DEditWorld(plugin, this, folder); EditWorldGenerateEvent event = new EditWorldGenerateEvent(editWorld); Bukkit.getPluginManager().callEvent(event);