This method to take Gold away not working..

Discussion in 'Plugin Development' started by Liam Allan, Feb 25, 2013.

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

    Liam Allan

    Hey guys. Another problem, someone mind telling me why this doesn't work or fixing it?

    Code:
    public void rmoney(Player player, int xam) {
            int rem = 0;
            for (ItemStack i : player.getInventory().getContents()) {
                if (i != null) {
                    if (i.getType() == Material.GOLD_INGOT) {
                        while (i.getAmount() > 0  || rem < xam) {
                            i.setAmount(i.getAmount() - 1);
                            rem++;
                        }
                    }
                }
            }
        }
    Thank you in advanced.
     
  2. Well you're not removing the item after it gets to amount 0, you must set it to null.

    And instead of while() you should do a basic math calculation instead... something like i.getAmount() - xam and if that is below or equal to 0 then subtract i.getAmount() from rem and set item to null, otherwise set the amount and return because you've removed the amount you needed.
     
  3. Offline

    Liam Allan

    Okay, thank you for this.
     
  4. Offline

    Frazz86

    Also if you have multiple stacks of gold this will take 1 from each stack. Shove break; in after rem++; and then after that bracket } put return;
     
  5. Frazz86
    No it won't, mind the while()
     
  6. Offline

    Liam Allan

    This.
    And thanks, I got it fixed up.
     
Thread Status:
Not open for further replies.

Share This Page