diff --git a/src/main/java/org/maxgamer/quickshop/Listeners/ShopProtectionListener.java b/src/main/java/org/maxgamer/quickshop/Listeners/ShopProtectionListener.java index f1a48e39c..61931455a 100644 --- a/src/main/java/org/maxgamer/quickshop/Listeners/ShopProtectionListener.java +++ b/src/main/java/org/maxgamer/quickshop/Listeners/ShopProtectionListener.java @@ -39,9 +39,11 @@ public class ShopProtectionListener implements Listener { if (ListenerHelper.isDisabled(e.getClass())) { return; } + for (int i = 0; i < e.blockList().size(); i++) { - Block b = e.blockList().get(i); - Shop shop = plugin.getShopManager().getShopIncludeAttached(b.getLocation()); + final Block b = e.blockList().get(i); + final Shop shop = plugin.getShopManager().getShopIncludeAttached(b.getLocation()); + if (shop != null) { if (plugin.getConfig().getBoolean("protect.explode")) { e.setCancelled(true); @@ -54,32 +56,34 @@ public class ShopProtectionListener implements Listener { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onBlockFromTo(BlockFromToEvent e) { - if (ListenerHelper.isDisabled(e.getClass())) { + if (ListenerHelper.isDisabled(e.getClass()) || + !useEnhanceProtection) { return; } - if (!useEnhanceProtection) { - return; - } - Shop shop = plugin.getShopManager().getShopIncludeAttached(e.getToBlock().getLocation()); + + final Shop shop = plugin.getShopManager().getShopIncludeAttached(e.getToBlock().getLocation()); + if (shop == null) { return; } + e.setCancelled(true); } //Protect Redstone active shop @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onBlockRedstoneChange(BlockRedstoneEvent event) { - if (ListenerHelper.isDisabled(event.getClass())) { + if (ListenerHelper.isDisabled(event.getClass()) || + !useEnhanceProtection) { return; } - if (!useEnhanceProtection) { - return; - } - Shop shop = plugin.getShopManager().getShopIncludeAttached(event.getBlock().getLocation()); + + final Shop shop = plugin.getShopManager().getShopIncludeAttached(event.getBlock().getLocation()); + if (shop == null) { return; } + event.setNewCurrent(event.getOldCurrent()); //plugin.getLogger().warning("[Exploit Alert] a Redstone tried to active of " + shop); //Util.debugLog(ChatColor.RED + "[QuickShop][Exploit alert] Redstone was activated on the following shop " + shop); @@ -87,15 +91,15 @@ public class ShopProtectionListener implements Listener { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onBlockSpread(BlockSpreadEvent e) { - if (ListenerHelper.isDisabled(e.getClass())) { + if (ListenerHelper.isDisabled(e.getClass()) || + !useEnhanceProtection) { return; } - if (!useEnhanceProtection) { - return; - } - Block newBlock = e.getNewState().getBlock(); - Shop thisBlockShop = plugin.getShopManager().getShopIncludeAttached(newBlock.getLocation()); - Shop underBlockShop = plugin.getShopManager().getShopIncludeAttached(newBlock.getRelative(BlockFace.DOWN).getLocation()); + + final Block newBlock = e.getNewState().getBlock(); + final Shop thisBlockShop = plugin.getShopManager().getShopIncludeAttached(newBlock.getLocation()); + final Shop underBlockShop = plugin.getShopManager().getShopIncludeAttached(newBlock.getRelative(BlockFace.DOWN).getLocation()); + if (thisBlockShop == null && underBlockShop == null) { return; } @@ -110,9 +114,11 @@ public class ShopProtectionListener implements Listener { if (ListenerHelper.isDisabled(e.getClass())) { return; } + for (int i = 0; i < e.blockList().size(); i++) { - Block b = e.blockList().get(i); - Shop shop = plugin.getShopManager().getShopIncludeAttached(b.getLocation()); + final Block b = e.blockList().get(i); + final Shop shop = plugin.getShopManager().getShopIncludeAttached(b.getLocation()); + if (shop != null) { if (plugin.getConfig().getBoolean("protect.explode")) { e.setCancelled(true); @@ -129,58 +135,67 @@ public class ShopProtectionListener implements Listener { if (ListenerHelper.isDisabled(event.getClass())) { return; } - Location loc = event.getSource().getLocation(); + + final Location loc = event.getSource().getLocation(); + if (loc == null) { return; } - HashMap shopsInChunk = plugin.getShopManager().getShops(loc.getChunk()); - if (shopsInChunk == null || shopsInChunk.isEmpty()) { - return; - } - if (shopsInChunk.get(loc) == null) { + + final HashMap shopsInChunk = plugin.getShopManager().getShops(loc.getChunk()); + + if (shopsInChunk == null || shopsInChunk.isEmpty() || shopsInChunk.get(loc) == null) { return; } + event.setCancelled(true); - Location location = event.getInitiator().getLocation(); - if (location != null) { - location.getBlock().breakNaturally(); - MsgUtil.sendGlobalAlert("[DisplayGuard] Breaked the block at " + location + " try steal the items for shop " + loc); + + final Location location = event.getInitiator().getLocation(); + + if (location == null) { + return; } + + location.getBlock().breakNaturally(); + MsgUtil.sendGlobalAlert("[DisplayGuard] Breaked the block at " + location + " try steal the items for shop " + loc); } //Protect Entity pickup shop @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onMobChangeBlock(EntityChangeBlockEvent event) { - if (ListenerHelper.isDisabled(event.getClass())) { + if (ListenerHelper.isDisabled(event.getClass()) || + !useEnhanceProtection) { return; } - if (!useEnhanceProtection) { - return; - } - Shop shop = plugin.getShopManager().getShopIncludeAttached(event.getBlock().getLocation()); + + final Shop shop = plugin.getShopManager().getShopIncludeAttached(event.getBlock().getLocation()); + if (shop == null) { return; } + if (plugin.getConfig().getBoolean("protect.entity")) { event.setCancelled(true); - } else { - shop.delete(); + return; } + + shop.delete(); } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onStructureGrow(StructureGrowEvent event) { - if (ListenerHelper.isDisabled(event.getClass())) { - return; - } - if (!useEnhanceProtection) { + if (ListenerHelper.isDisabled(event.getClass()) || + !useEnhanceProtection) { return; } + for (BlockState blockstate : event.getBlocks()) { - Shop shop = plugin.getShopManager().getShopIncludeAttached(blockstate.getLocation()); + final Shop shop = plugin.getShopManager().getShopIncludeAttached(blockstate.getLocation()); + if (shop == null) { continue; } + event.setCancelled(true); return; //plugin.getLogger().warning("[Exploit Alert] a StructureGrowing tried to break the shop of " + shop); diff --git a/src/main/java/org/maxgamer/quickshop/Listeners/WorldListener.java b/src/main/java/org/maxgamer/quickshop/Listeners/WorldListener.java index 2fa31f46a..aa016d5cb 100644 --- a/src/main/java/org/maxgamer/quickshop/Listeners/WorldListener.java +++ b/src/main/java/org/maxgamer/quickshop/Listeners/WorldListener.java @@ -9,6 +9,7 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.world.WorldLoadEvent; import org.bukkit.event.world.WorldUnloadEvent; +import org.jetbrains.annotations.NotNull; import org.maxgamer.quickshop.QuickShop; import org.maxgamer.quickshop.Shop.Shop; import org.maxgamer.quickshop.Shop.ShopChunk; @@ -18,7 +19,9 @@ import java.util.Map.Entry; @AllArgsConstructor public class WorldListener implements Listener { - private QuickShop plugin; + + @NotNull + private final QuickShop plugin; @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onWorldLoad(WorldLoadEvent e) { @@ -28,24 +31,27 @@ public class WorldListener implements Listener { * world value is the same. * ************************************ */ + final World world = e.getWorld(); - World world = e.getWorld(); plugin.getShopLoader().loadShops(world.getName()); // New world data - HashMap> inWorld = new HashMap<>(1); + final HashMap> inWorld = new HashMap<>(1); // Old world data - HashMap> oldInWorld = plugin.getShopManager().getShops(world.getName()); + final HashMap> oldInWorld = plugin.getShopManager().getShops(world.getName()); // Nothing in the old world, therefore we don't care. No locations to // update. if (oldInWorld == null) { return; } + for (Entry> oldInChunk : oldInWorld.entrySet()) { - HashMap inChunk = new HashMap<>(1); + final HashMap inChunk = new HashMap<>(1); // Put the new chunk were the old chunk was inWorld.put(oldInChunk.getKey(), inChunk); + for (Entry entry : oldInChunk.getValue().entrySet()) { - Shop shop = entry.getValue(); + final Shop shop = entry.getValue(); + shop.getLocation().setWorld(world); inChunk.put(shop.getLocation(), shop); } @@ -56,10 +62,12 @@ public class WorldListener implements Listener { // world first loads.... // So manually tell all of these shops they're loaded. for (Chunk chunk : world.getLoadedChunks()) { - HashMap inChunk = plugin.getShopManager().getShops(chunk); + final HashMap inChunk = plugin.getShopManager().getShops(chunk); + if (inChunk == null || inChunk.isEmpty()) { continue; } + for (Shop shop : inChunk.values()) { shop.onLoad(); } @@ -68,6 +76,7 @@ public class WorldListener implements Listener { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onWorldUnload(WorldUnloadEvent e) { + // FIXME: 24/11/2019 It's not necessary but ok. if (e.isCancelled()) { return; } @@ -75,10 +84,12 @@ public class WorldListener implements Listener { // world unloads, I think... // So manually tell all of these shops they're unloaded. for (Chunk chunk : e.getWorld().getLoadedChunks()) { - HashMap inChunk = plugin.getShopManager().getShops(chunk); + final HashMap inChunk = plugin.getShopManager().getShops(chunk); + if (inChunk == null || inChunk.isEmpty()) { continue; } + for (Shop shop : inChunk.values()) { shop.onUnload(); }