PaperMC/patches/api/Clone-mutables-to-prevent-unexpected-issues.patch

151 lines
6.9 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/event/block/BlockCanBuildEvent.java
+++ b/src/main/java/org/bukkit/event/block/BlockCanBuildEvent.java
@@ -0,0 +0,0 @@ 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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/event/entity/EntityChangeBlockEvent.java
+++ b/src/main/java/org/bukkit/event/entity/EntityChangeBlockEvent.java
@@ -0,0 +0,0 @@ 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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/event/entity/EntityExplodeEvent.java
+++ b/src/main/java/org/bukkit/event/entity/EntityExplodeEvent.java
@@ -0,0 +0,0 @@ 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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/event/entity/EntityPortalEnterEvent.java
+++ b/src/main/java/org/bukkit/event/entity/EntityPortalEnterEvent.java
@@ -0,0 +0,0 @@ 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/src/main/java/org/bukkit/event/entity/ItemDespawnEvent.java b/src/main/java/org/bukkit/event/entity/ItemDespawnEvent.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/event/entity/ItemDespawnEvent.java
+++ b/src/main/java/org/bukkit/event/entity/ItemDespawnEvent.java
@@ -0,0 +0,0 @@ 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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/event/vehicle/VehicleBlockCollisionEvent.java
+++ b/src/main/java/org/bukkit/event/vehicle/VehicleBlockCollisionEvent.java
@@ -0,0 +0,0 @@ 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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/event/vehicle/VehicleMoveEvent.java
+++ b/src/main/java/org/bukkit/event/vehicle/VehicleMoveEvent.java
@@ -0,0 +0,0 @@ public class VehicleMoveEvent extends VehicleEvent {
*/
@NotNull
public Location getFrom() {
- return from;
+ return from.clone(); // Paper - clone to avoid changes
}
/**
@@ -0,0 +0,0 @@ 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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/event/world/GenericGameEvent.java
+++ b/src/main/java/org/bukkit/event/world/GenericGameEvent.java
@@ -0,0 +0,0 @@ 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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/event/world/SpawnChangeEvent.java
+++ b/src/main/java/org/bukkit/event/world/SpawnChangeEvent.java
@@ -0,0 +0,0 @@ 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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/event/world/StructureGrowEvent.java
+++ b/src/main/java/org/bukkit/event/world/StructureGrowEvent.java
@@ -0,0 +0,0 @@ public class StructureGrowEvent extends WorldEvent implements Cancellable {
*/
@NotNull
public Location getLocation() {
- return location;
+ return location.clone(); // Paper - clone to avoid changes
}
/**