Uploaded image for project: 'Spigot'
  1. Spigot
  2. SPIGOT-7067

Name mismatch for enchantments in YamlConfigurations Serialization

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Invalid
    • Icon: Minor Minor
    • None
    • None
    • Windows 10, using Eclipse Adoptium 17
      Launching script has no arguments besides ram and config files are default except for the render distance

    • CraftBukkit version 3506-Spigot-ee73712-c3f219e (MC: 1.19) (Implementing API version 1.19-R0.1-SNAPSHOT)
    • Yes

      Whenever you store an itemstack with enchantments into a YamlConfiguration, say for example a diamond pickaxe with efficiency 5, the string version of the itemstack shows SILK_TOUCH instead of MINING_SPEED.

      HOWEVER, whenever you retrieve the itemstack through FileConfiguration#getItemstack(), it translates back to efficiency correctly

      ===STEP BY STEP BUG REPRODUCING===
      set up a 1.19 spigot server with the sent plugin

      pick up any enchanted item, then run /test
      it convert the item to a string and print it
      then use that string to create the itemstack back and give it to you (so you can check both items are identical)

      package fr.stormer3428.test;import org.bukkit.command.Command;
      import org.bukkit.command.CommandSender;
      import org.bukkit.configuration.file.YamlConfiguration;
      import org.bukkit.entity.Player;
      import org.bukkit.inventory.ItemStack;
      import org.bukkit.plugin.java.JavaPlugin;public class Test extends JavaPlugin{    @Override
          public void onEnable() {
              getCommand("test").setExecutor(this);
          }
          
          @Override
          public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
              if(!(sender instanceof Player) || !command.getName().equalsIgnoreCase("test")) {
                  return super.onCommand(sender, command, label, args);
              }
              Player p = (Player) sender;
              
              ItemStack it = p.getInventory().getItemInMainHand(); //we use the item in the player's main hand
              if(it == null) return false;                
              
              String blob = itemToStringBlob(it);         //convert itemstack to string
              p.sendMessage(blob);                         //display it
              ItemStack copy = stringBlobToItem(blob);    //use string to make a new itemstack that should (and is to my experience) identical
              p.getInventory().addItem(copy);                //give the copy
              
              return true;
          }
          
          /**
           * Serializes an {@link ItemStack} to a {@link String} 
           * 
           * The way it works is by creating a temporary {@link YamlConfiguration} and using it's method for serializing
           * it then simply returns the entire config file containing nothing but the item
           */
          public static String itemToStringBlob(ItemStack itemStack) {
              YamlConfiguration config = new YamlConfiguration();
              config.set("i", itemStack);
              return config.saveToString();
          } 
          
          /**
           * Deserializes a {@link String} back to an {@link ItemStack}, or null if malformed
           * 
           * It uses {@link YamlConfiguration}.getItemStack({@link String}) to retrieve the {@link ItemStack} from the given string
           */
          public static ItemStack stringBlobToItem(String stringBlob) {
              YamlConfiguration config = new YamlConfiguration();
              try {
                  config.loadFromString(stringBlob);
              } catch (Exception e) {
                  e.printStackTrace();
                  return null;
              }
              return config.getItemStack("i", null);
          }
          
      }
       

       

            Unassigned Unassigned
            azertyuiopcharles@gmail.com PASTOR Charles
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: