From 960b7b2a8b843e7e8a123390debe803f8a59640e Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Sun, 22 Dec 2024 11:12:10 +0100 Subject: [PATCH] Add option to prevent entities from being moved from plot (#4554) --- .../bukkit/listener/PaperListener.java | 54 ++++++++----------- .../core/configuration/Config.java | 2 + .../core/configuration/Settings.java | 2 + 3 files changed, 26 insertions(+), 32 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PaperListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PaperListener.java index 6de5f32755..aa6474be17 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PaperListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PaperListener.java @@ -47,6 +47,7 @@ import com.plotsquared.core.plot.flag.types.BooleanFlag; import com.plotsquared.core.plot.world.PlotAreaManager; import com.plotsquared.core.util.PlotFlagUtil; +import io.papermc.paper.event.entity.EntityMoveEvent; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.tag.Tag; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; @@ -58,6 +59,7 @@ import org.bukkit.entity.Player; import org.bukkit.entity.Projectile; import org.bukkit.entity.Slime; +import org.bukkit.event.Cancellable; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; @@ -104,33 +106,7 @@ public void onEntityPathfind(EntityPathfindEvent event) { if (!Settings.Paper_Components.ENTITY_PATHING) { return; } - Location toLoc = BukkitUtil.adapt(event.getLoc()); - Location fromLoc = BukkitUtil.adapt(event.getEntity().getLocation()); - PlotArea tarea = toLoc.getPlotArea(); - if (tarea == null) { - return; - } - PlotArea farea = fromLoc.getPlotArea(); - if (farea == null) { - return; - } - if (tarea != farea) { - event.setCancelled(true); - return; - } - Plot tplot = toLoc.getPlot(); - Plot fplot = fromLoc.getPlot(); - if (tplot == null ^ fplot == null) { - event.setCancelled(true); - return; - } - if (tplot == null || tplot.getId().hashCode() == fplot.getId().hashCode()) { - return; - } - if (fplot.isMerged() && fplot.getConnectedPlots().contains(fplot)) { - return; - } - event.setCancelled(true); + handleEntityMovement(event, event.getEntity().getLocation(), event.getLoc()); } @EventHandler @@ -145,8 +121,23 @@ public void onEntityPathfind(SlimePathfindEvent event) { return; } - Location toLoc = BukkitUtil.adapt(b.getLocation()); - Location fromLoc = BukkitUtil.adapt(event.getEntity().getLocation()); + handleEntityMovement(event, event.getEntity().getLocation(), b.getLocation()); + } + + @EventHandler + public void onEntityMove(EntityMoveEvent event) { + if (!Settings.Paper_Components.ENTITY_MOVEMENT) { + return; + } + if (!event.hasExplicitlyChangedBlock()) { + return; + } + handleEntityMovement(event, event.getFrom(), event.getTo()); + } + + private static void handleEntityMovement(Cancellable event, org.bukkit.Location from, org.bukkit.Location target) { + Location toLoc = BukkitUtil.adapt(target); + Location fromLoc = BukkitUtil.adapt(from); PlotArea tarea = toLoc.getPlotArea(); if (tarea == null) { return; @@ -155,7 +146,6 @@ public void onEntityPathfind(SlimePathfindEvent event) { if (farea == null) { return; } - if (tarea != farea) { event.setCancelled(true); return; @@ -166,10 +156,10 @@ public void onEntityPathfind(SlimePathfindEvent event) { event.setCancelled(true); return; } - if (tplot == null || tplot.getId().hashCode() == fplot.getId().hashCode()) { + if (tplot == null || tplot.getId().equals(fplot.getId())) { return; } - if (fplot.isMerged() && fplot.getConnectedPlots().contains(fplot)) { + if (fplot.isMerged() && fplot.getConnectedPlots().contains(tplot)) { return; } event.setCancelled(true); diff --git a/Core/src/main/java/com/plotsquared/core/configuration/Config.java b/Core/src/main/java/com/plotsquared/core/configuration/Config.java index b2b5f90bed..ae18f7f596 100644 --- a/Core/src/main/java/com/plotsquared/core/configuration/Config.java +++ b/Core/src/main/java/com/plotsquared/core/configuration/Config.java @@ -26,6 +26,7 @@ import java.io.File; import java.io.PrintWriter; +import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -372,6 +373,7 @@ private static void setAccessible(Field field) { */ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD, ElementType.TYPE}) + @Documented public @interface Comment { String[] value(); diff --git a/Core/src/main/java/com/plotsquared/core/configuration/Settings.java b/Core/src/main/java/com/plotsquared/core/configuration/Settings.java index 896a294b57..cb0de514cf 100644 --- a/Core/src/main/java/com/plotsquared/core/configuration/Settings.java +++ b/Core/src/main/java/com/plotsquared/core/configuration/Settings.java @@ -651,6 +651,8 @@ public static final class Paper_Components { public static boolean PAPER_LISTENERS = true; @Comment("Prevent entities from leaving plots") public static boolean ENTITY_PATHING = true; + @Comment("Prevent entities from leaving plots, even by pushing or pulling") + public static boolean ENTITY_MOVEMENT = false; @Comment( "Cancel entity spawns when the chunk is loaded if the PlotArea's mob spawning is off") public static boolean CANCEL_CHUNK_SPAWN = true;