Shulkerbullet not targeting entities

Discussion in 'Plugin Development' started by Zenya4, Jun 7, 2017.

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

    Zenya4

    Hi guys,

    I am making this plugin that allows players to spawn a shulker bullet that attacks the nearest entity :D

    However, it just seems to be randomly going and not targeting anything. Here's my code.
    Code:
    
    
    ShulkerBullet shulkerbullet = player.getWorld().spawn(player.getEyeLocation(), ShulkerBullet.class);
    
    for (int i = 0; i < 101; i++) {
    
    List<Entity> entities = player.getNearbyEntities(i,64,i);
    
    for (@SuppressWarnings("unused") Entity yes : entities) {
    
    Entity no = null;
    
    shulkerbullet.setShooter(player);
    
    shulkerbullet.setVelocity(player.getLocation().getDirection().multiply(1)); //CONFIG
    
    shulkerbullet.setTarget(yes); //CONFIG
    
    }
    
    }
    
    If i set .setTarget to no, it will just fire as a looped projectile, which is ok.
    But if i set it to yes, the shulker bullet will just go randomly and not hit anything. Any help will be appreciated thanks!
     
  2. Offline

    Zombie_Striker

    @Zenya4
    It is seeming random because the target is always changing. Entity can only have one target, so the target will always be the last entity in the list. An easy way around this would be to compare distances: if 'yes' is closer to the shulker then the shulker's last target, set it as the new target.
     
  3. Offline

    Zenya4

    I tried using this
    Code:
    ShulkerBullet shulkerbullet = player.getWorld().spawn(player.getEyeLocation(), ShulkerBullet.class);
    
    int x = player.getLocation().getBlockX();
    
    int y = player.getLocation().getBlockY();
    
    int z = player.getLocation().getBlockZ();
    
    List<Entity> entities = player.getNearbyEntities(x + 100, y + 100, z + 100);
    
    for (Entity e : entities) {
    
    if(e.getLocation().distance(player.getLocation()) <= 100) {
    
    if(e.getLocation().distance(player.getLocation()) <= e.getLocation().distance(player.getLocation())) {
    
    shulkerbullet.setShooter(player);
    
    shulkerbullet.setVelocity(player.getLocation().getDirection().multiply(1)); //CONFIG
    
    shulkerbullet.setTarget(e); //CONFIG
    
    }
    
    }
    
    }
    but it still doesn't work. If I use <= for comparing the distance the bullet goes randomly and if I use < it just goes downwards and crashes to the floor (not targeting).
     
  4. Offline

    Zombie_Striker

    You forgot to replace one of the 'e's with the other target instance. In this case, it is checking if the distance is less than itself.

    Also, by adding the XYZ, you are massivly increasing the radius. Those values are the radius, not the center. If the player is at X = 1,000, then the radius will try to get all the players within a 1,100 block radius. Remove the XYZ from the getNearbyEntities.

    Then, after that is done, remove the if statement that checks if the distance is less than 100. Since you already specified the radius to be 100 in the getNearbyEntities method, all entities that would be over 100 away would already be ignored.
     
  5. Offline

    Zenya4

    @Zombie_Striker

    Okayy, I did all of the above but I didn't quite understand what the first one meant. Is it something like this?
    Code:
    
    if(args[0].equalsIgnoreCase("Shulker_Bullet")) {
    
    ShulkerBullet shulkerbullet = player.getWorld().spawn(player.getEyeLocation(), ShulkerBullet.class);
    
    shulkerbullet.setShooter(player);
    
    shulkerbullet.setVelocity(player.getLocation().getDirection().multiply(1)); //CONFIG
    
    List<Entity> entities = player.getNearbyEntities(100, 100, 100);
    
    for(Entity e : entities) {
    
    for(Entity e2 : entities) {
    
    if(e.getLocation().distance(player.getLocation()) < e2.getLocation().distance(player.getLocation())) {
    
    shulkerbullet.setTarget(e); //CONFIG
    
    }
    
    }
    
      }
    
      }
    Still doesnt work though.
     
  6. Offline

    Zombie_Striker

    @Zenya4
    Nope. Remove the second for loop, add an if statement that makes sure the shulker's target is not null, and replace "e2" with "shulkerbullet.getTarget()"
     
  7. Offline

    Zenya4

    @Zombie_Striker

    It still doesn't work. Am I doing anything wrong?
    Code:
    
    ShulkerBullet shulkerbullet = player.getWorld().spawn(player.getEyeLocation(), ShulkerBullet.class);
    
                                                                        shulkerbullet.setShooter(player);
    
                                                                        shulkerbullet.setVelocity(player.getLocation().getDirection().multiply(1));//CONFIG
    
                                                                        int xrange = 100; //CONFIG
    
                                                                        int yrange = 128; //CONFIG
    
                                                                        int zrange = 100; //CONFIG
    
                                                                        List<Entity> entities = player.getNearbyEntities(xrange, yrange, zrange);
    
                                                                        for (Entity e : entities) {
    
                                                                          shulkerbullet.setTarget(e);
    
                                                                            if(shulkerbullet.getTarget() != null) {
    
                                                                                if(e.getLocation().distance(player.getLocation()) < shulkerbullet.getTarget().getLocation().distance(player.getLocation())) {
    
                                                                                    shulkerbullet.setTarget(e);
    
                                                                                }
    
                                                                            }
    
                                                                        }
    
    EDIT: By "doesn't work" I mean it still moves at random. If I remove the 1st setTarget() it will move in a fixed trajectory.
     
  8. Offline

    Zombie_Striker

    @Zenya4
    Do not set the target unless you know it is the one you want. Remove the setTarget line that is above the null check.
     
  9. Offline

    Zenya4

    @Zombie_Striker

    If I remove it the shulker bullet doesn't target anything (tested it)


    Sent from my iPhone using Tapatalk
     
  10. Offline

    Zombie_Striker

    @Zenya4
    Try this:
    Code:
    
    if(shulkerbullet.getTarget() != null) {
    
    if(e.getLocation().distance(player.getLocation()) < shulkerbullet.getTarget().getLocation().distance(player.getLocation())) {
    
    shulkerbullet.setTarget(e);
    
    }
    
    }else{
    shulkerbullet.setTarget(e);
    }
    
     
  11. Offline

    Zenya4

    @Zombie_Striker

    I've tried using your code and adding a check.
    Code:
    if(shulkerbullet.getTarget() != null) {
    
    if(e.getLocation().distance(player.getLocation()) < shulkerbullet.getTarget().getLocation().distance(player.getLocation())) {
    
    shulkerbullet.setTarget(e);
    sender.sendMessage("If executed!") //CHECK
    
    }
    
    }else{
    shulkerbullet.setTarget(e);
    sender.sendMessage("Else executed!") //CHECK
    }
    
    The shulker bullet still goes in a random direction and the check keeps returning "If executed!" and ends with "Else executed!" (when the bullet crashes)
     
Thread Status:
Not open for further replies.

Share This Page