Uploaded image for project: 'Minecraft: Java Edition'
  1. Minecraft: Java Edition
  2. MC-131290

Enchantments are saved as shorts, but are loaded as and function with integers values

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Resolution: Unresolved
    • Affects Version/s: Minecraft 1.13-pre1, Minecraft 1.13.2, Minecraft 18w50a, 1.15.2, 20w07a, 20w15a, 21w03a
    • Fix Version/s: None
    • Confirmation Status:
      Confirmed
    • Category:
      (Unassigned)

      Description

      The bug

      The game saves natural enchantments as shorts, yet using integer values (eg 32768 (max short + 1)) makes the game load them fine as integers numbers.
      Note that the enchantment saving and loading logic exists in multiple methods which all behave differently.

      20w07a Mojang names:

      • ItemStack.enchant(Enchantment, int): Casts the int enchantment level to byte (-128 to 127) and then stores it as short
      • EnchantmentHelper.setEnchantments(...): Casts the int enchantment level to short (-32768 to 32767) and then stores it as short
      • EnchantmentHelper.getItemEnchantmentLevel(Enchantment, ItemStack): Reads as int and then clamps it to 0 to 255
      • EnchantmentHelper.deserializeEnchantments(ListTag): Reads as int
      • EnchantmentHelper.runIterationOnItem(EnchantmentVisitor, ItemStack): Reads as int
      • EnchantedBookItem.addEnchantment(ItemStack, EnchantmentInstance): Reads as int, then writes as short
      • ItemStack.appendEnchantmentNames(List<Component>, ListTag): Reads as int

      How to reproduce

      1. /give @s stick{Enchantments:[{id:"sharpness",lvl:32768}]}
      2. Close and reopen the game
        Note that the enchantment level was loaded just fine
      3. Put the stick in an anvil and rename it
        The level overflows and becomes -32768

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              slicedlime [Mojang] slicedlime
              Reporter:
              urielsalis [Mod] Urielsalis
              Votes:
              4 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                CHK: