Enderman Automatically angry when spawned

Discussion in 'Plugin Development' started by R3YGamer, Sep 7, 2020.

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

    R3YGamer

    This is my code so far but it doesn't work. Can someone help plz?

    @EventHandler
    public void onCreatureSpawn(CreatureSpawnEvent event) {
    if (event.getEntityType() == EntityType.ENDERMAN) {
    event.getEntity().attack(Player);
    }
    }
    }
     
  2. Offline

    timtower Administrator Administrator Moderator

    @R3YGamer What player should it target then?
     
  3. Offline

    R3YGamer

    The closest player
     
  4. Offline

    timtower Administrator Administrator Moderator

    Then you need to find the closest player first.
     
    Kars likes this.
  5. Offline

    R3YGamer

    How?
     
  6. Offline

    timtower Administrator Administrator Moderator

    Loop over the players, compare the distance to the enderman.
     
  7. Offline

    R3YGamer

    Is that the loop to get the location of each other? but how do you get the closes player from the enderman and how to make the enderman attack it?

    @EventHandlerpublic void onCreatureSpawn(CreatureSpawnEvent event) {
    if (event.getEntityType() == EntityType.ENDERMAN) {
    event.getLocation(); for (Player people : Bukkit.getOnlinePlayers()) {
    Location location = people.getLocation();}
    }
    }
    }
     
  8. Offline

    xelatercero

    So I just did it (i don't know if is the best way) , I looped over the players and then checked the distance from the Enderman , if the distance from the current player is lower than the last player in the loop I just made that player the target with a variable (targetPlyer) , and so on until the loop is finished , this way at the end of the loop you will get the closest player to the Enderman. My english is not the best so i don't know if i explained myselft correctly, let me know.
     
  9. Offline

    R3YGamer

    So how would i code this because i don't know how to check which player is the closest. Can you give me the basic template for me to use?

    Edit: Nevermind the code, i found out how to code it but now i have the closest player, how do i make the enderman automatically start attacking the player?
     
    Last edited: Sep 7, 2020
  10. Offline

    xelatercero

    well when you have the target player just use enderman(or whatever you called it).setTarget(targetPlayer);
     
  11. Offline

    R3YGamer

    oh ok

    edit:
    this code doesn't work, can someone help?
    the setTarget option isnt available!

    @EventHandler
    public void onCreatureSpawn(CreatureSpawnEvent event) {
    if (event.getEntityType() == EntityType.ENDERMAN) {
    double closest = Double.MAX_VALUE;
    Player closestp = null;
    for(Player i : Bukkit.getOnlinePlayers()){
    double dist = i.getLocation().distance(event.getEntity().getLocation());
    if (closest == Double.MAX_VALUE || dist < closest){
    closest = dist;
    closestp = i;
    }
    }
    if (closestp == null){
    //No players found
    }
    else{
    event.setTarget(closestp);
    }
    }
    }
    }

    Edit: nevermind this code works, thx for the help
     
    Last edited: Sep 7, 2020
Thread Status:
Not open for further replies.

Share This Page