How can I let target a Zombie target a Zombie

Discussion in 'Plugin Development' started by R44, Oct 6, 2018.

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

    R44

    This wont work and I dont know why can any one Help me ??
    I worked one this more than 2 weeks and it wont work....


    Code:
    @EventHandler   
    public void targetevent(EntitySpawnEvent e) {
       Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.getPlugin(), new Runnable() {
         
         @Override
         public void run() {
        String name =e.getEntity().getName();// get name
         if(name == "1") {//prof for Entity whit the name 1
         for(Entity target : e.getEntity().getNearbyEntities(30.0,10.0,30.0)) {// get all Enities in a 30 20 30 Block  radius
         if(target.getName()== "2") {
         LivingEntity attack =(LivingEntity) e.getEntity();   
         Creature attacking = (Creature) attack;
         attacking.setTarget((LivingEntity) target);// set the target
         if(target.isDead()) {
         }
         }else if(target instanceof Player) {
         LivingEntity unused =(LivingEntity) e.getEntity();
         Creature unuse = (Creature) unused;
         unuse.setTarget(unused);
         }     
         }         
         }           
         }
       }, 0, 20);   
       }
     
    Last edited by a moderator: Oct 7, 2018
  2. @R44
    I noticed a few problems with your code:

    1. You use 'name == "1"', but you should be using name.equals("1"). (Same for name 2)
    2. You use Entity target : e.getEntity().getNearby...
    Use something like List<Entity> nearby = e.getEntity().getNearbyEntities... instead.
    And then Entity target : nearby

    That is because this might cause endless loops when the .getNearbyEntities returns a new list every iteration.

    Editing these things may or may not solve your problem instantly, but it is a step in the right direction at least.
     
  3. Offline

    R44

    can you send me a code??

    I dont know how to do that!
    Pleas can you give me a exaple code??

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 9, 2018
  4. @R44
    Code:
    @EventHandler  
    
    public void targetevent(EntitySpawnEvent e) {
       Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.getPlugin(), new Runnable() {
        
         @Override
         public void run() {
        String name =e.getEntity().getName();// get name
         if(name.equals("1")) {//prof for Entity whit the name 1
    List<Entity> targets = e.getEntity().getNearbyEntities(30.0,10.0,30.0);
         for(Entity target : targets) {// get all Enities in a 30 20 30 Block  radius
         if(target.getName().equals("2")) {
         LivingEntity attack =(LivingEntity) e.getEntity();  
         Creature attacking = (Creature) attack;
         attacking.setTarget((LivingEntity) target);// set the target
         if(target.isDead()) {
         }
         }else if(target instanceof Player) {
         LivingEntity unused =(LivingEntity) e.getEntity();
         Creature unuse = (Creature) unused;
         unuse.setTarget(unused);
         }    
         }        
         }          
         }
       }, 0, 20);  
       }
    I think it should be something like this.
    But why can't you edit a few things if you can write this?
     
Thread Status:
Not open for further replies.

Share This Page