From ca7e9c9bd8f3a0f901bdd9a476b8f4ac359bf4ba Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Sat, 18 Jun 2022 17:23:29 +0100 Subject: [PATCH] Implement build height limits into more events --- .../bukkit/listener/BlockEventListener.java | 73 +++++++++++++++---- .../listener/BlockEventListener117.java | 52 ++++++++----- .../bukkit/listener/PlayerEventListener.java | 15 +++- .../core/util/EventDispatcher.java | 10 +++ 4 files changed, 112 insertions(+), 38 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 63b44ef63..f4c35090a 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/BlockEventListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/BlockEventListener.java @@ -281,6 +281,7 @@ public class BlockEventListener implements Listener { Template.of("minHeight", String.valueOf(area.getMinBuildHeight())), Template.of("maxHeight", String.valueOf(area.getMaxBuildHeight())) ); + return; } if (!plot.hasOwner()) { if (!Permissions.hasPermission(pp, Permission.PERMISSION_ADMIN_BUILD_UNOWNED)) { @@ -367,6 +368,7 @@ public class BlockEventListener implements Listener { Template.of("minHeight", String.valueOf(area.getMinBuildHeight())), Template.of("maxHeight", String.valueOf(area.getMaxBuildHeight())) ); + return; } if (!plot.hasOwner()) { if (!Permissions @@ -846,6 +848,11 @@ public class BlockEventListener implements Listener { return; } + if (location.getY() >= area.getMaxBuildHeight() || location.getY() < area.getMinBuildHeight()) { + event.setCancelled(true); + return; + } + Plot plot = location.getOwnedPlot(); if (plot == null || !plot.getFlag(CropGrowFlag.class)) { if (plot != null) { @@ -887,17 +894,23 @@ 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()); - if (!area.contains(bloc.getX(), bloc.getZ()) || !area.contains( - bloc.getX() + relative.getBlockX(), - bloc.getZ() + relative.getBlockZ() - )) { + Location newLoc = bloc.add(relative.getBlockX(), relative.getBlockY(), relative.getBlockZ()); + if (!area.contains(bloc.getX(), bloc.getZ()) || !area.contains(newLoc)) { event.setCancelled(true); return; } - if (!plot.equals(area.getOwnedPlot(bloc)) || !plot - .equals(area.getOwnedPlot(bloc.add(relative.getBlockX(), relative.getBlockY(), relative.getBlockZ())))) { + if (!plot.equals(area.getOwnedPlot(bloc)) || !plot.equals(area.getOwnedPlot(newLoc))) { + event.setCancelled(true); + return; + } + if (bloc.getY() < minBuildHeight + || bloc.getY() >= maxBuildHeight + || newLoc.getY() < minBuildHeight + || newLoc.getY() >= maxBuildHeight) { event.setCancelled(true); return; } @@ -923,9 +936,8 @@ public class BlockEventListener implements Listener { } for (Block block1 : event.getBlocks()) { Location bloc = BukkitUtil.adapt(block1.getLocation()); - if (bloc.isPlotArea() || bloc - .add(relative.getBlockX(), relative.getBlockY(), relative.getBlockZ()) - .isPlotArea()) { + Location newLoc = bloc.add(relative.getBlockX(), relative.getBlockY(), relative.getBlockZ()); + if (bloc.isPlotArea() || newLoc.isPlotArea()) { event.setCancelled(true); return; } @@ -937,17 +949,23 @@ 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()); - if (!area.contains(bloc.getX(), bloc.getZ()) || !area.contains( - bloc.getX() + relative.getBlockX(), - bloc.getZ() + relative.getBlockZ() - )) { + Location newLoc = bloc.add(relative.getBlockX(), relative.getBlockY(), relative.getBlockZ()); + if (!area.contains(bloc.getX(), bloc.getZ()) || !area.contains(newLoc)) { event.setCancelled(true); return; } - if (!plot.equals(area.getOwnedPlot(bloc)) || !plot - .equals(area.getOwnedPlot(bloc.add(relative.getBlockX(), relative.getBlockY(), relative.getBlockZ())))) { + if (!plot.equals(area.getOwnedPlot(bloc)) || !plot.equals(area.getOwnedPlot(newLoc))) { + event.setCancelled(true); + return; + } + if (bloc.getY() < minBuildHeight + || bloc.getY() >= maxBuildHeight + || newLoc.getY() < minBuildHeight + || newLoc.getY() >= maxBuildHeight) { event.setCancelled(true); return; } @@ -971,6 +989,11 @@ public class BlockEventListener implements Listener { Location location = BukkitUtil.adapt(event.getBlock().getRelative(targetFace).getLocation()); if (location.isPlotRoad()) { event.setCancelled(true); + return; + } + PlotArea area = location.getPlotArea(); + if (area != null && (location.getY() < area.getMinBuildHeight() || location.getY() >= area.getMaxBuildHeight())) { + event.setCancelled(true); } } } @@ -1010,6 +1033,10 @@ public class BlockEventListener implements Listener { Plot plot = area.getOwnedPlot(location); if (!Objects.equals(plot, origin)) { event.getBlocks().remove(i); + continue; + } + if (location.getY() < area.getMinBuildHeight() || location.getY() >= area.getMaxBuildHeight()) { + event.getBlocks().remove(i); } } } @@ -1101,6 +1128,17 @@ 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 + .hasPermission(pp, Permission.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT)) { + event.setCancelled(true); + pp.sendMessage( + TranslatableCaption.of("height.height_limit"), + Template.of("minHeight", String.valueOf(area.getMinBuildHeight())), + Template.of("maxHeight", String.valueOf(area.getMaxBuildHeight())) + ); + return; + } if (plot == null) { if (!Permissions.hasPermission(pp, Permission.PERMISSION_ADMIN_BUILD_ROAD)) { pp.sendMessage( @@ -1208,7 +1246,10 @@ public class BlockEventListener implements Listener { return true; } Plot plot = area.getOwnedPlot(blockLocation); - return !Objects.equals(plot, origin); + if (!Objects.equals(plot, origin)) { + return true; + } + return blockLocation.getY() < area.getMinBuildHeight() || blockLocation.getY() >= area.getMaxBuildHeight(); }); } if (blocks.isEmpty()) { 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 a4e672959..bf24a24cd 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/BlockEventListener117.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/BlockEventListener117.java @@ -21,11 +21,15 @@ package com.plotsquared.bukkit.listener; import com.google.inject.Inject; import com.plotsquared.bukkit.player.BukkitPlayer; import com.plotsquared.bukkit.util.BukkitUtil; +import com.plotsquared.core.configuration.caption.TranslatableCaption; import com.plotsquared.core.location.Location; +import com.plotsquared.core.permissions.Permission; import com.plotsquared.core.plot.Plot; import com.plotsquared.core.plot.PlotArea; import com.plotsquared.core.plot.flag.implementations.CopperOxideFlag; import com.plotsquared.core.plot.flag.implementations.MiscInteractFlag; +import com.plotsquared.core.util.Permissions; +import net.kyori.adventure.text.minimessage.Template; import org.bukkit.block.Block; import org.bukkit.entity.Entity; import org.bukkit.entity.Item; @@ -59,10 +63,26 @@ public class BlockEventListener117 implements Listener { return; } + BukkitPlayer plotPlayer = null; + + if (entity instanceof Player player) { + plotPlayer = BukkitUtil.adapt(player); + if ((location.getY() >= area.getMaxBuildHeight() || location.getY() < area + .getMinBuildHeight()) && !Permissions + .hasPermission(plotPlayer, Permission.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT)) { + event.setCancelled(true); + plotPlayer.sendMessage( + TranslatableCaption.of("height.height_limit"), + Template.of("minHeight", String.valueOf(area.getMinBuildHeight())), + Template.of("maxHeight", String.valueOf(area.getMaxBuildHeight())) + ); + return; + } + } + Plot plot = location.getOwnedPlot(); if (plot == null || !plot.getFlag(MiscInteractFlag.class)) { - if (entity instanceof Player player) { - BukkitPlayer plotPlayer = BukkitUtil.adapt(player); + if (plotPlayer != null) { if (plot != null) { if (!plot.isAdded(plotPlayer.getUUID())) { plot.debug(plotPlayer.getName() + " couldn't trigger sculk sensors because misc-interact = false"); @@ -94,12 +114,12 @@ public class BlockEventListener117 implements Listener { PlotArea area = location.getPlotArea(); if (area == null) { for (int i = blocks.size() - 1; i >= 0; i--) { - location = BukkitUtil.adapt(blocks.get(i).getLocation()); - if (location.isPlotArea()) { + Location blockLocation = BukkitUtil.adapt(blocks.get(i).getLocation()); + blockLocation = BukkitUtil.adapt(blocks.get(i).getLocation()); + if (blockLocation.isPlotArea()) { blocks.remove(i); } } - return; } else { Plot origin = area.getOwnedPlot(location); if (origin == null) { @@ -107,27 +127,19 @@ public class BlockEventListener117 implements Listener { return; } for (int i = blocks.size() - 1; i >= 0; i--) { - location = BukkitUtil.adapt(blocks.get(i).getLocation()); - if (!area.contains(location.getX(), location.getZ())) { + Location blockLocation = BukkitUtil.adapt(blocks.get(i).getLocation()); + if (!area.contains(blockLocation.getX(), blockLocation.getZ())) { blocks.remove(i); continue; } - Plot plot = area.getOwnedPlot(location); + Plot plot = area.getOwnedPlot(blockLocation); if (!Objects.equals(plot, origin)) { event.getBlocks().remove(i); + continue; + } + if (blockLocation.getY() < area.getMinBuildHeight() || blockLocation.getY() >= area.getMaxBuildHeight()) { + event.getBlocks().remove(i); } - } - } - Plot origin = area.getPlot(location); - if (origin == null) { - event.setCancelled(true); - return; - } - for (int i = blocks.size() - 1; i >= 0; i--) { - location = BukkitUtil.adapt(blocks.get(i).getLocation()); - Plot plot = area.getOwnedPlot(location); - if (!Objects.equals(plot, origin) && (!plot.isMerged() && !origin.isMerged())) { - 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 6199420d6..f4c7d74ec 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java @@ -145,13 +145,11 @@ import org.bukkit.util.Vector; import org.checkerframework.checker.nullness.qual.NonNull; import java.lang.reflect.Field; -import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Locale; import java.util.Set; import java.util.UUID; -import java.util.regex.Pattern; /** * Player Events involving plots. @@ -1705,6 +1703,7 @@ public class PlayerEventListener extends PlotListener implements Listener { if (PlotSquared.get().getPlotAreaManager().getPlotAreasSet(world).size() == 0) { return; } + BukkitPlayer pp = (event.getEntity() instanceof Player player) ? BukkitUtil.adapt(player) : null; int minX = Integer.MAX_VALUE; int maxX = Integer.MIN_VALUE; int minZ = Integer.MAX_VALUE; @@ -1726,6 +1725,18 @@ public class PlayerEventListener extends PlotListener implements Listener { if (area == null) { continue; } + if (pp != null && (location.getY() >= area.getMaxBuildHeight() || location.getY() < area.getMinBuildHeight()) && !Permissions.hasPermission( + pp, + Permission.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT + )) { + event.setCancelled(true); + pp.sendMessage( + TranslatableCaption.of("height.height_limit"), + Template.of("minHeight", String.valueOf(area.getMinBuildHeight())), + Template.of("maxHeight", String.valueOf(area.getMaxBuildHeight())) + ); + return; + } Plot plot = location.getOwnedPlot(); if (plot == null) { if (area.isRoadFlags() && area.getRoadFlag(DenyPortalsFlag.class)) { 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 7285e1981..7ecbc2e42 100644 --- a/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java +++ b/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java @@ -332,6 +332,16 @@ public class EventDispatcher { ) { PlotArea area = location.getPlotArea(); assert area != null; + if ((location.getY() >= area.getMaxBuildHeight() || location.getY() < area + .getMinBuildHeight()) && !Permissions + .hasPermission(player, Permission.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT)) { + player.sendMessage( + TranslatableCaption.of("height.height_limit"), + Template.of("minHeight", String.valueOf(area.getMinBuildHeight())), + Template.of("maxHeight", String.valueOf(area.getMaxBuildHeight())) + ); + return false; + } Plot plot = area.getPlot(location); if (plot != null) { if (plot.isAdded(player.getUUID())) {