Solved remove item from inventory and put in chest

Discussion in 'Plugin Development' started by drillbitsworkshop, Sep 12, 2021.

  1. So I am working on a chest based shop plugin. I got everything working but just found a serious bug in it.

    when selling an item it will check the players inventory, see if they have the item, then make an itemstack of that item and remove it and then adds that item stack to the chest. which works flawlessly, UNLESS the item is damaged. if the item is damaged it will do everything its supposed to but doesn't actually remove it from the players inventory.
    Code:
    player.getInventory().contains(Material.valueOf(this.plugin.getItem(block)), amount)
    This line checks the players inventory to see if they have enough of the item
    Code:
    player.getInventory().removeItem(new ItemStack[] { new ItemStack(Material.valueOf(this.plugin.getItem(block)), amount) });
    this is the line of code that's used to remove the item from the players inventory
    is there another check I would have to do to check for damaged items?
     
  2. Offline

    man_in_matrix

  3. So I added this.
    Code:
    @SuppressWarnings("deprecation")
          public boolean hasItems(Player player, String itemname, int amount) {
                  boolean hasitems = false;
                  int amountininv = 0;
                  for(int i = 0; i <= 36; i++)
                  {
                      try
                      {
                          if (player.getInventory().getItem(i).getDurability() > 0) {
                              if (player.getInventory().getItem(i).getType().equals(Material.valueOf(itemname))) {
                                  amountininv++;
                              }
                          }
                      }
                      catch(Exception e){}
                  }
                  if (amountininv >= amount) {
                      hasitems = true;
                  }
                  return hasitems;
              }
    and it works great for tools. but now doesn't detect any items that aren't tools. Am I going to have to run a check if the item is a tool first and then go from there and if so is there an easy way to check that?

    UPDATE: Got it fixed. thank you for all the help
     
    Last edited: Sep 13, 2021

Share This Page