Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to prevent entities from being moved from plot #4554

Merged
merged 1 commit into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -155,7 +146,6 @@ public void onEntityPathfind(SlimePathfindEvent event) {
if (farea == null) {
return;
}

if (tarea != farea) {
event.setCancelled(true);
return;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -372,6 +373,7 @@ private static void setAccessible(Field field) {
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE})
@Documented
public @interface Comment {

String[] value();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading