Solved Angry chicken entities tickle players instead of attacking

Discussion in 'Plugin Development' started by Ne0nx3r0, May 2, 2014.

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

    Ne0nx3r0

    I'm trying to create a hostile chicken, but for some reason the chickens will track and circle the player as if they were going to attack, but instead they don't attack and players laugh at the hyperactive flock of chickens encircling them.

    Here's the class I created for testing:
    Code:java
    1.  
    2. package com.ne0nx3r0.rih.entities;
    3.  
    4. import java.lang.reflect.Field;
    5. import net.minecraft.server.v1_7_R3.EntityChicken;
    6. import net.minecraft.server.v1_7_R3.EntityHuman;
    7. import net.minecraft.server.v1_7_R3.GenericAttributes;
    8. import net.minecraft.server.v1_7_R3.PathfinderGoalFloat;
    9. import net.minecraft.server.v1_7_R3.PathfinderGoalHurtByTarget;
    10. import net.minecraft.server.v1_7_R3.PathfinderGoalLookAtPlayer;
    11. import net.minecraft.server.v1_7_R3.PathfinderGoalMeleeAttack;
    12. import net.minecraft.server.v1_7_R3.PathfinderGoalMoveTowardsRestriction;
    13. import net.minecraft.server.v1_7_R3.PathfinderGoalNearestAttackableTarget;
    14. import net.minecraft.server.v1_7_R3.PathfinderGoalRandomLookaround;
    15. import net.minecraft.server.v1_7_R3.PathfinderGoalRandomStroll;
    16. import net.minecraft.server.v1_7_R3.PathfinderGoalSelector;
    17. import net.minecraft.server.v1_7_R3.World;
    18. import org.bukkit.craftbukkit.v1_7_R3.util.UnsafeList;
    19.  
    20. public class RIHEntityChicken extends EntityChicken{
    21.  
    22. public RIHEntityChicken(World world) {
    23. super(world);
    24.  
    25. try {
    26. Field bField = PathfinderGoalSelector.class.getDeclaredField("b");
    27. bField.setAccessible(true);
    28. Field cField = PathfinderGoalSelector.class.getDeclaredField("c");
    29. cField.setAccessible(true);
    30. bField.set(goalSelector, new UnsafeList<PathfinderGoalSelector>());
    31. bField.set(targetSelector, new UnsafeList<PathfinderGoalSelector>());
    32. cField.set(goalSelector, new UnsafeList<PathfinderGoalSelector>());
    33. cField.set(targetSelector, new UnsafeList<PathfinderGoalSelector>());
    34. }
    35. catch (Exception exc) {
    36. exc.printStackTrace();
    37. }
    38.  
    39. this.goalSelector.a(0, new PathfinderGoalFloat(this));
    40. this.goalSelector.a(2, new PathfinderGoalMeleeAttack(this, EntityHuman.class, 1.0D, false));
    41. this.goalSelector.a(5, new PathfinderGoalMoveTowardsRestriction(this, 1.0D));
    42. this.goalSelector.a(7, new PathfinderGoalRandomStroll(this, 1.0D));
    43. this.goalSelector.a(8, new PathfinderGoalLookAtPlayer(this, EntityHuman.class, 8.0F));
    44. this.goalSelector.a(8, new PathfinderGoalRandomLookaround(this));
    45. this.targetSelector.a(1, new PathfinderGoalHurtByTarget(this, true));
    46. this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityHuman.class, 0, true));
    47. }
    48.  
    49. @Override
    50. public String getName(){
    51. return "Cucco";
    52. }
    53.  
    54. @Override
    55. protected void aC() {
    56. super.aC();
    57.  
    58. // follow range
    59. this.getAttributeInstance(GenericAttributes.b).setValue(40.0D);
    60.  
    61. // movement speed
    62. //this.getAttributeInstance(GenericAttributes.d).setValue(0.23000000417232513D);
    63. this.getAttributeInstance(GenericAttributes.d).setValue(0.53000000417232513D);
    64.  
    65. // attack damage
    66. //this.getAttributeInstance(GenericAttributes.e).setValue(3.0D);
    67.  
    68. //this.bb().b(bp).setValue(this.random.nextDouble() * 0.10000000149011612D);
    69. }
    70. }
    71.  


    Curiously, if I use this same class without changing or uncommenting anything but extending from Ocelot instead of Chicken (to create an angry cat) that works as expected.

    Is there something specific about chickens that prevents them from attacking?
     
  2. Offline

    xyfonix

    Maybe a chicken can't attack?
     
  3. Offline

    Ne0nx3r0

    I did a little digging and what didn't occur to me before was that Ocelots do attack, but somewhat ironically only go for chickens. I didn't know cats attacked anything which is why I choose them as a comparison test.

    After some digging in these classes:
    https://github.com/Bukkit/mc-dev/blob/master/net/minecraft/server/EntityChicken.java
    https://github.com/Bukkit/mc-dev/blob/master/net/minecraft/server/EntityZombie.java
    https://github.com/Bukkit/mc-dev/blob/master/net/minecraft/server/EntityOcelot.java

    It seems overriding these two methods might do the trick:

    Code:java
    1. @Override
    2. public boolean n(Entity entity) {
    3. return entity.damageEntity(DamageSource.mobAttack(this), 3.0F);
    4. }
    5.  
    6. @Override
    7. public boolean damageEntity(DamageSource damagesource, float f) {
    8. if (this.isInvulnerable()) {
    9. return false;
    10. } else {
    11. return super.damageEntity(damagesource, f);
    12. }
    13. }

    I wont be able to test this until I get home, but I'll give it a shot.

    *edit* It worked :)
     
Thread Status:
Not open for further replies.

Share This Page