From c6e3cd8e6f855926b0f7960636b344730204797b Mon Sep 17 00:00:00 2001 From: William Date: Fri, 9 Aug 2024 13:55:14 +0100 Subject: [PATCH] fix: load claims for late-loading worlds, close #162 Improves support for world managers/multiverse. --- .../huskclaims/listener/BukkitListener.java | 13 +++++++++++++ .../huskclaims/claim/ClaimManager.java | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/bukkit/src/main/java/net/william278/huskclaims/listener/BukkitListener.java b/bukkit/src/main/java/net/william278/huskclaims/listener/BukkitListener.java index 5ca2c8d4..fb35c890 100644 --- a/bukkit/src/main/java/net/william278/huskclaims/listener/BukkitListener.java +++ b/bukkit/src/main/java/net/william278/huskclaims/listener/BukkitListener.java @@ -25,6 +25,7 @@ import net.william278.cloplib.operation.OperationUser; import net.william278.huskclaims.BukkitHuskClaims; import net.william278.huskclaims.moderation.SignListener; +import net.william278.huskclaims.position.World; import net.william278.huskclaims.user.BukkitUser; import net.william278.huskclaims.user.User; import org.bukkit.Location; @@ -36,6 +37,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.player.*; +import org.bukkit.event.world.WorldLoadEvent; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -102,6 +104,17 @@ public void onUserTeleport(@NotNull PlayerTeleportEvent e) { } } + @EventHandler(ignoreCancelled = true) + public void onWorldLoad(@NotNull WorldLoadEvent e) { + plugin.runAsync(() -> { + final World world = BukkitHuskClaims.Adapter.adapt(e.getWorld()); + plugin.loadClaimWorld(world); + plugin.getClaimWorld(world).ifPresent(loaded -> plugin.getMapHooks().forEach( + hook -> hook.markClaims(loaded.getClaims(), loaded)) + ); + }); + } + @Override public void onUserTamedEntityAction(@NotNull Cancellable event, @Nullable Entity player, @NotNull Entity entity) { // If pets are enabled, check if the entity is tamed diff --git a/common/src/main/java/net/william278/huskclaims/claim/ClaimManager.java b/common/src/main/java/net/william278/huskclaims/claim/ClaimManager.java index 16a5d836..e21dd362 100644 --- a/common/src/main/java/net/william278/huskclaims/claim/ClaimManager.java +++ b/common/src/main/java/net/william278/huskclaims/claim/ClaimManager.java @@ -370,6 +370,25 @@ default void loadClaimWorlds() throws IllegalStateException { pruneClaims(); } + /** + * Load a single claim world from the database + * + * @since 1.3.5 + */ + @Blocking + default void loadClaimWorld(@NotNull World serverWorld) { + if (getPlugin().getSettings().getClaims().isWorldUnclaimable(serverWorld)) { + return; + } + + final Map worlds = getDatabase().getClaimWorlds(getPlugin().getServerName()); + if (worlds.keySet().stream().map(World::getName).noneMatch(uuid -> uuid.equals(serverWorld.getName()))) { + getPlugin().log(Level.INFO, String.format("Creating new claim world for %s...", serverWorld.getName())); + getClaimWorlds().put(serverWorld.getName(), getDatabase().createClaimWorld(serverWorld)); + } + pruneClaims(); + } + /** * Load the claim highlighter *