Java 类org.bukkit.util.io.BukkitObjectOutputStream 实例源码

项目:Vaults    文件:Invtobase.java   
/**
 * 
 * A method to serialize an {@link ItemStack} array to Base64 String.
 * 
 * <p />
 * 
 * Based off of {@link #toBase64(Inventory)}.
 * 
 * @param items to turn into a Base64 String.
 * @return Base64 string of the items.
 * @throws IllegalStateException
 */
public static String itemStackArrayToBase64(ItemStack[] items) throws IllegalStateException {
    try {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);

        // Write the size of the inventory
        dataOutput.writeInt(items.length);

        // Save every element in the list
        for (int i = 0; i < items.length; i++) {
            dataOutput.writeObject(items[i]);
        }

        // Serialize that array
        dataOutput.close();
        return Base64Coder.encodeLines(outputStream.toByteArray());
    } catch (Exception e) {
        throw new IllegalStateException("Unable to save item stacks.", e);
    }
}
项目:Vaults    文件:Invtobase.java   
/**
 * A method to serialize an inventory to Base64 string.
 * 
 * <p />
 * 
 * Special thanks to Comphenix in the Bukkit forums or also known
 * as aadnk on GitHub.
 * 
 * <a href="https://gist.github.com/aadnk/8138186">Original Source</a>
 * 
 * @param inventory to serialize
 * @return Base64 string of the provided inventory
 * @throws IllegalStateException
 */
public static String toBase64(Inventory inventory) throws IllegalStateException {
    try {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);

        dataOutput.writeInt(inventory.getSize());

        for (int i = 0; i < inventory.getSize(); i++) {
            dataOutput.writeObject(inventory.getItem(i));
        }

        dataOutput.close();
        return Base64Coder.encodeLines(outputStream.toByteArray());
    } catch (Exception e) {
        throw new IllegalStateException("Unable to save item stacks.", e);
    }
}
项目:Bags    文件:InventorySerializer.java   
/**
 * 
 * @param items
 * @return A base64 encoded String
 * @throws IllegalStateException
 * 
 * It encodes an {@link ItemStack} array to a base64 String 
 */
public static String itemStackArrayToBase64(ItemStack[] items) throws IllegalStateException
{
    try {
           ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
           BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);

           dataOutput.writeInt(items.length);

           for (int i = 0; i < items.length; i++) {
               dataOutput.writeObject(items[i]);
           }

           dataOutput.close();
           return Base64Coder.encodeLines(outputStream.toByteArray());
       } catch (Exception e) {
           throw new IllegalStateException("Unable to save item stacks.", e);
       }
 }
项目:iZenith-PVP    文件:Util.java   
public static String itemStackArrayToBase64(ItemStack[] items) throws IllegalStateException {
    try {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);

        // Write the size of the inventory
        dataOutput.writeInt(items.length);

        // Save every element in the list
        for (int i = 0; i < items.length; i++) {
            dataOutput.writeObject(items[i]);
        }

        // Serialize that array
        dataOutput.close();
        return Base64Coder.encodeLines(outputStream.toByteArray());
    } catch (Exception e) {
        throw new IllegalStateException("Unable to save item stacks.", e);
    }
}
项目:Bukkit_Bungee_PluginLib    文件:BukkitItemStackSerializer.java   
/**
 * Serializes a ItemStack array to a byte array.
 *
 * @param itemStacks The ItemStacks that should be serialized.
 * @return Serialized ItemsStacks as byte array. null if serialization failed.
 */
