Making iron golem target a player

Discussion in 'Plugin Development' started by Ziden, May 14, 2012.

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

    Ziden

    Hi there !
    I wish to know how to make an IronGolem attack a player, does anyone managed to do that ?
    A simple setTarget + setPlayerCreated(false) isnt doing the thing :(
    Thanks for your attention !
     
  2. Offline

    LucasEmanuel

    Wouldn't this work?

    PHP:
    golem.setTarget((LivingEntityplayer);
     
  3. Offline

    Ziden

    no :(

    well, do you have to cast to LivingEntity ? im just passing tha player
     
  4. Offline

    kumpelblase2

    As long as you declare it as a type of player then no. E.g. if you save it as an object then you'd need to.
     
  5. Offline

    Ziden

    i would use directly ev.getPlayer()

    nawt working :(
     
  6. Offline

    Ziden

    no one knows the solution ? :( would be so fun !
     
  7. Offline

    HappyPikachu

  8. Offline

    Njol

    HappyPikachu: read the OP again:
    @op:
    Are you within a 16 blocks radius of the golem when you test this?
     
  9. Offline

    HappyPikachu

    Njol: Sorry, I got confused by the thread title / Lucas's comment. Allow me to clarify:

    In order for an Iron Golem to target a player, one must .setPlayerCreated(false), and then .setTarget(player).

    As of right now, golems cannot attack a player. Nothing along the lines of .setAngry() has been implemented for Iron Golems yet. Sorry, Ziden. :(
     
  10. Offline

    TheRealZuriki

    Possible solution to simply initiate an attack?

    .setPlayerCreated(false);
    .damage(0, Player);

    That would make the server assume the IGolem was naturally created and the player attacked it. The result being that the IGolem would pursue the player (naturally occuring IGolems can attack players according to the wiki).

    That is unless I'm missing the bigger problem of bukkit not having the correct behaviour for the IGolem?
     
  11. Offline

    thebiologist13

    I tried this out and it works for me :D

    Here is my code:

    Method to get near players. "source" is the point to get players around. "max" is the maximum distance the player can be from "source".

    Code:
    private ArrayList<Player> getNearbyPlayers(Location source, double max) {
        ArrayList<Player> players = new ArrayList<Player>();
        // "server" is a server object basically just in place of getServer().
        for(Player p : server.getOnlinePlayers()) {
            //Finds distance between source and player in 3D space.
            double distance = Math.sqrt(
                    Math.pow((p.getLocation().getX() - source.getX()), 2) + // x coordinate
                    Math.pow((p.getLocation().getY() - source.getY()), 2) + // y coordinate
                    Math.pow((p.getLocation().getZ() - source.getZ()), 2)  // z coordinate
                    );
            if(distance <= max) {
                players.add(p);
            }
        }
        return players;
    }
    This piece of code chooses a random player from nearby ones and damages the golem from the player. Also added .setTarget, but I'm not sure if it's necessary.

    Code:
    //Golem is a LivingEntity cast into a Golem earlier in the code
    IronGolem i = (IronGolem) golem;
     
    ArrayList<Player> nearPlayers = getNearbyPlayers(i.getLocation(), 16);
    int index = (int) Math.round(Math.rint(nearPlayers.size() - 1));
    if(nearPlayers != null) {
          i.setPlayerCreated(false);
          i.damage(0, nearPlayers.get(index));
          i.setTarget(nearPlayers.get(index));
    }
    
    Hope this helps!:)
     
  12. Offline

    Njol

    Why don't you simply use p.getLocation().distance(source)?

    edit: or use distanceSquared if you want the code to run slightly faster - just remember to square 'max' as well ;)
     
  13. Offline

    skore87

    Another method you could call on an entity is getNearbyEntities. I believe the entities fall within a cube shape, but it should take less processing power than looping through all players on a server
     
  14. Offline

    Njol

    This is not true since there are likely far less player on the whole server than there are entities in a world (getNearbyEntities loops through all entities of the world to get the nearest ones)
     
  15. Offline

    thebiologist13

    I didn't use .distance() method because I wrote the method before I knew it existed :D After that I just haven't changed the method it because it works right now.
     
Thread Status:
Not open for further replies.

Share This Page