Skip to content

Commit

Permalink
Don't validate unloaded signs on startup (#85)
Browse files Browse the repository at this point in the history
* Don't validate unloaded signs in startup

* Actually stop loading chunks

* Fix crash issues due to unwanted chunk loads in the sign ChunkLoadEvent listener

* Delay chunk handling, fixes CMEs
  • Loading branch information
sgdc3 committed Sep 7, 2020
1 parent bee85b0 commit 45b9688
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
7 changes: 6 additions & 1 deletion src/main/java/world/bentobox/warps/WarpSignsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,12 @@ void loadWarpList() {
// 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.getWorld().isChunkLoaded(k.getBlockX() >> 4, k.getBlockZ() >> 4)
&& !k.getBlock().getType().name().contains("SIGN")) {
return;
}

// Add to map
getWarpMap(k.getWorld()).put(v, k);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package world.bentobox.warps.listeners;

import java.util.Iterator;
import java.util.Map;
import java.util.UUID;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
Expand All @@ -16,6 +14,8 @@
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.SignChangeEvent;

import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.scheduler.BukkitRunnable;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.events.team.TeamEvent.TeamKickEvent;
import world.bentobox.bentobox.api.events.team.TeamEvent.TeamLeaveEvent;
Expand Down Expand Up @@ -44,6 +44,35 @@ public WarpSignsListener(Warp addon) {
this.plugin = addon.getPlugin();
}

@EventHandler(priority = EventPriority.NORMAL)
public void onChunkLoad(ChunkLoadEvent event) {
// Delay to wait the chunk to be fully loaded
new BukkitRunnable() {
@Override
public void run() {
boolean changed = false;
Iterator<Map.Entry<UUID, Location>> iterator =
addon.getWarpSignsManager().getWarpMap(event.getWorld()).entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<UUID, Location> entry = iterator.next();
UUID uuid = entry.getKey();
Location location = entry.getValue();
if (event.getChunk().getX() == location.getBlockX() >> 4
&& event.getChunk().getZ() == location.getBlockZ() >> 4
&& !Tag.SIGNS.isTagged(location.getBlock().getType())) {
iterator.remove();
// Remove sign from warp panel cache
addon.getWarpPanelManager().removeWarp(event.getWorld(), uuid);
changed = true;
}
}
if (changed) {
addon.getWarpSignsManager().saveWarpList();
}
}
}.runTask(plugin);
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onPlayerLeave(TeamLeaveEvent e) {
// Remove any warp signs from this game mode
Expand Down
11 changes: 3 additions & 8 deletions src/test/java/world/bentobox/warps/WarpSignsManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@
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;
Expand Down Expand Up @@ -165,7 +159,8 @@ public void setUp() throws Exception {
when(location.getBlockY()).thenReturn(24);
when(location.getBlockZ()).thenReturn(25);
when(world.getEnvironment()).thenReturn(Environment.NORMAL);

when(world.isChunkLoaded(anyInt(), anyInt())).thenReturn(true);

// Block
when(block.getType()).thenReturn(Material.ACACIA_SIGN);
when(block.getLocation()).thenReturn(location);
Expand Down

0 comments on commit 45b9688

Please sign in to comment.