Solved Getting player inventory item amount

Discussion in 'Plugin Development' started by EnchantedMiners, Aug 12, 2014.

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

    EnchantedMiners

    Hey there i wanted to make a sell sign plugin and it seems i cant check if the player have the item and that item have the sign specific amount it doesnt pass the event but it also don't send any error so i am stuck there i tried to fix it for hours and cant make it work can i get a reply here is my code:
    Code:java
    1. if(sign.getLine(0).equalsIgnoreCase(ChatColor.BLUE + "[sell]")){
    2. Material items = Material.valueOf(sign.getLine(1).toUpperCase());
    3. Inventory inv = p.getInventory();
    4.  
    5. if(items != null){
    6. String oldi = sign.getLine(2);
    7. String newi = oldi.replace("$", "");
    8. int i = Integer.parseInt(newi);
    9. if(i > 0L && i <= 1000000000000L){
    10. String oldiamount = sign.getLine(3);
    11. String newoldiamount = oldiamount.replace(ChatColor.DARK_BLUE + "Amount: ", "");
    12. int newiamount = Integer.parseInt(newoldiamount);
    13. ItemStack it = new ItemStack(items);
    14. if(it.getAmount() >= newiamount){
    15. if(p.getInventory().contains(it)){
    16. this.plugin.economy.depositPlayer(p, i);
    17. sounds.playSound(p, p.getLocation(), "random.levelup", 1, 1);
    18. p.getWorld().playEffect(p.getLocation(), Effect.STEP_SOUND, Material.EMERALD_BLOCK.getId());
    19. p.sendMessage("§eYou sold §dx" + newiamount + " §5" + items.toString().toUpperCase() + " §efor §a$" + Integer.toString(i));
    20. p.updateInventory();
    21. } else {
    22. p.sendMessage("§4You do not have this item!");
    23. sounds.playSound(p, p.getLocation(), "random.break", 1, 1);
    24. }
    25. p.sendMessage("Amount is done!");
    26. } else {
    27. p.sendMessage("amount problem");
    28. }
    29. } else {
    30. p.sendMessage("§4You cant sell this item for that much!");
    31. sounds.playSound(p, p.getLocation(), "random.break", 1, 1);
    32. }
    33. }
    34. }
     
  2. Offline

    TheDarkLord197

  3. Offline

    EnchantedMiners

    Yes lol, everything works but the amount problem message shows up
     
  4. Offline

    TheDarkLord197

    EnchantedMiners Then this returned false..
    Code:java
    1. it.getAmount() >= newiamount
     
  5. Offline

    EnchantedMiners

    TheDarkLord197 yes just that i do have the items but im getting the wrong amount of an item
     
  6. Offline

    TheDarkLord197

    EnchantedMiners It could be that getting the newoldiamount is bugged. Confirm this by putting this underneath line 11.
    Code:java
    1. System.out.println(newoldiamount);
     
  7. Offline

    EnchantedMiners

    TheDarkLord197 it returns 10 as it should be, in sign is 10 so it returns 10
     
  8. Offline

    TheDarkLord197

    EnchantedMiners Sorry, Didn't see this before. it.getAmount() will always return 1, Because you create the ItemStack just before it.
     
  9. Offline

    EnchantedMiners

    TheDarkLord197 ok, how would i get an item from the player inventory then check its amount?
     
  10. Offline

    TheDarkLord197

    EnchantedMiners If you want to get the item in the players hand, Use this instead:
    Code:java
    1. ItemStack it = p.getItemInHand();
     
  11. Offline

    fireblast709

  12. Offline

    EnchantedMiners

    TheDarkLord197 well not actually the player item in hand because if they click the sign with the hand it will not make sense so yeah i am trying to get the specified item by the sign from the player inventory then get its amount and if its higher than what the sign specify then do the rest of the code
     
  13. Offline

    TheDarkLord197

  14. Offline

    EnchantedMiners

    TheDarkLord197 fireblast709 ok i am lost can you edit my code so ik what you two did lol
    Please i understand better like that ik i have to break my head first but trust me i understand better visually and playing with the code lol
     
  15. Offline

    TheDarkLord197

    EnchantedMiners
    Code:java
    1. if(inv.containsAtLeast(newiamount) {
    Instead of
    Code:java
    1. if(it.getAmount() >= newiamount){
     
  16. Offline

    EnchantedMiners

Thread Status:
Not open for further replies.

Share This Page