From f5b59d7537d410fac35fbb4e0181a61a485ae1a5 Mon Sep 17 00:00:00 2001 From: kernitus <2789734+kernitus@users.noreply.github.com> Date: Mon, 28 Oct 2024 16:24:38 +0000 Subject: [PATCH] fix: listen to dynamically loaded worlds for modesets Fixes #747 --- .../OldCombatMechanics/utilities/Config.java | 26 ++++++++++++++----- .../utilities/storage/ModesetListener.java | 17 ++++++++++++ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/utilities/Config.java b/src/main/java/kernitus/plugin/OldCombatMechanics/utilities/Config.java index 71fcce7b..09c6fecc 100644 --- a/src/main/java/kernitus/plugin/OldCombatMechanics/utilities/Config.java +++ b/src/main/java/kernitus/plugin/OldCombatMechanics/utilities/Config.java @@ -134,17 +134,29 @@ private static void reloadWorlds() { for (String worldName : worldsSection.getKeys(false)) { final World world = Bukkit.getWorld(worldName); if(world == null){ - Messenger.warn("Configured world " + worldName + " not found, skipping..."); + Messenger.warn("Configured world " + worldName + " not found, skipping (might be loaded later?)..."); continue; } + addWorld(world, worldsSection); + } + } + + public static void addWorld(World world){ + final ConfigurationSection worldsSection = config.getConfigurationSection("worlds"); + addWorld(world, worldsSection); + } - // Retrieve the list of modeset names for the current world - // Using a linkedhashset to remove duplicates but retain insertion order (important for default modeset) - final LinkedHashSet modesetsSet = new LinkedHashSet<>(worldsSection.getStringList(worldName)); + public static void addWorld(World world, ConfigurationSection worldsSection) { + // Retrieve the list of modeset names for the current world + // Using a linkedhashset to remove duplicates but retain insertion order (important for default modeset) + final LinkedHashSet modesetsSet = new LinkedHashSet<>(worldsSection.getStringList(world.getName())); - // Add the current world and its modesets to the map - worlds.put(world.getUID(), modesetsSet); - } + // Add the current world and its modesets to the map + worlds.put(world.getUID(), modesetsSet); + } + + public static void removeWorld(World world){ + worlds.remove(world.getUID()); } /** diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/utilities/storage/ModesetListener.java b/src/main/java/kernitus/plugin/OldCombatMechanics/utilities/storage/ModesetListener.java index a4a54700..1c86da15 100644 --- a/src/main/java/kernitus/plugin/OldCombatMechanics/utilities/storage/ModesetListener.java +++ b/src/main/java/kernitus/plugin/OldCombatMechanics/utilities/storage/ModesetListener.java @@ -10,11 +10,14 @@ import kernitus.plugin.OldCombatMechanics.module.OCMModule; import kernitus.plugin.OldCombatMechanics.utilities.Config; import kernitus.plugin.OldCombatMechanics.utilities.Messenger; +import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.player.PlayerChangedWorldEvent; import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.world.WorldLoadEvent; +import org.bukkit.event.world.WorldUnloadEvent; import java.util.Set; import java.util.UUID; @@ -82,4 +85,18 @@ public void onPlayerJoin(PlayerJoinEvent event) { final Player player = event.getPlayer(); updateModeset(player, player.getWorld().getUID(), null); } + + @EventHandler(ignoreCancelled = false) + public void onWorldLoad(WorldLoadEvent event) { + final World world = event.getWorld(); + Config.addWorld(world); + Messenger.info("Loaded configured world " + world.getName()); + } + + @EventHandler(ignoreCancelled = false) + public void onWorldUnload(WorldUnloadEvent event) { + final World world = event.getWorld(); + Config.removeWorld(world); + Messenger.info("Unloaded configured world " + world.getName()); + } }