Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BackpackUtils {
- public static void storeItems(HashMap<Integer, ItemStack> content, ItemStack item){
- ItemMeta data = item.getItemMeta();
- if (content.size() == 0){
- data.getPersistentDataContainer().set(new NamespacedKey(Backpack.getPlugin(), "backpack_id"), PersistentDataType.STRING, "");
- item.setItemMeta(data);
- } else {
- try {
- ByteArrayOutputStream input = new ByteArrayOutputStream();
- BukkitObjectOutputStream output = new BukkitObjectOutputStream(input);
- output.writeObject(content.size());
- content.forEach((Integer, ItemStack) -> {
- try {
- output.writeObject(content);
- } catch (IOException ex) {
- System.out.println(ex);
- }
- } );
- output.flush();
- } catch (IOException ex) {
- System.out.println(ex);
- }
- }
- }
- public static HashMap<Integer, ItemStack> getContents(ItemStack item) {
- PersistentDataContainer data = item.getItemMeta().getPersistentDataContainer();
- HashMap<Integer, ItemStack> contents = new HashMap<>();
- String encodedItems = data.get(new NamespacedKey(Backpack.getPlugin(), "Backpack_id"), PersistentDataType.STRING);
- try {
- assert encodedItems != null;
- if (!encodedItems.isEmpty()){
- byte[] rawData = Base64.getDecoder().decode(encodedItems);
- ByteArrayInputStream io = new ByteArrayInputStream(rawData);
- BukkitObjectInputStream in = new BukkitObjectInputStream(io);
- int itemsCount = in.readInt();
- for(int i = 0; i < itemsCount; i++){
- contents.put((Integer) in.readObject(), (ItemStack) in.readObject());
- in.readObject();
- }
- in.close();
- }
- } catch (ClassNotFoundException | IOException | NullPointerException ex) {
- System.out.println(ex);
- }
- return contents;
- }
- }
Add Comment
Please, Sign In to add comment