Search in sources :

Example 1 with NBTOutputStream

use of com.iCo6.util.nbt.NBTOutputStream in project Core by iConomy.

the class InventoryDB method writeInventory.

private void writeInventory(String name, ItemStack[] stacks) {
    try {
        NBTInputStream in = new NBTInputStream(new FileInputStream(new File(dataDir, name + ".dat")));
        CompoundTag tag = (CompoundTag) in.readTag();
        in.close();
        ArrayList tagList = new ArrayList<Tag>();
        for (int i = 0; i < stacks.length; ++i) {
            if (stacks[i] == null)
                continue;
            ByteTag count = new ByteTag("Count", (byte) stacks[i].getAmount());
            ByteTag slot = new ByteTag("Slot", (byte) i);
            ShortTag damage = new ShortTag("Damage", stacks[i].getDurability());
            ShortTag id = new ShortTag("id", (short) stacks[i].getTypeId());
            HashMap<String, Tag> tagMap = new HashMap<String, Tag>();
            tagMap.put("Count", count);
            tagMap.put("Slot", slot);
            tagMap.put("Damage", damage);
            tagMap.put("id", id);
            tagList.add(new CompoundTag("", tagMap));
        }
        ListTag inventory = new ListTag("Inventory", CompoundTag.class, tagList);
        HashMap<String, Tag> tagCompound = new HashMap<String, Tag>(tag.getValue());
        tagCompound.put("Inventory", inventory);
        tag = new CompoundTag("Player", tagCompound);
        NBTOutputStream out = new NBTOutputStream(new FileOutputStream(new File(dataDir, name + ".dat")));
        out.writeTag(tag);
        out.close();
    } catch (IOException ex) {
        iConomy.Server.getLogger().log(Level.WARNING, "[iCo/InvDB] error writing inventory {0}: {1}", new Object[] { name, ex.getMessage() });
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ListTag(com.iCo6.util.nbt.ListTag) NBTOutputStream(com.iCo6.util.nbt.NBTOutputStream) FileInputStream(java.io.FileInputStream) ShortTag(com.iCo6.util.nbt.ShortTag) ByteTag(com.iCo6.util.nbt.ByteTag) FileOutputStream(java.io.FileOutputStream) NBTInputStream(com.iCo6.util.nbt.NBTInputStream) ShortTag(com.iCo6.util.nbt.ShortTag) Tag(com.iCo6.util.nbt.Tag) ListTag(com.iCo6.util.nbt.ListTag) CompoundTag(com.iCo6.util.nbt.CompoundTag) ByteTag(com.iCo6.util.nbt.ByteTag) File(java.io.File) CompoundTag(com.iCo6.util.nbt.CompoundTag)

Aggregations

ByteTag (com.iCo6.util.nbt.ByteTag)1 CompoundTag (com.iCo6.util.nbt.CompoundTag)1 ListTag (com.iCo6.util.nbt.ListTag)1 NBTInputStream (com.iCo6.util.nbt.NBTInputStream)1 NBTOutputStream (com.iCo6.util.nbt.NBTOutputStream)1 ShortTag (com.iCo6.util.nbt.ShortTag)1 Tag (com.iCo6.util.nbt.Tag)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1