|
| 1 | +/* |
| 2 | + * To change this license header, choose License Headers in Project Properties. |
| 3 | + * To change this template file, choose Tools | Templates |
| 4 | + * and open the template in the editor. |
| 5 | + */ |
| 6 | +package pw.hwk.tutorial.util; |
| 7 | + |
| 8 | +import java.io.ByteArrayInputStream; |
| 9 | +import java.io.ByteArrayOutputStream; |
| 10 | +import java.io.IOException; |
| 11 | +import org.bukkit.Bukkit; |
| 12 | +import org.bukkit.inventory.Inventory; |
| 13 | +import org.bukkit.inventory.ItemStack; |
| 14 | +import org.bukkit.util.io.BukkitObjectInputStream; |
| 15 | +import org.bukkit.util.io.BukkitObjectOutputStream; |
| 16 | +import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder; |
| 17 | + |
| 18 | +/** |
| 19 | + * |
| 20 | + * @author Frostalf |
| 21 | + */ |
| 22 | +public class Base64Serialize { |
| 23 | + public static String toBase64(Inventory inventory) { |
| 24 | + try { |
| 25 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 26 | + BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream); |
| 27 | + |
| 28 | + // Write the size of the inventory |
| 29 | + dataOutput.writeInt(inventory.getSize()); |
| 30 | + |
| 31 | + // Save every element in the list |
| 32 | + for (int i = 0; i < inventory.getSize(); i++) { |
| 33 | + dataOutput.writeObject(inventory.getItem(i)); |
| 34 | + } |
| 35 | + |
| 36 | + // Serialize that array |
| 37 | + dataOutput.close(); |
| 38 | + return Base64Coder.encodeLines(outputStream.toByteArray()); |
| 39 | + } catch (Exception e) { |
| 40 | + throw new IllegalStateException("Cannot into itemstacksz!", e); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public static String toBase64(ItemStack[] is, int size) { |
| 45 | + Inventory inventory = Bukkit.createInventory(null, size); |
| 46 | + inventory.setContents(is); |
| 47 | + return toBase64(inventory); |
| 48 | + } |
| 49 | + |
| 50 | + public static Inventory fromBase64(String data) { |
| 51 | + try { |
| 52 | + ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data)); |
| 53 | + BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream); |
| 54 | + Inventory inventory = Bukkit.getServer().createInventory(null, dataInput.readInt()); |
| 55 | + |
| 56 | + // Read the serialized inventory |
| 57 | + for (int i = 0; i < inventory.getSize(); i++) { |
| 58 | + inventory.setItem(i, (ItemStack) dataInput.readObject()); |
| 59 | + } |
| 60 | + dataInput.close(); |
| 61 | + return inventory; |
| 62 | + } catch (Exception e) { |
| 63 | + } |
| 64 | + return null; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * |
| 69 | + * A method to serialize an {@link ItemStack} array to Base64 String. |
| 70 | + * |
| 71 | + * <p /> |
| 72 | + * |
| 73 | + * Based off of {@link #toBase64(Inventory)}. |
| 74 | + * |
| 75 | + * @param items to turn into a Base64 String. |
| 76 | + * @return Base64 string of the items. |
| 77 | + * @throws IllegalStateException |
| 78 | + */ |
| 79 | + public static String itemStackArrayToBase64(ItemStack[] items){ |
| 80 | + try { |
| 81 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 82 | + BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream); |
| 83 | + |
| 84 | + // Write the size of the inventory |
| 85 | + dataOutput.writeInt(items.length); |
| 86 | + |
| 87 | + // Save every element in the list |
| 88 | + for (int i = 0; i < items.length; i++) { |
| 89 | + dataOutput.writeObject(items[i]); |
| 90 | + } |
| 91 | + |
| 92 | + // Serialize that array |
| 93 | + dataOutput.close(); |
| 94 | + return Base64Coder.encodeLines(outputStream.toByteArray()); |
| 95 | + } catch (Exception e) { |
| 96 | + throw new IllegalStateException("Unable to save item stacks.", e); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Gets an array of ItemStacks from Base64 string. |
| 102 | + * |
| 103 | + * <p /> |
| 104 | + * |
| 105 | + * Base off of {@link #fromBase64(String)}. |
| 106 | + * |
| 107 | + * @param data Base64 string to convert to ItemStack array. |
| 108 | + * @return ItemStack array created from the Base64 string. |
| 109 | + * @throws IOException |
| 110 | + */ |
| 111 | + public static ItemStack[] itemStackArrayFromBase64(String data) { |
| 112 | + try { |
| 113 | + ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data)); |
| 114 | + BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream); |
| 115 | + ItemStack[] items = new ItemStack[dataInput.readInt()]; |
| 116 | + |
| 117 | + // Read the serialized inventory |
| 118 | + for (int i = 0; i < items.length; i++) { |
| 119 | + items[i] = (ItemStack) dataInput.readObject(); |
| 120 | + } |
| 121 | + |
| 122 | + dataInput.close(); |
| 123 | + return items; |
| 124 | + } catch (IOException | ClassNotFoundException e) { |
| 125 | + } |
| 126 | + return null; |
| 127 | +} |
| 128 | +} |
0 commit comments