From 738f89f5dd8cc706bc688bc76345cd2baa75fb14 Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Mon, 20 Jun 2022 12:11:41 +0100 Subject: [PATCH] Implement util method to check if y is within world build limits --- .../bukkit/listener/BlockEventListener.java | 37 ++++++------------- .../listener/BlockEventListener117.java | 5 +-- .../bukkit/listener/PlayerEventListener.java | 2 +- .../com/plotsquared/core/plot/PlotArea.java | 10 +++++ .../core/util/EventDispatcher.java | 3 +- 5 files changed, 26 insertions(+), 31 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/BlockEventListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/BlockEventListener.java index f4c35090a..8caa59524 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/BlockEventListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/BlockEventListener.java @@ -272,8 +272,7 @@ public class BlockEventListener implements Listener { BukkitPlayer pp = BukkitUtil.adapt(player); Plot plot = area.getPlot(location); if (plot != null) { - if ((location.getY() >= area.getMaxBuildHeight() || location.getY() < area - .getMinBuildHeight()) && !Permissions + if (!area.buildRangeContainsY(location.getY()) && !Permissions .hasPermission(pp, Permission.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT)) { event.setCancelled(true); pp.sendMessage( @@ -359,8 +358,7 @@ public class BlockEventListener implements Listener { event.setCancelled(true); return; } - } else if ((location.getY() >= area.getMaxBuildHeight() || location.getY() < area - .getMinBuildHeight()) && !Permissions + } else if (!area.buildRangeContainsY(location.getY()) && !Permissions .hasPermission(plotPlayer, Permission.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT)) { event.setCancelled(true); plotPlayer.sendMessage( @@ -546,7 +544,7 @@ public class BlockEventListener implements Listener { if (plot == null) { return; } - if (location.getY() >= area.getMaxBuildHeight() || location.getY() < area.getMinBuildHeight()) { + if (!area.buildRangeContainsY(location.getY())) { event.setCancelled(true); return; } @@ -774,7 +772,7 @@ public class BlockEventListener implements Listener { } return; } - if (toLocation.getY() >= toArea.getMaxBuildHeight() || toLocation.getY() < toArea.getMinBuildHeight()) { + if (!toArea.buildRangeContainsY(toLocation.getY())) { event.setCancelled(true); return; } @@ -848,7 +846,7 @@ public class BlockEventListener implements Listener { return; } - if (location.getY() >= area.getMaxBuildHeight() || location.getY() < area.getMinBuildHeight()) { + if (!area.buildRangeContainsY(location.getY())) { event.setCancelled(true); return; } @@ -894,8 +892,6 @@ public class BlockEventListener implements Listener { event.setCancelled(true); return; } - int minBuildHeight = area.getMinBuildHeight(); - int maxBuildHeight = area.getMaxBuildHeight(); for (Block block1 : event.getBlocks()) { Location bloc = BukkitUtil.adapt(block1.getLocation()); Location newLoc = bloc.add(relative.getBlockX(), relative.getBlockY(), relative.getBlockZ()); @@ -907,10 +903,7 @@ public class BlockEventListener implements Listener { event.setCancelled(true); return; } - if (bloc.getY() < minBuildHeight - || bloc.getY() >= maxBuildHeight - || newLoc.getY() < minBuildHeight - || newLoc.getY() >= maxBuildHeight) { + if (!area.buildRangeContainsY(bloc.getY()) || !area.buildRangeContainsY(newLoc.getY())) { event.setCancelled(true); return; } @@ -949,8 +942,6 @@ public class BlockEventListener implements Listener { event.setCancelled(true); return; } - int minBuildHeight = area.getMinBuildHeight(); - int maxBuildHeight = area.getMaxBuildHeight(); for (Block block1 : event.getBlocks()) { Location bloc = BukkitUtil.adapt(block1.getLocation()); Location newLoc = bloc.add(relative.getBlockX(), relative.getBlockY(), relative.getBlockZ()); @@ -962,10 +953,7 @@ public class BlockEventListener implements Listener { event.setCancelled(true); return; } - if (bloc.getY() < minBuildHeight - || bloc.getY() >= maxBuildHeight - || newLoc.getY() < minBuildHeight - || newLoc.getY() >= maxBuildHeight) { + if (!area.buildRangeContainsY(bloc.getY()) || !area.buildRangeContainsY(newLoc.getY())) { event.setCancelled(true); return; } @@ -992,7 +980,7 @@ public class BlockEventListener implements Listener { return; } PlotArea area = location.getPlotArea(); - if (area != null && (location.getY() < area.getMinBuildHeight() || location.getY() >= area.getMaxBuildHeight())) { + if (area != null && !area.buildRangeContainsY(location.getY())) { event.setCancelled(true); } } @@ -1035,7 +1023,7 @@ public class BlockEventListener implements Listener { event.getBlocks().remove(i); continue; } - if (location.getY() < area.getMinBuildHeight() || location.getY() >= area.getMaxBuildHeight()) { + if (!area.buildRangeContainsY(location.getY())) { event.getBlocks().remove(i); } } @@ -1128,8 +1116,7 @@ public class BlockEventListener implements Listener { Plot plot = area.getOwnedPlot(location1); if (player != null) { BukkitPlayer pp = BukkitUtil.adapt(player); - if ((location1.getY() >= area.getMaxBuildHeight() || location1.getY() < area - .getMinBuildHeight()) && !Permissions + if (!area.buildRangeContainsY(location1.getY()) && !Permissions .hasPermission(pp, Permission.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT)) { event.setCancelled(true); pp.sendMessage( @@ -1249,7 +1236,7 @@ public class BlockEventListener implements Listener { if (!Objects.equals(plot, origin)) { return true; } - return blockLocation.getY() < area.getMinBuildHeight() || blockLocation.getY() >= area.getMaxBuildHeight(); + return !area.buildRangeContainsY(location.getY()); }); } if (blocks.isEmpty()) { @@ -1294,7 +1281,7 @@ public class BlockEventListener implements Listener { if (Permissions.hasPermission(pp, Permission.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT)) { continue; } - if (currentLocation.getY() >= area.getMaxBuildHeight() || currentLocation.getY() < area.getMinBuildHeight()) { + if (!area.buildRangeContainsY(currentLocation.getY())) { pp.sendMessage( TranslatableCaption.of("height.height_limit"), Template.of("minHeight", String.valueOf(area.getMinBuildHeight())), diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/BlockEventListener117.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/BlockEventListener117.java index bf24a24cd..c2fc3b048 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/BlockEventListener117.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/BlockEventListener117.java @@ -67,8 +67,7 @@ public class BlockEventListener117 implements Listener { if (entity instanceof Player player) { plotPlayer = BukkitUtil.adapt(player); - if ((location.getY() >= area.getMaxBuildHeight() || location.getY() < area - .getMinBuildHeight()) && !Permissions + if (!area.buildRangeContainsY(location.getY()) && !Permissions .hasPermission(plotPlayer, Permission.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT)) { event.setCancelled(true); plotPlayer.sendMessage( @@ -137,7 +136,7 @@ public class BlockEventListener117 implements Listener { event.getBlocks().remove(i); continue; } - if (blockLocation.getY() < area.getMinBuildHeight() || blockLocation.getY() >= area.getMaxBuildHeight()) { + if (!area.buildRangeContainsY(location.getY())) { event.getBlocks().remove(i); } } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java index f4c7d74ec..801e23a65 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java @@ -1725,7 +1725,7 @@ public class PlayerEventListener extends PlotListener implements Listener { if (area == null) { continue; } - if (pp != null && (location.getY() >= area.getMaxBuildHeight() || location.getY() < area.getMinBuildHeight()) && !Permissions.hasPermission( + if (pp != null && !area.buildRangeContainsY(location.getY()) && !Permissions.hasPermission( pp, Permission.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT )) { diff --git a/Core/src/main/java/com/plotsquared/core/plot/PlotArea.java b/Core/src/main/java/com/plotsquared/core/plot/PlotArea.java index fb42e9881..28771a489 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/PlotArea.java +++ b/Core/src/main/java/com/plotsquared/core/plot/PlotArea.java @@ -624,6 +624,16 @@ public abstract class PlotArea { getRegionAbs() == null || this.region.contains(location.getBlockVector3())); } + /** + * Get if the PlotArea's build range (min build height -> max build height) contains the given y value + * + * @param y y height + * @return if build height contains y + */ + public boolean buildRangeContainsY(int y) { + return y >= minBuildHeight && y < maxBuildHeight; + } + public @NonNull Set getPlotsAbs(final UUID uuid) { if (uuid == null) { return Collections.emptySet(); diff --git a/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java b/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java index 7ecbc2e42..b809b5107 100644 --- a/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java +++ b/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java @@ -332,8 +332,7 @@ public class EventDispatcher { ) { PlotArea area = location.getPlotArea(); assert area != null; - if ((location.getY() >= area.getMaxBuildHeight() || location.getY() < area - .getMinBuildHeight()) && !Permissions + if (!area.buildRangeContainsY(location.getY()) && !Permissions .hasPermission(player, Permission.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT)) { player.sendMessage( TranslatableCaption.of("height.height_limit"),