Solved Casting to the correct Entity

Discussion in 'Plugin Development' started by The_Nesko, Mar 14, 2016.

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

    The_Nesko

    Hello i have a question is there i way to cast the Entity to the correct one i don't really know how to explain this but here is basically what am i trying to do is it possible if not is there another way to do this?

    Code:
    @EventHandler
    public void on5thAttackOnAnimal(EntityDamageByEntityEvent e) {
           if (e.getDamager().getType() == EntityType.PLAYER) {
                Player attacker = (Player) e.getDamager();
                int strikesCount = 0;
               
                if (attacker.getItemInHand() != null) {
                     if (attacker.getItemInHand().getType() == Material.DIAMOND_SWORD ||
                          attacker.getItemInHand().getType() == Material.IRON_SWORD) {
                               
                          if (animalsAndMonsters.contains(e.getEntity().getType())) {
                               List<Entity> nearbyEntitey = e.getEntity().getNearbyEntities(3, 3, 3);
                                           
                               for (Entity targets : nearbyEntitey) {
    
                                    if (strikesCount < 5) {
                                         if (animalsAndMonsters.contains(targets.getType())) {
                                                                            Bukkit.getServer().getWorld(targets.getWorld().getName()).strikeLightningEffect(targets.getLocation());
                                                       
                                               for (EntityType type : animalsAndMonsters) {
                                                      
                                                     if (type == e.getEntity().getType()) {
                                                          //cast the Entity in the type that fits?
                                                          Entity.damage(2D);
                                                          strikesCount++;
                                                     }
                                                     
                                                }
                                          }
                                          else if (strikesCount >= 5) {
                                               break;
                                          }
                                      }
                                  }
                              }
                          }
                      }
                  }
              }  
    
    This is mainly the part that i need help with:

    Code:
    if (type == e.getEntity().getType()) {
        //cast the Entity in the type that fits?
        Entity.damage(2D);
        strikesCount++;
     }
    
     
  2. Offline

    Zombie_Striker

    @The_Nesko
    In this case, it does not seem like you have to cast it to anything. Simply use Entity or LivingEntity to catch whatever entity it is.
     
    The_Nesko likes this.
  3. Offline

    The_Nesko

    @Zombie_Striker
    Entity doesn't work but LivingEntity does didn't know LivingEntity had .damage() thanks for the help. :)
     
Thread Status:
Not open for further replies.

Share This Page