Removing item just from hand.

Discussion in 'Plugin Development' started by Snowl, Feb 3, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Snowl

    I need to remove 1 item from the players inventory, is this possible? Everytime I try and do it it removes it from all the slots and removes all of the item, not just one.

    Thanks :)
     
  2. Offline

    lostaris

    Do you want to remove one item from a stack of items?
     
  3. Offline

    JoeMaximum

    Something like this should work :

    Code:
    player.getInventory().getItemInHand().setAmount(player.getInventory().getItemInHand().getAmount()-1);
     
  4. Offline

    Snowl

    Yes.
    Doesnt work :(
     
  5. Offline

    JoeMaximum

    What doesnt work ?
    What error you get?
    Code sample?

    Hard to help with so little infos.
    The example i gave you, need the latest bukkit.
     
  6. Offline

    Snowl

    It just doesn't remove the item :( I'll update teh bukkitz
     
  7. Offline

    JoeMaximum

    Post some of your code if it still not working.
     
  8. Offline

    Archelaus

    Code:
            ItemStack old = new ItemStack(event.getPlayer().getItemInHand().getTypeId(), event.getPlayer().getItemInHand().getAmount() - 1);
            event.getPlayer().setItemInHand(old);
     
  9. Offline

    DerpinLlama

    Code:
    player.getInventory().removeItem(player.getInventory().getItemInHand());
    ?
     
  10. Offline

    Snowl

    Nope, still doesnt remove the item D: It just stays there and hangs on for dear life
     
  11. Offline

    DerpinLlama

    Mine or RightLegRed's one? We posted at basically the same time. >_<
     
  12. Offline

    Snowl

    Urgh, let me test both of yours :p Derpin's removes it, but if you relog it removes all the instances of the items in the whole inventory, not just in the hand

    No wait it doesn't, stupid me :D I'll test out right's anyway.
     
  13. Offline

    JoeMaximum

    Like i said , post your code ... its something really easy to do ...
     
  14. Offline

    Archelaus

    Please don't claim it's easy to do without posting how to do it, he doesn't need to post code for this. It's something that doesn't require his code to make.
     
  15. Offline

    Snowl

    Yeah, they both work, thanks guys! :D
     
  16. Offline

    JoeMaximum

    well, i posted a code and it work for me ...

    This work fine ...
    Code:
    player.getInventory().getItemInHand().setAmount(player.getInventory().getItemInHand().getAmount()-1);
    
     
  17. Offline

    redpois0n

    thanks..
     
  18. Offline

    DrBowe

    To be fair, he did post code about 'how to do it' already. And after being told his snippet "doesn't work", he was trying to get some clarification as to "what didn't work", so he could help further.

    EDIT:
    Oh crap. I just looked at the post dates.
    *hides in shame*
     
  19. Offline

    darkdeath1332

    player.setItemInHand(null);
     
  20. Offline

    Bammerbom

    darkdeath1332 nice work, reply to a post, that is posted 3 years ago
     
  21. Offline

    darkdeath1332

    People still google this question and will see the awnser
     
    ThunderWaffeMC likes this.
  22. Offline

    Diegokoen

    Hey,
    I have to remove 1 item from a item stack but by the last one is he not removing?

    Sorry for my bad english:)
     
  23. Diegokoen Try:

    PHP:
    ItemStack is player.getItemInHand();
    if(
    is != null) {
        
    is.getAmount() > is.setAmount(is.getAmount()-1) : player.setItemInHand(null);
    }
     
    Diegokoen likes this.
  24. Offline

    Diegokoen

    I get a Error on line 2 of your code " is.getAmount() > 1 ? is.setAmount(is.getAmount()-1) : player.setItemInHand(null);"
    This is my code:


    Code:
    @EventHandler
    public void onPlayerClickEvent(PlayerInteractEvent event) {
     
    final Player player = event.getPlayer();
     
    ItemStack blood = new ItemStack(Material.INK_SACK, 1, (short) 1);
    ItemMeta bloodMeta = (ItemMeta) blood.getItemMeta();
    bloodMeta.setDisplayName(ChatColor.RED + "Blood Bag");
    List<String> bloodList = new ArrayList<String>();
    bloodList.add(ChatColor.WHITE + "§8Right click to heal!");
    bloodMeta.setLore(bloodList);
    blood.setItemMeta(bloodMeta);
     
    ItemStack kit = new ItemStack(Material.FEATHER, 1, (short) 1);
    ItemMeta kitMeta = (ItemMeta) kit.getItemMeta();
    kitMeta.setDisplayName(ChatColor.RED + "Kits");
    List<String> kitList = new ArrayList<String>();
    kitList.add(ChatColor.WHITE + "§8Right-click to open the kit Menu!");
    kitMeta.setLore(kitList);
    kit.setItemMeta(kitMeta);
     
    if(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.RED+ "Kits")){
    if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK) || event.getAction().equals(Action.RIGHT_CLICK_AIR)){
    menu.show(event.getPlayer());
    }
    }
     
    if(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.RED+ "Blood Bag")){
    if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK) || event.getAction().equals(Action.RIGHT_CLICK_AIR)){
    player.sendMessage("§cYou have been healed!");
    ItemStack is = player.getItemInHand();
    if(is != null) {
    is.getAmount() > 1 ? is.setAmount(is.getAmount()-1) : player.setItemInHand(null);
    }
    }
    }
    }
     
  25. Diegokoen Ah, sorry, I messed that up. I see that wouldn't work :p Try replacing that line with
    PHP:
    if(is.getAmount() > 1)
        
    is.setAmount(is.getAmount() - 1);
    else 
    player.setItemInHand(null);
     
  26. Offline

    Diegokoen

    Tnx now is it working! ;)

    can you help me with me scorebord problem?

    here is the link:

    http://forums.bukkit.org/threads/need-help-with-scoreboard.249019/
     
  27. Offline

    1Camer0471

    Here is the code I use:
    Code:java
    1. ItemStack old = new ItemStack(e.getPlayer().getItemInHand().getType(), e.getPlayer().getItemInHand().getAmount() - 1);
     
  28. Offline

    CheesyFreezy

    Code:java
    1. if(player.getItemInHand().getAmount > 1) {
    2. player.getItemInHand().setAmount(player.getItemInHand().getAmount() - 1);
    3. } else {
    4. player.getItemInHand().setAmount(0);
    5. }
     
  29. Offline

    tincopper2

    The creator of JRat plays Minecraft?
     
Thread Status:
Not open for further replies.

Share This Page