Add some methods to serialize an inventory to Base64 string.
Special thanks to Comphenix, @aadnk, for the code.
This commit is contained in:
parent
fc0bde6255
commit
5e4bc77579
@ -1,15 +1,24 @@
|
|||||||
package com.graywolf336.jail;
|
package com.graywolf336.jail;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.inventory.Inventory;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
import org.bukkit.util.Vector;
|
import org.bukkit.util.Vector;
|
||||||
|
import org.bukkit.util.io.BukkitObjectInputStream;
|
||||||
|
import org.bukkit.util.io.BukkitObjectOutputStream;
|
||||||
|
|
||||||
|
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
|
||||||
|
|
||||||
public class Util {
|
public class Util {
|
||||||
private final static Pattern DURATION_PATTERN = Pattern.compile("^(\\d+)\\s*(m(?:inute)?s?|h(?:ours?)?|d(?:ays?)?|s(?:econd)?s?)?$", Pattern.CASE_INSENSITIVE);
|
private final static Pattern DURATION_PATTERN = Pattern.compile("^(\\d+)\\s*(m(?:inute)?s?|h(?:ours?)?|d(?:ays?)?|s(?:econd)?s?)?$", Pattern.CASE_INSENSITIVE);
|
||||||
@ -98,4 +107,72 @@ public class Util {
|
|||||||
|
|
||||||
return Long.valueOf(t);
|
return Long.valueOf(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* A method to get an {@link Inventory} from an encoded, 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 data Base64 string of data containing an inventory.
|
||||||
|
* @return Inventory created from the Base64 string.
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public static Inventory fromBase64(String data) throws IOException {
|
||||||
|
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 (ClassNotFoundException e) {
|
||||||
|
throw new IOException("Unable to decode class type.", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user