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

    lewysryan

    ok so i found a bug in my plugin. i just don't know how to fix :( please help me!
    i just want 1 of the item stack to remove
    Code:java
    1. @EventHandler(priority = EventPriority.NORMAL)
    2. public void onPlayerInteractPotato(PlayerInteractEvent event) {
    3. if (event.getAction() == Action.RIGHT_CLICK_AIR) {
    4. Player p = event.getPlayer();
    5. if(p.getItemInHand().getType() == Material.BAKED_POTATO) {
    6. if(p.hasPermission("arrow.powerup"));
    7. p.setFoodLevel(20);
    8. p.sendMessage(ChatColor.GRAY+" ["+ ChatColor.AQUA + "Explosive PvP" + ChatColor.GRAY + "]" + ChatColor.GREEN + " You used InstandFood power up!");
    9. PlayerInventory pi = p.getInventory();
    10. ItemStack Ifood = new ItemStack(Material.BAKED_POTATO, 1);
    11. pi.remove(Ifood);
    12.  
    13. }
    14. }
    15. }
     
  2. Offline

    Sour

    You're using the remove method.
    Code:
     void    remove(ItemStack item)
              Removes all stacks in the inventory matching the given stack.
    You should be using the removeItem method
    Code:
     HashMap<Integer,ItemStack>    removeItem(ItemStack... items)
              Removes the given ItemStacks from the inventory.
     
    lewysryan likes this.
  3. Offline

    lewysryan

    how would i use that Sour Someone please help me!!!

    could someone explain why this would not work?

    Code:java
    1. pi.remove(new ItemStack(Material.BAKED_POTATO, 1));


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

    TheUpdater

    you might édit its name? so try like ex:
    pi.remove(name of modded itemstack);
     
    lewysryan likes this.
  5. Offline

    lewysryan

    still nothing :/
     
  6. Offline

    Cycryl

    lewysryan likes this.
  7. Offline

    lewysryan

    no. what i have even tried: nothing still

    Code:java
    1. @EventHandler(priority = EventPriority.NORMAL)
    2. public void onPlayerInteractPotato(PlayerInteractEvent event) {
    3. if (event.getAction() == Action.RIGHT_CLICK_AIR) {
    4. Player p = event.getPlayer();
    5. if(p.getItemInHand().getType() == Material.COOKED_FISH) {
    6. if(p.hasPermission("arrow.powerup"));
    7. p.setFoodLevel(20);
    8. p.sendMessage(ChatColor.GRAY+" ["+ ChatColor.AQUA + "Explosive PvP" + ChatColor.GRAY + "]" + ChatColor.GREEN + " You used InstandFood power up!");
    9. PlayerInventory pi = p.getInventory();
    10. ItemStack fish = new ItemStack(Material.COOKED_FISH, 1);
    11. pi.remove(fish);
    12. pi.remove(new ItemStack(Material.COOKED_FISH, 1));
    13. p.updateInventory();
    14.  
    15. }
    16. }
    17. }
     
  8. Offline

    Sour

    Try
    Code:java
    1. @EventHandler(priority = EventPriority.NORMAL)
    2. public void onPlayerInteractPotato(PlayerInteractEvent event) {
    3. if (event.getAction() == Action.RIGHT_CLICK_AIR) {
    4. Player p = event.getPlayer();
    5. if(p.getItemInHand().getType() == Material.COOKED_FISH) {
    6. if(p.hasPermission("arrow.powerup")){
    7. p.setFoodLevel(20);
    8. p.sendMessage(ChatColor.GRAY+" ["+ ChatColor.AQUA + "Explosive PvP" + ChatColor.GRAY + "]" + ChatColor.GREEN + " You used InstantFood power up!");
    9. PlayerInventory pi = p.getInventory();
    10. ItemStack fish = new ItemStack(Material.COOKED_FISH, 1);
    11. pi.removeItem(fish);
    12. }
    13. }
    14. }
    15. }
     
    lewysryan likes this.
  9. Offline

    lewysryan

    Sour TheUpdater

    ok, so it will remove the item but only if there is more than one. it will not remove the item if there is just one of it!
     
  10. Offline

    TheUpdater

    PHP:
    if(fish != 0){
    pi.removeItem(fish);
    }
     
  11. Offline

    AndyMcB1

    You should be able to code a solution to this..
    If the stack is == 1 remove it..
     
  12. Offline

    Goblom

    Code:java
    1. Ifood.setAmount(Ifood.getAmount() - 1);

    ...Geeze people, its not that hard to remove a single item from an item stack...
     
    Rocoty likes this.
  13. Offline

    lewysryan

    Goblom AndyMcB1 TheUpdater Sour
    I tried all that with no luck

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

    TheUpdater

    ok i will try adn see if i can get it to work ok =)
     
    lewysryan likes this.
  15. Offline

    lewysryan

    TheUpdater THANKS SO MUCH! this is really becoming annoying :eek:
     
  16. Offline

    Rocoty

    Code:java
    1. ItemStack cookie = p.getItemInHand();
    2.  
    3. if(cookie.getAmount() == 1){
    4. p.setItemInHand(new ItemStack(Material.AIR));
    5. } else {
    6. cookie.setAmount(cookie.getAmount() - 1);
    7. }
    8.  
    9. //And for god's sake:
    10. p.updateInventory();
     
  17. Offline

    TheUpdater


    PHP:
        @EventHandler
        
    public void onPlayerInteractCookie(PlayerInteractEvent event) {
        if (
    event.getAction() == Action.RIGHT_CLICK_AIR) {
            
    Player p event.getPlayer();
            
    ItemStack coo = new ItemStack(Material.COOKIE1);
                if(
    p.getItemInHand().getType() == Material.SAND) {
                        
    p.setFoodLevel(20);
                        
    p.sendMessage(ChatColor.GRAY+" ["ChatColor.AQUA "Explosive PvP" ChatColor.GRAY "]" ChatColor.GREEN " You used InstantFood power up!");
                        
    p.getInventory().remove(coo);
                        
    p.updateInventory();
                }
            }
        }
    add
    ItemStack coo = new ItemStack(Material.COOKIE, 1);
    p.getInventory().remove(coo);
    p.updateInventory();
    to your code
     
  18. Offline

    lewysryan

    still does this:


    code:

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

    Rocoty

    Won't cut it at all:

    "Removes all stacks in the inventory matching the given stack.
    This will only match a slot if both the type and the amount of the stack match"
    http://jd.bukkit.org/dev/doxygen/d3...entory.html#a69d04565048b48a0495eaa2c854fc01e
     
  20. Offline

    lewysryan



    i have tried something like that. nothing still (from earlier)

    Code:
    @EventHandler(priority = EventPriority.NORMAL)
      public void onPlayerInteractPotato(PlayerInteractEvent event) {
          if (event.getAction() == Action.RIGHT_CLICK_AIR) {
              Player p = event.getPlayer();
              if(p.getItemInHand().getType() == Material.COOKIE) {
                  if(p.hasPermission("arrow.powerup")){
                      p.setFoodLevel(20);
                      p.sendMessage(ChatColor.GRAY+" ["+ ChatColor.AQUA + "Explosive PvP" + ChatColor.GRAY + "]" + ChatColor.GREEN + " You used InstantFood power up!");
                      PlayerInventory pi =  p.getInventory();
                      ItemStack fishy = new ItemStack(Material.COOKIE, 1);
                      pi.removeItem(fishy);
                      p.updateInventory;
                  }
              }
          }
      }
     
  21. Offline

    Cycryl

  22. Offline

    _Filip

    That isn't required as he likely made the constructor that implements Listener.
     
  23. Offline

    lewysryan

    Cycryl

    Yes i did. the message comes up and the rest of the plugin works fine. just having this problem.
     
  24. Offline

    TheUpdater

    did you add the premission to plugin.yml?
     
  25. Offline

    lewysryan

    TheUpdater

    Wait i have added no permissions to my plugin.yml

    plugin.yml:

    Code:
    name: Explosive Arrow PVP
    version: 1
    main: arrow.explosive.lewys.explosivearrowpvp
    commands:
      explosivepvp:
        description: get the info of the plugin
    do you need to add them?
     
  26. Offline

    awesomelemonade

    lewysryan If you are OP, I don't believe so.
     
  27. Offline

    Goblom

    lewysryan I can write the entire listener for you in a few minutes if this is not already fixed
     
    lewysryan likes this.
  28. Offline

    lewysryan

    Please do Goblom ! i still have no answer! not fixed yet :(
     
  29. Offline

    Goblom

    lewysryan This should do it...

    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event) {
    3. if (event.getAction().equals(Action.RIGHT_CLICK_AIR)) {
    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. player.updateInventory();
    15. }
    16. }
    17. }
    18. }
     
    lewysryan likes this.
  30. Offline

    lewysryan

    Does not remove anything now Goblom

    tried your code and this code:

    Code:java
    1. @EventHandler(priority = EventPriority.NORMAL)
    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. ItemStack cookie = new ItemStack(Material.COOKIE);
    12.  
    13. if (handItem.getAmount() >= 2){
    14. cookie.setAmount(cookie.getAmount() - 1);
    15.  
    16. }else{
    17. player.getInventory().remove(cookie);
    18. player.updateInventory();
    19. }
    20. }
    21. }
    22. }
    23. }
    24.  


    Thanks for the help so far! really appreciate it!
     
Thread Status:
Not open for further replies.

Share This Page