PaperMC / Paper

The most widely used, high performance Minecraft server that aims to fix gameplay and mechanics inconsistencies
https://papermc.io/
Other
9.37k stars 2.19k forks source link

ItemStack#removeEnchantments makes the item unenchantable #10873

Closed TothAdam0012 closed 2 weeks ago

TothAdam0012 commented 3 weeks ago

Expected behavior

Calling ItemStack#removeEnchantments should remove all current enchantments, making it possible to re-enchant the item with the enchanting table or commands.

Observed/Actual behavior

After ItemStack#removeEnchantments is called on an item it can't be re-enchanted again. Putting it in the enchanting table doesn't show options and the /enchant command says the item has been enchanted but there are no enchantments present. What I can see is that a diamond chestplate has 8 components by default and calling this method reduces that to 7.

Steps/models to reproduce

I created an empty plugin with the following onEnable (and registered the ench command in the plugin.yml)

    @Override
    public void onEnable() {
        PluginCommand ench = getCommand("ench");
        if(ench != null) {
            ench.setExecutor((sender, command, label, arg) -> {
                if(sender instanceof Player player) {
                    player.getInventory().getItemInMainHand().removeEnchantments();
                }
                return true;
            });
        }
    }

Holding an item and using the /ench command makes the item unenchantable, even if it didn't have enchantments.

Plugin and Datapack List

Only the small plugin i wrote to test this.

Paper version

This server is running Paper version 1.20.6-138-master@906df69 (2024-06-10T06:32:15Z) (Implementing API version 1.20.6-R0.1-SNAPSHOT)

Other

There is a workaround: if I get the enchantments first and remove the 1 by 1 after removing the last one the item remains enchantable. This issue is only present if ItemStack#removeEnchantments is called.

Machine-Maker commented 3 weeks ago

The removeEnchantments method needs to set the map on CraftMetaItem to null instead of just clearing. Should be a 1 line fix