Hello together, I got a problem with my plugin "Mortar". I'm trying to add a feature which takes a specified amount of an item when shooting the mortar, but it won't work properly. This is the part of my players.java which deals with the costs. mortar.costitemid = ID of the Item to pay with (set in the mortar.java) mortar.costamount = Amount of the Item to pay per shot (set in the mortar.java as well). Code: if(player.getInventory().contains(mortar.costitemid, mortar.costamount)) { int index = player.getInventory().first(mortar.costitemid); System.out.println("Index: " + index); ItemStack stack = player.getInventory().getItem(index); int amountold = stack.getAmount(); System.out.println("Old Amount: " + amountold); int amount = amountold - mortar.costamount; System.out.println("New Amount: " + amount); stack.setAmount(amount); int amountnewlyset = stack.getAmount(); System.out.println("Newly set Amount: " + amountnewlyset); player.chat(red + mortar.warning); Egg egg = player.throwEgg(); eggid = egg.getEntityId(); }else{ player.sendMessage("You don't have enough resources to pay for the mortar."); } I had hoped to get it to work this way, BUT: The console prints out: Code: 2011-11-02 01:39:53 [INFO] Old Amount: 3 2011-11-02 01:39:53 [INFO] New Amount: 2 2011-11-02 01:39:53 [INFO] Newly set Amount: 2 2011-11-02 01:39:53 [INFO] <Phiwa> Fire in the hole! //Above is the first shot 2011-11-02 01:39:54 [INFO] Old Amount: 2 2011-11-02 01:39:54 [INFO] New Amount: 1 2011-11-02 01:39:54 [INFO] Newly set Amount: 1 2011-11-02 01:39:54 [INFO] <Phiwa> Fire in the hole! //Above is the second shot 2011-11-02 01:39:54 [INFO] Old Amount: 1 2011-11-02 01:39:54 [INFO] New Amount: 0 2011-11-02 01:39:54 [INFO] Newly set Amount: 0 2011-11-02 01:39:54 [INFO] <Phiwa> Fire in the hole! // Above is the third shot 2011-11-02 01:39:54 [INFO] Old Amount: 1 2011-11-02 01:39:54 [INFO] New Amount: 0 2011-11-02 01:39:54 [INFO] Newly set Amount: 0 2011-11-02 01:39:54 [INFO] <Phiwa> Fire in the hole! // Above is the fourth shot // ... Many more shots like the fourth one ... It seems to work like it should until the fourth shot where the player suddenly has 1 (in my example I took gold_ingots) again. The second problem is that no resources are taken away until the player relogs and after that he has got 1 ingot again. Do you have an idea how to solve the problem or do you have an idea how to achieve my aim with a different code? Thanks to all of you Phiwa
Try Code: Inventory inventory = player.getInventory(); if(inventory.contains(mortar.costitemid, mortar.costamount){ inventory.remove(new ItemStack(mortar.costitemid, mortar.costamount)); }else{ player.sendMessage("You don't have enough...") } Don't do itemStack.setAmount(oldamount -cost). Lets suppose the inventory has two items WOOD:1 and WOOD:2 (Material:amount). contains(WOOD, 3) will give a positive result, but if you do: Code: int index = player.getInventory().first(mortar.costitemid); ItemStack stack = player.getInventory().getItem(index); stack.setAmount(amount); the stack will be WOOD:1. After subtracting 3 the inventory will contain WOOD:-2, WOOD:2. Not sure why the amount doesn't go to zero though.
I searched for a method like this in the jDocs, but couldn't find one. Thanks mate, will try this version. EDIT: Your third line is missing a ")" But even after fixing that, it doesn't really work and I don't know why. -.- It doesn't do anything when doing it this way...
Yeah, thanks mate. Found that out with Smex during the night. updateInventory(); seems to be the old version, but there isn't a newer one yet I think. I'll use it until I find a different one. Finally I used this version and it works. Code: Inventory inv = player.getInventory(); if (inv.contains(mortar.costitemid, mortar.costamount)) { inv.removeItem(new ItemStack(mortar.costitemid, mortar.costamount)); player.updateInventory(); //... more methods ... } This thread can be closed.