From de620b8480372e2896633f36e04e15dd01443265 Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Wed, 20 Mar 2024 13:42:29 -0700 Subject: [PATCH] Clone mutable types in events when changes are discarded (#10333) --- patches/api/0034-EntityPathfindEvent.patch | 4 +- patches/api/0076-AsyncTabCompleteEvent.patch | 8 +- patches/api/0082-PreCreatureSpawnEvent.patch | 4 +- ...-constructor-and-getChangedBlockData.patch | 4 +- patches/api/0155-PreSpawnerSpawnEvent.patch | 4 +- patches/api/0158-Turtle-API.patch | 8 +- patches/api/0168-BlockDestroyEvent.patch | 9 +- .../api/0247-Add-StructuresLocateEvent.patch | 4 +- patches/api/0260-Add-worldborder-events.patch | 4 +- ...utables-to-prevent-unexpected-issues.patch | 151 ++++++++++++++++++ 10 files changed, 175 insertions(+), 25 deletions(-) create mode 100644 patches/api/0468-Clone-mutables-to-prevent-unexpected-issues.patch diff --git a/patches/api/0034-EntityPathfindEvent.patch b/patches/api/0034-EntityPathfindEvent.patch index f4e8d04eb9..3586652e5c 100644 --- a/patches/api/0034-EntityPathfindEvent.patch +++ b/patches/api/0034-EntityPathfindEvent.patch @@ -7,7 +7,7 @@ Fires when an Entity decides to start moving to a location. diff --git a/src/main/java/com/destroystokyo/paper/event/entity/EntityPathfindEvent.java b/src/main/java/com/destroystokyo/paper/event/entity/EntityPathfindEvent.java new file mode 100644 -index 0000000000000000000000000000000000000000..5804497f34bac9a9b6b16c6e5decb0119467df7b +index 0000000000000000000000000000000000000000..8267e8f1f0ade29a284831069e1268ee4b29e109 --- /dev/null +++ b/src/main/java/com/destroystokyo/paper/event/entity/EntityPathfindEvent.java @@ -0,0 +1,87 @@ @@ -74,7 +74,7 @@ index 0000000000000000000000000000000000000000..5804497f34bac9a9b6b16c6e5decb011 + */ + @NotNull + public Location getLoc() { -+ return this.location; ++ return this.location.clone(); + } + + @Override diff --git a/patches/api/0076-AsyncTabCompleteEvent.patch b/patches/api/0076-AsyncTabCompleteEvent.patch index d18880ff5f..9a7669b39b 100644 --- a/patches/api/0076-AsyncTabCompleteEvent.patch +++ b/patches/api/0076-AsyncTabCompleteEvent.patch @@ -15,7 +15,7 @@ Co-authored-by: Aikar diff --git a/src/main/java/com/destroystokyo/paper/event/server/AsyncTabCompleteEvent.java b/src/main/java/com/destroystokyo/paper/event/server/AsyncTabCompleteEvent.java new file mode 100644 -index 0000000000000000000000000000000000000000..e0847b6f0202bd8267c537f72a5e8c8cb7c6b1db +index 0000000000000000000000000000000000000000..8965974988ad20fbe1d45885f20a3a98d2e9595f --- /dev/null +++ b/src/main/java/com/destroystokyo/paper/event/server/AsyncTabCompleteEvent.java @@ -0,0 +1,339 @@ @@ -208,7 +208,7 @@ index 0000000000000000000000000000000000000000..e0847b6f0202bd8267c537f72a5e8c8c + */ + @Nullable + public Location getLocation() { -+ return this.location; ++ return this.location != null ? this.location.clone() : null; + } + + /** @@ -534,7 +534,7 @@ index 0000000000000000000000000000000000000000..6f560a51277ccbd46a9142cfa057d276 + } +} diff --git a/src/main/java/org/bukkit/event/server/TabCompleteEvent.java b/src/main/java/org/bukkit/event/server/TabCompleteEvent.java -index 270e6d8ad4358baa256cee5f16cff281f063ce3b..4a3451af454295ac3e1b688e6665cad9fc594c82 100644 +index 270e6d8ad4358baa256cee5f16cff281f063ce3b..b43c3cb5c88eada186d6f81712c244aaa18fb53e 100644 --- a/src/main/java/org/bukkit/event/server/TabCompleteEvent.java +++ b/src/main/java/org/bukkit/event/server/TabCompleteEvent.java @@ -29,6 +29,13 @@ public class TabCompleteEvent extends Event implements Cancellable { @@ -570,7 +570,7 @@ index 270e6d8ad4358baa256cee5f16cff281f063ce3b..4a3451af454295ac3e1b688e6665cad9 + */ + @org.jetbrains.annotations.Nullable + public org.bukkit.Location getLocation() { -+ return loc; ++ return this.loc != null ? this.loc.clone() : null; + } + // Paper end + diff --git a/patches/api/0082-PreCreatureSpawnEvent.patch b/patches/api/0082-PreCreatureSpawnEvent.patch index 156b50a9c1..60f65dc98d 100644 --- a/patches/api/0082-PreCreatureSpawnEvent.patch +++ b/patches/api/0082-PreCreatureSpawnEvent.patch @@ -16,7 +16,7 @@ See: https://github.com/PaperMC/Paper/issues/917 diff --git a/src/main/java/com/destroystokyo/paper/event/entity/PreCreatureSpawnEvent.java b/src/main/java/com/destroystokyo/paper/event/entity/PreCreatureSpawnEvent.java new file mode 100644 -index 0000000000000000000000000000000000000000..1d582118f19a41b499ee73b5e27ddb66ed34f06b +index 0000000000000000000000000000000000000000..633ef4dcc701916f2dbfefbbebd5994f93ffc2a4 --- /dev/null +++ b/src/main/java/com/destroystokyo/paper/event/entity/PreCreatureSpawnEvent.java @@ -0,0 +1,109 @@ @@ -64,7 +64,7 @@ index 0000000000000000000000000000000000000000..1d582118f19a41b499ee73b5e27ddb66 + */ + @NotNull + public Location getSpawnLocation() { -+ return this.location; ++ return this.location.clone(); + } + + /** diff --git a/patches/api/0142-Add-source-block-constructor-and-getChangedBlockData.patch b/patches/api/0142-Add-source-block-constructor-and-getChangedBlockData.patch index 9f9e760855..f12e6ac1f5 100644 --- a/patches/api/0142-Add-source-block-constructor-and-getChangedBlockData.patch +++ b/patches/api/0142-Add-source-block-constructor-and-getChangedBlockData.patch @@ -6,7 +6,7 @@ Subject: [PATCH] Add source block constructor and getChangedBlockData() to diff --git a/src/main/java/org/bukkit/event/block/BlockPhysicsEvent.java b/src/main/java/org/bukkit/event/block/BlockPhysicsEvent.java -index e3a5f5824ed882058f5bac5003f66ce79733a868..1d496e5a3d1541bf0a257a4358b3943fd6415204 100644 +index e3a5f5824ed882058f5bac5003f66ce79733a868..b23686d2e452dc0b8bde453b4a375f424cb7504a 100644 --- a/src/main/java/org/bukkit/event/block/BlockPhysicsEvent.java +++ b/src/main/java/org/bukkit/event/block/BlockPhysicsEvent.java @@ -32,6 +32,13 @@ public class BlockPhysicsEvent extends BlockEvent implements Cancellable { @@ -46,7 +46,7 @@ index e3a5f5824ed882058f5bac5003f66ce79733a868..1d496e5a3d1541bf0a257a4358b3943f + */ + @NotNull + public BlockData getChangedBlockData() { -+ return changed; ++ return changed.clone(); + } + // Paper end + diff --git a/patches/api/0155-PreSpawnerSpawnEvent.patch b/patches/api/0155-PreSpawnerSpawnEvent.patch index 8ef2cd0db2..4b6bbe6f7a 100644 --- a/patches/api/0155-PreSpawnerSpawnEvent.patch +++ b/patches/api/0155-PreSpawnerSpawnEvent.patch @@ -12,7 +12,7 @@ Dropped as it does not apply due to the earlier PreCreatureSpawnEvent patch not diff --git a/src/main/java/com/destroystokyo/paper/event/entity/PreSpawnerSpawnEvent.java b/src/main/java/com/destroystokyo/paper/event/entity/PreSpawnerSpawnEvent.java new file mode 100644 -index 0000000000000000000000000000000000000000..9a450a0095ed0aa9c92655c2f4e5732bccdd59c9 +index 0000000000000000000000000000000000000000..2910f0bf929d918c86510f29d9361bbc19411256 --- /dev/null +++ b/src/main/java/com/destroystokyo/paper/event/entity/PreSpawnerSpawnEvent.java @@ -0,0 +1,29 @@ @@ -42,6 +42,6 @@ index 0000000000000000000000000000000000000000..9a450a0095ed0aa9c92655c2f4e5732b + + @NotNull + public Location getSpawnerLocation() { -+ return this.spawnerLocation; ++ return this.spawnerLocation.clone(); + } +} diff --git a/patches/api/0158-Turtle-API.patch b/patches/api/0158-Turtle-API.patch index 4d3712fd8c..3ec867d360 100644 --- a/patches/api/0158-Turtle-API.patch +++ b/patches/api/0158-Turtle-API.patch @@ -65,7 +65,7 @@ index 0000000000000000000000000000000000000000..cfbc8aaf862ac90e794ee38bf8a6cb9e +} diff --git a/src/main/java/com/destroystokyo/paper/event/entity/TurtleLayEggEvent.java b/src/main/java/com/destroystokyo/paper/event/entity/TurtleLayEggEvent.java new file mode 100644 -index 0000000000000000000000000000000000000000..e917e64e8ea266a3fc7387c42aad95927f0f0501 +index 0000000000000000000000000000000000000000..3029e406cd684efb5645e38711dff9c0bb7b01e4 --- /dev/null +++ b/src/main/java/com/destroystokyo/paper/event/entity/TurtleLayEggEvent.java @@ -0,0 +1,92 @@ @@ -116,7 +116,7 @@ index 0000000000000000000000000000000000000000..e917e64e8ea266a3fc7387c42aad9592 + */ + @NotNull + public Location getLocation() { -+ return this.location; ++ return this.location.clone(); + } + + /** @@ -163,7 +163,7 @@ index 0000000000000000000000000000000000000000..e917e64e8ea266a3fc7387c42aad9592 +} diff --git a/src/main/java/com/destroystokyo/paper/event/entity/TurtleStartDiggingEvent.java b/src/main/java/com/destroystokyo/paper/event/entity/TurtleStartDiggingEvent.java new file mode 100644 -index 0000000000000000000000000000000000000000..5cdc6b0c3db43279e2bb8043922b31f750ddb3f5 +index 0000000000000000000000000000000000000000..7a2fa4a11b47e4982d1644830d7e28f12b4378ec --- /dev/null +++ b/src/main/java/com/destroystokyo/paper/event/entity/TurtleStartDiggingEvent.java @@ -0,0 +1,66 @@ @@ -210,7 +210,7 @@ index 0000000000000000000000000000000000000000..5cdc6b0c3db43279e2bb8043922b31f7 + */ + @NotNull + public Location getLocation() { -+ return this.location; ++ return this.location.clone(); + } + + @Override diff --git a/patches/api/0168-BlockDestroyEvent.patch b/patches/api/0168-BlockDestroyEvent.patch index 0235746334..d6ac7bfb8d 100644 --- a/patches/api/0168-BlockDestroyEvent.patch +++ b/patches/api/0168-BlockDestroyEvent.patch @@ -12,10 +12,10 @@ This can replace many uses of BlockPhysicsEvent diff --git a/src/main/java/com/destroystokyo/paper/event/block/BlockDestroyEvent.java b/src/main/java/com/destroystokyo/paper/event/block/BlockDestroyEvent.java new file mode 100644 -index 0000000000000000000000000000000000000000..d0421a4cbcb672cc06b40ad41fc06a67f6d2dbbb +index 0000000000000000000000000000000000000000..c0742b58ca2c098c27394915b624889ece1a9168 --- /dev/null +++ b/src/main/java/com/destroystokyo/paper/event/block/BlockDestroyEvent.java -@@ -0,0 +1,123 @@ +@@ -0,0 +1,122 @@ +package com.destroystokyo.paper.event.block; + +import org.bukkit.block.Block; @@ -79,9 +79,8 @@ index 0000000000000000000000000000000000000000..d0421a4cbcb672cc06b40ad41fc06a67 + /** + * @return The new state of this block (Air, or a Fluid type) + */ -+ @NotNull -+ public BlockData getNewState() { -+ return this.newState; ++ public @NotNull BlockData getNewState() { ++ return this.newState.clone(); + } + + /** diff --git a/patches/api/0247-Add-StructuresLocateEvent.patch b/patches/api/0247-Add-StructuresLocateEvent.patch index ae3aef5102..746b674c5d 100644 --- a/patches/api/0247-Add-StructuresLocateEvent.patch +++ b/patches/api/0247-Add-StructuresLocateEvent.patch @@ -176,7 +176,7 @@ index 0000000000000000000000000000000000000000..1ac3369455972aeb1ade5dc023d1f818 +} diff --git a/src/main/java/io/papermc/paper/event/world/StructuresLocateEvent.java b/src/main/java/io/papermc/paper/event/world/StructuresLocateEvent.java new file mode 100644 -index 0000000000000000000000000000000000000000..be86894561ff808d911e123b09bda72c9ed09185 +index 0000000000000000000000000000000000000000..582af444b058708638683e7d6f9b79685c04c061 --- /dev/null +++ b/src/main/java/io/papermc/paper/event/world/StructuresLocateEvent.java @@ -0,0 +1,213 @@ @@ -241,7 +241,7 @@ index 0000000000000000000000000000000000000000..be86894561ff808d911e123b09bda72c + * @return {@link Location} where search begins + */ + public @NotNull Location getOrigin() { -+ return this.origin; ++ return this.origin.clone(); + } + + /** diff --git a/patches/api/0260-Add-worldborder-events.patch b/patches/api/0260-Add-worldborder-events.patch index 1c889b6cdd..9f3ae70435 100644 --- a/patches/api/0260-Add-worldborder-events.patch +++ b/patches/api/0260-Add-worldborder-events.patch @@ -202,7 +202,7 @@ index 0000000000000000000000000000000000000000..a44964593b7f78c5086dc4928e75ad89 +} diff --git a/src/main/java/io/papermc/paper/event/world/border/WorldBorderCenterChangeEvent.java b/src/main/java/io/papermc/paper/event/world/border/WorldBorderCenterChangeEvent.java new file mode 100644 -index 0000000000000000000000000000000000000000..a90f67e902c6ae35b2399e6f72a3ff8a5c15d05a +index 0000000000000000000000000000000000000000..dd96dcc0dd68d71bf27c758ed496153d434fb386 --- /dev/null +++ b/src/main/java/io/papermc/paper/event/world/border/WorldBorderCenterChangeEvent.java @@ -0,0 +1,79 @@ @@ -242,7 +242,7 @@ index 0000000000000000000000000000000000000000..a90f67e902c6ae35b2399e6f72a3ff8a + */ + @NotNull + public Location getOldCenter() { -+ return this.oldCenter; ++ return this.oldCenter.clone(); + } + + /** diff --git a/patches/api/0468-Clone-mutables-to-prevent-unexpected-issues.patch b/patches/api/0468-Clone-mutables-to-prevent-unexpected-issues.patch new file mode 100644 index 0000000000..e84ea7d3aa --- /dev/null +++ b/patches/api/0468-Clone-mutables-to-prevent-unexpected-issues.patch @@ -0,0 +1,151 @@ +From 0000000000000000000000000000000000000000 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. + +diff --git a/src/main/java/org/bukkit/event/block/BlockCanBuildEvent.java b/src/main/java/org/bukkit/event/block/BlockCanBuildEvent.java +index 08d09c2a92d8aa6adf6610cc05905d58a76fce1f..c74ac0cb004aa219ce2f761969a4bb46cb7c9160 100644 +--- a/src/main/java/org/bukkit/event/block/BlockCanBuildEvent.java ++++ b/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/src/main/java/org/bukkit/event/entity/EntityChangeBlockEvent.java b/src/main/java/org/bukkit/event/entity/EntityChangeBlockEvent.java +index 1a9575ad4c81aefa5ef0b927f6ac8f7064b55c49..24e1a49e48dd8f9eb2515b2ffe472a0c4d2bc09b 100644 +--- a/src/main/java/org/bukkit/event/entity/EntityChangeBlockEvent.java ++++ b/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/src/main/java/org/bukkit/event/entity/EntityExplodeEvent.java b/src/main/java/org/bukkit/event/entity/EntityExplodeEvent.java +index 099efafa14c10910e4ed04abb1823f0c1a96b6a6..8506fa03293c575c35b55b052224807470fdbd98 100644 +--- a/src/main/java/org/bukkit/event/entity/EntityExplodeEvent.java ++++ b/src/main/java/org/bukkit/event/entity/EntityExplodeEvent.java +@@ -59,7 +59,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/src/main/java/org/bukkit/event/entity/EntityPortalEnterEvent.java b/src/main/java/org/bukkit/event/entity/EntityPortalEnterEvent.java +index 6818e9f0ba32ca1a1e612703f7526b29f5a6438f..e4e3d2e22c28ef251d76c48ade267b4eb3749e7d 100644 +--- a/src/main/java/org/bukkit/event/entity/EntityPortalEnterEvent.java ++++ b/src/main/java/org/bukkit/event/entity/EntityPortalEnterEvent.java +@@ -24,7 +24,7 @@ public class EntityPortalEnterEvent extends EntityEvent { + */ + @NotNull + public Location getLocation() { +- return location; ++ return location.clone(); // Paper - clone to avoid changes + } + + @NotNull +diff --git a/src/main/java/org/bukkit/event/entity/ItemDespawnEvent.java b/src/main/java/org/bukkit/event/entity/ItemDespawnEvent.java +index 6fc66197eb2c5d59c70d8d028b7963748371edbe..2bb29fa449cd6c90b52d2786ed15b6154d591607 100644 +--- a/src/main/java/org/bukkit/event/entity/ItemDespawnEvent.java ++++ b/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/src/main/java/org/bukkit/event/vehicle/VehicleBlockCollisionEvent.java b/src/main/java/org/bukkit/event/vehicle/VehicleBlockCollisionEvent.java +index d0a437bd8aeec18f800893f51ece06deb0c8972c..50fad23cf4d9f591b12a9eaebeb4e26f18e8528d 100644 +--- a/src/main/java/org/bukkit/event/vehicle/VehicleBlockCollisionEvent.java ++++ b/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/src/main/java/org/bukkit/event/vehicle/VehicleMoveEvent.java b/src/main/java/org/bukkit/event/vehicle/VehicleMoveEvent.java +index 7bfb84d3948c773e943374316ea25a19288ec7d0..fc4cf7b21b24fe38617fa150f697bc29da76754e 100644 +--- a/src/main/java/org/bukkit/event/vehicle/VehicleMoveEvent.java ++++ b/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/src/main/java/org/bukkit/event/world/GenericGameEvent.java b/src/main/java/org/bukkit/event/world/GenericGameEvent.java +index 2a2a329877d8da45c2d6afecf78ce88d52635cad..fb975fefc74d8c9746cab4c02860f55654cf92f7 100644 +--- a/src/main/java/org/bukkit/event/world/GenericGameEvent.java ++++ b/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/src/main/java/org/bukkit/event/world/SpawnChangeEvent.java b/src/main/java/org/bukkit/event/world/SpawnChangeEvent.java +index 9ce93d00935446589cb2bb970cb540d109616e85..73997ae04ff39ac3984c59de32aaced9eb72ce16 100644 +--- a/src/main/java/org/bukkit/event/world/SpawnChangeEvent.java ++++ b/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/src/main/java/org/bukkit/event/world/StructureGrowEvent.java b/src/main/java/org/bukkit/event/world/StructureGrowEvent.java +index 7af8d6e51c824cf0592b722b834f1d4986e3cc08..783e74bc382f0f6d24203fde7b811f588a674731 100644 +--- a/src/main/java/org/bukkit/event/world/StructureGrowEvent.java ++++ b/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 + } + + /**