Solved Forcing Iron Golems to Attack Players

Discussion in 'Plugin Development' started by Derthmonuter, Sep 26, 2012.

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

    Derthmonuter

    Hello everyone.

    I would like to make iron golems aggressive towards players.
    The only way I know of doing this is using
    golem.damage(0, player);
    But due to recent changes in Minecraft, this no longer works. Golems have the tendency to "forgive" players now, and will not pursue them. A workaround to this that I have tried is to use a repeating task and periodically damage the iron golem, but this is not something I want. It's ugly when the golem randomly lights up red, shakes, and gets knocked back. The knock-back especially makes them unable to actually move towards the player.

    Can someone help me achieve aggressive iron golems? The way I see there are two ways to do this:
    1) Suppress the red knock-back hurt effect on the golem when they get damaged through the repeating task.
    2) Remove the golems ability to forgive the player.

    Any help on this would be greatly appreciated.
    Thanks!
     
  2. Offline

    travja

    Use PlayerMoveEvent and check if they are near an iron golem, if they are, see if the iron golem has a target, if it does, don't do anything, otherwise, set the target to the player and he should go attack it.
     
  3. Offline

    Derthmonuter

    Unless there have been some big changes to how iron golems work in the past few months, this has been verified by a great many other threads as simply not working. I'll try it myself in the morning, but for now a bump.
     
  4. Offline

    Lolmewn

    How about creating an entityTargetEvent?
     
  5. Offline

    Derthmonuter

    Adding in this code to make the iron golem never untarget a targeted player also has no effect.
    Code:java
    1. @EventHandler
    2. public void noForgiving(EntityTargetEvent e) {
    3. if(e.getEntity().getType() == EntityType.IRON_GOLEM){
    4. e.setTarget(e.getTarget());
    5. }
    6. }

    Proves that setTarget() does nothing with regards to iron golems.

    Bump.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  6. Offline

    Derthmonuter

  7. Offline

    Derthmonuter

    Accomplished using Jacek's tutorial here: http://forums.bukkit.org/threads/tutorial-how-to-customize-the-behaviour-of-a-mob-or-entity.54547/

    And this class I wrote:
    Code:java
    1. package aurum.survival;
    2.  
    3. import java.lang.reflect.Field;
    4. import java.util.List;
    5. import net.minecraft.server.*;
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.craftbukkit.CraftServer;
    8. import org.bukkit.craftbukkit.entity.CraftIronGolem;
    9. import org.bukkit.plugin.Plugin;
    10.  
    11. /**
    12. * @author Tim Cancy
    13. */
    14. public class AngryIronGolem extends net.minecraft.server.EntityIronGolem {
    15.  
    16. private AurumSurvival plugin;
    17.  
    18. @SuppressWarnings("unchecked")
    19. public AngryIronGolem(World world) {
    20. super(world);
    21. Plugin pluginAurum = Bukkit.getPluginManager().getPlugin("Aurum Survival");
    22. if (pluginAurum == null || !(pluginAurum instanceof AurumSurvival)) {
    23. this.world.removeEntity(this);
    24. return;
    25. }
    26. this.plugin = (AurumSurvival) pluginAurum;
    27. this.bukkitEntity = new CraftIronGolem((CraftServer) this.plugin.getServer(), this);
    28. try {
    29. Field navigation = EntityLiving.class.getDeclaredField("navigation");
    30. navigation.setAccessible(true);
    31. navigation.set(this, new AurumSurvivalNavigation(this.plugin, this, this.world, 16.0f));
    32. }
    33. try {
    34. Field goala = this.goalSelector.getClass().getDeclaredField("a");
    35. goala.setAccessible(true);
    36. ((List<PathfinderGoal>) goala.get(this.goalSelector)).clear();
    37. Field targeta = this.targetSelector.getClass().getDeclaredField("a");
    38. targeta.setAccessible(true);
    39. ((List<PathfinderGoal>) targeta.get(this.targetSelector)).clear();
    40. this.goalSelector.a(1, new PathfinderGoalMeleeAttack(this, 0.25F, true));
    41. this.goalSelector.a(2, new PathfinderGoalMoveTowardsTarget(this, 0.22F, 32.0F));
    42. this.goalSelector.a(3, new PathfinderGoalMoveThroughVillage(this, 0.16F, true));
    43. this.goalSelector.a(4, new PathfinderGoalMoveTowardsRestriction(this, 0.16F));
    44. this.goalSelector.a(5, new PathfinderGoalOfferFlower(this));
    45. this.goalSelector.a(6, new PathfinderGoalRandomStroll(this, 0.16F));
    46. this.goalSelector.a(7, new PathfinderGoalLookAtPlayer(this, EntityHuman.class, 6.0F));
    47. this.goalSelector.a(8, new PathfinderGoalRandomLookaround(this));
    48. this.targetSelector.a(1, new PathfinderGoalDefendVillage(this));
    49. this.targetSelector.a(2, new PathfinderGoalHurtByTarget(this, false));
    50. this.targetSelector.a(3, new PathfinderGoalNearestAttackableTarget(this, EntityMonster.class, 16.0F, 0, false, true));
    51. }
    52. }
    53.  
    54. public void anger() {
    55. this.targetSelector.a(3, new PathfinderGoalNearestAttackableTarget(this, EntityHuman.class, 16.0F, 0, false, true));
    56. }
    57.  
    58. public void rose(boolean bool) {
    59. super.e(bool);
    60. }
    61. }
     
    hawkfalcon likes this.
  8. did you try this in the creaturespawn event?
    from the top of my head, could work.
    Code:
    if(entity instanceof IronGolem){
        IronGolem ig = (IronGolem)entity;
        ig.setAngry(true);
    }
    
     
  9. Offline

    Derthmonuter

    There is no setAngry() method for Iron Golems.
     
Thread Status:
Not open for further replies.

Share This Page