Not completely getting rid of items.

Discussion in 'Plugin Development' started by Eliteninja42, Feb 18, 2014.

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

    Eliteninja42

    Why does this work until the player only holds one item. If they are holding one apple it will never go away.

    Code:
    int a = player.getInventory().getItemInHand().getAmount();
                            player.getItemInHand().setAmount(a-1);  
     
  2. Offline

    Gater12

    Eliteninja42
    Check if the amount they are holding is 1, them just simply set it to Material.AIR
     
  3. Offline

    Eliteninja42

    Gater12 I tried that and it still wouldn't work

    bump

    anyone?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  4. Offline

    Gater12

    Eliteninja42
    Don't bump threads unless it has been 12 hours.
    Maybe player.updateInventory(); ?
     
  5. Offline

    Eliteninja42

  6. Offline

    Drew1080

    Eliteninja42
    All you would have to-do is use an if else method to check when the amount they are holding is equal to one and if it is use the removeItem method to remove the item. Then for you else statement you would use the setAmount method you got above.
     
  7. Offline

    Eliteninja42

  8. Offline

    Drew1080

    Eliteninja42
    Would you be able to paste more of your class than just those two lines.
    Because I used this code and it worked for me fine:
    Code:java
    1. if (player.getItemInHand().getAmount() == 1)
    2. {
    3. player.getInventory().removeItem(player.getInventory().getItemInHand());
    4. }
    5. else
    6. {
    7. player.getInventory().getItemInHand().setAmount(player.getInventory().getItemInHand().getAmount() - 1);
    8. }

    The variable 'player' is just your player object in my code.
     
  9. Offline

    Eliteninja42

    Drew1080
    Code:
    @EventHandler
            public void onPlayerInteract(PlayerInteractEvent e) {
                Player player = e.getPlayer();
     
                if (e.getPlayer().getItemInHand() != null) {
                    if (e.getPlayer().getItemInHand().getType() == Material.APPLE) {
                    if (e.getAction() == Action.RIGHT_CLICK_AIR) {
                        if (Miner.contains(player.getName())) {
                            player.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 400, 2));
                            int a = 0;
                            if(player.getInventory().getItemInHand().getAmount() == 1){
                                player.getInventory().remove(Material.APPLE);
                            }else
                                a = player.getInventory().getItemInHand().getAmount();
                                player.getItemInHand().setAmount(a-1);
                            }
                    } else if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                        if (Miner.contains(player.getName())) {
                            player.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 400, 2));
                            int a=0;
                            if(player.getInventory().getItemInHand().getAmount() == 1){
                                player.getInventory().remove(Material.APPLE);
                            }else
                                a = player.getInventory().getItemInHand().getAmount();
                                player.getItemInHand().setAmount(a-1);
                            }
     
  10. Offline

    Drew1080

    Eliteninja42
    Replace this line with the line of code I actually gave you:
    Replace:
    Code:java
    1. player.getInventory().remove(Material.APPLE);

    With:
    Code:java
    1. player.getInventory().removeItem(player.getInventory().getItemInHand());
     
  11. Offline

    Eliteninja42

    Drew1080 It still leaves 1 apple when i test it
     
  12. Offline

    Borlea

    Code:java
    1.  
    2. ItemStack m = new ItemStack(Material.APPLE, 1);
    3. player.getInventory().removeItem(m);
    4.  

    Removes 1 apple from inventory
     
  13. Offline

    Eliteninja42

    Borlea It still isn't, I don't get why it won't do it
     
  14. Offline

    Borlea

    I'll try to see whats wrong, make sure they aren't ghost items. add player.updateInventory() at end ignore deprecation

    Also do some debug code and make sure you are actually getting to where you need to remove the apple because it works for me
     
  15. Offline

    Eliteninja42

    Borlea I've tried everything I can think of and it won't work for me.
     
  16. Eliteninja42 This is a snippet from a gold apple code I wrote for some guy.
    Code:java
    1. if(item.getData().toString().equals("GOLDEN_APPLE(1)")) {
    2. e.setCancelled(true);
    3. ItemStack remove = new ItemStack(Material.GOLDEN_APPLE, 1);
    4. ItemStack[] contents = e.getPlayer().getInventory().getContents();
    5. remove.setData(item.getData());
    6. for(int i=0;i<e.getPlayer().getInventory().getContents().length;i++) {
    7. if(e.getPlayer().getInventory().getContents()[i] != null) {
    8. if(e.getPlayer().getInventory().getContents()[i].getType() == remove.getType()) {
    9. if(e.getPlayer().getInventory().getContents()[i].getData().toString().equals(remove.getData().toString())) {
    10. if(e.getPlayer().getInventory().getContents()[i].getAmount() > 1) {
    11. e.getPlayer().getInventory().getItem(i).setAmount(item.getAmount()-1);
    12. e.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 300, 10), true);
    13. e.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 200, 2), true);
    14. } else {
    15. e.getPlayer().getInventory().clear(i);
    16. e.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 300, 10), true);
    17. e.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 200, 2), true);
    18. }
    19. }
    20. }
    21. }
    22. }
    23. }[/i][/i][/i][/i]
     
  17. Offline

    Eliteninja42

    Im_Zeus Does anything look wrong in my code too you?
     
  18. Offline

    Drew1080

    Eliteninja42
    Modified your code, this removes the item now if its the last one.

    Code:java
    1. @EventHandler
    2. public void onPlayerInteract1(PlayerInteractEvent e) {
    3. Player player = e.getPlayer();
    4.  
    5. if (e.getPlayer().getItemInHand() != null)
    6. {
    7. if (e.getPlayer().getItemInHand().getType() == Material.APPLE)
    8. {
    9. if (e.getAction() == Action.RIGHT_CLICK_AIR)
    10. {
    11. if (Miner.contains(player.getName()))
    12. {
    13. player.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 400, 2));
    14. int a = 0;
    15. if(player.getInventory().getItemInHand().getAmount() == 1)
    16. {
    17. e.setCancelled(true);
    18. player.getInventory().removeItem(player.getItemInHand());
    19. }
    20. else
    21. {
    22. a = player.getInventory().getItemInHand().getAmount();
    23. player.getItemInHand().setAmount(a-1);
    24. }
    25. }
    26. }
    27. }
    28. else if (e.getAction() == Action.RIGHT_CLICK_BLOCK)
    29. {
    30. if (Miner.contains(player.getName()))
    31. {
    32. player.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 400, 2));
    33. int a=0;
    34. if(player.getInventory().getItemInHand().getAmount() == 1)
    35. {
    36. e.setCancelled(true);
    37. player.getInventory().removeItem(player.getItemInHand());
    38. }
    39. else
    40. {
    41. a = player.getInventory().getItemInHand().getAmount();
    42. player.getItemInHand().setAmount(a-1);
    43. }
    44. }
    45. }
    46. }
    47. }
     
    Eliteninja42 likes this.
  19. Offline

    mazentheamazin

    *24 hours
     
  20. Offline

    phildachil

    Code:
    e.getPlayer().getInventory().removeItem(new ItemStack(Material.Material));
     
  21. Offline

    Eliteninja42

    Drew1080 It finally works! Thank you!
     
Thread Status:
Not open for further replies.

Share This Page