Inventory#setItem() replaces Item in Inv with AIR

Discussion in 'Spigot Plugin Development' started by Brainmix, Jun 3, 2016.

  1. I don't know, if it is a bug or am i just to stupid to figure it out myself.
    When i am trying to use the method Inventory#setItem() with given slot and ItemStack,
    the game only replaces the slot with an AIR ItemStack

    Here some example code:
    Code (Text):
    @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
     
            if(!command.getName().equalsIgnoreCase("test")) return true;
            if(!(sender instanceof Player))                 return true;
     
            Player                 p             = (Player) sender;
            ItemStack              item          = new ItemStack(Material.POTATO, 1);
            PlayerInventory        inv           = p.getInventory();
     
            p.sendMessage("expected item: §2" + item );
            p.sendMessage("item in hand (before): §3" + p.getItemInHand());
            inv.setItem(inv.getHeldItemSlot(), item);
            p.sendMessage("item in hand (after): §a" + p.getItemInHand());
     
            return true;
     
        }
    (using Spigot 1.8.8)

    When I run this command, the output of the chat is:

    expected item: ItemStack{POTATO x 1}
    item in hand (before): ItemStack{STONE x 64}
    item in hand (after): ItemStack{AIR x 0}


    I never had this issue before.
    I tried diffrent Spigot and CraftBukkit version, but it still happens.
    I tried diffrent ways to set the Item.
    Even to send a PacketPlayOutSetSlot, i have tried.

    There are no exceptions, which makes it a way more harder, to finde the issue.
    I don't have anymore ideas to go on
     
    #1 Brainmix, Jun 3, 2016
    Last edited: Jun 3, 2016
  2. Think u need to use Material.POTATO_ITEM instead of Material.POTATO... hope that helps :)
     
  3. lol, thanks a lot, xD
    So, why there is a POTATO Material?!?!
     
  4. If u plant a potato, the block material is "POTATO".
    You can't turn a block material to potato item :D
     
  5. Which slot are you trying to put it in?
     
  6. If you want to put it on the hotbar, the slots are 0-8.