org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder Java Examples

The following examples show how to use org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Base64Serialize.java    From ServerTutorial with MIT License 6 votes vote down vote up
/**
     * Gets an array of ItemStacks from Base64 string.
     *
     * <p />
     *
     * Base off of {@link #fromBase64(String)}.
     *
     * @param data Base64 string to convert to ItemStack array.
     * @return ItemStack array created from the Base64 string.
     * @throws IOException
     */
    public static ItemStack[] itemStackArrayFromBase64(String data) {
    	try {
            ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
            BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
            ItemStack[] items = new ItemStack[dataInput.readInt()];

            // Read the serialized inventory
            for (int i = 0; i < items.length; i++) {
            	items[i] = (ItemStack) dataInput.readObject();
            }

            dataInput.close();
            return items;
        } catch (IOException | ClassNotFoundException e) {
        }
        return null;
}
 
Example #2
Source File: ItemSerializer.java    From PerWorldInventory with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Get an ItemStack from a JsonObject.
 *
 * @param data The Json to read.
 * @param format The data format being used. Refer to {@link PlayerSerializer#serialize(PWIPlayer)}.
 * @return The deserialized item stack.
 */
public ItemStack deserializeItem(JsonObject data, int format) {
    switch (format) {
        case 0:
            return getItem(data);
        case 1:
        case 2:
            try (ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data.get("item").getAsString()));
                 BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream)) {
                return (ItemStack) dataInput.readObject();
            } catch (IOException | ClassNotFoundException ex) {
                ConsoleLogger.severe("Unable to deserialize an item:", ex);
                return new ItemStack(Material.AIR);
            }
        default:
            throw new IllegalArgumentException("Unknown data format '" + format + "'");
    }
}
 
Example #3
Source File: ItemSerializer.java    From PerWorldInventory with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Get an ItemStack from a JsonObject.
 *
 * @param data The Json to read.
 * @param format The data format being used. Refer to {@link PlayerSerializer#serialize(PWIPlayer)}.
 * @return The deserialized item stack.
 */
public ItemStack deserializeItem(JsonObject data, int format) {
    switch (format) {
        case 0:
            return getItem(data);
        case 1:
        case 2:
            try (ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data.get("item").getAsString()));
                 BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream)) {
                return (ItemStack) dataInput.readObject();
            } catch (IOException | ClassNotFoundException ex) {
                ConsoleLogger.severe("Unable to deserialize an item:", ex);
                return new ItemStack(Material.AIR);
            }
        default:
            throw new IllegalArgumentException("Unknown data format '" + format + "'");
    }
}
 
Example #4
Source File: SafeRepresenter.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
public Node representData(Object data) {
    Tag tag = Tag.STR;
    Character style = null;
    String value = data.toString();
    if (StreamReader.NON_PRINTABLE.matcher(value).find()) {
        tag = Tag.BINARY;
        char[] binary;
        try {
            binary = Base64Coder.encode(value.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            throw new YAMLException(e);
        }
        value = String.valueOf(binary);
        style = '|';
    }
    // if no other scalar style is explicitly set, use literal style for
    // multiline scalars
    if (defaultScalarStyle == null && MULTILINE_PATTERN.matcher(value).find()) {
        style = '|';
    }
    return representScalar(tag, value, style);
}
 
Example #5
Source File: Base64Serialization.java    From PlayerVaults with GNU General Public License v3.0 6 votes vote down vote up
public static Inventory fromBase64(String data, String target) {
    try {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
        BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
        Inventory inventory = Bukkit.getServer().createInventory(null, dataInput.readInt());
        // Read the serialized inventory
        for (int i = 0; i < inventory.getSize(); i++) {
            inventory.setItem(i, (ItemStack) dataInput.readObject());
        }
        dataInput.close();
        PlayerVaults.debug("Read " + inventory.getSize() + " items");
        return inventory;
    } catch (Exception e) {
        PlayerVaults.getInstance().getLogger().log(Level.SEVERE, "Failed to load vault " + target, e);
    }
    return null;
}
 
Example #6
Source File: SafeRepresenter.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
public Node representData(Object data) {
    Tag tag = Tag.STR;
    Character style = null;
    String value = data.toString();
    if (StreamReader.NON_PRINTABLE.matcher(value).find()) {
        tag = Tag.BINARY;
        char[] binary;
        try {
            binary = Base64Coder.encode(value.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            throw new YAMLException(e);
        }
        value = String.valueOf(binary);
        style = '|';
    }
    // if no other scalar style is explicitly set, use literal style for
    // multiline scalars
    if (defaultScalarStyle == null && MULTILINE_PATTERN.matcher(value).find()) {
        style = '|';
    }
    return representScalar(tag, value, style);
}
 
Example #7
Source File: Base64Serialize.java    From ServerTutorial with MIT License 6 votes vote down vote up
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);
    }
}
 
