Lore and enchant in config help please

Discussion in 'Spigot Help' started by DevPazzo, Feb 14, 2020.

  1. Lol
     
    #1 DevPazzo, Feb 14, 2020
    Last edited: Feb 15, 2020
  2. Strahan

    Benefactor

    Easiest thing to do would be just write the whole ItemStack to the config, as it implements ConfigurationSerializable. So you could read/write it like:
    Code (Text):
    getConfig().set("myitem", someItemStackobject);
    saveConfig();
    ItemStack item = getConfig().getItemStack("myitem");
    if (item == null) return;
    playerObject.getInventory().addItem(item);
    That will save the item, its lore, enchants, everything.
     
    • Useful Useful x 1
  3. Use ItemMeta
     
    • Agree Agree x 1
  4. Strahan

    Benefactor

    ItemMeta also implements ConfigurationSerializable, so you can write it directly to the config. Just retrieve it as an Object with a generic "get" call to getConfig() then check to ensure it's an ItemMeta, then you can do whatever with it. I suppose that's extra steps that may be unnecessary, but I like to treat any external input as suspect.
     
  5. Strahan

    Benefactor

    Well, is the code in the graphic switcher.java? If so, it's saying the config isn't returning anything. I've never seen a getConfig call annotated like that (path: "lore"). I have to assume you are using something to make that work, as when I try it won't even compile but perhaps just for testing use a regular string in there, like getConfig().getStringList("lore"); Also why do you read the lore into a List then when it comes time to apply it, read the config again instead of the List you just made? That makes the List rather pointless.

    Also this is not what writing ItemMeta to the config looks like anyway. You're still dealing with individual parts of a meta. What I mean is like:
    Code (Text):
    ItemStack sword = new ItemStack(Material.DIAMOND_SWORD);
    ItemMeta im = sword.getItemMeta();
    im.setDisplayName("Pig sticker");
    im.setLore(new ArrayList<String>(Arrays.asList("This makes bacon quite well","Nice and sharp")));
    im.addEnchant(Enchantment.DAMAGE_ALL, 50, true);

    getConfig().set("pigsticker", im);  <--- This saves the ItemMeta to the config, with all the settings

    Object loadedMeta = getConfig().get("pigsticker"); <-- Load as object as there is no getItemMeta()
    if (!(loadedMeta instanceof ItemMeta)) return; <-- Check if it's actually an ItemMeta

    sword.setItemMeta((ItemMeta)loadedMeta);
    You could just do ItemMeta loadedMeta = (ItemMeta)getConfig().get("pigsticker"); but I don't like blind casts.