How can I save and load a Item/Block from or to a config?

Discussion in 'Spigot Plugin Development' started by Andre_601, Jun 5, 2017.

  1. Andre_601


    Hello everyone!

    The question is in the title.
    I try to save a item with a command.
    The player should hold a item in his mainhand and type a command, to save it in the config.

    I know, there is this way, by just saving it with every data, that the item has, wich look like that:
    Code (Text):
    ==: org.bukkit.inventory.ItemStack
          type: PAPER
            ==: ItemMeta
            meta-type: UNSPECIFIC
            display-name: DisplayName
    But I want to save it in a clean and easy to read look:
    Code (Text):
      Item: PAPER
      Meta: 0
      Displayname: DisplayName
    I only need to save one item.
    I already can save the Itemname with p.getInventory().getItemInMainHand().getType().toString(); and the Displayname with p.getInventory().getItemInMainHand().getItemMeta().getDisplayName();

    That means, that the only problems are saving the Meta (for example, if you using orange wool, that it would be 1) and to load everything again.
    In my case should rightclicking with that exact Item run a comman (p.performCommand)

    I really hope, that you guys can help me.

    Kind regards
  2. You could store it exactly like you showed in the example.

    For the meta, you could save the data value (itemstack.getData() I think, could be wrong).

    To load it, just make a method that will read in that data and return an ItemStack
  3. Andre_601


    I already tried that, but it gived me the itemname with the number in brackets (In my example "paper(0)")
    But thanks for your idea...

    I'm not sure, how to "convert" the itemname and the integer to a ItemStack.
    Its the first time, I try to make something like that...
  4. Note that your first method will be much more flexible, though it's not very editable by hand. If you plan on saving items in-game, the first method is better.

    If you want people to be able to enter items by hand, then your 2nd method will work but it won't (easily) support items with metadata, like books, enchantments, and loads of other stuff.