Solved How to remove and add certain item from player inventory

Discussion in 'Spigot Plugin Development' started by --TOMAS--, Jun 23, 2016.

  1. As the title says, I would like to know how to do that.
    For example, if a player has an item called: Disable players
    When, player click on that item, it will change for another item called: Enable plugins
    I have an idea.
    Create an itemStack
    Code (Text):
                        //disable players
                        ItemStack enablePlayers = new ItemStack(Material.REDSTONE_TORCH_ON, 1);
                        ItemMeta epMeta = enablePlayers.getItemMeta();
                        epMeta.setDisplayName("§cShow all players!");
                        //enable players
                        ItemStack disablePlayers = new ItemStack(Material.REDSTONE_TORCH_OFF, 1);
                        ItemMeta dpMeta = disablePlayers.getItemMeta();
                        dpMeta.setDisplayName("§cHide all players!");
     
    on player click on "enablePlayers"
    Code (Text):
                        player.getInventory().remove(enablePlayers);
                        player.getInventory().addItem(disablePlayers);
                        player.updateInventory();
     
  2. Use:

    player.setItemInHand(ITEMSTACK);

    DONT FORGET TO SET THE ITEMMETA!!!!

    If I helped you. Just like this Post.
     
  3. I'm assuming you would do this in the interact event, just simply do player.setItemInHand(disablePlayers);

    EDIT - Snipe'd :p
     
    • Like Like x 1
  4. Well, so:

    player.getInventory().remove(enablePlayers);
    player.setItemInHand(disablePlayers);
     
  5. Well, so:

    player.getInventory().remove(enablePlayers);
    player.setItemInHand(disablePlayers);
     
  6. Noo you dont have to remove this ItemStack. It Overrides the old.

    You just need this player.setItemInHand(disablePlayers);
    and if the ItemStack equals to disablePlayers this player.setItemInHand(enablePlayers);
     
  7. So? You can write an example? Because i can't understand
     
  8. Sure.
    if i helped like this post.
    Code (Text):
        @EventHandler
        public void onInteract(PlayerInteractEvent e){
         
            if(!(e.getAction() == Action.RIGHT_CLICK_AIR) && !(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
         
            if(e.getPlayer().getItemInHand().equals(enablePlayers)) e.getPlayer().setItemInHand(disablePlayers);
            if(e.getPlayer().getItemInHand().equals(disablePlayers)) e.getPlayer().setItemInHand(enablePlayers);
         
        }
     
    • Like Like x 1
  9. Well, is very easy! Thanks you!!!
     
    • Like Like x 1