Solved Setting skeletons target to null

Discussion in 'Plugin Development' started by Ward1246, Sep 20, 2015.

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

    Ward1246

    I am spawning a skeleton, and setting it's target to a specific entity. Similar to a bug that occurred earlier in the game, even after the entity is dead, the skeleton still shoots at the entities last location, even though it's dead. I have tried many things, most recently this:
    Code:
     ((EntityInsentient) ((CraftEntity) ((Skeleton) skeleton)).getHandle()).setGoalTarget(null);
    That code does not seem to set the entities target to nothing. If anyone could post a way to get a proper way of doing this, or even an event to check everything I need to, I will greatly appreciate it. I am using 1.8.
     
  2. Offline

    ShadowLAX

  3. Offline

    Ward1246

    I tried that, and that didn't seem to work either. Any other ideas? Thank you though.
     
  4. Offline

    ShadowLAX

    @Ward1246 I'm sure it should work, as if it didn't it wouldn't be part of Bukkit. However, the only other solution I can think of it to override the skeleton's target pathfinder by creating a custom entity and creating your own target event with a check to see if the target is dead etc. I'm sure there is a simpler way by using setTarget or what you mentioned, but I haven't messed around with them enough to know.
     
  5. Offline

    pedrinholuizf

    You can spawn the skeleton, and listen to EntityTargetEvent. If the entity that is targetting is your skeleton, then cancel the event.
     
  6. Offline

    Ward1246

    There is only 1 problem with that, I do want the skeleton to attack the entity until it dies, this would not allow the skeleton to attack at all.


    Edit: Here is a piece of code I tried to use when setting the entities target to null
    Code:
        @EventHandler
                public void onEntityShootBow(EntityShootBowEvent event) {
                    Entity entity = event.getEntity();
                    if (entity instanceof Skeleton) {
                        if (skeleton.contains(entity)) {
                            Entity target = ((Creature) entity).getTarget();
                            if (target.isDead()) {
                ((EntityInsentient) ((CraftEntity) ((Skeleton) entity)).getHandle()).setGoalTarget(null);
                ((Creature) entity).setTarget(null);
                        }}}}
                        
    Upon doing this, I had it send me a message, it sent me a message once, then not again, so what I get from this is: The entity is dead, the skeleton shot the bow, it set the target to null, but the skeleton still shoots, it only displays the message one because it tests if the skeleton has a target, and upon firing it removes the target. There must be a different way to stop the skeleton from shooting, is there any minecraft skeleton source files someone could link me to? That might have the solution there.

    Final edit: Solved. To anyone with this problem, all that is needed to be done is set the skeletons follow range to 0, then a few seconds later set it back to normal, looking like this:
    Code:
    @EventHandler
                public void onEntityShootBow(EntityShootBowEvent event) {
                    final Entity entity = event.getEntity();
                    if (entity instanceof Skeleton) {
                        if (skeleton.contains(entity)) {
                            Entity target = ((Creature) entity).getTarget();
                            if (target.isDead()) {
                                AttributeInstance attributes = ((EntityInsentient)((CraftEntity)entity).getHandle()).getAttributeInstance(GenericAttributes.b);
                                attributes.setValue(0);
                ((EntityInsentient) ((CraftEntity) ((Skeleton) entity)).getHandle()).setGoalTarget(null);
                this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() { public void run() {
                AttributeInstance attributes1 = ((EntityInsentient)((CraftEntity)entity).getHandle()).getAttributeInstance(GenericAttributes.b);
                attributes1.setValue(16);
                }}, 20 * 2);
                }
                }
    Hope this helps someone in the future.
     
    Last edited: Sep 20, 2015
Thread Status:
Not open for further replies.

Share This Page