[SOLVED]Removing 1 item from ItemStack

Discussion in 'Plugin Development' started by Razorcane, Nov 1, 2011.

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

    Razorcane

    Ok, so I'm needing to remove items from a player inventory(on click), but the problem is, whenever I click, it removes the entirety of the ItemStack, not just 1 entity. How would I correct this?

    EDIT: If code will help, I can give you small snippets of how I'm currently doing it.
    Code:
    protected boolean ****(Player p, ItemStack inHand, Block clicked)
    
    int blockamnt = inHand.getAmount();
    inHand.setAmount(blockamnt--);
     
  2. Offline

    SirTyler

    Try refeering to the Inventory instead of the hand, so like
    Code:
    protected boolean ****(Player p, ItemStack inHand, Block clicked) {
    Item item = p.getIteminHand();
    p.remove(new ItemStack(item.getID,1));
    p.updateInventory
    }
    I didn't test it but that is the basic idea behind doing it
     
  3. Offline

    Razorcane

    That is a good idea, but getIteminHand returns an ItemStack, not an item. :p Also, p.remove doesn't work, pity, that. :(
     
  4. Offline

    SirTyler

    Like I said, didnt test this xP and my bad, use p.removeItem instead
     
  5. Offline

    Razorcane

    removeItem isn't an available class. :p
     
  6. Offline

    SirTyler

    You have to put the itemStack stuff in it xD replace remove with removeItem
     
  7. Offline

    scranner

    try sifting through this. this does it!
    Code:
    if(label.equalsIgnoreCase("item") && (args[0].equalsIgnoreCase("exchange")))
            {
                if(player.getItemInHand().getTypeId() == ItemID)
                {
                    if(args.length < 2)
                    {
                        player.sendMessage("Not enough arguments");
                        return true;
                    }
                    else if(args.length > 2)
                    {
                        player.sendMessage("Too many arguments");
                        return true;
                    }
                    int amountSold = Integer.parseInt(args[1]);
                    int handAmount = player.getItemInHand().getAmount();
                    int giveAmount = handAmount - amountSold;
                    Material item = player.getItemInHand().getType();
    
                    if(handAmount < amountSold)
                    {
                        player.sendMessage("not enough diamonds!");
                        return true;
                    }
                    else if(handAmount >= amountSold)
                    {
                        if(handAmount == amountSold)
                        {
                            player.setItemInHand(null);
                            return true;
                        }
                        else
                        {
                            player.setItemInHand(new ItemStack(item, giveAmount));
                            return true;
                        }
                    }
                }
                else
                {
                    player.sendMessage("Please hold "+ ItemID);
                }
            }
            return false;
        }
     
  8. Offline

    McDjuady

    problem is realy simple.
    Code:
    blockamnt--
    tells java to FIRST execute
    Code:
    inHand.setAmmount(blockamnt)
    and THEN decrement blockamnt.
    Correct way:
    Code:
    inHand.setAmmount(--blockamnt)
    Java will now FIRST decrement blockamnt and THEN set it

    Hope i helped you
    Greetings
    Djuady

    Ps: Sorry... My english seems to be fucked up today... been awake for 48 hours
    PPs: And of course you should chek if blockamnt is greater 1, because you don't want to have a stack with ammount 0. Remove it from if the ammount equals 1
     
  9. Offline

    scranner

    set it null if its 0 or you crash their client
     
  10. Offline

    Razorcane

    I fixed the issue, thanks for all the help. :)
     
Thread Status:
Not open for further replies.

Share This Page