Solved Convert itemstack string

Discussion in 'Spigot Plugin Development' started by flyvendetorsk, Jun 13, 2016.

  1. Hi, I was just wondering how to convert an ItemStack string to an ItemStack object. I could not find any solution to my problem.

    Example: ItemStack{QUARTZ_BLOCK x 50}
     
  2. Try:
    Code (Text):

    ItemStack stack = new ItemStack(Material.QUARTZ_BLOCK);
     
    Import org.bukkit.inventory.itemstack
     
    • Winner Winner x 1
  3. The MemorySection class provides methods for doing this since ItemStack implements ConfigurationSerializable.

    To save the ItemStack use:
    Code (Text):

    getConfig().set(someKey, someItemStack);
     
    and to get it from the config:
    Code (Text):

    ItemStack = getConfig().getItemStack(someKey);
     
    I am using getConfig() here as example, if you want to use a seperate config file you will need to handle that.

    EDIT: Don't forget to save the config after the call to set.
     

  4. Code (Text):
    public ArrayList<ItemStack> getActiveItems() throws SQLException {
            ArrayList<ItemStack> i = new ArrayList<ItemStack>();
            ResultSet rs = sql.query("SELECT * FROM Auction");
            while (rs.next()) {
                config.getData().set("ItemStack", rs.getString("ITEM"));
                config.saveData();
                i.add(config.getData().getItemStack("ItemStack"));
                Bukkit.broadcastMessage(config.getData().getItemStack("ItemStack") + "");
            }

            return i;
        }
    EDIT: I tried to check if (config.getData().isItemStack("ItemStack")) and it returned false.
    EDIT2: I see now, it saves differently in config. I found an solution to my problem

    returns only null.
     
    #4 flyvendetorsk, Jun 13, 2016
    Last edited: Jun 13, 2016