From 9c474570c8b40acd5f66e1f7ec31becbd3102cd3 Mon Sep 17 00:00:00 2001 From: Jordan Date: Wed, 5 Jan 2022 21:21:25 +0100 Subject: [PATCH] Fix: use portal corners to test for event cancellation as entity can be null (#3417) * Fix: use portal corners to test for event cancellation as entity can be null * Remove duplicate flag check? * Simplify to Set#of --- .../bukkit/listener/PlayerEventListener.java | 53 ++++++++++++++----- 1 file changed, 40 insertions(+), 13 deletions(-) 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 2685ce2ca..ef3fcdf74 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java @@ -68,11 +68,9 @@ import com.plotsquared.core.plot.flag.implementations.PlayerInteractFlag; import com.plotsquared.core.plot.flag.implementations.PreventCreativeCopyFlag; import com.plotsquared.core.plot.flag.implementations.TamedInteractFlag; import com.plotsquared.core.plot.flag.implementations.UntrustedVisitFlag; -import com.plotsquared.core.plot.flag.implementations.UseFlag; import com.plotsquared.core.plot.flag.implementations.VehicleBreakFlag; import com.plotsquared.core.plot.flag.implementations.VehicleUseFlag; import com.plotsquared.core.plot.flag.implementations.VillagerInteractFlag; -import com.plotsquared.core.plot.flag.types.BlockTypeWrapper; import com.plotsquared.core.plot.world.PlotAreaManager; import com.plotsquared.core.util.EventDispatcher; import com.plotsquared.core.util.MathMan; @@ -94,6 +92,7 @@ import org.bukkit.FluidCollisionMode; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; +import org.bukkit.block.BlockState; import org.bukkit.block.data.Waterlogged; import org.bukkit.command.PluginCommand; import org.bukkit.entity.ArmorStand; @@ -155,6 +154,7 @@ import org.checkerframework.checker.nullness.qual.NonNull; import java.lang.reflect.Field; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Locale; @@ -1702,22 +1702,49 @@ public class PlayerEventListener extends PlotListener implements Listener { @EventHandler public void onPortalCreation(PortalCreateEvent event) { - Location location = BukkitUtil.adapt(event.getEntity().getLocation()); - PlotArea area = location.getPlotArea(); - if (area == null) { + String world = event.getWorld().getName(); + if (PlotSquared.get().getPlotAreaManager().getPlotAreasSet(world).size() == 0) { return; } - Plot plot = location.getOwnedPlot(); - if (plot == null) { - if (area.isRoadFlags() && area.getRoadFlag(DenyPortalsFlag.class)) { - event.setCancelled(true); + int minX = Integer.MAX_VALUE; + int maxX = Integer.MIN_VALUE; + int minZ = Integer.MAX_VALUE; + int maxZ = Integer.MIN_VALUE; + for (BlockState state : event.getBlocks()) { + minX = Math.min(state.getX(), minX); + maxX = Math.max(state.getX(), maxX); + minZ = Math.min(state.getZ(), minZ); + maxZ = Math.max(state.getZ(), maxZ); + } + int y = event.getBlocks().get(0).getY(); // Don't need to worry about this too much + for (Location location : Set.of( // Use Set to lazily avoid duplicate locations + Location.at(world, minX, y, maxX), + Location.at(world, minZ, y, maxZ), + Location.at(world, minX, y, maxZ), + Location.at(world, minZ, y, maxX) + )) { + PlotArea area = location.getPlotArea(); + if (area == null) { + continue; + } + Plot plot = location.getOwnedPlot(); + if (plot == null) { + if (area.isRoadFlags() && area.getRoadFlag(DenyPortalsFlag.class)) { + event.setCancelled(true); + return; + } + continue; } - return; - } - if (plot.getFlag(DenyPortalsFlag.class)) { if (plot.getFlag(DenyPortalsFlag.class)) { - plot.debug(event.getEntity().getName() + " did not create a portal because of deny-portals = true"); + StringBuilder builder = new StringBuilder(); + if (event.getEntity() != null) { + builder.append(event.getEntity().getName()).append(" did not create a portal"); + } else { + builder.append("Portal creation cancelled"); + } + plot.debug(builder.append(" because of deny-portals = true").toString()); event.setCancelled(true); + return; } } }