Skip to content

Commit

Permalink
fix: listen to dynamically loaded worlds for modesets
Browse files Browse the repository at this point in the history
Fixes #747
  • Loading branch information
kernitus committed Oct 28, 2024
1 parent 822fb1f commit f5b59d7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> 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());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

1 comment on commit f5b59d7

@Brasil150
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, this really solved #747

Thanks for your attention.

Please sign in to comment.