Implement util method to check if y is within world build limits

This commit is contained in:
dordsor21 2022-06-20 12:11:41 +01:00
parent ca7e9c9bd8
commit 738f89f5dd
No known key found for this signature in database
GPG Key ID: 1E53E88969FFCF0B
5 changed files with 26 additions and 31 deletions

View File

@ -272,8 +272,7 @@ public class BlockEventListener implements Listener {
BukkitPlayer pp = BukkitUtil.adapt(player); BukkitPlayer pp = BukkitUtil.adapt(player);
Plot plot = area.getPlot(location); Plot plot = area.getPlot(location);
if (plot != null) { if (plot != null) {
if ((location.getY() >= area.getMaxBuildHeight() || location.getY() < area if (!area.buildRangeContainsY(location.getY()) && !Permissions
.getMinBuildHeight()) && !Permissions
.hasPermission(pp, Permission.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT)) { .hasPermission(pp, Permission.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT)) {
event.setCancelled(true); event.setCancelled(true);
pp.sendMessage( pp.sendMessage(
@ -359,8 +358,7 @@ public class BlockEventListener implements Listener {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
} else if ((location.getY() >= area.getMaxBuildHeight() || location.getY() < area } else if (!area.buildRangeContainsY(location.getY()) && !Permissions
.getMinBuildHeight()) && !Permissions
.hasPermission(plotPlayer, Permission.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT)) { .hasPermission(plotPlayer, Permission.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT)) {
event.setCancelled(true); event.setCancelled(true);
plotPlayer.sendMessage( plotPlayer.sendMessage(
@ -546,7 +544,7 @@ public class BlockEventListener implements Listener {
if (plot == null) { if (plot == null) {
return; return;
} }
if (location.getY() >= area.getMaxBuildHeight() || location.getY() < area.getMinBuildHeight()) { if (!area.buildRangeContainsY(location.getY())) {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
@ -774,7 +772,7 @@ public class BlockEventListener implements Listener {
} }
return; return;
} }
if (toLocation.getY() >= toArea.getMaxBuildHeight() || toLocation.getY() < toArea.getMinBuildHeight()) { if (!toArea.buildRangeContainsY(toLocation.getY())) {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
@ -848,7 +846,7 @@ public class BlockEventListener implements Listener {
return; return;
} }
if (location.getY() >= area.getMaxBuildHeight() || location.getY() < area.getMinBuildHeight()) { if (!area.buildRangeContainsY(location.getY())) {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
@ -894,8 +892,6 @@ public class BlockEventListener implements Listener {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
int minBuildHeight = area.getMinBuildHeight();
int maxBuildHeight = area.getMaxBuildHeight();
for (Block block1 : event.getBlocks()) { for (Block block1 : event.getBlocks()) {
Location bloc = BukkitUtil.adapt(block1.getLocation()); Location bloc = BukkitUtil.adapt(block1.getLocation());
Location newLoc = bloc.add(relative.getBlockX(), relative.getBlockY(), relative.getBlockZ()); Location newLoc = bloc.add(relative.getBlockX(), relative.getBlockY(), relative.getBlockZ());
@ -907,10 +903,7 @@ public class BlockEventListener implements Listener {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
if (bloc.getY() < minBuildHeight if (!area.buildRangeContainsY(bloc.getY()) || !area.buildRangeContainsY(newLoc.getY())) {
|| bloc.getY() >= maxBuildHeight
|| newLoc.getY() < minBuildHeight
|| newLoc.getY() >= maxBuildHeight) {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
@ -949,8 +942,6 @@ public class BlockEventListener implements Listener {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
int minBuildHeight = area.getMinBuildHeight();
int maxBuildHeight = area.getMaxBuildHeight();
for (Block block1 : event.getBlocks()) { for (Block block1 : event.getBlocks()) {
Location bloc = BukkitUtil.adapt(block1.getLocation()); Location bloc = BukkitUtil.adapt(block1.getLocation());
Location newLoc = bloc.add(relative.getBlockX(), relative.getBlockY(), relative.getBlockZ()); Location newLoc = bloc.add(relative.getBlockX(), relative.getBlockY(), relative.getBlockZ());
@ -962,10 +953,7 @@ public class BlockEventListener implements Listener {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
if (bloc.getY() < minBuildHeight if (!area.buildRangeContainsY(bloc.getY()) || !area.buildRangeContainsY(newLoc.getY())) {
|| bloc.getY() >= maxBuildHeight
|| newLoc.getY() < minBuildHeight
|| newLoc.getY() >= maxBuildHeight) {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
@ -992,7 +980,7 @@ public class BlockEventListener implements Listener {
return; return;
} }
PlotArea area = location.getPlotArea(); 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); event.setCancelled(true);
} }
} }
@ -1035,7 +1023,7 @@ public class BlockEventListener implements Listener {
event.getBlocks().remove(i); event.getBlocks().remove(i);
continue; continue;
} }
if (location.getY() < area.getMinBuildHeight() || location.getY() >= area.getMaxBuildHeight()) { if (!area.buildRangeContainsY(location.getY())) {
event.getBlocks().remove(i); event.getBlocks().remove(i);
} }
} }
@ -1128,8 +1116,7 @@ public class BlockEventListener implements Listener {
Plot plot = area.getOwnedPlot(location1); Plot plot = area.getOwnedPlot(location1);
if (player != null) { if (player != null) {
BukkitPlayer pp = BukkitUtil.adapt(player); BukkitPlayer pp = BukkitUtil.adapt(player);
if ((location1.getY() >= area.getMaxBuildHeight() || location1.getY() < area if (!area.buildRangeContainsY(location1.getY()) && !Permissions
.getMinBuildHeight()) && !Permissions
.hasPermission(pp, Permission.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT)) { .hasPermission(pp, Permission.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT)) {
event.setCancelled(true); event.setCancelled(true);
pp.sendMessage( pp.sendMessage(
@ -1249,7 +1236,7 @@ public class BlockEventListener implements Listener {
if (!Objects.equals(plot, origin)) { if (!Objects.equals(plot, origin)) {
return true; return true;
} }
return blockLocation.getY() < area.getMinBuildHeight() || blockLocation.getY() >= area.getMaxBuildHeight(); return !area.buildRangeContainsY(location.getY());
}); });
} }
if (blocks.isEmpty()) { if (blocks.isEmpty()) {
@ -1294,7 +1281,7 @@ public class BlockEventListener implements Listener {
if (Permissions.hasPermission(pp, Permission.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT)) { if (Permissions.hasPermission(pp, Permission.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT)) {
continue; continue;
} }
if (currentLocation.getY() >= area.getMaxBuildHeight() || currentLocation.getY() < area.getMinBuildHeight()) { if (!area.buildRangeContainsY(currentLocation.getY())) {
pp.sendMessage( pp.sendMessage(
TranslatableCaption.of("height.height_limit"), TranslatableCaption.of("height.height_limit"),
Template.of("minHeight", String.valueOf(area.getMinBuildHeight())), Template.of("minHeight", String.valueOf(area.getMinBuildHeight())),

View File

@ -67,8 +67,7 @@ public class BlockEventListener117 implements Listener {
if (entity instanceof Player player) { if (entity instanceof Player player) {
plotPlayer = BukkitUtil.adapt(player); plotPlayer = BukkitUtil.adapt(player);
if ((location.getY() >= area.getMaxBuildHeight() || location.getY() < area if (!area.buildRangeContainsY(location.getY()) && !Permissions
.getMinBuildHeight()) && !Permissions
.hasPermission(plotPlayer, Permission.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT)) { .hasPermission(plotPlayer, Permission.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT)) {
event.setCancelled(true); event.setCancelled(true);
plotPlayer.sendMessage( plotPlayer.sendMessage(
@ -137,7 +136,7 @@ public class BlockEventListener117 implements Listener {
event.getBlocks().remove(i); event.getBlocks().remove(i);
continue; continue;
} }
if (blockLocation.getY() < area.getMinBuildHeight() || blockLocation.getY() >= area.getMaxBuildHeight()) { if (!area.buildRangeContainsY(location.getY())) {
event.getBlocks().remove(i); event.getBlocks().remove(i);
} }
} }

View File

@ -1725,7 +1725,7 @@ public class PlayerEventListener extends PlotListener implements Listener {
if (area == null) { if (area == null) {
continue; continue;
} }
if (pp != null && (location.getY() >= area.getMaxBuildHeight() || location.getY() < area.getMinBuildHeight()) && !Permissions.hasPermission( if (pp != null && !area.buildRangeContainsY(location.getY()) && !Permissions.hasPermission(
pp, pp,
Permission.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT Permission.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT
)) { )) {

View File

@ -624,6 +624,16 @@ public abstract class PlotArea {
getRegionAbs() == null || this.region.contains(location.getBlockVector3())); 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<Plot> getPlotsAbs(final UUID uuid) { public @NonNull Set<Plot> getPlotsAbs(final UUID uuid) {
if (uuid == null) { if (uuid == null) {
return Collections.emptySet(); return Collections.emptySet();

View File

@ -332,8 +332,7 @@ public class EventDispatcher {
) { ) {
PlotArea area = location.getPlotArea(); PlotArea area = location.getPlotArea();
assert area != null; assert area != null;
if ((location.getY() >= area.getMaxBuildHeight() || location.getY() < area if (!area.buildRangeContainsY(location.getY()) && !Permissions
.getMinBuildHeight()) && !Permissions
.hasPermission(player, Permission.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT)) { .hasPermission(player, Permission.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT)) {
player.sendMessage( player.sendMessage(
TranslatableCaption.of("height.height_limit"), TranslatableCaption.of("height.height_limit"),