@Override
public byte[] serialize(ItemStack[] itemStacks)
{
    byte[] ba = null;
    if(itemStacks != null)
    {
        try(ByteArrayOutputStream b = new ByteArrayOutputStream(); BukkitObjectOutputStream output = new BukkitObjectOutputStream(b))
        {
            output.writeObject(itemStacks);
            output.flush();
            ba = b.toByteArray();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    return ba;
}
项目:Peacecraft    文件:WorldPlayerData.java   
private void saveEnderchest(Inventory inv) {
    Map<String, Object> enderchest = new HashMap<String, Object>();
    for(int slot = 0; slot < inv.getSize(); slot++) {
        ItemStack item = inv.getItem(slot);
        if(item != null) {
            enderchest.put(String.valueOf(slot), inv.getItem(slot).serialize());
        }
    }

    try {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        BukkitObjectOutputStream out = new BukkitObjectOutputStream(byteOut);
        out.writeObject(enderchest);
        out.close();

        this.database.setValue(this.key + ".enderchest", byteOut.toByteArray());
    } catch(Exception e) {
        this.module.getLogger().log(Level.SEVERE, "Failed to save enderchest.", e);
    }
}
项目:IZenith-Main    文件:Util.java   
public static String itemStackArrayToBase64(ItemStack[] items) throws IllegalStateException {
    try {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);

        // Write the size of the inventory
        dataOutput.writeInt(items.length);

        // Save every element in the list
        for (int i = 0; i < items.length; i++) {
            dataOutput.writeObject(items[i]);
        }

        // Serialize that array
        dataOutput.close();
        return Base64Coder.encodeLines(outputStream.toByteArray());
    } catch (Exception e) {
        throw new IllegalStateException("Unable to save item stacks.", e);
    }
}
项目:Kettle    文件:Util.java   
public static String toBase64(Inventory inventory) throws IllegalStateException {
    try {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);

        // Write the size of the inventory
        dataOutput.writeInt(inventory.getSize());

        // Save every element in the list
        for (int i = 0; i < inventory.getSize(); i++) {
            dataOutput.writeObject(inventory.getItem(i));
        }

        // Serialize that array
        dataOutput.close();
        return Base64Coder.encodeLines(outputStream.toByteArray());
    } catch (Exception e) {
        throw new IllegalStateException("Unable to save item stacks.", e);
    }
}
项目:Stoa    文件:ItemUtil.java   
public static String serializeItemStack(ItemStack inv) {
    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        BukkitObjectOutputStream bos = new BukkitObjectOutputStream(os);
        bos.writeObject(inv);

        String hex = BukkitObjectUtil.byteArrayToHexString(os.toByteArray());

        bos.close();
        os.close();
        return hex;
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return null;
}
项目:Stoa    文件:ItemUtil.java   
public static String serializeItemStacks(ItemStack[] inv) {
    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        BukkitObjectOutputStream bos = new BukkitObjectOutputStream(os);
        bos.writeObject(inv);

        String hex = BukkitObjectUtil.byteArrayToHexString(os.toByteArray());

        bos.close();
        os.close();
        return hex;
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return null;
}
项目:Stoa    文件:PotionEffectUtil.java   
public static String serializePotionEffects(Collection<PotionEffect> effects) {
    PotionEffect[] eff = new PotionEffect[effects.size()];
    int count = 0;
    for (PotionEffect effect : effects) {
        eff[count] = effect;
        count++;
    }

    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        BukkitObjectOutputStream bos = new BukkitObjectOutputStream(os);
        bos.writeObject(eff);

        String hex = BukkitObjectUtil.byteArrayToHexString(os.toByteArray());

        bos.close();
        os.close();
        return hex;
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return null;
}
项目:pvpmain    文件:BukkitSerialization.java   
public static String toBase64(Inventory inventory) {
    try {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);

        // Write the size of the inventory
        dataOutput.writeInt(inventory.getSize());

        // Save every element in the list
        for (int i = 0; i < inventory.getSize(); i++) {
            dataOutput.writeObject(inventory.getItem(i));
        }

        // Serialize that array
        dataOutput.close();
        return Base64Coder.encodeLines(outputStream.toByteArray());
    } catch (Exception e) {
        throw new IllegalStateException("Unable to save item stacks.", e);
    }        
}
项目:DemigodsRPG    文件:ItemUtil.java   
public static String serializeItemStack(ItemStack inv) {
    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        BukkitObjectOutputStream bos = new BukkitObjectOutputStream(os);
        bos.writeObject(inv);

        String hex = BukkitObjectUtil.byteArrayToHexString(os.toByteArray());

        bos.close();
        os.close();
        return hex;
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return null;
}
项目:DemigodsRPG    文件:ItemUtil.java   
public static String serializeItemStacks(ItemStack[] inv) {
    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        BukkitObjectOutputStream bos = new BukkitObjectOutputStream(os);
        bos.writeObject(inv);

        String hex = BukkitObjectUtil.byteArrayToHexString(os.toByteArray());

        bos.close();
        os.close();
        return hex;
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return null;
}
项目:DemigodsRPG    文件:PotionEffectUtil.java   
public static String serializePotionEffects(Collection<PotionEffect> effects) {
    PotionEffect[] eff = new PotionEffect[effects.size()];
    int count = 0;
    for (PotionEffect effect : effects) {
        eff[count] = effect;
        count++;
    }

    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        BukkitObjectOutputStream bos = new BukkitObjectOutputStream(os);
        bos.writeObject(eff);

        String hex = BukkitObjectUtil.byteArrayToHexString(os.toByteArray());

        bos.close();
        os.close();
        return hex;
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return null;
}
项目:NewNations    文件:NationsContainer.java   
public void save(NBTCompoundTag rootTag) throws IOException
{
    rootTag.setTag("X", new NBTShortTag((short)(location.getBlockX() - location.getChunk().getX() * 16)));
    rootTag.setTag("Y", new NBTShortTag((short)location.getBlockY()));
    rootTag.setTag("Z", new NBTShortTag((short)(location.getBlockZ() - location.getChunk().getZ() * 16)));

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BukkitObjectOutputStream boos = new BukkitObjectOutputStream(baos);
    boos.writeObject( items );

    rootTag.setTag("BukkitItemStackArray", new NBTByteArrayTag(baos.toByteArray()));
    boos.close();

    rootTag.setTag("InventoryType", new NBTStringTag(invType.name()));

}
项目:CTBAPI    文件:CTBAPI.java   
/**
 * Transfer a inventory into a string
 *
 * @param inventory Inventory
 * @return String
 */
