Fix ItemStack#addUnsafeEnchantment ignored for missing enchantment component (#12549)

This commit is contained in:
Pedro 2025-05-17 10:13:08 -04:00 committed by GitHub
parent 369ad1706b
commit 841d634230
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 3 deletions

View File

@ -0,0 +1,18 @@
--- a/net/minecraft/world/item/enchantment/EnchantmentHelper.java
+++ b/net/minecraft/world/item/enchantment/EnchantmentHelper.java
@@ -52,8 +_,14 @@
}
public static ItemEnchantments updateEnchantments(ItemStack stack, Consumer<ItemEnchantments.Mutable> updater) {
+ // Paper start - allowing updating enchantments on items without component
+ return updateEnchantments(stack, updater, false);
+ }
+
+ public static ItemEnchantments updateEnchantments(ItemStack stack, Consumer<ItemEnchantments.Mutable> updater, final boolean createComponentIfMissing) {
+ // Paper end - allowing updating enchantments on items without component
DataComponentType<ItemEnchantments> componentType = getComponentType(stack);
- ItemEnchantments itemEnchantments = stack.get(componentType);
+ ItemEnchantments itemEnchantments = createComponentIfMissing ? stack.getOrDefault(componentType, ItemEnchantments.EMPTY) : stack.get(componentType); // Paper - allowing updating enchantments on items without component
if (itemEnchantments == null) {
return ItemEnchantments.EMPTY;
} else {

View File

@ -268,15 +268,13 @@ public final class CraftItemStack extends ItemStack {
public void addUnsafeEnchantment(Enchantment enchant, int level) {
Preconditions.checkArgument(enchant != null, "Enchantment cannot be null");
// Paper start
if (this.handle == null) {
return;
}
EnchantmentHelper.updateEnchantments(this.handle, mutable -> { // data component api doesn't really support mutable things once already set yet
mutable.set(CraftEnchantment.bukkitToMinecraftHolder(enchant), level);
});
// Paper end
}, true);
}
@Override