[SPIGOT-7067] Name mismatch for enchantments in YamlConfigurations Serialization Created: 16/Jun/22  Updated: 25/Dec/24  Resolved: 16/Jun/22

Status: Resolved
Project: Spigot
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Minor
Reporter: PASTOR Charles Assignee: Unassigned
Resolution: Invalid Votes: 0
Labels: bug, enchantments, itemstack, serialization, yaml
Environment:

Windows 10, using Eclipse Adoptium 17
Launching script has no arguments besides ram and config files are default except for the render distance


Attachments: PNG File NameMismatch-1.PNG     File Test.jar    
Version: CraftBukkit version 3506-Spigot-ee73712-c3f219e (MC: 1.19) (Implementing API version 1.19-R0.1-SNAPSHOT)
Guidelines Read: Yes

 Description   

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);
    }
    
}
 

 



 Comments   
Comment by PASTOR Charles [ 17/Jun/22 ]

Indeed... Apologies for not having checked if i was on the latest version.

Comment by md_5 [ 16/Jun/22 ]

Way outdated, this was fixed ages ago.

Generated at Tue Apr 15 09:10:57 UTC 2025 using Jira 10.3.3#10030003-sha1:d220e3fefc8dfc6d47f522d3b9a20c1455e12b7b.