Solved A problem with removing amount from hand

Discussion in 'Plugin Development' started by JjPwN1, Mar 9, 2013.

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

    JjPwN1

    I am making a feature in my plugin where you can right-click with items that correspond to a mob and it will spawn that mob, if you have 10 of that item.

    The problem is, with certain mobs, it doesn't take the 10 away from your hand, while with others, it does.

    For example, here is my code for spawning in an ocelot and a wolf:
    Code:
            if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
                Location blockloc = new Location(player.getWorld(), event.getClickedBlock().getLocation().getX(), event.getClickedBlock().getLocation().getY() + 1, event.getClickedBlock().getLocation().getZ());
                //
                if(player.getItemInHand().getType() == Material.RAW_FISH && player.getItemInHand().getAmount() >= 10 && rpgplayer.getSkillLevel(player.getName(), "Summoning") >= 0){
                    Bukkit.getWorld(player.getWorld().getName()).spawnEntity(blockloc, EntityType.OCELOT);
                    if(player.getItemInHand().getAmount() > 10){
                        player.getItemInHand().setAmount(player.getItemInHand().getAmount() - 10);
                    }else{
                        player.setItemInHand(new ItemStack(Material.AIR));
                    }
                    int chance = 1 + (int)(Math.random() * ((80 - 1) + 1));
                    if(chance == 1){
                        if(rpgplayer.getSkillLevel(player.getName(), "Summoning") < 500){
                            rpgplayer.increaseSkill(player.getName(), "Summoning", rpgplayer.getSkillLevel(player.getName(), "Summoning") + 1);
                            player.sendMessage(blue + "You have leveled up in the skill '" + gold + "Summoning" + blue + "'! Level: " + gold + rpgplayer.getSkillLevel(player.getName(), "Summoning"));
                        }
                    }
                }
                //
                if(player.getItemInHand().getType() == Material.BONE && player.getItemInHand().getAmount() >= 10 && rpgplayer.getSkillLevel(player.getName(), "Summoning") >= 10){
                    Bukkit.getWorld(player.getWorld().getName()).spawnEntity(blockloc, EntityType.WOLF);
                    if(player.getItemInHand().getAmount() > 10){
                        player.getItemInHand().setAmount(player.getItemInHand().getAmount() - 10);
                    }else{
                        player.setItemInHand(new ItemStack(Material.AIR));
                    }
                    int chance = 1 + (int)(Math.random() * ((80 - 1) + 1));
                    if(chance == 1){
                        if(rpgplayer.getSkillLevel(player.getName(), "Summoning") < 500){
                            rpgplayer.increaseSkill(player.getName(), "Summoning", rpgplayer.getSkillLevel(player.getName(), "Summoning") + 1);
                            player.sendMessage(blue + "You have leveled up in the skill '" + gold + "Summoning" + blue + "'! Level: " + gold + rpgplayer.getSkillLevel(player.getName(), "Summoning"));
                        }
                    }
                }
                if(rpgplayer.getSkillLevel(player.getName(), "Summoning") < 10 && player.getItemInHand().getType() == Material.BONE && player.getItemInHand().getAmount() >= 10){
                    player.sendMessage(blue + "Your " + gold + "Summoning " + blue + "skill is too low to spawn a wolf (Required: " + gold + "10" + blue + ")");
                }
            }
    When right-clicking with 10 raw fish in hand, it spawns an ocelot. But it doesn't take 10 away from the hand unless you have 11 or more in your hand. When right-clicking with 10 bone in hand, it spawns a wolf. It does take away 10 from your hand no matter what amount you have in your hand.
     
  2. Offline

    GodzOfMadness

    JjPwN1 you set the iteminhand to null instead
     
  3. Offline

    JjPwN1

    I've tried this, and the results are the same.
    Same goes with setting the item in hand amount to 0, it doesn't take away the 10.
     
  4. Offline

    GodzOfMadness

    JjPwN1 try making a PlayerInventory object as so
    PlayerInventory pi = player.getInventory();
    then set their hand to null
    pi.setItem(pi.getHeldItemSlot(), null);
    (tested and works)
     
  5. Offline

    JjPwN1

    This still does not fix the problem.
     
  6. Offline

    GodzOfMadness

    JjPwN1 try doing event.setCancelled(true) once you did your checks and before you run the code to take it out of their inventory
     
    JjPwN1 likes this.
  7. Offline

    JjPwN1

    This does the trick, thanks!
     
Thread Status:
Not open for further replies.

Share This Page