Solved create ItemStack from toString()

Discussion in 'Spigot Plugin Development' started by BlockMcGee, Jun 10, 2017.

  1. Hello,
    the ItemStack object has the method toString() which gives fairly detailed information about the item, so i was thinking if there was a way to turn the ItemStack into a string, and then use that string later to recreate that item,

    pseudo code:
    Code (Java):

    ItemStack is = new ItemStack(Material.whatever);
    String s = is.toString();

    //later in the code..

    ItemStack fs = new ItemStack(s);/ ItemStack.fromString(s);?
     
    Please note that i need it as String, because i need to place it in lores, so dont ask "why not just store the item in a list or something" and i cant just store the material and amount, that will just delete all the other NBT data(enchants, itemflags etc)
     
  2. Instead of serializing the entire itemstack into the lore, why not keep a reference to it externally, and just append the displayname to the lore?

    Why in lores? What are you attempting to accomplish by doing this?
     
    • Like Like x 1
  3. Code (Text):
        public static String itemStackArrayToBase64(ItemStack[] items) {
            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) {
                return null;
            }
        }
     
        public static ItemStack[] itemStackArrayFromBase64(String data) {
            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 (Exception e) {
                return null;
            }
        }
    Code (Text):
    // Example

            Player player = Bukkit.getPlayer("random");
            ItemStack sword = new ItemStack(Material.DIAMOND_SWORD);
            ItemStack[] array = new ItemStack[]{sword};
            String str = itemStackArrayToBase64(array);
            player.getInventory().addItem(itemStackArrayFromBase64(str));
            player.updateInventory();
     
  4. But why? What do you gain from doing that?
     
  5. Serialize items? I understand that is what the user needs
     
  6. well its spoonfeeding but it will do i guess...
    i am trying to save items stacks into strings a lores of a specific item, i cant store it externally because, well, it all too complicated... ill just close this thread as i got what i wanted. thank you all.

    EDIT:
    long story short, basicly the items are first stored in a yaml file under a section that represents a certain block that contains those items, when i break that block, that section is no longer relevant so i remove it and set items as string(what i wanted) in the lore of the dropped block, when i place the block again, i will refrence the lore as to what items where stored before, and set the config accordingly. hope its understandable..
     
  7. If you need item serialization, you have ConfigurationSerializable
    If you need to store item attributes, there's NBT data and various other methods

    OP you never actually asked about your problem, just your attempted solution to the problem. This is known as the XY problem: http://xyproblem.info/

    There's probably an infinitely better way to go about what you're attempting to do.
     
  8. read the edit if that will help clear things up ^