Save/Load Inventory's to YML

Discussion in 'Plugin Development' started by stuntguy3000, Jan 18, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    stuntguy3000

    Hey guys,

    I wonder if somebody could help me with this, basically i want to with a command open up a inventory which is loaded from a yml file, and auto save on close.
     
  2. Offline

    fireblast709

    ItemStack implements ConfigurationSerializable, so you can just get and set them.
    Code:java
    1. getConfig().set("itemstack", new ItemStack(Material.STONE, 1));
    2. ItemStack i = getConfig().getItemStack("itemstack");
     
  3. Offline

    gomeow

  4. Offline

    fireblast709

  5. Offline

    gomeow

    I guess to could just use that and save to something like
    Code:
    Inventories:
      gomeow:
        1:
        2:
        Etc
     
  6. Offline

    tehbeard

    since ItemStack can be serialized, just pass in/out arrays of itemstacks (inventory.getContents() / setContents())
     
  7. Offline

    keto23

    Code:java
    1. public void save(Player p) {
    2. ItemStack[] i = player.getInventory().getContents();
    3. int pos = 0;
    4. for (ItemStack stack : i) {
    5. getConfig().set(player.getName() + "." + pos + "." stack);
    6. pos++;
    7. }
    8. }
    9.  
    10. public void load(Player p) {
    11. Set<String> sl = getConfig().getConfigurationSection(p.getName()).getKeys(false);
    12.  
    13. for (String s : sl) {
    14. player.getInventory().setItem(Integer.parseInt(s), getConfig().getItemStack(p.getName() + "." + s));
    15. }
    16.  
    17. }


    That might work, haven't tested it though. Post results please.
     
  8. Offline

    Scipione

    i would encode an serialize it with base32/64 or you might loose enchantements, lore, colors etc..
     
  9. Offline

    Comphenix

    It should work fine after the ItemMeta update.

    I also wrote a inventory serializer for configuration files.
     
  10. Offline

    LegoPal92

    Comphenix

    Sorry to bump an old thread, but what is the point of

    config.loadFromString(data);

    in public static ItemStack[] loadInventory(String data) throws InvalidConfigurationException
     
  11. Offline

    Comphenix

    It causes the configuration object to parse and load the content of the string as YAML. There's also a documentation for this method.
     
Thread Status:
Not open for further replies.

Share This Page