Getting the Nearest Player?

Discussion in 'Plugin Development' started by Haerar, Feb 24, 2011.

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

    Haerar

    Okay, so I'm now working on my Monster Assault Plugin... The current plan is a simple one. Once every few nights, all monsters will attack the nearest player. This would cause monsters to attempt to attack settled areas through forcing their target even if they can't see it due to high walls etc. I also intend on modifying Creeper behaviour to explode against any obviously man-made constructions. However! I'm having trouble working out how precisely to designate any players at all, let alone the closest to the mob. Any ideas? I can easily define the mob in its own Listener - after that it's a simple setTarget() to pull this off as a basic frame to expand on...
     
  2. Offline

    Edward Hand

    I'm assuming you only want to target players. If so, its relatively simple.

    Code:
    CraftEntity ent = (CraftEntity)theMob;
    CraftWorld world = (CraftWorld)theWorld;
    EntityHuman target = world.getHandle().a(ent.getHandle(),RADIUS);
    theMob.setTarget(new CraftLivingEntity((CraftServer)theServer,target))
    (requires CraftBukkit obviously)

    (which you would do triggered by all entityTargetEvents that set a mob's target to null)

    The only problem you might have is that just because a mob is targeting an entity doesn't mean it will run towards it. As an example, ghasts will ignore their target unless <64 blocks away. In that case, you may have to nudge the things yourself...
     
  3. Offline

    Haerar

    Thanks Edward, greatly appreciated :)

    My counter for the distance issue is that since mobs spawn within a certain radius of players, I'll be using their onCreatureSpawn() and immediately getting them to attack should force them closer to the player from there... hopefully o.- Lots of testing to be done, ofc.
     
Thread Status:
Not open for further replies.

Share This Page