@Deprecated
public static String inventoryToString(Inventory inventory) {
    try {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);

        // Write the size of the inventory
        dataOutput.writeInt(inventory.getSize());

        // Save every element in the list
        for (int i = 0; i < inventory.getSize(); i++) {
            dataOutput.writeObject(inventory.getItem(i));
        }

        // Serialize that array
        dataOutput.close();
        return Base64Coder.encodeLines(outputStream.toByteArray());
    } catch (Exception e) {
        throw new IllegalStateException("Unable to save item stacks.", e);
    }
}
项目:CensoredLib    文件:ItemUtil.java   
public static String serializeItemStack(ItemStack inv) {
    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        BukkitObjectOutputStream bos = new BukkitObjectOutputStream(os);
        bos.writeObject(inv);

        String hex = BukkitObjectUtil.byteArrayToHexString(os.toByteArray());

        bos.close();
        os.close();
        return hex;
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return null;
}
项目:CensoredLib    文件:ItemUtil.java   
public static String serializeItemStacks(ItemStack[] inv) {
    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        BukkitObjectOutputStream bos = new BukkitObjectOutputStream(os);
        bos.writeObject(inv);

        String hex = BukkitObjectUtil.byteArrayToHexString(os.toByteArray());

        bos.close();
        os.close();
        return hex;
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return null;
}
项目:CensoredLib    文件:PotionEffectUtil.java   
public static String serializePotionEffects(Collection<PotionEffect> effects) {
    PotionEffect[] eff = new PotionEffect[effects.size()];
    int count = 0;
    for (PotionEffect effect : effects) {
        eff[count] = effect;
        count++;
    }

    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        BukkitObjectOutputStream bos = new BukkitObjectOutputStream(os);
        bos.writeObject(eff);

        String hex = BukkitObjectUtil.byteArrayToHexString(os.toByteArray());

        bos.close();
        os.close();
        return hex;
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return null;
}
项目:PlayerVaults    文件:Base64Serialization.java   
public static String toBase64(Inventory inventory) {
    try {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);

        // Write the size of the inventory
        dataOutput.writeInt(inventory.getSize());

        // Save every element in the list
        for (int i = 0; i < inventory.getSize(); i++) {
            dataOutput.writeObject(inventory.getItem(i));
        }

        // Serialize that array
        dataOutput.close();
        return Base64Coder.encodeLines(outputStream.toByteArray());
    } catch (Exception e) {
        throw new IllegalStateException("Cannot into itemstacksz!", e);
    }
}
项目:Jail    文件:Util.java   
/**
 *
 * A method to serialize an {@link ItemStack} array to Base64 String.
 *
 * <p>
 *
 * Based off of {@link #toBase64(Inventory)}.
 *
 * @param items to turn into a Base64 String.
 * @return Base64 string of the items.
 * @throws IllegalStateException if any of the {@link ItemStack}s couldn't be parsed
 */
