Skip to content

Commit

Permalink
Fix a bunch more issues arising from mutable types (#11769)
Browse files Browse the repository at this point in the history
  • Loading branch information
Machine-Maker authored Dec 22, 2024
1 parent 8ad15d6 commit 083c083
Show file tree
Hide file tree
Showing 19 changed files with 39 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public BlockData getEffectBlock() {
* @param effectBlock block effect
*/
public void setEffectBlock(final BlockData effectBlock) {
this.effectBlock = effectBlock;
this.effectBlock = effectBlock.clone();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public Location getFrom() {
public void setFrom(final Location from) {
Preconditions.checkArgument(from != null, "Cannot use null from location!");
Preconditions.checkArgument(from.getWorld() != null, "Cannot use from location with null world!");
this.from = from;
this.from = from.clone();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public Cause getCause() {
* @param location the spawn location, or {@code null} to remove the spawn location
*/
public void setLocation(final @Nullable Location location) {
this.location = location;
this.location = location != null ? location.clone() : null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Location getFrom() {
*/
public void setFrom(final Location from) {
this.validateLocation(from);
this.from = from;
this.from = from.clone();
}

/**
Expand All @@ -72,7 +72,7 @@ public Location getTo() {
*/
public void setTo(final Location to) {
this.validateLocation(to);
this.to = to;
this.to = to.clone();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public Location getNewCenter() {
* @param newCenter the new center
*/
public void setNewCenter(final Location newCenter) {
this.newCenter = newCenter;
this.newCenter = newCenter.clone();
}

@Override
Expand Down
12 changes: 6 additions & 6 deletions paper-api/src/main/java/io/papermc/paper/potion/PotionMix.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public final class PotionMix implements Keyed {
*/
public PotionMix(final NamespacedKey key, final ItemStack result, final RecipeChoice input, final RecipeChoice ingredient) {
this.key = key;
this.result = result;
this.input = input;
this.ingredient = ingredient;
this.result = result.clone();
this.input = input.clone();
this.ingredient = ingredient.clone();
}

/**
Expand All @@ -58,7 +58,7 @@ public NamespacedKey getKey() {
* @return the result itemstack
*/
public ItemStack getResult() {
return this.result;
return this.result.clone();
}

/**
Expand All @@ -67,7 +67,7 @@ public ItemStack getResult() {
* @return the bottom 3 slot ingredients
*/
public RecipeChoice getInput() {
return this.input;
return this.input.clone();
}

/**
Expand All @@ -76,7 +76,7 @@ public RecipeChoice getInput() {
* @return the top slot input
*/
public RecipeChoice getIngredient() {
return this.ingredient;
return this.ingredient.clone();
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions paper-api/src/main/java/org/bukkit/Vibration.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static class BlockDestination implements Destination {
private final Location block;

public BlockDestination(@NotNull Location block) {
this.block = block;
this.block = block.clone();
}

public BlockDestination(@NotNull Block block) {
Expand All @@ -88,7 +88,7 @@ public BlockDestination(@NotNull Block block) {

@NotNull
public Location getLocation() {
return block;
return block.clone();
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Vector getVelocity() {
* @param vel the velocity of the item being dispensed
*/
public void setVelocity(@NotNull Vector vel) {
velocity = vel;
velocity = vel.clone();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void setNewData(@NotNull BlockData newData) {
Preconditions.checkArgument(newData != null, "newData null");
Preconditions.checkArgument(this.newData.getMaterial().equals(newData.getMaterial()), "Cannot change fluid type");

this.newData = newData;
this.newData = newData.clone();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public Vector getFinalKnockback() {
public void setFinalKnockback(@NotNull Vector knockback) {
Preconditions.checkArgument(knockback != null, "Knockback cannot be null");

this.knockback = knockback;
this.knockback = knockback.clone();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public Location getFrom() {
* @param from New location this entity moved from
*/
public void setFrom(@NotNull Location from) {
this.from = from;
this.from = from.clone();
}

/**
Expand All @@ -71,7 +71,7 @@ public Location getTo() {
* @param to New Location this entity moved to
*/
public void setTo(@Nullable Location to) {
this.to = to;
this.to = to != null ? to.clone() : null;
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public EquipmentSlot getHand() {
@Nullable
@Deprecated // Paper
public Vector getClickedPosition() {
return clickedPosistion;
return clickedPosistion.clone();
}

// Paper start
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public Location getFrom() {
*/
public void setFrom(@NotNull Location from) {
validateLocation(from);
this.from = from;
this.from = from.clone();
}

/**
Expand All @@ -90,7 +90,7 @@ public Location getTo() {
*/
public void setTo(@NotNull Location to) {
validateLocation(to);
this.to = to;
this.to = to.clone();
}

// Paper start - PlayerMoveEvent improvements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void setRespawnLocation(@NotNull Location respawnLocation) {
Preconditions.checkArgument(respawnLocation != null, "Respawn location can not be null");
Preconditions.checkArgument(respawnLocation.getWorld() != null, "Respawn world can not be null");

this.respawnLocation = respawnLocation;
this.respawnLocation = respawnLocation.clone();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Vector getVelocity() {
* @param velocity The velocity vector that will be sent to the player
*/
public void setVelocity(@NotNull Vector velocity) {
this.velocity = velocity;
this.velocity = velocity.clone();
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Structure getStructure() {
*/
@NotNull
public BoundingBox getBoundingBox() {
return boundingBox;
return boundingBox.clone();
}

/**
Expand Down
6 changes: 3 additions & 3 deletions paper-api/src/main/java/org/bukkit/loot/LootContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public final class LootContext {
private LootContext(@NotNull Location location, float luck, int lootingModifier, @Nullable Entity lootedEntity, @Nullable HumanEntity killer) {
Preconditions.checkArgument(location != null, "LootContext location cannot be null");
Preconditions.checkArgument(location.getWorld() != null, "LootContext World cannot be null");
this.location = location;
this.location = location.clone();
this.luck = luck;
this.lootingModifier = lootingModifier;
this.lootedEntity = lootedEntity;
Expand All @@ -38,7 +38,7 @@ private LootContext(@NotNull Location location, float luck, int lootingModifier,
*/
@NotNull
public Location getLocation() {
return location;
return location.clone();
}

/**
Expand Down Expand Up @@ -110,7 +110,7 @@ public static class Builder {
* @param location the location the LootContext should use
*/
public Builder(@NotNull Location location) {
this.location = location;
this.location = location.clone();
}

/**
Expand Down
20 changes: 10 additions & 10 deletions paper-api/src/main/java/org/bukkit/util/Transformation.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public Transformation(@NotNull Vector3f translation, @NotNull AxisAngle4f leftRo
Preconditions.checkArgument(scale != null, "scale cannot be null");
Preconditions.checkArgument(rightRotation != null, "rightRotation cannot be null");

this.translation = translation;
this.translation = new Vector3f(translation);
this.leftRotation = new Quaternionf(leftRotation);
this.scale = scale;
this.scale = new Vector3f(scale);
this.rightRotation = new Quaternionf(rightRotation);
}

Expand All @@ -39,10 +39,10 @@ public Transformation(@NotNull Vector3f translation, @NotNull Quaternionf leftRo
Preconditions.checkArgument(scale != null, "scale cannot be null");
Preconditions.checkArgument(rightRotation != null, "rightRotation cannot be null");

this.translation = translation;
this.leftRotation = leftRotation;
this.scale = scale;
this.rightRotation = rightRotation;
this.translation = new Vector3f(translation);
this.leftRotation = new Quaternionf(leftRotation);
this.scale = new Vector3f(scale);
this.rightRotation = new Quaternionf(rightRotation);
}

/**
Expand All @@ -52,7 +52,7 @@ public Transformation(@NotNull Vector3f translation, @NotNull Quaternionf leftRo
*/
@NotNull
public Vector3f getTranslation() {
return this.translation;
return new Vector3f(this.translation);
}

/**
Expand All @@ -62,7 +62,7 @@ public Vector3f getTranslation() {
*/
@NotNull
public Quaternionf getLeftRotation() {
return this.leftRotation;
return new Quaternionf(this.leftRotation);
}

/**
Expand All @@ -72,7 +72,7 @@ public Quaternionf getLeftRotation() {
*/
@NotNull
public Vector3f getScale() {
return this.scale;
return new Vector3f(this.scale);
}

/**
Expand All @@ -82,7 +82,7 @@ public Vector3f getScale() {
*/
@NotNull
public Quaternionf getRightRotation() {
return this.rightRotation;
return new Quaternionf(this.rightRotation);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Location getSpawnLocation() {
* @param location the spawn location
*/
public void setSpawnLocation(@NotNull Location location) {
this.spawnLocation = location;
this.spawnLocation = location.clone();
}

@NotNull
Expand Down

2 comments on commit 083c083

@SergioK29
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idiot proofing at the cost of significant performance 😕

@lynxplay
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you can find us a spark report where these kind of measures actually matter, feel free to open a performance issue regarding such "significant" downsides.
Location/vector/quaternion cloning is dirt cheap.
ItemStacks used to not be, hence we were hesitant, but since 1.20.5 cloning these has also become really cheap.

Please sign in to comment.