forked from PaperMC/Paper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deprecate ItemStack#setType & add ItemStack#withType (PaperMC#10290)
- Loading branch information
1 parent
3263470
commit 4445d23
Showing
17 changed files
with
258 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 | ||
From: Owen1212055 <[email protected]> | ||
Date: Thu, 29 Feb 2024 17:54:26 -0500 | ||
Subject: [PATCH] Deprecate ItemStack#setType | ||
|
||
|
||
diff --git a/src/main/java/org/bukkit/inventory/ItemStack.java b/src/main/java/org/bukkit/inventory/ItemStack.java | ||
index 245a730a54c4b241a9a67eccceefafd2763bd238..7414b4fa690d393a8e9557cc1fd1ce12fa426940 100644 | ||
--- a/src/main/java/org/bukkit/inventory/ItemStack.java | ||
+++ b/src/main/java/org/bukkit/inventory/ItemStack.java | ||
@@ -134,8 +134,18 @@ public class ItemStack implements Cloneable, ConfigurationSerializable, Translat | ||
* {@link Material#isItem()} returns false.</b> | ||
* | ||
* @param type New type to set the items in this stack to | ||
+ * @deprecated <b>Setting the material type of ItemStacks is no longer supported.</b> | ||
+ * <p> | ||
+ * This method is deprecated due to potential illegal behavior that may occur | ||
+ * during the context of which this ItemStack is being used, allowing for certain item validation to be bypassed. | ||
+ * It is recommended to instead create a new ItemStack object with the desired | ||
+ * Material type, and if possible, set it in the appropriate context. | ||
+ * | ||
+ * Using this method in ItemStacks passed in events will result in undefined behavior. | ||
+ * @see ItemStack#withType(Material) | ||
*/ | ||
@Utility | ||
+ @Deprecated // Paper | ||
public void setType(@NotNull Material type) { | ||
Preconditions.checkArgument(type != null, "Material cannot be null"); | ||
this.type = type; | ||
@@ -148,6 +158,24 @@ public class ItemStack implements Cloneable, ConfigurationSerializable, Translat | ||
this.data = null; | ||
} | ||
} | ||
+ // Paper start | ||
+ /** | ||
+ * Creates a new ItemStack with the specified Material type, where the item count and item meta is preserved. | ||
+ * | ||
+ * @param type The Material type of the new ItemStack. | ||
+ * @return A new ItemStack instance with the specified Material type. | ||
+ */ | ||
+ @NotNull | ||
+ @org.jetbrains.annotations.Contract(value = "_ -> new", pure = true) | ||
+ public ItemStack withType(@NotNull Material type) { | ||
+ ItemStack itemStack = new ItemStack(type, this.amount); | ||
+ if (this.hasItemMeta()) { | ||
+ itemStack.setItemMeta(this.getItemMeta()); | ||
+ } | ||
+ | ||
+ return itemStack; | ||
+ } | ||
+ // Paper end | ||
|
||
/** | ||
* Gets the amount of items in this stack |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 | ||
From: Owen1212055 <[email protected]> | ||
Date: Wed, 20 Mar 2024 20:42:31 -0400 | ||
Subject: [PATCH] Item Mutation Fixes | ||
|
||
|
||
diff --git a/src/main/java/org/bukkit/event/block/InventoryBlockStartEvent.java b/src/main/java/org/bukkit/event/block/InventoryBlockStartEvent.java | ||
index f30ed841864f426c59143f392f39c96f864ba924..16fd600c25991e641a1be6fb78238256a6ce3d3f 100644 | ||
--- a/src/main/java/org/bukkit/event/block/InventoryBlockStartEvent.java | ||
+++ b/src/main/java/org/bukkit/event/block/InventoryBlockStartEvent.java | ||
@@ -21,7 +21,7 @@ import org.jetbrains.annotations.NotNull; | ||
public class InventoryBlockStartEvent extends BlockEvent { | ||
|
||
private static final HandlerList handlers = new HandlerList(); | ||
- private final ItemStack source; | ||
+ protected ItemStack source; // Paper | ||
|
||
public InventoryBlockStartEvent(@NotNull final Block block, @NotNull ItemStack source) { | ||
super(block); | ||
diff --git a/src/main/java/org/bukkit/event/enchantment/EnchantItemEvent.java b/src/main/java/org/bukkit/event/enchantment/EnchantItemEvent.java | ||
index 1829529c9915937dcdd0e6d1ceba9e64819fb93f..e7c243038b70ca13b7eabdf88ce518b6198c6db9 100644 | ||
--- a/src/main/java/org/bukkit/event/enchantment/EnchantItemEvent.java | ||
+++ b/src/main/java/org/bukkit/event/enchantment/EnchantItemEvent.java | ||
@@ -20,7 +20,7 @@ import org.jetbrains.annotations.NotNull; | ||
public class EnchantItemEvent extends InventoryEvent implements Cancellable { | ||
private static final HandlerList handlers = new HandlerList(); | ||
private final Block table; | ||
- private final ItemStack item; | ||
+ private ItemStack item; // Paper | ||
private int level; | ||
private boolean cancelled; | ||
private final Map<Enchantment, Integer> enchants; | ||
@@ -72,6 +72,17 @@ public class EnchantItemEvent extends InventoryEvent implements Cancellable { | ||
return item; | ||
} | ||
|
||
+ // Paper start | ||
+ /** | ||
+ * Sets the item to be enchanted | ||
+ * | ||
+ * @param item item | ||
+ */ | ||
+ public void setItem(@NotNull final ItemStack item) { | ||
+ this.item = item; | ||
+ } | ||
+ // Paper end | ||
+ | ||
/** | ||
* Gets the cost (minimum level) which is displayed as a number on the right | ||
* hand side of the enchantment offer. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.