PaperMC/patches/api/0243-EntityMoveEvent.patch

159 lines
4.9 KiB
Diff
Raw Normal View History

2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: William Blake Galbreath <Blake.Galbreath@GMail.com>
Date: Tue, 11 Feb 2020 21:56:38 -0600
Subject: [PATCH] EntityMoveEvent
diff --git a/src/main/java/io/papermc/paper/event/entity/EntityMoveEvent.java b/src/main/java/io/papermc/paper/event/entity/EntityMoveEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..49ace395393839b3652a537207b4cf5b24beeac0
2021-06-11 14:02:28 +02:00
--- /dev/null
+++ b/src/main/java/io/papermc/paper/event/entity/EntityMoveEvent.java
@@ -0,0 +1,146 @@
2021-06-11 14:02:28 +02:00
+package io.papermc.paper.event.entity;
+
+import com.google.common.base.Preconditions;
+import org.bukkit.Location;
+import org.bukkit.entity.LivingEntity;
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.HandlerList;
+import org.bukkit.event.entity.EntityEvent;
+import org.bukkit.event.player.PlayerMoveEvent;
2024-02-01 10:15:57 +01:00
+import org.jetbrains.annotations.ApiStatus;
+import org.jspecify.annotations.NullMarked;
2021-06-11 14:02:28 +02:00
+
+/**
+ * Holds information for living entity movement events
+ * <p>
+ * Does not fire for players; use {@link PlayerMoveEvent} for player movement.
2021-06-11 14:02:28 +02:00
+ */
+@NullMarked
2021-06-11 14:02:28 +02:00
+public class EntityMoveEvent extends EntityEvent implements Cancellable {
2024-02-01 10:15:57 +01:00
+
+ private static final HandlerList HANDLER_LIST = new HandlerList();
+
2021-06-11 14:02:28 +02:00
+ private Location from;
+ private Location to;
+
2024-02-01 10:15:57 +01:00
+ private boolean cancelled;
+
+ @ApiStatus.Internal
+ public EntityMoveEvent(final LivingEntity entity, final Location from, final Location to) {
2021-06-11 14:02:28 +02:00
+ super(entity);
+ this.from = from;
+ this.to = to;
+ }
+
+ @Override
+ public LivingEntity getEntity() {
2024-02-01 10:15:57 +01:00
+ return (LivingEntity) super.getEntity();
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * Gets the location this entity moved from
+ *
+ * @return Location the entity moved from
+ */
+ public Location getFrom() {
2024-02-01 10:15:57 +01:00
+ return this.from;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * Sets the location to mark as where the entity moved from
+ *
+ * @param from New location to mark as the entity's previous location
+ */
+ public void setFrom(final Location from) {
+ this.validateLocation(from);
2021-06-11 14:02:28 +02:00
+ this.from = from;
+ }
+
+ /**
+ * Gets the location this entity moved to
+ *
+ * @return Location the entity moved to
+ */
+ public Location getTo() {
2024-02-01 10:15:57 +01:00
+ return this.to;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * Sets the location that this entity will move to
+ *
+ * @param to New Location this entity will move to
+ */
+ public void setTo(final Location to) {
+ this.validateLocation(to);
2021-06-11 14:02:28 +02:00
+ this.to = to;
+ }
+
+ /**
+ * Check if the entity has changed position (even within the same block) in the event
+ *
+ * @return whether the entity has changed position or not
+ */
+ public boolean hasChangedPosition() {
+ return this.hasExplicitlyChangedPosition() || !this.from.getWorld().equals(this.to.getWorld());
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * Check if the entity has changed position (even within the same block) in the event, disregarding a possible world change
+ *
+ * @return whether the entity has changed position or not
+ */
+ public boolean hasExplicitlyChangedPosition() {
2024-02-01 10:15:57 +01:00
+ return this.from.getX() != this.to.getX() || this.from.getY() != this.to.getY() || this.from.getZ() != this.to.getZ();
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * Check if the entity has moved to a new block in the event
+ *
+ * @return whether the entity has moved to a new block or not
+ */
+ public boolean hasChangedBlock() {
+ return this.hasExplicitlyChangedBlock() || !this.from.getWorld().equals(this.to.getWorld());
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * Check if the entity has moved to a new block in the event, disregarding a possible world change
+ *
+ * @return whether the entity has moved to a new block or not
+ */
+ public boolean hasExplicitlyChangedBlock() {
2024-02-01 10:15:57 +01:00
+ return this.from.getBlockX() != this.to.getBlockX() || this.from.getBlockY() != this.to.getBlockY() || this.from.getBlockZ() != this.to.getBlockZ();
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * Check if the entity has changed orientation in the event
+ *
+ * @return whether the entity has changed orientation or not
+ */
+ public boolean hasChangedOrientation() {
2024-02-01 10:15:57 +01:00
+ return this.from.getPitch() != this.to.getPitch() || this.from.getYaw() != this.to.getYaw();
2021-06-11 14:02:28 +02:00
+ }
+
+ private void validateLocation(final Location loc) {
2021-06-11 14:02:28 +02:00
+ Preconditions.checkArgument(loc != null, "Cannot use null location!");
+ Preconditions.checkArgument(loc.getWorld() != null, "Cannot use null location with null world!");
+ }
+
+ @Override
2024-02-01 10:15:57 +01:00
+ public boolean isCancelled() {
+ return this.cancelled;
+ }
+
+ @Override
+ public void setCancelled(final boolean cancel) {
2024-02-01 10:15:57 +01:00
+ this.cancelled = cancel;
+ }
+
+ @Override
2021-06-11 14:02:28 +02:00
+ public HandlerList getHandlers() {
2024-02-01 10:15:57 +01:00
+ return HANDLER_LIST;
2021-06-11 14:02:28 +02:00
+ }
+
+ public static HandlerList getHandlerList() {
2024-02-01 10:15:57 +01:00
+ return HANDLER_LIST;
2021-06-11 14:02:28 +02:00
+ }
+}