IronGolem NMS Attack Player

Discussion in 'Plugin Development' started by TheIronKill, Sep 26, 2014.

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

    TheIronKill

    I need help to make an Iron Golem attack on human entity, I edited the code of the class, modified the goals, but the iron gomel not attack me and continues attacking the mobs before.

    Anyone have a customized class Iron Golem, where this attack humans? Please help me with this, thanks
     
  2. Offline

    AlphaRLee

    TheIronKill can you post your code? It makes it a lot easier to help if we can see what you have :)

    I don't know if this will work, but try adding these lines somewhere inside your pathfindergoals:
    Code:java
    1. this.goalSelector.a(2,newPathfinderGoalMeleeAttack(this,EntityHuman.class,1.0D,false));
    2. this.targetSelector.a(2,newPathfinderGoalNearestAttackableTarget(this,EntityHuman.class,0,true));

    I just borrowed this from the zombie class, hopefully that will work. I actually haven't tested iron golems myself...
     
  3. Offline

    97WaterPolo

    TheIronKill
    I believe you need to set the iron golems owner and target to null. That should make them target humans.

    As for it targeting mobs, use EnityTargetEvent , check if the entity is instance of iron golem and the target is instance of your mob, if so cancel the event.
     
  4. Offline

    fireblast709

    TheIronKill Listen for the CreatureSpawnEvent, check if it's an IronGolem and if the SpawnReason was BUILD_IRONGOLEM. If those two are true, then cast to IronGolem and invoke the method setPlayerCreated(false).
     
  5. Offline

    TheIronKill

    Thanks guys, try with your help, before, AlphaRLee this is my code
    I am new in this, tell me if my code is bad...please

    Code:java
    1. public class CustomEntityIronGolem extends EntityIronGolem {
    2. @SuppressWarnings("rawtypes")
    3. public CustomEntityIronGolem(World world) {
    4. super(world);
    5.  
    6. List goalB = (List)getPrivateField("b", PathfinderGoalSelector.class, goalSelector); goalB.clear();
    7. List goalC = (List)getPrivateField("c", PathfinderGoalSelector.class, goalSelector); goalC.clear();
    8. List targetB = (List)getPrivateField("b", PathfinderGoalSelector.class, targetSelector); targetB.clear();
    9. List targetC = (List)getPrivateField("c", PathfinderGoalSelector.class, targetSelector); targetC.clear();
    10.  
    11. this.goalSelector.a(2, new PathfinderGoalMeleeAttack(this, EntityHuman.class, 1.0D, false));
    12. this.goalSelector.a(2, new PathfinderGoalMoveTowardsTarget(this, 0.9D, 32.0F));
    13. this.goalSelector.a(3, new PathfinderGoalMoveThroughVillage(this, 0.6D, true));
    14. this.goalSelector.a(4, new PathfinderGoalMoveTowardsRestriction(this, 1.0D));
    15. this.goalSelector.a(5, new PathfinderGoalOfferFlower(this));
    16. this.goalSelector.a(6, new PathfinderGoalRandomStroll(this, 0.6D));
    17. this.goalSelector.a(7, new PathfinderGoalLookAtPlayer(this, EntityHuman.class, 6.0F));
    18. this.goalSelector.a(8, new PathfinderGoalRandomLookaround(this));
    19. this.targetSelector.a(1, new PathfinderGoalHurtByTarget(this, true));
    20. this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityHuman.class, 0, true));
    21.  
    22. }
    23.  
    24. @SuppressWarnings("rawtypes")
    25. public static Object getPrivateField(String fieldName, Class clazz, Object object)
    26. {
    27. Field field;
    28. Object o = null;
    29. try
    30. {
    31. field = clazz.getDeclaredField(fieldName);
    32. field.setAccessible(true);
    33. o = field.get(object);
    34. }
    35. {
    36. e.printStackTrace();
    37. }
    38. {
    39. e.printStackTrace();
    40. }
    41. return o;
    42. }
    43.  
    44. }
     
  6. Offline

    97WaterPolo

    TheIronKill
    I really don't get why you are using NMS when you can easily accomplish the same thing within the bukkit api...
     
  7. Offline

    TheIronKill



    Hi, mm, how exactly, Im sorry but i dont know about this... I need help, thanks...
     
  8. Offline

    97WaterPolo

    TheIronKill
    Not going to spoonfeed you code, but since you made this I am guessing you are pretty advanced with bukkit so it should be relatively easy to figure this out, so here is a gist of how I did it.
    Code:java
    1.  
    2. EntityTargetEvent
    3. if(getEntity() instanceof IronGolem && getTarget() instanceof Zombie)
    4. e.setCancelled(true);
    5.  

    Code:java
    1. IronGolem boss =spawnIronBoss or listen to creature spawn event
    2. boss.setPlayerCreated(false);
    3. boss.setRemoveWhenFarAway(false);


    Setting it to playerCreated false will allow it to attack players. Cancelling the IronGolem from targeting zombies will prevent it from attacking zombies.
     
  9. Offline

    TheIronKill

    @97WaterPolo

    Forgive my ignorance, but when the Iron Golem begins attack player?

    any idea?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  10. Offline

    AlphaRLee

    I've tried the Bukkit's EntityTargetEvent, and it doesn't work all the best. For example, if a skeleton targets a zombie (through the NMS method), then the targeting event will register in the EntityTargetEvent. However, when I set the target to be null (because I don't want entities to target teammates in my plugin), they still fight each other...so I would prefer the NMS alternative if possible, because I know there has been records of EntityTargetEvent not working the best.
     
Thread Status:
Not open for further replies.

Share This Page