From 6eb3261faab0907fa5de2a01636ec41f7c111ac9 Mon Sep 17 00:00:00 2001 From: Gabriele C Date: Mon, 3 Aug 2020 18:49:23 +0200 Subject: [PATCH] Don't validate unloaded signs in startup --- pom.xml | 2 +- .../bentobox/warps/WarpSignsManager.java | 6 ++++- .../warps/listeners/WarpSignsListener.java | 22 +++++++++++++++++++ .../bentobox/warps/WarpSignsManagerTest.java | 16 +++++++------- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/pom.xml b/pom.xml index 9d2c275..3df88bd 100644 --- a/pom.xml +++ b/pom.xml @@ -66,7 +66,7 @@ -LOCAL - 1.10.0 + 1.10.1 diff --git a/src/main/java/world/bentobox/warps/WarpSignsManager.java b/src/main/java/world/bentobox/warps/WarpSignsManager.java index 1295c24..e714e8f 100644 --- a/src/main/java/world/bentobox/warps/WarpSignsManager.java +++ b/src/main/java/world/bentobox/warps/WarpSignsManager.java @@ -186,7 +186,11 @@ public class WarpSignsManager { // Load into map if (warpsData != null) { warpsData.getWarpSigns().forEach((k,v) -> { - if (k != null && k.getWorld() != null && k.getBlock().getType().name().contains("SIGN")) { + if (k != null && k.getWorld() != null) { + if (k.getBlock().getChunk().isLoaded() && !k.getBlock().getType().name().contains("SIGN")) { + return; + } + // Add to map getWarpMap(k.getWorld()).put(v, k); } diff --git a/src/main/java/world/bentobox/warps/listeners/WarpSignsListener.java b/src/main/java/world/bentobox/warps/listeners/WarpSignsListener.java index 3def898..61683f9 100644 --- a/src/main/java/world/bentobox/warps/listeners/WarpSignsListener.java +++ b/src/main/java/world/bentobox/warps/listeners/WarpSignsListener.java @@ -1,5 +1,6 @@ package world.bentobox.warps.listeners; +import java.util.Iterator; import java.util.Map; import java.util.UUID; @@ -16,6 +17,7 @@ import org.bukkit.event.Listener; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.SignChangeEvent; +import org.bukkit.event.world.ChunkLoadEvent; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.events.team.TeamEvent.TeamKickEvent; import world.bentobox.bentobox.api.events.team.TeamEvent.TeamLeaveEvent; @@ -44,6 +46,26 @@ public class WarpSignsListener implements Listener { this.plugin = addon.getPlugin(); } + @EventHandler(priority = EventPriority.NORMAL) + public void onChunkLoad(ChunkLoadEvent event) { + boolean changed = false; + Iterator> iterator = + addon.getWarpSignsManager().getWarpMap(event.getWorld()).entrySet().iterator(); + while (iterator.hasNext()) { + Map.Entry entry = iterator.next(); + if (entry.getValue().getChunk().equals(event.getChunk()) + && !entry.getValue().getBlock().getType().name().contains("SIGN")) { + iterator.remove(); + // Remove sign from warp panel cache + addon.getWarpPanelManager().removeWarp(event.getWorld(), entry.getKey()); + changed = true; + } + } + if (changed) { + addon.getWarpSignsManager().saveWarpList(); + } + } + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onPlayerLeave(TeamLeaveEvent e) { // Remove any warp signs from this game mode diff --git a/src/test/java/world/bentobox/warps/WarpSignsManagerTest.java b/src/test/java/world/bentobox/warps/WarpSignsManagerTest.java index 0e86108..893b354 100644 --- a/src/test/java/world/bentobox/warps/WarpSignsManagerTest.java +++ b/src/test/java/world/bentobox/warps/WarpSignsManagerTest.java @@ -20,13 +20,7 @@ import java.util.Map; import java.util.UUID; import java.util.logging.Logger; -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.OfflinePlayer; -import org.bukkit.Server; -import org.bukkit.World; +import org.bukkit.*; import org.bukkit.World.Environment; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; @@ -90,6 +84,8 @@ public class WarpSignsManagerTest { @Mock private Location location; @Mock + private Chunk chunk; + @Mock private Block block; @Mock private PluginManager pim; @@ -165,10 +161,14 @@ public class WarpSignsManagerTest { when(location.getBlockY()).thenReturn(24); when(location.getBlockZ()).thenReturn(25); when(world.getEnvironment()).thenReturn(Environment.NORMAL); - + + // Chunk + when(chunk.isLoaded()).thenReturn(true); + // Block when(block.getType()).thenReturn(Material.ACACIA_SIGN); when(block.getLocation()).thenReturn(location); + when(block.getChunk()).thenReturn(chunk); Sign sign = mock(Sign.class); String[] lines = {"[Welcome]", "line2", "line3", "line4"}; when(sign.getLines()).thenReturn(lines);