Prevent blocks moving/generating below and above build height (#3641)

* fix: cancel BlockFormEvent outside of build limit

* refactor: rename variables for easier readability

* fix: cancel liquid flow outside build limit

* refactor: implement to/from context
This commit is contained in:
Bernhard 2022-06-05 20:51:07 +02:00 committed by GitHub
parent ae3b8c06f6
commit 0a32268784
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -551,6 +551,10 @@ public class BlockEventListener implements Listener {
if (plot == null) { if (plot == null) {
return; return;
} }
if (location.getY() >= area.getMaxBuildHeight() || location.getY() < area.getMinBuildHeight()) {
event.setCancelled(true);
return;
}
switch (event.getNewState().getType()) { switch (event.getNewState().getType()) {
case SNOW: case SNOW:
case SNOW_BLOCK: case SNOW_BLOCK:
@ -750,62 +754,66 @@ public class BlockEventListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onChange(BlockFromToEvent event) { public void onChange(BlockFromToEvent event) {
Block from = event.getBlock(); Block fromBlock = event.getBlock();
// Check liquid flow flag inside of origin plot too // Check liquid flow flag inside of origin plot too
final Location fLocation = BukkitUtil.adapt(from.getLocation()); final Location fromLocation = BukkitUtil.adapt(fromBlock.getLocation());
final PlotArea fromArea = fLocation.getPlotArea(); final PlotArea fromArea = fromLocation.getPlotArea();
if (fromArea != null) { if (fromArea != null) {
final Plot plot = fromArea.getOwnedPlot(fLocation); final Plot fromPlot = fromArea.getOwnedPlot(fromLocation);
if (plot != null && plot.getFlag(LiquidFlowFlag.class) == LiquidFlowFlag.FlowStatus.DISABLED && event if (fromPlot != null && fromPlot.getFlag(LiquidFlowFlag.class) == LiquidFlowFlag.FlowStatus.DISABLED && event
.getBlock() .getBlock()
.isLiquid()) { .isLiquid()) {
plot.debug("Liquid could not flow because liquid-flow = disabled"); fromPlot.debug("Liquid could not flow because liquid-flow = disabled");
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
} }
Block to = event.getToBlock(); Block toBlock = event.getToBlock();
Location tLocation = BukkitUtil.adapt(to.getLocation()); Location toLocation = BukkitUtil.adapt(toBlock.getLocation());
PlotArea area = tLocation.getPlotArea(); PlotArea toArea = toLocation.getPlotArea();
if (area == null) { if (toArea == null) {
if (from.getType() == Material.DRAGON_EGG && fromArea != null) { if (fromBlock.getType() == Material.DRAGON_EGG && fromArea != null) {
event.setCancelled(true); event.setCancelled(true);
} }
return; return;
} }
Plot plot = area.getOwnedPlot(tLocation); if (toLocation.getY() >= toArea.getMaxBuildHeight() || toLocation.getY() < toArea.getMinBuildHeight()) {
event.setCancelled(true);
return;
}
Plot toPlot = toArea.getOwnedPlot(toLocation);
if (from.getType() == Material.DRAGON_EGG && fromArea != null) { if (fromBlock.getType() == Material.DRAGON_EGG && fromArea != null) {
final Plot fromPlot = fromArea.getOwnedPlot(fLocation); final Plot fromPlot = fromArea.getOwnedPlot(fromLocation);
if (fromPlot != null || plot != null) { if (fromPlot != null || toPlot != null) {
if ((fromPlot == null || !fromPlot.equals(plot)) && (plot == null || !plot.equals(fromPlot))) { if ((fromPlot == null || !fromPlot.equals(toPlot)) && (toPlot == null || !toPlot.equals(fromPlot))) {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
} }
} }
if (plot != null) { if (toPlot != null) {
if (!area.contains(fLocation.getX(), fLocation.getZ()) || !Objects.equals(plot, area.getOwnedPlot(fLocation))) { if (!toArea.contains(fromLocation.getX(), fromLocation.getZ()) || !Objects.equals(toPlot, toArea.getOwnedPlot(fromLocation))) {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
if (plot.getFlag(LiquidFlowFlag.class) == LiquidFlowFlag.FlowStatus.ENABLED && event.getBlock().isLiquid()) { if (toPlot.getFlag(LiquidFlowFlag.class) == LiquidFlowFlag.FlowStatus.ENABLED && event.getBlock().isLiquid()) {
return; return;
} }
if (plot.getFlag(DisablePhysicsFlag.class)) { if (toPlot.getFlag(DisablePhysicsFlag.class)) {
plot.debug(event.getBlock().getType() + " could not update because disable-physics = true"); toPlot.debug(event.getBlock().getType() + " could not update because disable-physics = true");
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
if (plot.getFlag(LiquidFlowFlag.class) == LiquidFlowFlag.FlowStatus.DISABLED && event.getBlock().isLiquid()) { if (toPlot.getFlag(LiquidFlowFlag.class) == LiquidFlowFlag.FlowStatus.DISABLED && event.getBlock().isLiquid()) {
plot.debug("Liquid could not flow because liquid-flow = disabled"); toPlot.debug("Liquid could not flow because liquid-flow = disabled");
event.setCancelled(true); event.setCancelled(true);
} }
} else if (!area.contains(fLocation.getX(), fLocation.getZ()) || !Objects.equals(null, area.getOwnedPlot(fLocation))) { } else if (!toArea.contains(fromLocation.getX(), fromLocation.getZ()) || !Objects.equals(null, toArea.getOwnedPlot(fromLocation))) {
event.setCancelled(true); event.setCancelled(true);
} else if (event.getBlock().isLiquid()) { } else if (event.getBlock().isLiquid()) {
final org.bukkit.Location location = event.getBlock().getLocation(); final org.bukkit.Location location = event.getBlock().getLocation();