Save in a file an inventory?

Discussion in 'Plugin Development' started by charbania, Mar 4, 2017.

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

    charbania

    Hello everyone, I am creating a plugin that saves in a file the items in an inventory.
    I succeeded, but does not save the name of 'items.
    This is my code:
    YamlConfiguration fileyml YamlConfiguration = new ();
    c.set ("inv.content", inv.getContents ());
    c.save (new File ("plugins / test", playername +. "yml"));
    How could I do?
    P.S. Sorry for my English, but I'm Italian XD
     
  2. Offline

    N00BHUN73R

    You could always encode it with base64, and back. It works quite well and it's what I use whenever I have to store inventories.

    Here's an example to encode it( Not actual working code ):
    PHP:
    public String toBase64(inventory) {
        try {
             
    ByteArrayoutputstream str = new bytestream()
             
    BukkitObjectoutputstream data = new bukkitobject;
         
              
    data.writeInt(inventory.getSize());
          
               for (
    int i 0inventory.sizei++ {
                   
    data.writeObject(inventory.getitem(i))
               }

               
    data.close();
               return 
    base64coder.encodelines(outputstream.tobytearray();

        } catch (
    Exception e) {
              
    e.printstack()
        }
    }
    aaaand to get it back:
    PHP:
    public Inventory fromBase(string data) {
        
    ByteArrayinputstream stream = new bytearrayinputstream(base64coder.decodeLines(data))

       
    Bukkitobjectinputstream data = new bukkitobjectinputstream(stream)

       
    Inventory inv bukkit.createinventory(nulldata.readInt())

       for (
    int i 0inventory.sizei++) {
            
    inventory.setitem(i, (invdata.readObject()
        }


        
    data.close();
        return 
    inventory;

    }
     
Thread Status:
Not open for further replies.

Share This Page