Example #8
Source File: Base64Serialize.java    From ServerTutorial with MIT License 6 votes vote down vote up
public static Inventory fromBase64(String data) {
    try {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
        BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
        Inventory inventory = Bukkit.getServer().createInventory(null, dataInput.readInt());

        // Read the serialized inventory
        for (int i = 0; i < inventory.getSize(); i++) {
            inventory.setItem(i, (ItemStack) dataInput.readObject());
        }
        dataInput.close();
        return inventory;
    } catch (Exception e) {
    }
    return null;
}
 
Example #9
Source File: Base64Serialize.java    From ServerTutorial with MIT License 6 votes vote down vote up
/**
 *
 * 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){
	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);
    }
}
 
Example #10
Source File: InventoryTypeAdapter.java    From IridiumSkyblock with GNU General Public License v2.0 6 votes vote down vote up
public static Inventory fromBase64(String data) {
    try {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
        BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
        Inventory inventory = Bukkit.getServer().createInventory(null, dataInput.readInt());

        // Read the serialized inventory
        for (int i = 0; i < inventory.getSize(); i++) {
            inventory.setItem(i, (ItemStack) dataInput.readObject());
        }
        dataInput.close();
        return inventory;
    } catch (Exception e) {
    }
    return null;
}
 
Example #11
Source File: InventoryTypeAdapter.java    From IridiumSkyblock with GNU General Public License v2.0 6 votes vote down vote up
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);
    }
}
 
Example #12
Source File: NBTChecker_v1_13_R1.java    From ProRecipes with GNU General Public License v2.0 5 votes vote down vote up
public String getPotionType(String s) {
    //HashMap<String, String> ke = new HashMap<String, String>();
    ArrayList<String> t = new ArrayList<String>();
    byte[] testBytes = Base64Coder.decode(s);
    ByteArrayInputStream buf = new ByteArrayInputStream(testBytes);
    try
    {
        NBTTagCompound tag = NBTCompressedStreamTools.a(buf);
        Set<String> keys = tag.getKeys();
        for (String key : keys) {
            // System.out.println(key);
            String b = tag.get(key).toString();
            //System.out.println(b);
            String[] arr = b.split(",");
            for(int c = 0; c < arr.length; c++){

                //System.out.println("SOMETHING IN A LOOP ?   "  + arr[c]);
                if(arr[c].contains("minecraft:")){
                    return arr[c].replace("minecraft:", "").replace('"', ' ').replace(" ", "");
                }

            }
        }
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
    }
    return t.toString();
}
 
Example #13
Source File: NBTChecker_v1_12_R1.java    From ProRecipes with GNU General Public License v2.0 5 votes vote down vote up
@Override
public List<String> getTags(String s) {
	ArrayList<String> t = new ArrayList<String>();
	byte[] testBytes = Base64Coder.decode(s);
	ByteArrayInputStream buf = new ByteArrayInputStream(testBytes);
   	 try
       {
         NBTTagCompound tag = NBTCompressedStreamTools.a(buf);
         Set<String> keys = tag.c();
         for (String key : keys) {
       	 // System.out.println(key);
       	  String b = tag.get(key).toString();
       	  //System.out.println(b);
       	  String[] arr = b.split(",");
       	  ArrayList<String> list = new ArrayList<String>();
       	  for(int c = 0; c < arr.length; c++){
       		  //System.out.println(arr[c]);
       		  if(arr[c].toLowerCase().contains("uuid")){
       			
       		  }else{
       			  list.add(arr[c]);
       		  }
       	  }
       	  
       	  b =  new StringBuilder().append(list).toString();
       	t.add(key + ":" + b);
         }
       }
       catch (IOException ex)
       {
       	ex.printStackTrace();
       }
	return t;
}
 
Example #14
Source File: NBTChecker_v1_12_R1.java    From ProRecipes with GNU General Public License v2.0 5 votes vote down vote up
public String getPotionType(String s) {
	//HashMap<String, String> ke = new HashMap<String, String>();
	ArrayList<String> t = new ArrayList<String>();
	byte[] testBytes = Base64Coder.decode(s);
	ByteArrayInputStream buf = new ByteArrayInputStream(testBytes);
   	 try
       {
         NBTTagCompound tag = NBTCompressedStreamTools.a(buf);
         Set<String> keys = tag.c();
         for (String key : keys) {
       	 // System.out.println(key);
       	  String b = tag.get(key).toString();
       	  //System.out.println(b);
       	  String[] arr = b.split(",");
       	  for(int c = 0; c < arr.length; c++){
       		 
       		 //System.out.println("SOMETHING IN A LOOP ?   "  + arr[c]);
       		  if(arr[c].contains("minecraft:")){
       			  return arr[c].replace("minecraft:", "").replace('"', ' ').replace(" ", "");
       		  }
       		  
       	  }
         }
       }
       catch (Exception ex)
       {
       	ex.printStackTrace();
       }
	return t.toString();
}
 
Example #15
Source File: NBTChecker_v1_8_R3.java    From ProRecipes with GNU General Public License v2.0 5 votes vote down vote up
@Override
public List<String> getTags(String s) {
	ArrayList<String> t = new ArrayList<String>();
	byte[] testBytes = Base64Coder.decode(s);
	ByteArrayInputStream buf = new ByteArrayInputStream(testBytes);
   	 try
       {
         NBTTagCompound tag = NBTCompressedStreamTools.a(buf);
         Set<String> keys = tag.c();
         for (String key : keys) {
       	 // System.out.println(key);
       	  String b = tag.get(key).toString();
       	  //System.out.println(b);
       	  String[] arr = b.split(",");
       	  ArrayList<String> list = new ArrayList<String>();
       	  for(int c = 0; c < arr.length; c++){
       		  //System.out.println(arr[c]);
       		  if(arr[c].toLowerCase().contains("uuid")){
       			
       		  }else{
       			  list.add(arr[c]);
       		  }
       	  }
       	  
       	  b =  new StringBuilder().append(list).toString();
       	t.add(key + ":" + b);
         }
       }
       catch (IOException ex)
       {
       	ex.printStackTrace();
       }
	return t;
}
 
Example #16
Source File: NBTChecker_v1_8_R3.java    From ProRecipes with GNU General Public License v2.0 5 votes vote down vote up
public String getPotionType(String s) {
	ArrayList<String> t = new ArrayList<String>();
	byte[] testBytes = Base64Coder.decode(s);
	ByteArrayInputStream buf = new ByteArrayInputStream(testBytes);
   	 try
       {
         NBTTagCompound tag = NBTCompressedStreamTools.a(buf);
         Set<String> keys = tag.c();
         for (String key : keys) {
       	  String b = tag.get(key).toString();
       	  
       	  String[] arr = b.split(",");
       	  for(int c = 0; c < arr.length; c++){
       		 
       		  //System.out.println("SOMETHING IN A LOOP ?   "  + arr[c]);
       		  if(arr[c].contains("minecraft:")){
       			  return arr[c].replace("minecraft:", "").replace('"', ' ').replace(" ", "");
       		  }
       		  
       	  }
         }
       }
       catch (Exception ex)
       {
       	ex.printStackTrace();
       }
	return t.toString();
}
 
Example #17
Source File: NBTChecker_v1_10_R1.java    From ProRecipes with GNU General Public License v2.0 5 votes vote down vote up
@Override
public List<String> getTags(String s) {
	ArrayList<String> t = new ArrayList<String>();
	byte[] testBytes = Base64Coder.decode(s);
	ByteArrayInputStream buf = new ByteArrayInputStream(testBytes);
   	 try
       {
         NBTTagCompound tag = NBTCompressedStreamTools.a(buf);
         Set<String> keys = tag.c();
         for (String key : keys) {
       	 // System.out.println(key);
       	  String b = tag.get(key).toString();
       	  //System.out.println(b);
       	  String[] arr = b.split(",");
       	  ArrayList<String> list = new ArrayList<String>();
       	  for(int c = 0; c < arr.length; c++){
       		  //System.out.println(arr[c]);
       		  if(arr[c].toLowerCase().contains("uuid")){
       			
       		  }else{
       			  list.add(arr[c]);
       		  }
       	  }
       	  
       	  b =  new StringBuilder().append(list).toString();
       	t.add(key + ":" + b);
         }
       }
       catch (IOException ex)
       {
       	ex.printStackTrace();
       }
	return t;
}
 
Example #18
Source File: NBTChecker_v1_8_R1.java    From ProRecipes with GNU General Public License v2.0 5 votes vote down vote up
public String getPotionType(String s) {
	ArrayList<String> t = new ArrayList<String>();
	byte[] testBytes = Base64Coder.decode(s);
	ByteArrayInputStream buf = new ByteArrayInputStream(testBytes);
   	 try
       {
         NBTTagCompound tag = NBTCompressedStreamTools.a(buf);
         Set<String> keys = tag.c();
         for (String key : keys) {
       	  String b = tag.get(key).toString();
       	  
       	  String[] arr = b.split(",");
       	  for(int c = 0; c < arr.length; c++){
       		 
       		//  System.out.println("SOMETHING IN A LOOP ?   "  + arr[c]);
       		  if(arr[c].contains("minecraft:")){
       			  return arr[c].replace("minecraft:", "").replace('"', ' ').replace(" ", "");
       		  }
       		  
       	  }
         }
       }
       catch (Exception ex)
       {
       	ex.printStackTrace();
       }
	return t.toString();
}
 
Example #19
Source File: NBTChecker_v1_10_R1.java    From ProRecipes with GNU General Public License v2.0 5 votes vote down vote up
public String getPotionType(String s) {
	//HashMap<String, String> ke = new HashMap<String, String>();
	ArrayList<String> t = new ArrayList<String>();
	byte[] testBytes = Base64Coder.decode(s);
	ByteArrayInputStream buf = new ByteArrayInputStream(testBytes);
   	 try
       {
         NBTTagCompound tag = NBTCompressedStreamTools.a(buf);
         Set<String> keys = tag.c();
         for (String key : keys) {
       	 // System.out.println(key);
       	  String b = tag.get(key).toString();
       	  //System.out.println(b);
       	  String[] arr = b.split(",");
       	  for(int c = 0; c < arr.length; c++){
       		 
       		 //System.out.println("SOMETHING IN A LOOP ?   "  + arr[c]);
       		  if(arr[c].contains("minecraft:")){
       			  return arr[c].replace("minecraft:", "").replace('"', ' ').replace(" ", "");
       		  }
       		  
       	  }
         }
       }
       catch (Exception ex)
       {
       	ex.printStackTrace();
       }
	return t.toString();
}
 
Example #20
Source File: NBTChecker_v1_7_R4.java    From ProRecipes with GNU General Public License v2.0 5 votes vote down vote up
@Override
public List<String> getTags(String s) {
	ArrayList<String> t = new ArrayList<String>();
	byte[] testBytes = Base64Coder.decode(s);
	ByteArrayInputStream buf = new ByteArrayInputStream(testBytes);
   	 try
       {
         NBTTagCompound tag = NBTCompressedStreamTools.a(buf);
         Set<String> keys = tag.c();
         for (String key : keys) {
       	 // System.out.println(key);
       	  String b = tag.get(key).toString();
       	  //System.out.println(b);
       	  String[] arr = b.split(",");
       	  ArrayList<String> list = new ArrayList<String>();
       	  for(int c = 0; c < arr.length; c++){
       		  //System.out.println(arr[c]);
       		  if(arr[c].toLowerCase().contains("uuid")){
       			
       		  }else{
       			  list.add(arr[c]);
       		  }
       	  }
       	  
       	  b =  new StringBuilder().append(list).toString();
       	t.add(key + ":" + b);
         }
       }
       catch (Exception ex)
       {
       	ex.printStackTrace();
       }
	return t;
}
 
Example #21
Source File: NBTChecker_v1_7_R4.java    From ProRecipes with GNU General Public License v2.0 5 votes vote down vote up
public String getPotionType(String s) {
	ArrayList<String> t = new ArrayList<String>();
	byte[] testBytes = Base64Coder.decode(s);
	ByteArrayInputStream buf = new ByteArrayInputStream(testBytes);
   	 try
       {
         NBTTagCompound tag = NBTCompressedStreamTools.a(buf);
         Set<String> keys = tag.c();
         for (String key : keys) {
       	  String b = tag.get(key).toString();
       	  
       	  String[] arr = b.split(",");
       	  for(int c = 0; c < arr.length; c++){
       		 
       		//  System.out.println("SOMETHING IN A LOOP ?   "  + arr[c]);
       		  if(arr[c].contains("minecraft:")){
       			  return arr[c].replace("minecraft:", "").replace('"', ' ').replace(" ", "");
       		  }
       		  
       	  }
         }
       }
       catch (Exception ex)
       {
       	ex.printStackTrace();
       }
	return t.toString();
}
 
Example #22
Source File: ItemSerializer.java    From PerWorldInventory with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #23
Source File: AbstractRepresent.java    From Diorite with MIT License 5 votes vote down vote up
public final Node representString(String data)
{
    Tag tag = Tag.STR;
    Character style = null;
    if (StreamReader.NON_PRINTABLE.matcher(data).find())
    {
        tag = Tag.BINARY;
        char[] binary;
        try
        {
            binary = Base64Coder.encode(data.getBytes("UTF-8"));
        }
        catch (UnsupportedEncodingException e)
        {
            throw new YAMLException(e);
        }
        data = String.valueOf(binary);
        style = '|';
    }
    // if no other scalar style is explicitly set, use literal style for
    // multiline scalars
    if ((this.getDefaultScalarStyle() == null) && Representer.MULTILINE_PATTERN.matcher(data).find())
    {
        style = '|';
    }
    return this.representer.representScalar(tag, data, style);
}
 
Example #24
Source File: RandomEvent.java    From civcraft with GNU General Public License v2.0 5 votes vote down vote up
private void loadSavedMessages(String input) {
	String[] messages = input.split(",");
	
	for (String encodedMessage : messages) {
		String message = StringUtils.toAsciiString(Base64Coder.decode(encodedMessage));
		this.savedMessages.add(message);
	}
}
 
Example #25
Source File: RandomEvent.java    From civcraft with GNU General Public License v2.0 5 votes vote down vote up
private String getSavedMessagesSaveString() {
	String out = "";
	
	for (String message : this.savedMessages) {
		
		String msgEncoded = new String(Base64Coder.encode(message.getBytes())); 
		out += msgEncoded+",";
	}
	
	return out;
}
 
Example #26
Source File: LoreCraftableMaterial.java    From civcraft with GNU General Public License v2.0 5 votes vote down vote up
public static String serializeEnhancements(ItemStack stack) {
	String out = "";
	
	for (LoreEnhancement enh : LoreCraftableMaterial.getEnhancements(stack)) {
		out += enh.getClass().getName()+"@"+enh.serialize(stack)+",";
	}

	String outEncoded = new String(Base64Coder.encode(out.getBytes())); 
	return outEncoded;
}
 
Example #27
Source File: ItemSerializer.java    From PerWorldInventory with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #28
Source File: Base64Serialization.java    From PlayerVaults with GNU General Public License v3.0 5 votes vote down vote up
public static String toBase64(Inventory inventory, int size, String target) {
    try {
        ByteArrayOutputStream finalOutputStream = new ByteArrayOutputStream();
        ByteArrayOutputStream temporaryOutputStream = new ByteArrayOutputStream();
        BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(temporaryOutputStream);
        int failedItems = 0;

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

        // Save every element in the list
        for (int i = 0; i < inventory.getSize(); i++) {
            try {
                dataOutput.writeObject(inventory.getItem(i));
            } catch (Exception ignored) {
                failedItems++;
                temporaryOutputStream.reset();
            } finally {
                if (temporaryOutputStream.size() == 0) {
                    dataOutput.writeObject(null);
                }
                finalOutputStream.write(temporaryOutputStream.toByteArray());
                temporaryOutputStream.reset();
            }
        }

        if (failedItems > 0) {
            PlayerVaults.getInstance().getLogger().severe("Failed to save " + failedItems + " invalid items to vault " + target);
        }
        PlayerVaults.debug("Serialized " + inventory.getSize() + " items");

        // Serialize that array
        dataOutput.close();
        return Base64Coder.encodeLines(finalOutputStream.toByteArray());
    } catch (Exception e) {
        throw new IllegalStateException("Cannot into itemstacksz!", e);
    }
}
 
Example #29
Source File: InventoryTypeAdapter.java    From IridiumSkyblock with GNU General Public License v2.0 5 votes vote down vote up
public static String InventoryToString(ItemStack[] items) throws IllegalStateException {
    try {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
        dataOutput.writeInt(items.length);
        for (ItemStack item : items) {
            dataOutput.writeObject(item);
        }
        dataOutput.close();
        return Base64Coder.encodeLines(outputStream.toByteArray());
    } catch (Exception e) {
        throw new IllegalStateException("Unable to save item stacks.", e);
    }
}
 
Example #30
Source File: InventoryTypeAdapter.java    From IridiumSkyblock with GNU General Public License v2.0 5 votes vote down vote up
public static ItemStack[] StringToInventory(String data) throws IOException {
    try {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
        BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
        ItemStack[] items = new ItemStack[dataInput.readInt()];
        for (int i = 0; i < items.length; i++) {
            items[i] = (ItemStack) dataInput.readObject();
        }
        dataInput.close();
        return items;
    } catch (ClassNotFoundException e) {
        throw new IOException("Unable to decode class type.", e);
    }
}