Solved 1.9.4 player.getInventory().removeItem(ItemStack) does not remove item from the offHand

Discussion in 'Spigot Plugin Development' started by liveinacupboard, May 24, 2016.

  1. Code (Text):

    @EventHandler
    public void onItemSell(SellEvent e) {

        Player player = e.getPlayer();
        ItemStack item = e.getItem();

        boolean isFound = false;
        int totalItems = 0;

        for (ItemStack i : player.getInventory().getContents()) {

            if (i == null || i.getType() == Material.AIR) continue;

            if (!i.isSimilar(item)) continue;

                if (i.getMaxStackSize() > 1)
                    totalItems += i.getAmount();
                else
                    totalItems++;

                player.getInventory().remove(i);
                isFound = true;

        }
     
    My problem is in the title pretty much. If the server is running the 1.9 spigot jar, the item in the offHand doesn't get detected at all. If the server is running the 1.9.4 spigot jar, the item gets detected but doesn't get removed. Any ideas as to why this happens?

    TIA!
     
  2. I believe the off hand item is in this array "#getInventory().getExtraContents()"
     
  3. Unfortunately that doesn't solve my problem though :/. player.getInventory().getContents() does include the offHand as well, just printed out all the items in my inventory and it did include the item in my offHand as well.

    My problem is,
    doesn't remove the item in the offHand. I've even tried setting the type of the item in the offHand to air. (I do call player.updateInventory() in case you were wondering).

    Sorry, if I misunderstood your reply though.
     
  4. From CraftPlayerInventory
    Code (Text):

      @Override
      public ItemStack getItemInOffHand() {
      return CraftItemStack.asCraftMirror(getInventory().extraSlots[0]);
      }

      @Override
      public void setItemInOffHand(ItemStack item) {
      ItemStack[] extra = getExtraContents();
      extra[0] = item;
      setExtraContents(extra);
      }

      @Override
      public ItemStack[] getExtraContents() {
      int start = getInventory().items.length + getInventory().armor.length;
      return Arrays.copyOfRange(getContents(), start, start + getInventory().extraSlots.length);
      }

      @Override
      public void setExtraContents(ItemStack[] items) {
      setSlots(items, getInventory().items.length + getInventory().armor.length, getInventory().extraSlots.length);
      }
     
     
    • Friendly Friendly x 1