diff --git a/core/src/main/java/de/erethon/dungeonsxl/DungeonsXL.java b/core/src/main/java/de/erethon/dungeonsxl/DungeonsXL.java index c2c7765c..3035cf90 100644 --- a/core/src/main/java/de/erethon/dungeonsxl/DungeonsXL.java +++ b/core/src/main/java/de/erethon/dungeonsxl/DungeonsXL.java @@ -367,6 +367,7 @@ public class DungeonsXL extends DREPlugin implements DungeonsAPI { commandScriptRegistry.add(cmd.getName(), cmd); } protections = new GlobalProtectionCache(this); + protections.loadAll(); /* Integrations */ if (LWCUtil.isLWCLoaded()) { diff --git a/core/src/main/java/de/erethon/dungeonsxl/global/GlobalProtection.java b/core/src/main/java/de/erethon/dungeonsxl/global/GlobalProtection.java index 80d69e9e..a3bc9560 100644 --- a/core/src/main/java/de/erethon/dungeonsxl/global/GlobalProtection.java +++ b/core/src/main/java/de/erethon/dungeonsxl/global/GlobalProtection.java @@ -88,6 +88,17 @@ public abstract class GlobalProtection { public abstract void save(ConfigurationSection config); + public UnloadedProtection unload() { + String path = getDataPath() + "." + getWorld().getName() + "." + getId(); + ConfigurationSection config; + if (!plugin.getGlobalProtectionCache().getConfig().contains(path)) { + config = plugin.getGlobalProtectionCache().getConfig().createSection(path); + } else { + config = plugin.getGlobalProtectionCache().getConfig().getConfigurationSection(path); + } + return UnloadedProtection.create(plugin, getClass(), getWorld().getName(), getId(), config); + } + /** * @return a collection of all blocks covered by this protection */ diff --git a/core/src/main/java/de/erethon/dungeonsxl/global/GlobalProtectionCache.java b/core/src/main/java/de/erethon/dungeonsxl/global/GlobalProtectionCache.java index 3ec4993b..d1f70064 100644 --- a/core/src/main/java/de/erethon/dungeonsxl/global/GlobalProtectionCache.java +++ b/core/src/main/java/de/erethon/dungeonsxl/global/GlobalProtectionCache.java @@ -58,7 +58,10 @@ public class GlobalProtectionCache { } } config = YamlConfiguration.loadConfiguration(file); - loadAll(); + } + + public FileConfiguration getConfig() { + return config; } /** @@ -141,7 +144,8 @@ public class GlobalProtectionCache { if (world != null) { addProtection(new GameSign(plugin, world, NumberUtil.parseInt(entry.getKey()), ws.getConfigurationSection(entry.getKey()))); } else { - unloaded.put(new UnloadedProtection<>(plugin, GameSign.class, worldName, NumberUtil.parseInt(entry.getKey()), ws.getConfigurationSection(entry.getKey())), worldName); + UnloadedProtection protection = UnloadedProtection.create(plugin, GameSign.class, worldName, NumberUtil.parseInt(entry.getKey()), ws.getConfigurationSection(entry.getKey())); + unloaded.put(protection, worldName); } } } @@ -158,7 +162,8 @@ public class GlobalProtectionCache { if (world != null) { addProtection(new GroupSign(plugin, world, NumberUtil.parseInt(entry.getKey()), ws.getConfigurationSection(entry.getKey()))); } else { - unloaded.put(new UnloadedProtection<>(plugin, GroupSign.class, worldName, NumberUtil.parseInt(entry.getKey()), ws.getConfigurationSection(entry.getKey())), worldName); + UnloadedProtection protection = UnloadedProtection.create(plugin, GroupSign.class, worldName, NumberUtil.parseInt(entry.getKey()), ws.getConfigurationSection(entry.getKey())); + unloaded.put(protection, worldName); } } } @@ -175,7 +180,8 @@ public class GlobalProtectionCache { if (world != null) { addProtection(new LeaveSign(plugin, world, NumberUtil.parseInt(entry.getKey()), ws.getConfigurationSection(entry.getKey()))); } else { - unloaded.put(new UnloadedProtection<>(plugin, LeaveSign.class, worldName, NumberUtil.parseInt(entry.getKey()), ws.getConfigurationSection(entry.getKey())), worldName); + UnloadedProtection protection = UnloadedProtection.create(plugin, LeaveSign.class, worldName, NumberUtil.parseInt(entry.getKey()), ws.getConfigurationSection(entry.getKey())); + unloaded.put(protection, worldName); } } } @@ -192,7 +198,8 @@ public class GlobalProtectionCache { if (world != null) { addProtection(new DPortal(plugin, world, NumberUtil.parseInt(entry.getKey()), ws.getConfigurationSection(entry.getKey()))); } else { - unloaded.put(new UnloadedProtection<>(plugin, DPortal.class, worldName, NumberUtil.parseInt(entry.getKey()), ws.getConfigurationSection(entry.getKey())), worldName); + UnloadedProtection protection = UnloadedProtection.create(plugin, DPortal.class, worldName, NumberUtil.parseInt(entry.getKey()), ws.getConfigurationSection(entry.getKey())); + unloaded.put(protection, worldName); } } } @@ -207,9 +214,11 @@ public class GlobalProtectionCache { exception.printStackTrace(); } } - config.set("protections", null); for (GlobalProtection protection : protections) { - protection.save(config.createSection(protection.getDataPath() + "." + protection.getWorld().getName() + "." + protection.getId())); + String path = protection.getDataPath() + "." + protection.getWorld().getName() + "." + protection.getId(); + if (!config.contains(path)) { + protection.save(config.createSection(path)); + } } try { config.save(file); diff --git a/core/src/main/java/de/erethon/dungeonsxl/global/GlobalProtectionListener.java b/core/src/main/java/de/erethon/dungeonsxl/global/GlobalProtectionListener.java index 589c3cae..2399dc7a 100644 --- a/core/src/main/java/de/erethon/dungeonsxl/global/GlobalProtectionListener.java +++ b/core/src/main/java/de/erethon/dungeonsxl/global/GlobalProtectionListener.java @@ -23,9 +23,11 @@ import de.erethon.dungeonsxl.config.DMessage; import de.erethon.dungeonsxl.player.DGlobalPlayer; import de.erethon.dungeonsxl.player.DPermission; import de.erethon.dungeonsxl.player.DPlayerListener; +import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Map.Entry; +import java.util.Set; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; @@ -46,6 +48,7 @@ import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerPortalEvent; import org.bukkit.event.world.WorldLoadEvent; +import org.bukkit.event.world.WorldUnloadEvent; import org.bukkit.inventory.ItemStack; /** @@ -299,9 +302,29 @@ public class GlobalProtectionListener implements Listener { @EventHandler public void onWorldLoad(WorldLoadEvent event) { World world = event.getWorld(); - for (Entry entry : new HashSet<>(plugin.getGlobalProtectionCache().getUnloadedProtections().entrySet())) { + plugin.log("New world detected."); + Set> protections = plugin.getGlobalProtectionCache().getUnloadedProtections().entrySet(); + for (Entry entry : protections.toArray(new Entry[protections.size()])) { + plugin.log("Checking unloaded protection " + entry); if (world.getName().equals(entry.getValue())) { - entry.getKey().load(world); + plugin.log("New world has global DXL data: " + entry); + plugin.getGlobalProtectionCache().addProtection(entry.getKey().load(world)); + } + } + } + + @EventHandler + public void onWorldUnload(WorldUnloadEvent event) { + World world = event.getWorld(); + if (world.getName().startsWith("DXL_")) { + return; + } + plugin.log("Unloaded world detected."); + Collection protections = plugin.getGlobalProtectionCache().getProtections(); + for (GlobalProtection protection : protections.toArray(new GlobalProtection[protections.size()])) { + if (protection.getWorld().getName().equals(world.getName())) { + protection.delete(); + plugin.getGlobalProtectionCache().getUnloadedProtections().put(protection.unload(), world.getName()); } } } diff --git a/core/src/main/java/de/erethon/dungeonsxl/global/UnloadedProtection.java b/core/src/main/java/de/erethon/dungeonsxl/global/UnloadedProtection.java index 54ee9c81..cf0917ba 100644 --- a/core/src/main/java/de/erethon/dungeonsxl/global/UnloadedProtection.java +++ b/core/src/main/java/de/erethon/dungeonsxl/global/UnloadedProtection.java @@ -36,19 +36,22 @@ public class UnloadedProtection { private int id; private ConfigurationSection config; - public UnloadedProtection(DungeonsXL plugin, Class type, String worldName, int id, ConfigurationSection config) { - this.plugin = plugin; - try { - constructor = type.getConstructor(DungeonsXL.class, World.class, int.class, ConfigurationSection.class); - } catch (NoSuchMethodException | SecurityException exception) { - // Don't register - return; - } - this.worldName = worldName; - this.id = id; - this.config = config; + private UnloadedProtection() {} - cache = plugin.getGlobalProtectionCache(); + public static UnloadedProtection create(DungeonsXL plugin, Class type, String worldName, int id, ConfigurationSection config) { + UnloadedProtection protection = new UnloadedProtection(); + protection.plugin = plugin; + try { + protection.constructor = type.getConstructor(DungeonsXL.class, World.class, int.class, ConfigurationSection.class); + } catch (NoSuchMethodException | SecurityException exception) { + return null; + } + protection.worldName = worldName; + protection.id = id; + protection.config = config; + + protection.cache = plugin.getGlobalProtectionCache(); + return protection; } public T load(World world) {