Solved Removing inventory items

Discussion in 'Spigot Plugin Development' started by DisCoder212, Aug 7, 2018.

  1. I have a simple sign set-up where you define either buy or sell, the item, amount and cost as a result your sign should look something like this -

    Code (Text):

    [Sell]
    DIAMOND
    10
    $100
     


    I then check if their clicking on a sign either containing 'sell' or 'buy' and either sell their items or purchase items, however when I click on the 'sell' sign it doesn't seem to remove the player items, I even tried updating the players inventory still nothing..

    In my code I simply grab the block the player is interacting with and if it's a 'sign' then I grab that sign and all the written contents of that sign. I then loop through the players inventory checking if the player contains any Items as the item the sign is trying to sell.

    Code (Text):

       @EventHandler
        public void onClickSign(PlayerInteractEvent event) {

            Player player = event.getPlayer();

            Block block = event.getClickedBlock();

            Action action = event.getAction();

            if (!event.getHand().equals(EquipmentSlot.OFF_HAND)) {

                if (block != null) {

                    Material myBlock = block.getType();

                    if (myBlock != null) {

                        if ((myBlock == Material.WALL_SIGN) || (myBlock == Material.SIGN)) {

                            if (action == Action.RIGHT_CLICK_BLOCK) {

                                Sign sign = (Sign) block.getState();

                                String[] lines = sign.getLines();

                                if (lines[0].equalsIgnoreCase(ChatColor.DARK_RED + "[Sell]")) {

                                    try {

                                        String type = lines[1];

                                        int amount = Integer.parseInt(lines[2]);

                                        String cost = lines[3].replace("$", "");

                                        int newCost = Integer.parseInt(cost);

                                        ItemStack item = new ItemStack(Material.matchMaterial(type), amount);

                                        for (int i = 0; i < player.getInventory().getSize(); i++) {

                                            ItemStack invItem = player.getInventory().getItem(i);

                                            if (invItem == item) {
                                                player.getInventory().remove(item);
                                                player.updateInventory();
                                            }
                                        }

                                    } catch (NumberFormatException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
     
     
  2. Code (Java):

       if (invItem == item) {
        player.getInventory().remove(item);
        player.updateInventory();
          }
     
    You don't compare itemstacks using == or !=
    Try using ItemStack#isSimilar instead.

    Inventory#remove will remove all itemstacks of that kind from the inventory
     
    • Like Like x 1
    • Winner Winner x 1
  3. use

    Code (Text):
     if (invItem.equals(item)) {
        player.getInventory().remove(item);
        player.updateInventory();
                          }
    as @darklazerog said you can't compare itemstacks using ==
     
  4. FrostedSnowman

    Resource Staff

    that wont work as wanted because:

    say the sign is selling 1 diamond for $30

    if you have a stack of 9 diamonds, it wont work. ItemStack#equals takes into account the itemstack size, and 1 diamond doesnt equal a stack of 9 diamonds. you'd rather want to loop through the amount you're selling and keep track of that index and check if the item is similar (ItemStack#isSimilar) to the one you're selling
     
  5. yes I know, I said ItemStack#equals because he used "==" to compare, it depends how prefers him
     
  6. Yeah that's my bad, I forgot about isSimilar availability when working with Item-stacks, but what you said should work because it loops through the player's inventory and for how every many items are similar to the item the player is trying to send it will add money to them so if it finds 5 similar item-stack it will add x5 the money
     
  7. FrostedSnowman

    Resource Staff

    also compare enums using == not Object#equals