From 96f60334504caac357f43c20536f01a3926f77db Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Sat, 16 Mar 2024 11:10:48 -0700 Subject: [PATCH] Clone mutables to prevent unexpected issues There are lots of locations in the API where mutable types are not cloned, either on return or when passed as a parameter and assigned to a field, which can cause unexpected behaviors. Let this be a lesson to use immutable types for simple things Location, Vector, and others. --- .../main/java/org/bukkit/event/block/BlockCanBuildEvent.java | 2 +- .../java/org/bukkit/event/entity/EntityChangeBlockEvent.java | 2 +- .../main/java/org/bukkit/event/entity/EntityExplodeEvent.java | 2 +- .../java/org/bukkit/event/entity/EntityPortalEnterEvent.java | 2 +- .../main/java/org/bukkit/event/entity/ItemDespawnEvent.java | 2 +- .../org/bukkit/event/vehicle/VehicleBlockCollisionEvent.java | 2 +- .../main/java/org/bukkit/event/vehicle/VehicleMoveEvent.java | 4 ++-- .../main/java/org/bukkit/event/world/GenericGameEvent.java | 2 +- .../main/java/org/bukkit/event/world/SpawnChangeEvent.java | 2 +- .../main/java/org/bukkit/event/world/StructureGrowEvent.java | 2 +- 10 files changed, 11 insertions(+), 11 deletions(-) diff --git a/paper-api/src/main/java/org/bukkit/event/block/BlockCanBuildEvent.java b/paper-api/src/main/java/org/bukkit/event/block/BlockCanBuildEvent.java index 31fd64187f..f5923049bf 100644 --- a/paper-api/src/main/java/org/bukkit/event/block/BlockCanBuildEvent.java +++ b/paper-api/src/main/java/org/bukkit/event/block/BlockCanBuildEvent.java @@ -102,7 +102,7 @@ public class BlockCanBuildEvent extends BlockEvent { */ @NotNull public BlockData getBlockData() { - return blockData; + return blockData.clone(); // Paper - clone because mutation isn't used } /** diff --git a/paper-api/src/main/java/org/bukkit/event/entity/EntityChangeBlockEvent.java b/paper-api/src/main/java/org/bukkit/event/entity/EntityChangeBlockEvent.java index 1a9575ad4c..24e1a49e48 100644 --- a/paper-api/src/main/java/org/bukkit/event/entity/EntityChangeBlockEvent.java +++ b/paper-api/src/main/java/org/bukkit/event/entity/EntityChangeBlockEvent.java @@ -61,7 +61,7 @@ public class EntityChangeBlockEvent extends EntityEvent implements Cancellable { */ @NotNull public BlockData getBlockData() { - return to; + return to.clone(); // Paper - clone because mutation isn't used } @NotNull diff --git a/paper-api/src/main/java/org/bukkit/event/entity/EntityExplodeEvent.java b/paper-api/src/main/java/org/bukkit/event/entity/EntityExplodeEvent.java index 50161d313c..e468e55d42 100644 --- a/paper-api/src/main/java/org/bukkit/event/entity/EntityExplodeEvent.java +++ b/paper-api/src/main/java/org/bukkit/event/entity/EntityExplodeEvent.java @@ -72,7 +72,7 @@ public class EntityExplodeEvent extends EntityEvent implements Cancellable { */ @NotNull public Location getLocation() { - return location; + return location.clone(); // Paper - clone to avoid changes } /** diff --git a/paper-api/src/main/java/org/bukkit/event/entity/EntityPortalEnterEvent.java b/paper-api/src/main/java/org/bukkit/event/entity/EntityPortalEnterEvent.java index d3724db0a5..8b2caf665b 100644 --- a/paper-api/src/main/java/org/bukkit/event/entity/EntityPortalEnterEvent.java +++ b/paper-api/src/main/java/org/bukkit/event/entity/EntityPortalEnterEvent.java @@ -35,7 +35,7 @@ public class EntityPortalEnterEvent extends EntityEvent implements org.bukkit.ev */ @NotNull public Location getLocation() { - return location; + return location.clone(); // Paper - clone to avoid changes } // Paper start diff --git a/paper-api/src/main/java/org/bukkit/event/entity/ItemDespawnEvent.java b/paper-api/src/main/java/org/bukkit/event/entity/ItemDespawnEvent.java index 6fc66197eb..2bb29fa449 100644 --- a/paper-api/src/main/java/org/bukkit/event/entity/ItemDespawnEvent.java +++ b/paper-api/src/main/java/org/bukkit/event/entity/ItemDespawnEvent.java @@ -46,7 +46,7 @@ public class ItemDespawnEvent extends EntityEvent implements Cancellable { */ @NotNull public Location getLocation() { - return location; + return location.clone(); // Paper - clone to avoid changes } @NotNull diff --git a/paper-api/src/main/java/org/bukkit/event/vehicle/VehicleBlockCollisionEvent.java b/paper-api/src/main/java/org/bukkit/event/vehicle/VehicleBlockCollisionEvent.java index d0a437bd8a..50fad23cf4 100644 --- a/paper-api/src/main/java/org/bukkit/event/vehicle/VehicleBlockCollisionEvent.java +++ b/paper-api/src/main/java/org/bukkit/event/vehicle/VehicleBlockCollisionEvent.java @@ -31,7 +31,7 @@ public class VehicleBlockCollisionEvent extends VehicleCollisionEvent { */ @NotNull public org.bukkit.util.Vector getVelocity() { - return velocity; + return velocity.clone(); } // Paper end diff --git a/paper-api/src/main/java/org/bukkit/event/vehicle/VehicleMoveEvent.java b/paper-api/src/main/java/org/bukkit/event/vehicle/VehicleMoveEvent.java index 7bfb84d394..fc4cf7b21b 100644 --- a/paper-api/src/main/java/org/bukkit/event/vehicle/VehicleMoveEvent.java +++ b/paper-api/src/main/java/org/bukkit/event/vehicle/VehicleMoveEvent.java @@ -27,7 +27,7 @@ public class VehicleMoveEvent extends VehicleEvent { */ @NotNull public Location getFrom() { - return from; + return from.clone(); // Paper - clone to avoid changes } /** @@ -37,7 +37,7 @@ public class VehicleMoveEvent extends VehicleEvent { */ @NotNull public Location getTo() { - return to; + return to.clone(); // Paper - clone to avoid changes } diff --git a/paper-api/src/main/java/org/bukkit/event/world/GenericGameEvent.java b/paper-api/src/main/java/org/bukkit/event/world/GenericGameEvent.java index 2a2a329877..fb975fefc7 100644 --- a/paper-api/src/main/java/org/bukkit/event/world/GenericGameEvent.java +++ b/paper-api/src/main/java/org/bukkit/event/world/GenericGameEvent.java @@ -49,7 +49,7 @@ public class GenericGameEvent extends WorldEvent implements Cancellable { */ @NotNull public Location getLocation() { - return location; + return location.clone(); // Paper - clone to avoid changes } /** diff --git a/paper-api/src/main/java/org/bukkit/event/world/SpawnChangeEvent.java b/paper-api/src/main/java/org/bukkit/event/world/SpawnChangeEvent.java index 9ce93d0093..73997ae04f 100644 --- a/paper-api/src/main/java/org/bukkit/event/world/SpawnChangeEvent.java +++ b/paper-api/src/main/java/org/bukkit/event/world/SpawnChangeEvent.java @@ -25,7 +25,7 @@ public class SpawnChangeEvent extends WorldEvent { */ @NotNull public Location getPreviousLocation() { - return previousLocation; + return previousLocation.clone(); // Paper - clone to avoid changes } @NotNull diff --git a/paper-api/src/main/java/org/bukkit/event/world/StructureGrowEvent.java b/paper-api/src/main/java/org/bukkit/event/world/StructureGrowEvent.java index 7af8d6e51c..783e74bc38 100644 --- a/paper-api/src/main/java/org/bukkit/event/world/StructureGrowEvent.java +++ b/paper-api/src/main/java/org/bukkit/event/world/StructureGrowEvent.java @@ -39,7 +39,7 @@ public class StructureGrowEvent extends WorldEvent implements Cancellable { */ @NotNull public Location getLocation() { - return location; + return location.clone(); // Paper - clone to avoid changes } /**