public static String itemStackArrayToBase64(ItemStack[] items) throws IllegalStateException {
    try {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);

        // Write the size of the inventory
        dataOutput.writeInt(items.length);

        // Save every element in the list
        for (ItemStack item : items) {
            dataOutput.writeObject(item);
        }

        // Serialize that array
        dataOutput.close();
        return Base64Coder.encodeLines(outputStream.toByteArray());
    } catch (Exception e) {
        throw new IllegalStateException("Unable to save item stacks.", e);
    }
}
项目:Jail    文件:Util.java   
/**
 * A method to serialize an inventory to Base64 string.
 *
 * <p>
 *
 * Special thanks to Comphenix in the Bukkit forums or also known
 * as aadnk on GitHub. <a href="https://gist.github.com/aadnk/8138186">Original Source</a>
 *
 * @param inventory to serialize
 * @return Base64 string of the provided inventory
 * @throws IllegalStateException if any of the {@link ItemStack}s couldn't be parsed
 */
public static String toBase64(Inventory inventory) throws IllegalStateException {
    try {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);

        // Write the size of the inventory
        dataOutput.writeInt(inventory.getSize());

        // Save every element in the list
        for (int i = 0; i < inventory.getSize(); i++) {
            dataOutput.writeObject(inventory.getItem(i));
        }

        // Serialize that array
        dataOutput.close();
        return Base64Coder.encodeLines(outputStream.toByteArray());
    } catch (Exception e) {
        throw new IllegalStateException("Unable to save item stacks.", e);
    }
}
项目:helper    文件:InventorySerialization.java   
public static byte[] encodeItemStack(ItemStack item) {
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        try (BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream)) {
            dataOutput.writeObject(item);
            return outputStream.toByteArray();
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
项目:helper    文件:InventorySerialization.java   
public static byte[] encodeItemStacks(ItemStack[] items) {
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        try (BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream)) {
            dataOutput.writeInt(items.length);
            for (ItemStack item : items) {
                dataOutput.writeObject(item);
            }
            return outputStream.toByteArray();
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
项目:helper    文件:InventorySerialization.java   
public static byte[] encodeInventory(Inventory inventory) {
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        try (BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream)) {
            dataOutput.writeInt(inventory.getSize());
            for (int i = 0; i < inventory.getSize(); i++) {
                dataOutput.writeObject(inventory.getItem(i));
            }
            return outputStream.toByteArray();
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
项目:Bukkit_Bungee_PluginLib    文件:BukkitItemStackSerializerTest.java   
@Test
public void testSerialize() throws Exception
{
    BukkitItemStackSerializer serializer = new BukkitItemStackSerializer();
    assertNull("Serialized data should be null", serializer.serialize(null));
    assertNull("Serialized data should be null when an error occurs", serializer.serialize(new ItemStack[] { new ItemStack(Material.APPLE, 10) }));
    BukkitObjectOutputStream mockedOutputStream = mock(BukkitObjectOutputStream.class);
    doNothing().when(mockedOutputStream).writeObject(any(at.pcgamingfreaks.TestClasses.NMS.ItemStack[].class));
    doNothing().when(mockedOutputStream).flush();
    whenNew(BukkitObjectOutputStream.class).withArguments(any(ByteArrayOutputStream.class)).thenReturn(mockedOutputStream);
    assertNotNull("Serialized data should not be null", serializer.serialize(new ItemStack[] { new ItemStack(Material.APPLE, 10) }));
    doThrow(new IOException()).when(mockedOutputStream).writeObject(any(at.pcgamingfreaks.TestClasses.NMS.ItemStack[].class));
    assertNull("Serialized data should be null when an error occurs", serializer.serialize(new ItemStack[] { new ItemStack(Material.APPLE, 10) }));
}
项目:PerWorldInventory    文件:ItemSerializer.java   
/**
 * Serialize an ItemStack to a JsonObject.
 * <p>
 *     The item itself will be saved as a Base64 encoded string to
 *     simplify the serialization and deserialization process. The result is
 *     not human readable.
 * </p>
 *
 * @param item The item to serialize.
 * @param index The position in the inventory.
 * @return A JsonObject with the serialized item.
 */
public JsonObject serializeItem(ItemStack item, int index) {
    JsonObject values = new JsonObject();
    if (item == null)
        return null;

    /*
     * Check to see if the item is a skull with a null owner.
     * This is because some people are getting skulls with null owners, which causes Spigot to throw an error
     * when it tries to serialize the item. If this ever gets fixed in Spigot, this will be removed.
     */
    if (item.getType() == Material.SKULL_ITEM) {
        SkullMeta meta = (SkullMeta) item.getItemMeta();
        if (meta.hasOwner() && (meta.getOwner() == null || meta.getOwner().isEmpty())) {
            item.setItemMeta(plugin.getServer().getItemFactory().getItemMeta(Material.SKULL_ITEM));
        }
    }

    values.addProperty("index", index);

    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
         BukkitObjectOutputStream bos = new BukkitObjectOutputStream(outputStream)) {
        bos.writeObject(item);
        String encoded = Base64Coder.encodeLines(outputStream.toByteArray());

        values.addProperty("item", encoded);
    } catch (IOException ex) {
        ConsoleLogger.severe("Unable to serialize item '" + item.getType().toString() + "':", ex);
        return null;
    }

    return values;
}
项目:NexusInventory    文件:SingleItemSerialization.java   
public static JSONObject serializeItem(ItemStack item, boolean useIndex, int index) {
    try {
        JSONObject values = new JSONObject();
        if (item == null) {
            return null;
        }

        /*
         * Check to see if the item is a skull with a null owner.
         * This is because some people are getting skulls with null owners, which causes Spigot to throw an error
         * when it tries to serialize the item. If this ever gets fixed in Spigot, this will be removed.
         */
        if (item.getType() == Material.SKULL_ITEM) {
            SkullMeta meta = (SkullMeta) item.getItemMeta();
            if (meta.hasOwner() && (meta.getOwner() == null || meta.getOwner().isEmpty())) {
                item.setItemMeta(NexusInventory.getInstance().getServer().getItemFactory().getItemMeta(Material.SKULL_ITEM));
            }
        }

        if (useIndex) {
            values.put("index", index);
        }


        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        BukkitObjectOutputStream dataObject = new BukkitObjectOutputStream(outputStream);
        dataObject.writeObject(item);

        dataObject.close();
        values.put("item", Base64Coder.encodeLines(outputStream.toByteArray()));

        return values;
    } catch (JSONException | IOException ex) {
        ex.printStackTrace();
        return null;
    }
}
项目:PerWorldInventory    文件:ItemSerializer.java   
/**
 * Serialize an ItemStack to a JsonObject.
 * <p>
 *     The item itself will be saved as a Base64 encoded string to
 *     simplify the serialization and deserialization process. The result is
 *     not human readable.
 * </p>
 *
 * @param item The item to serialize.
 * @param index The position in the inventory.
 * @return A JsonObject with the serialized item.
 */
public JsonObject serializeItem(ItemStack item, int index) {
    JsonObject values = new JsonObject();
    if (item == null)
        return null;

    /*
     * Check to see if the item is a skull with a null owner.
     * This is because some people are getting skulls with null owners, which causes Spigot to throw an error
     * when it tries to serialize the item. If this ever gets fixed in Spigot, this will be removed.
     */
    if (item.getType() == Material.SKULL_ITEM) {
        SkullMeta meta = (SkullMeta) item.getItemMeta();
        if (meta.hasOwner() && (meta.getOwner() == null || meta.getOwner().isEmpty())) {
            item.setItemMeta(plugin.getServer().getItemFactory().getItemMeta(Material.SKULL_ITEM));
        }
    }

    values.addProperty("index", index);

    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
         BukkitObjectOutputStream bos = new BukkitObjectOutputStream(outputStream)) {
        bos.writeObject(item);
        String encoded = Base64Coder.encodeLines(outputStream.toByteArray());

        values.addProperty("item", encoded);
    } catch (IOException ex) {
        ConsoleLogger.severe("Unable to serialize item '" + item.getType().toString() + "':", ex);
        return null;
    }

    return values;
}
项目:sensibletoolbox    文件:BukkitSerialization.java   
public static String toBase64(Inventory inventory, int maxItems) {
    if (maxItems <= 0) maxItems = inventory.getSize();
    else if (maxItems > inventory.getSize())
        maxItems = inventory.getSize();

    try {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);

        // Write the size of the inventory
        dataOutput.writeInt(maxItems);

        // Save every element in the list
        for (int i = 0; i < maxItems; i++) {
            ItemStack stack = inventory.getItem(i);
            Attributes attributes = stack == null ? null : new Attributes(stack);
            dataOutput.writeObject(stack);
            if (attributes != null) {
                dataOutput.writeInt(attributes.size());
                for (Attributes.Attribute a : attributes.values()) {
                    String s = Joiner.on(";;").join(
                            a.getUUID().toString(), a.getOperation(), a.getName(),
                            a.getAmount(), a.getAttributeType().getMinecraftId()
                    );
                    dataOutput.writeObject(s);
                }
            } else {
                dataOutput.writeInt(0);
            }
        }

        // Serialize that array
        dataOutput.close();
        return Base64Coder.encodeLines(outputStream.toByteArray());
    } catch (IOException e) {
        throw new IllegalStateException("Unable to save item stacks.", e);
    }
}
项目:Bags    文件:InventorySerializer.java   
/**
 * 
 * @param obj
 * @return An Base64 String of an {@link Object}
 * 
 * 
 */
