You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
3.4 KiB
82 lines
3.4 KiB
package cx.sfy.TheBridge.kit;
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.IOException;
|
|
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.inventory.Inventory;
|
|
import org.bukkit.inventory.ItemStack;
|
|
import org.bukkit.inventory.PlayerInventory;
|
|
import org.bukkit.util.io.BukkitObjectInputStream;
|
|
import org.bukkit.util.io.BukkitObjectOutputStream;
|
|
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
|
|
|
|
public class Kit {
|
|
|
|
public String playerInventoryToBase64(PlayerInventory playerInventory) throws IllegalStateException {
|
|
String content = toBase64(playerInventory);
|
|
return content;
|
|
}
|
|
|
|
public 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);
|
|
}
|
|
}
|
|
|
|
public 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);
|
|
}
|
|
}
|
|
|
|
public 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());
|
|
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);
|
|
}
|
|
}
|
|
|
|
public ItemStack[] itemStackArrayFromBase64(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);
|
|
}
|
|
}
|
|
|
|
}
|