Solved [Now found a possible bug] Removing only 1 consumable item out of a itemstack

Discussion in 'Plugin Development' started by lewysryan, Oct 25, 2013.

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

    Goblom

    lewysryan The exact code i pasted into the forums should work. And i see that you are using == (which can get mistaken sometimes)

    On line 12 change ">=" to ">"
     
    lewysryan likes this.
  2. Offline

    lewysryan

    Goblom still get stuck on one :/

    Code:java
    1. @EventHandler
    2. public void onPlayerInteractCookie(PlayerInteractEvent event) {
    3. if ((event.getAction() == Action.RIGHT_CLICK_AIR) || (event.getAction() == Action.RIGHT_CLICK_BLOCK)) {
    4. Player player = event.getPlayer();
    5. ItemStack handItem = player.getItemInHand();
    6.  
    7. if (handItem.getType().equals(Material.COOKIE)) {
    8. if (player.hasPermission("arrow.powerup")) {
    9. player.setFoodLevel(20);
    10. player.sendMessage(ChatColor.GRAY+" ["+ ChatColor.AQUA + "Explosive PvP" + ChatColor.GRAY + "]" + ChatColor.GREEN + " You used InstantFood power up!");
    11.  
    12. if (handItem.getAmount() > 2) handItem.setAmount(handItem.getAmount() - 1);
    13. else player.getInventory().remove(handItem);
    14.  
    15. player.updateInventory();
    16. }
    17. }
    18. }
    19. }
     
  3. Offline

    Goblom

    line 2 & 13 change to this...

    Code:java
    1. if (handItem.getAmount() == 1) player.getInventory.remove(handItem);
    2. else if (handItem.getAmount() > 2) handItem.setAmount(handItem.getAmount() - 1);
    3. else player.getInventory().remove(handItem);


    If that doesnt work, replace every "handItem" in this code from this post to "player.getItemInHand()"
     
  4. Offline

    lewysryan

    Goblom

    now it dont remove the last two. code:

    Code:java
    1. @EventHandler
    2. public void onPlayerInteractCookie(PlayerInteractEvent event) {
    3. if ((event.getAction() == Action.RIGHT_CLICK_AIR) || (event.getAction() == Action.RIGHT_CLICK_BLOCK)) {
    4. Player player = event.getPlayer();
    5. ItemStack handItem = player.getItemInHand();
    6.  
    7. if (handItem.getType().equals(Material.COOKIE)) {
    8. if (player.hasPermission("arrow.powerup")) {
    9. player.setFoodLevel(20);
    10. player.sendMessage(ChatColor.GRAY+" ["+ ChatColor.AQUA + "Explosive PvP" + ChatColor.GRAY + "]" + ChatColor.GREEN + " You used InstantFood power up!");
    11.  
    12. if (handItem.getAmount() == 1) player.getInventory().remove(handItem);
    13. else if (handItem.getAmount() > 2) handItem.setAmount(handItem.getAmount() - 1);
    14. else player.getInventory().remove(handItem);
    15.  
    16. player.updateInventory();
    17. }
    18. }
    19. }
    20. }
     
  5. Offline

    Goblom

    lewysryan
     
  6. Offline

    lewysryan

    Goblom did that to: Sorry was updating my post as u posted that lol

    Code:java
    1. @EventHandler
    2. public void onPlayerInteractCookie(PlayerInteractEvent event) {
    3. if ((event.getAction() == Action.RIGHT_CLICK_AIR) || (event.getAction() == Action.RIGHT_CLICK_BLOCK)) {
    4. Player player = event.getPlayer();
    5. if (player.getItemInHand().getType().equals(Material.COOKIE)) {
    6. if (player.hasPermission("arrow.powerup")) {
    7. player.setFoodLevel(20);
    8. player.sendMessage(ChatColor.GRAY+" ["+ ChatColor.AQUA + "Explosive PvP" + ChatColor.GRAY + "]" + ChatColor.GREEN + " You used InstantFood power up!");
    9.  
    10. if (player.getItemInHand().getAmount() == 1) player.getInventory().remove(player.getItemInHand());
    11. else if (player.getItemInHand().getAmount() > 2) player.getItemInHand().setAmount(player.getItemInHand().getAmount() - 1);
    12. else player.getInventory().remove(player.getItemInHand());
    13.  
    14. player.updateInventory();
    15. }
    16. }
    17. }
    18. }
     
  7. Offline

    Goblom

    lewysryan :/ I'm sorry, i am drawing a blank. If it doesn't work yet then i don't know what to do. My best suggestion is to keep playing around with it and see if something works
     
    lewysryan likes this.
  8. Offline

    lewysryan

    God dam Bukkit is it really this hard to just remove one item from a itemstack? Thanks Goblom anyway Anyone Else got a answer for this?
     
  9. Offline

    Drkmaster83

    After some debugging, I discovered that there is literally no way to remove just the singular cookie from the hand. It fails the if statement in all the ways, shapes, or forms I have tried:
    Code:
    if(handItem.getAmount == 1): false;
    if(handItem.getAmount <= 1): false;
    if(handItem.equals(new ItemStack(Material.COOKIE, 1)): false;
    
    Though, strangely enough, it somehow passes the if statement. I added a return statement after all of these and it never got to the message, which I put after this code. So, I don't know. This is impossible.
     
    lewysryan likes this.
  10. Offline

    lewysryan

    Drkmaster83

    :'( really. it works with balzerods and paper but not cookies :/

    i used this for paper and it worked but not for cookies:
    Code:
    @EventHandler(priority = EventPriority.NORMAL)
        public void onPlayerInteractBandage(PlayerInteractEvent event) {
            if ((event.getAction() == Action.RIGHT_CLICK_AIR) || (event.getAction() == Action.RIGHT_CLICK_BLOCK)) {
            Player p = event.getPlayer();
            if(p.getItemInHand().getType() == Material.PAPER) {
                if(p.hasPermission("arrow.powerup"));
                p.setHealth(20);
                p.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION,120,1));
                p.sendMessage(ChatColor.GRAY+" ["+ ChatColor.AQUA + "Explosive PvP" + ChatColor.GRAY + "]" + ChatColor.GREEN + " You used the Bandage power up!");
                PlayerInventory pi = p.getInventory();
                ItemStack Paper = new ItemStack(Material.PAPER, 1);
                pi.removeItem(Paper);
                pi.remove(new ItemStack(Material.PAPER, 1));
            }
        }
    }
    Well this sucks ;( Thanks all
     
  11. Offline

    Goblom

  12. Offline

    lewysryan

    I hope it is a bug and it does get sorted. guess instant food power up is going to have to be a bone. this works with a bone:

    Code:
    @EventHandler
        public void onPlayerInteractCookie(PlayerInteractEvent event) {
            if ((event.getAction() == Action.RIGHT_CLICK_AIR) || (event.getAction() == Action.RIGHT_CLICK_BLOCK)) {
                Player player = event.getPlayer(); 
                if (player.getItemInHand().getType().equals(Material.BONE)) {
                    if (player.hasPermission("arrow.powerup")) {
                        player.setFoodLevel(20);
                        player.sendMessage(ChatColor.GRAY+" ["+ ChatColor.AQUA + "Explosive PvP" + ChatColor.GRAY + "]" + ChatColor.GREEN + " You used InstantFood power up!");
                        PlayerInventory pi = player.getInventory();
                     
                        ItemStack bone = new ItemStack(Material.BONE, 1);
                        pi.removeItem(bone);
                     
                    }
                }
            }
        }
     
  13. Offline

    Drkmaster83

    Make it bacon!
     
    Blah1 likes this.
  14. Offline

    lewysryan

    Drkmaster83 dont work with any food ......... well bones are better than nothing :D wanted cookies though, taste better.
     
  15. Offline

    Drkmaster83

    Try potions? I'm interested if it's only consumables.
     
  16. Offline

    lewysryan

    Drkmaster83 i will tomorrow i gtg now. ill tag u again on this thread on the outcome. i personally believe its only consumables but not tried that yet so not 100% sure.
     
  17. Offline

    Drkmaster83

  18. Offline

    achap1989

    You have not registered the event try adding Bukkit.getServer().getPluginManager().registerEvents(this, this); under your onEnable
    Example.
    Code:java
    1. public void onEnable(){
    2. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    3.  
    4. }

    Example
     
  19. Offline

    Drkmaster83

    I don't think that's the problem...
     
  20. Offline

    achap1989

    Drkmaster83
    This varies I know in my code when I do not register the event it does not work. However I am still fairly new to this, simply attempting to help lewysryan This could be the problem, it might not be, but could.
     
  21. Offline

    sgavster

    PHP:
        @EventHandler
        
    public void onInteract(PlayerInteractEvent e)
        {
            
    Player p e.getPlayer();
            if(
    e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK)
            {
                if(
    p.getInventory().contains(Material.COOKIE))
                {
                    
    p.getInventory().removeItem(new ItemStack(Material.COOKIE1));
                }
            }
        }
    ...?
     
  22. Offline

    number1_Master

    I can confirm this bug. It occasionally works with another plugin of mine, and never works in another.
    I've received countless complaints by owners I've programmed for and I'm glad its a bukkit problem and not something with my programming :)
     
  23. Offline

    Drkmaster83

    And while I recognize that you're fairly new to this (and do respect it as so), I was simply stating that I don't think that's the problem because (if you read all of the posts), the event was being listened to, but it's supposed functions were being improperly performed.

    EDIT: Glad to hear it, number1_Master :D Now I know that I'm not going crazy!
     
    number1_Master likes this.
  24. Offline

    achap1989

    I see, lol I did not see it was being listened to. Sorry mate, I was not trying to say that you did not know what you were doing I was also stating what I thought could be the problem. Maybe I will have to attempt to fix the code myself (just to see if I can). Anyway Sorry for the inconvenience.
     
  25. Offline

    lewysryan



    achap1989 if you can that will be great but i think its impossible // bug pointed out by Drkmaster83 And yes i did register the events. Thanks for your help though!
     
  26. Offline

    achap1989

    lewysryan Drkmaster83 it appears it is only when you are removing the item in hand. If for instance, I use the cookie to remove a bone each time I right click with a cookie it will remove every single bone and change the last bone to air. However if I change the if player is holding a cookie statement back to bone it will not remove all of the bones.

    So for some reason you may only remove an item that you are not holding, otherwise it will not remove the last item.
    Code that removes all of the bones.
    Code:java
    1. @EventHandler
    2. public void onPlayerInteractCookie(PlayerInteractEvent event) {
    3. if ((event.getAction() == Action.RIGHT_CLICK_AIR) || (event.getAction() == Action.RIGHT_CLICK_BLOCK)) {
    4. Player player = event.getPlayer();
    5. if (player.getItemInHand().getType().equals(Material.COOKIE)) {
    6. if (player.hasPermission("arrow.powerup")) {
    7. player.setFoodLevel(20);
    8. player.sendMessage(ChatColor.GRAY+" ["+ ChatColor.AQUA + "Explosive PvP" + ChatColor.GRAY + "]" + ChatColor.GREEN + " You used InstantFood power up!");
    9. PlayerInventory pi = player.getInventory();
    10.  
    11. ItemStack bone = new ItemStack(Material.BONE, 1);
    12. pi.removeItem(bone);
    13.  
    14. }
    15. }
    16. }
    17. }


    Code the will keep the last bone
    Code:java
    1. @EventHandler
    2. public void onPlayerInteractCookie(PlayerInteractEvent event) {
    3. if ((event.getAction() == Action.RIGHT_CLICK_AIR) || (event.getAction() == Action.RIGHT_CLICK_BLOCK)) {
    4. Player player = event.getPlayer();
    5. if (player.getItemInHand().getType().equals(Material.BONE)) {
    6. if (player.hasPermission("arrow.powerup")) {
    7. player.setFoodLevel(20);
    8. player.sendMessage(ChatColor.GRAY+" ["+ ChatColor.AQUA + "Explosive PvP" + ChatColor.GRAY + "]" + ChatColor.GREEN + " You used InstantFood power up!");
    9. PlayerInventory pi = player.getInventory();
    10.  
    11. ItemStack bone = new ItemStack(Material.BONE, 1);
    12. pi.removeItem(bone);
    13.  
    14. }
    15. }
    16. }
    17. }
     
  27. Offline

    lewysryan

  28. Offline

    achap1989

    Code:java
    1. //First code
    2.  
    3. if (player.getItemInHand().getType().equals(Material.COOKIE)) {
    4.  
    5. //second code
    6.  
    7. if player.getItemInHand().getType().equals(Material.BONE)) {
     
  29. Offline

    lewysryan

    achap1989 now i feel stupid :p i use this now.

    Code:java
    1. public ItemStack getInstantFood() {
    2. ItemStack Ifood = new ItemStack(Material.BONE, 1);
    3. ItemMeta meta = Ifood.getItemMeta();
    4. meta.setDisplayName(ChatColor.AQUA + "Instant Food");
    5. List<String> lore = new ArrayList<String>();
    6. lore.add(ChatColor.GREEN + "Description:");
    7. lore.add(ChatColor.LIGHT_PURPLE + "Instantly eat the Bone");
    8. lore.add(ChatColor.LIGHT_PURPLE + "Fills your food bar completely");
    9. lore.add(ChatColor.GREEN + "Price:");
    10. lore.add(ChatColor.LIGHT_PURPLE + "$10");
    11. meta.setLore(lore);
    12. Ifood.setItemMeta(meta);
    13. return Ifood;
    14. }
    15.  
    16. @EventHandler
    17. public void onPlayerInteractBone(PlayerInteractEvent event) {
    18. if ((event.getAction() == Action.RIGHT_CLICK_AIR) || (event.getAction() == Action.RIGHT_CLICK_BLOCK)) {
    19. Player player = event.getPlayer();
    20. if (player.getItemInHand().getType().equals(Material.BONE)) {
    21. if (player.hasPermission("arrow.powerup")) {
    22. player.setFoodLevel(20);
    23. player.sendMessage(ChatColor.GRAY+" ["+ ChatColor.AQUA + "Explosive PvP" + ChatColor.GRAY + "]" + ChatColor.GREEN + " You used InstantFood power up!");
    24. PlayerInventory pi = player.getInventory();
    25. pi.removeItem(getInstantFood());
    26. }
    27. }
    28. }
    29.  
    30. }
    31.  
    32.  


    would not remove the bone if i gave them the instant food item stack and removed the material.bone // item stack bone though. same material?
     
  30. Offline

    achap1989

    lewysryan um, what? Haha, well I still went ahead and make you a power up if you want to use it.

    You of course would still need to add the permission if statement, but this is just a solution I came up with until the bug was fixed.
    Code:java
    1. @EventHandler
    2. public void onFoodLevelChange(PlayerItemConsumeEvent event) {
    3. Player p = event.getPlayer();
    4. if (p.getItemInHand().getType() == Material.COOKIE) {
    5. p.setFoodLevel(20);
    6. p.sendMessage(ChatColor.GREEN + "Powered up!");
    7. }
    8. }
    9.  
    10. //This portion can be removed this only uses a stick to set hunger to 0 to test program
    11. @EventHandler
    12. public void onPlayerInteract(PlayerInteractEvent event) {
    13. Player p = event.getPlayer();
    14. if (p.getItemInHand().getType() == Material.STICK) {
    15. if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    16. p.setFoodLevel(0);
    17. }
    18. }
    19. }
     
Thread Status:
Not open for further replies.

Share This Page