Skip to content

Commit

Permalink
fix: load claims for late-loading worlds, close #162
Browse files Browse the repository at this point in the history
Improves support for world managers/multiverse.
  • Loading branch information
WiIIiam278 committed Aug 9, 2024
1 parent 23e513e commit c6e3cd8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<World, ClaimWorld> 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
*
Expand Down

0 comments on commit c6e3cd8

Please sign in to comment.