From 47dcc38e94cd6cfb13bf34a87a5bde5d3acf3130 Mon Sep 17 00:00:00 2001 From: Boy Date: Sun, 28 Apr 2024 14:35:39 +0200 Subject: [PATCH 1/3] feat: add AxiomManipulateEntityEvent --- .../event/AxiomManipulateEntityEvent.java | 72 +++++++++++++++++++ .../ManipulateEntityPacketListener.java | 8 ++- 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/moulberry/axiom/event/AxiomManipulateEntityEvent.java diff --git a/src/main/java/com/moulberry/axiom/event/AxiomManipulateEntityEvent.java b/src/main/java/com/moulberry/axiom/event/AxiomManipulateEntityEvent.java new file mode 100644 index 0000000..794a8ea --- /dev/null +++ b/src/main/java/com/moulberry/axiom/event/AxiomManipulateEntityEvent.java @@ -0,0 +1,72 @@ +package com.moulberry.axiom.event; + +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.UUID; + +public class AxiomManipulateEntityEvent extends Event implements Cancellable { + + private static final HandlerList HANDLERS = new HandlerList(); + + private final Player player; + private final UUID entityUUID; + private final Location toLocation; + + private boolean cancelled = false; + + public AxiomManipulateEntityEvent(Player player, UUID entityUUID, Location toLocation) { + this.player = player; + this.entityUUID = entityUUID; + this.toLocation = toLocation; + } + + public Player getPlayer() { + return player; + } + + public Location getToLocation() { + return toLocation; + } + + /** + * Gets a BukkitEntity from the given UUID. + * Do note this might return null in cases where the UUID was from a client-side entity + * @return The BukkitEntity, if it exists + */ + @Nullable + public Entity getEntity() { + return Bukkit.getEntity(entityUUID); + } + + public UUID getUUID() { + return entityUUID; + } + + @Override + public boolean isCancelled() { + return this.cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } + + public static HandlerList getHandlerList() { + return HANDLERS; + } + + @Override + public @NotNull HandlerList getHandlers() { + return HANDLERS; + } +} diff --git a/src/main/java/com/moulberry/axiom/packet/ManipulateEntityPacketListener.java b/src/main/java/com/moulberry/axiom/packet/ManipulateEntityPacketListener.java index 63d7f31..b6c3ea8 100644 --- a/src/main/java/com/moulberry/axiom/packet/ManipulateEntityPacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/ManipulateEntityPacketListener.java @@ -2,9 +2,11 @@ package com.moulberry.axiom.packet; import com.moulberry.axiom.AxiomPaper; import com.moulberry.axiom.NbtSanitization; +import com.moulberry.axiom.event.AxiomManipulateEntityEvent; import com.moulberry.axiom.integration.Integration; import com.moulberry.axiom.integration.plotsquared.PlotSquaredIntegration; import io.netty.buffer.Unpooled; +import net.kyori.adventure.text.Component; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; @@ -134,6 +136,10 @@ public class ManipulateEntityPacketListener implements PluginMessageListener { double newZ = entry.relativeMovementSet.contains(RelativeMovement.Z) ? entity.position().z + entryPos.z : entryPos.z; float newYaw = entry.relativeMovementSet.contains(RelativeMovement.Y_ROT) ? entity.getYRot() + entry.yaw : entry.yaw; float newPitch = entry.relativeMovementSet.contains(RelativeMovement.X_ROT) ? entity.getXRot() + entry.pitch : entry.pitch; + Location newLocation = new Location(serverLevel.getWorld(), newX, newY, newZ, newYaw, newPitch); + + AxiomManipulateEntityEvent manipulateEvent = new AxiomManipulateEntityEvent(player, entity.getUUID(), newLocation); + if (!manipulateEvent.callEvent()) continue; if (entity instanceof HangingEntity hangingEntity) { float changedYaw = newYaw - entity.getYRot(); @@ -149,7 +155,7 @@ public class ManipulateEntityPacketListener implements PluginMessageListener { if (Integration.canPlaceBlock(player, new Location(player.getWorld(), containing.getX(), containing.getY(), containing.getZ()))) { - entity.teleportTo(serverLevel, newX, newY, newZ, Set.of(), newYaw, newPitch); + entity.teleportTo(serverLevel, newX, newY, newZ, Set.of(), newYaw, newPitch); } entity.setYHeadRot(newYaw); From ba2d7f9913ea1d9fd45a56a4816503781117c0ec Mon Sep 17 00:00:00 2001 From: Boy Date: Sun, 28 Apr 2024 15:05:59 +0200 Subject: [PATCH 2/3] refactor: add more properties to AxiomManipulateEntityEvent --- .../axiom/event/AxiomManipulateEntityEvent.java | 15 ++++++++++++++- .../packet/ManipulateEntityPacketListener.java | 7 ++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/moulberry/axiom/event/AxiomManipulateEntityEvent.java b/src/main/java/com/moulberry/axiom/event/AxiomManipulateEntityEvent.java index 794a8ea..3bfe81f 100644 --- a/src/main/java/com/moulberry/axiom/event/AxiomManipulateEntityEvent.java +++ b/src/main/java/com/moulberry/axiom/event/AxiomManipulateEntityEvent.java @@ -19,13 +19,22 @@ public class AxiomManipulateEntityEvent extends Event implements Cancellable { private final Player player; private final UUID entityUUID; + private final Location fromLocation; private final Location toLocation; private boolean cancelled = false; - public AxiomManipulateEntityEvent(Player player, UUID entityUUID, Location toLocation) { + public AxiomManipulateEntityEvent(Player player, Entity entity, Location toLocation) { + this.player = player; + this.entityUUID = entity.getUniqueId(); + this.fromLocation = entity.getLocation(); + this.toLocation = toLocation; + } + + public AxiomManipulateEntityEvent(Player player, UUID entityUUID, Location fromLocation, Location toLocation) { this.player = player; this.entityUUID = entityUUID; + this.fromLocation = fromLocation; this.toLocation = toLocation; } @@ -33,6 +42,10 @@ public class AxiomManipulateEntityEvent extends Event implements Cancellable { return player; } + public Location getFromLocation() { + return fromLocation; + } + public Location getToLocation() { return toLocation; } diff --git a/src/main/java/com/moulberry/axiom/packet/ManipulateEntityPacketListener.java b/src/main/java/com/moulberry/axiom/packet/ManipulateEntityPacketListener.java index b6c3ea8..34c6184 100644 --- a/src/main/java/com/moulberry/axiom/packet/ManipulateEntityPacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/ManipulateEntityPacketListener.java @@ -120,11 +120,16 @@ public class ManipulateEntityPacketListener implements PluginMessageListener { } if (entry.merge != null && !entry.merge.isEmpty()) { + final Location oldLocation = entity.getBukkitEntity().getLocation(); NbtSanitization.sanitizeEntity(entry.merge); CompoundTag compoundTag = entity.saveWithoutId(new CompoundTag()); compoundTag = merge(compoundTag, entry.merge); entity.load(compoundTag); + + Location newLocation = new Location(serverLevel.getWorld(), position.x, position.y, position.z, entity.getBukkitYaw(), entity.getXRot()); + AxiomManipulateEntityEvent manipulateEvent = new AxiomManipulateEntityEvent(player, entry.uuid, oldLocation, newLocation); + if (!manipulateEvent.callEvent()) continue; } entity.setPosRaw(position.x, position.y, position.z); @@ -138,7 +143,7 @@ public class ManipulateEntityPacketListener implements PluginMessageListener { float newPitch = entry.relativeMovementSet.contains(RelativeMovement.X_ROT) ? entity.getXRot() + entry.pitch : entry.pitch; Location newLocation = new Location(serverLevel.getWorld(), newX, newY, newZ, newYaw, newPitch); - AxiomManipulateEntityEvent manipulateEvent = new AxiomManipulateEntityEvent(player, entity.getUUID(), newLocation); + AxiomManipulateEntityEvent manipulateEvent = new AxiomManipulateEntityEvent(player, entity.getBukkitEntity(), newLocation); if (!manipulateEvent.callEvent()) continue; if (entity instanceof HangingEntity hangingEntity) { From 9364d7c546c4948c03736b6c548fa0b542acc5ba Mon Sep 17 00:00:00 2001 From: Boy Date: Sun, 28 Apr 2024 16:02:24 +0200 Subject: [PATCH 3/3] refactor: call AxiomManipulateEntityEvent before merging nbt for entity --- .../event/AxiomManipulateEntityEvent.java | 38 +++---------------- .../ManipulateEntityPacketListener.java | 14 ++----- 2 files changed, 8 insertions(+), 44 deletions(-) diff --git a/src/main/java/com/moulberry/axiom/event/AxiomManipulateEntityEvent.java b/src/main/java/com/moulberry/axiom/event/AxiomManipulateEntityEvent.java index 3bfe81f..b9567a7 100644 --- a/src/main/java/com/moulberry/axiom/event/AxiomManipulateEntityEvent.java +++ b/src/main/java/com/moulberry/axiom/event/AxiomManipulateEntityEvent.java @@ -1,5 +1,6 @@ package com.moulberry.axiom.event; +import net.minecraft.nbt.CompoundTag; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; @@ -18,50 +19,21 @@ public class AxiomManipulateEntityEvent extends Event implements Cancellable { private static final HandlerList HANDLERS = new HandlerList(); private final Player player; - private final UUID entityUUID; - private final Location fromLocation; - private final Location toLocation; + private final Entity entity; private boolean cancelled = false; - public AxiomManipulateEntityEvent(Player player, Entity entity, Location toLocation) { + public AxiomManipulateEntityEvent(Player player, @NotNull Entity entity) { this.player = player; - this.entityUUID = entity.getUniqueId(); - this.fromLocation = entity.getLocation(); - this.toLocation = toLocation; - } - - public AxiomManipulateEntityEvent(Player player, UUID entityUUID, Location fromLocation, Location toLocation) { - this.player = player; - this.entityUUID = entityUUID; - this.fromLocation = fromLocation; - this.toLocation = toLocation; + this.entity = entity; } public Player getPlayer() { return player; } - public Location getFromLocation() { - return fromLocation; - } - - public Location getToLocation() { - return toLocation; - } - - /** - * Gets a BukkitEntity from the given UUID. - * Do note this might return null in cases where the UUID was from a client-side entity - * @return The BukkitEntity, if it exists - */ - @Nullable public Entity getEntity() { - return Bukkit.getEntity(entityUUID); - } - - public UUID getUUID() { - return entityUUID; + return entity; } @Override diff --git a/src/main/java/com/moulberry/axiom/packet/ManipulateEntityPacketListener.java b/src/main/java/com/moulberry/axiom/packet/ManipulateEntityPacketListener.java index 34c6184..b2f7521 100644 --- a/src/main/java/com/moulberry/axiom/packet/ManipulateEntityPacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/ManipulateEntityPacketListener.java @@ -4,9 +4,7 @@ import com.moulberry.axiom.AxiomPaper; import com.moulberry.axiom.NbtSanitization; import com.moulberry.axiom.event.AxiomManipulateEntityEvent; import com.moulberry.axiom.integration.Integration; -import com.moulberry.axiom.integration.plotsquared.PlotSquaredIntegration; import io.netty.buffer.Unpooled; -import net.kyori.adventure.text.Component; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; @@ -119,17 +117,15 @@ public class ManipulateEntityPacketListener implements PluginMessageListener { continue; } + AxiomManipulateEntityEvent manipulateEvent = new AxiomManipulateEntityEvent(player, entity.getBukkitEntity()); + if (!manipulateEvent.callEvent()) continue; + if (entry.merge != null && !entry.merge.isEmpty()) { - final Location oldLocation = entity.getBukkitEntity().getLocation(); NbtSanitization.sanitizeEntity(entry.merge); CompoundTag compoundTag = entity.saveWithoutId(new CompoundTag()); compoundTag = merge(compoundTag, entry.merge); entity.load(compoundTag); - - Location newLocation = new Location(serverLevel.getWorld(), position.x, position.y, position.z, entity.getBukkitYaw(), entity.getXRot()); - AxiomManipulateEntityEvent manipulateEvent = new AxiomManipulateEntityEvent(player, entry.uuid, oldLocation, newLocation); - if (!manipulateEvent.callEvent()) continue; } entity.setPosRaw(position.x, position.y, position.z); @@ -141,10 +137,6 @@ public class ManipulateEntityPacketListener implements PluginMessageListener { double newZ = entry.relativeMovementSet.contains(RelativeMovement.Z) ? entity.position().z + entryPos.z : entryPos.z; float newYaw = entry.relativeMovementSet.contains(RelativeMovement.Y_ROT) ? entity.getYRot() + entry.yaw : entry.yaw; float newPitch = entry.relativeMovementSet.contains(RelativeMovement.X_ROT) ? entity.getXRot() + entry.pitch : entry.pitch; - Location newLocation = new Location(serverLevel.getWorld(), newX, newY, newZ, newYaw, newPitch); - - AxiomManipulateEntityEvent manipulateEvent = new AxiomManipulateEntityEvent(player, entity.getBukkitEntity(), newLocation); - if (!manipulateEvent.callEvent()) continue; if (entity instanceof HangingEntity hangingEntity) { float changedYaw = newYaw - entity.getYRot();