/** * * 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(),"Vault"); 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); } }
/** * 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) 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); } }
public static 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); } }
public static 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()]; // Read the serialized inventory 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); } }
/** * Deserialize a serialized byte array to an ItemStack array. * * @param data The data that should get deserialized. * @return The deserialized ItemStack array. null if deserialization failed. */ @Override public ItemStack[] deserialize(byte[] data) { if(data != null) { try(BukkitObjectInputStream bukkitObjectInputStream = new BukkitObjectInputStream(new ByteArrayInputStream(data))) { return (ItemStack[]) bukkitObjectInputStream.readObject(); } catch(Exception e) { e.printStackTrace(); } } return null; }
/** * 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 + "'"); } }
private void loadEnderchest(Inventory inv) { inv.clear(); if(!this.database.contains(this.key + ".enderchest")) { return; } Map<String, Object> enderchest = null; try { BukkitObjectInputStream in = new BukkitObjectInputStream(new ByteArrayInputStream(this.database.getBytes(this.key + ".enderchest"))); enderchest = (Map<String, Object>) in.readObject(); } catch(Exception e) { this.module.getLogger().log(Level.SEVERE, "Failed to load enderchest.", e); return; } for(int slot = 0; slot < inv.getSize(); slot++) { if(enderchest.containsKey(String.valueOf(slot))) { inv.setItem(slot, ItemStack.deserialize((Map<String, Object>) enderchest.get(String.valueOf(slot)))); } else { inv.setItem(slot, null); } } }
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); } }
public static ItemStack deserializeItemStack(String s) { try { byte[] b = BukkitObjectUtil.hexStringToByteArray(s); ByteArrayInputStream bais = new ByteArrayInputStream(b); BukkitObjectInputStream bois = new BukkitObjectInputStream(bais); ItemStack items = (ItemStack) bois.readObject(); bois.close(); bais.close(); return items; } catch (Exception ex) { ex.printStackTrace(); } return null; }
public static ItemStack[] deserializeItemStacks(String s) { try { byte[] b = BukkitObjectUtil.hexStringToByteArray(s); ByteArrayInputStream bais = new ByteArrayInputStream(b); BukkitObjectInputStream bois = new BukkitObjectInputStream(bais); ItemStack[] items = (ItemStack[]) bois.readObject(); bois.close(); bais.close(); return items; } catch (Exception ex) { ex.printStackTrace(); } return null; }
public static List<PotionEffect> deserializePotionEffects(String s) { try { byte[] b = BukkitObjectUtil.hexStringToByteArray(s); ByteArrayInputStream bais = new ByteArrayInputStream(b); BukkitObjectInputStream bois = new BukkitObjectInputStream(bais); PotionEffect[] eff = (PotionEffect[]) bois.readObject(); bois.close(); bais.close(); return Arrays.asList(eff); } catch (Exception ex) { ex.printStackTrace(); } return null; }
public NationsContainer(NBTCompoundTag rootTag, Chunk chunk) throws IOException, ClassNotFoundException { int x = ((NBTShortTag)rootTag.getTag("X")).getValue() + chunk.getX() * 16; int y = ((NBTShortTag)rootTag.getTag("Y")).getValue(); int z = ((NBTShortTag)rootTag.getTag("Z")).getValue() + chunk.getZ() * 16; location = new Location(chunk.getWorld(), x,y,z); byte[] itemsRaw = ((NBTByteArrayTag)rootTag.getTag("BukkitItemStackArray")).getValue(); ByteArrayInputStream itemsStream = new ByteArrayInputStream(itemsRaw); BukkitObjectInputStream bois = new BukkitObjectInputStream(itemsStream); items = (ItemStack[])bois.readObject(); bois.close(); invType = InventoryType.valueOf(((NBTStringTag)rootTag.getTag("InventoryType")).getValue()); }
/** * Transfer a string into an inventory * * @param data String * @return Inventory * @throws IOException Failed. */ @Deprecated public static Inventory stringToInventory(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); } }
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; }
/** * * 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 if we were unable to parse the base64 string */ public static Inventory fromBase64(String data) throws IOException { if(data.isEmpty()) return Bukkit.getServer().createInventory(null, 0); try { ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data)); BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream); int size = dataInput.readInt(); Inventory inventory = Bukkit.getServer().createInventory(null, (int)Math.ceil((double)size / inventoryMultipule) * inventoryMultipule); // Read the serialized inventory for (int i = 0; i < size; i++) { inventory.setItem(i, (ItemStack) dataInput.readObject()); } dataInput.close(); inputStream.close(); return inventory; } catch (ClassNotFoundException e) { throw new IOException("Unable to decode class type.", e); } }
/** * 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 if we was unable to parse the base64 string */ public static ItemStack[] itemStackArrayFromBase64(String data) throws IOException { if(data.isEmpty()) return new ItemStack[] {}; 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 (ClassNotFoundException e) { throw new IOException("Unable to decode class type.", e); } }
public static ItemStack decodeItemStack(byte[] buf) { try (ByteArrayInputStream inputStream = new ByteArrayInputStream(buf)) { try (BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream)) { return (ItemStack) dataInput.readObject(); } } catch (ClassNotFoundException | IOException e) { throw new RuntimeException(e); } }
public static ItemStack[] decodeItemStacks(byte[] buf) { try (ByteArrayInputStream inputStream = new ByteArrayInputStream(buf)) { try (BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream)) { ItemStack[] items = new ItemStack[dataInput.readInt()]; for (int i = 0; i < items.length; i++) { items[i] = (ItemStack) dataInput.readObject(); } return items; } } catch (ClassNotFoundException | IOException e) { throw new RuntimeException(e); } }
public static Inventory decodeInventory(byte[] buf, String title) { try (ByteArrayInputStream inputStream = new ByteArrayInputStream(buf)) { try (BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream)) { Inventory inventory = Bukkit.getServer().createInventory(null, dataInput.readInt(), title); for (int i = 0; i < inventory.getSize(); i++) { inventory.setItem(i, (ItemStack) dataInput.readObject()); } return inventory; } } catch (ClassNotFoundException | IOException e) { throw new RuntimeException(e); } }
public static Object fromBase64Obj(String data) { try { ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data)); BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream); Object obj = dataInput.readObject(); dataInput.close(); return obj; } catch (Exception e) { return null; } }
@Test public void testDeserialize() throws Exception { BukkitItemStackSerializer deserializer = new BukkitItemStackSerializer(); assertNull("Deserialized data should be null", deserializer.deserialize(null)); assertNull("Deserialized data should be null when an error occurs", deserializer.deserialize(new byte[] { 2, 5, 6 })); BukkitObjectInputStream mockedInputStream = mock(BukkitObjectInputStream.class); doReturn(new ItemStack[] {}).when(mockedInputStream).readObject(); whenNew(BukkitObjectInputStream.class).withAnyArguments().thenReturn(mockedInputStream); assertNotNull("Deserialized data should not be null", deserializer.deserialize(new byte[] { 22, 25, 65 })); }
private void loadInventory(PlayerInventory inv) { inv.clear(); inv.setArmorContents(new ItemStack[] {null, null, null, null}); if(!this.database.contains(this.key + ".inventory")) { return; } Map<String, Object> inventory = null; try { BukkitObjectInputStream in = new BukkitObjectInputStream(new ByteArrayInputStream(this.database.getBytes(this.key + ".inventory"))); inventory = (Map<String, Object>) in.readObject(); } catch(Exception e) { this.module.getLogger().log(Level.SEVERE, "Failed to load inventory.", e); return; } for(int slot = 0; slot < inv.getSize(); slot++) { if(inventory.containsKey(String.valueOf(slot))) { inv.setItem(slot, ItemStack.deserialize((Map<String, Object>) inventory.get(String.valueOf(slot)))); } else { inv.setItem(slot, null); } } if(inventory.containsKey("helmet")) { inv.setHelmet(ItemStack.deserialize((Map<String, Object>) inventory.get(String.valueOf("helmet")))); } if(inventory.containsKey("chestplate")) { inv.setChestplate(ItemStack.deserialize((Map<String, Object>) inventory.get(String.valueOf("chestplate")))); } if(inventory.containsKey("leggings")) { inv.setLeggings(ItemStack.deserialize((Map<String, Object>) inventory.get(String.valueOf("leggings")))); } if(inventory.containsKey("boots")) { inv.setBoots(ItemStack.deserialize((Map<String, Object>) inventory.get(String.valueOf("boots")))); } }
public static ItemStack deserializeItem(JSONObject data, int index) { try { ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data.getString("item"))); BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream); try { return (ItemStack) dataInput.readObject(); } finally { dataInput.close(); } } catch (JSONException | IOException | ClassNotFoundException ex) { ex.printStackTrace(); return null; } }
public static Inventory fromBase64(String data) throws IOException { try { ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data)); BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream); int maxItems = dataInput.readInt(); int invSize = STBUtil.roundUp(maxItems, 9); // Bukkit inventory size must be multiple of 9 Inventory inventory = Bukkit.getServer().createInventory(null, invSize); // Read the serialized inventory for (int i = 0; i < maxItems; i++) { ItemStack stack = (ItemStack) dataInput.readObject(); int nAttrs = dataInput.readInt(); if (nAttrs > 0) { Attributes attributes = new Attributes(stack); for (int n = 0; n < nAttrs; n++) { String s = (String) dataInput.readObject(); String[] fields = s.split(";;"); attributes.add(Attributes.Attribute.newBuilder(). name(fields[2]). amount(Double.parseDouble(fields[3])). uuid(UUID.fromString(fields[0])). operation(Attributes.Operation.valueOf(fields[1])). type(Attributes.AttributeType.fromId(fields[4])). build() ); } stack = attributes.getStack(); } if (stack != null) { inventory.setItem(i, stack); } } dataInput.close(); return inventory; } catch (ClassNotFoundException e) { throw new IOException("Unable to decode class type.", e); } }