public static String toBase64Obj(Object obj)
{
 try {
           ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
           BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);

           dataOutput.writeObject(obj);



           dataOutput.close();
           return Base64Coder.encodeLines(outputStream.toByteArray());
       } catch (Exception e) {
           throw new IllegalStateException("Unable to save obj.", e);
       }         


}
项目:Peacecraft    文件:WorldPlayerData.java   
private void saveInventory(PlayerInventory inv) {
    Map<String, Object> inventory = new HashMap<String, Object>();
    for(int slot = 0; slot < inv.getSize(); slot++) {
        ItemStack item = inv.getItem(slot);
        if(item != null) {
            inventory.put(String.valueOf(slot), inv.getItem(slot).serialize());
        }
    }

    ItemStack helmet = inv.getHelmet();
    ItemStack chestplate = inv.getChestplate();
    ItemStack leggings = inv.getLeggings();
    ItemStack boots = inv.getBoots();

    if(helmet != null) {
        inventory.put("helmet", helmet.serialize());
    }

    if(chestplate != null) {
        inventory.put("chestplate", chestplate.serialize());
    }

    if(leggings != null) {
        inventory.put("leggings", leggings.serialize());
    }

    if(boots != null) {
        inventory.put("boots", boots.serialize());
    }

    try {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        BukkitObjectOutputStream out = new BukkitObjectOutputStream(byteOut);
        out.writeObject(inventory);
        out.close();

        this.database.setValue(this.key + ".inventory", byteOut.toByteArray());
    } catch(IOException e) {
        this.module.getLogger().log(Level.SEVERE, "Failed to save inventory.", e);
    }
}