Solved Quick question about getNearbyEntities

Discussion in 'Plugin Development' started by kasszz, May 11, 2013.

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

    kasszz

    When I use this code:
    Code:
                                List<Entity> inRange =  player.getNearbyEntities(5,5,5);
                                for (Entity playerInRange: inRange){
                                    System.out.print("bla");
                                    if (playerInRange instanceof Player){
    and It's not a Player I get an error about not able to cast "creeper" to player.

    how can I fix it. I get that is that I need to give the server something when it's not a player, but what :p

    thanks :)
     
  2. Offline

    Compressions

    kasszz The code snippet you're showing us does not help us solve the problem; post the initialization of the player variable.
     
  3. Offline

    kasszz

    Compressions

    Code:
                            else if(player.getItemInHand().getItemMeta().getDisplayName().equals("Healing Wand") && !useCooldown.contains(player.getDisplayName() + " Healing Wand")){
                                useCooldown.add(player.getDisplayName() + " Healing Wand");
                                List<Entity> inRange =  player.getNearbyEntities(5,5,5);
                                for (Entity playerInRange: inRange){
                                    System.out.print("bla");
                                    if (playerInRange instanceof Player){
                                        System.out.print("bla 2");
                                        int currentHealth = ((Player) playerInRange).getHealth();
                                        if(((Player) playerInRange).getHealth() + 10 > 20){
                                            System.out.print("bla 3");
                                            ((Player) playerInRange).setHealth(20);
                                        }
                                        else{
                                            ((Player) playerInRange).setHealth(currentHealth + 10);
                                        }
                                    }
                                System.out.print(((Player) playerInRange).getHealth());
                                player.getItemInHand().setDurability((short)(player.getItemInHand().getDurability() + (player.getItemInHand().getType().getMaxDurability() / 10)));
                                  if(player.getItemInHand().getDurability() >= 60){ //12 changes
                                      player.setItemInHand(null);
                                      player.playSound(player.getLocation(), Sound.ITEM_BREAK, 10, 1);
                                  }
                                  Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                                    public void run(){
                                        useCooldown.remove(player.getDisplayName() + " Healing Wand");
                                    }
                                }, 100);
                                }
     
  4. Offline

    Compressions

    kasszz Again, still not showing the initialization of the player variable.
     
  5. Offline

    catageek

    The problem is here:
    Code:
    System.out.print(((Player) playerInRange).getHealth());
    playerInRange is an entity. casting to a Player without checking if it's a Player (like you do 8 lines above) causes the exception.
     
  6. Offline

    kasszz

    thanks :)
     
Thread Status:
Not open for further replies.

Share This Page