NMS - Make Animals attack players

Discussion in 'Plugin Development' started by jeroenhero123, Mar 8, 2015.

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

    jeroenhero123

    Hey, I was trying to make Animals attack players, but it won't really work for me...
    It crashes my server and client, or pigs spawning doesn't work at all...

    Code:
    Code:
    package me.jeroenhero123.Slaughter;
    import java.lang.reflect.Field;
    
    import net.minecraft.server.v1_7_R1.EntityHuman;
    import net.minecraft.server.v1_7_R1.EntityPig;
    import net.minecraft.server.v1_7_R1.EntitySkeleton;
    import net.minecraft.server.v1_7_R1.EntityZombie;
    import net.minecraft.server.v1_7_R1.GenericAttributes;
    import net.minecraft.server.v1_7_R1.PathfinderGoalFloat;
    import net.minecraft.server.v1_7_R1.PathfinderGoalHurtByTarget;
    import net.minecraft.server.v1_7_R1.PathfinderGoalLookAtPlayer;
    import net.minecraft.server.v1_7_R1.PathfinderGoalMeleeAttack;
    import net.minecraft.server.v1_7_R1.PathfinderGoalMoveThroughVillage;
    import net.minecraft.server.v1_7_R1.PathfinderGoalMoveTowardsRestriction;
    import net.minecraft.server.v1_7_R1.PathfinderGoalNearestAttackableTarget;
    import net.minecraft.server.v1_7_R1.PathfinderGoalRandomLookaround;
    import net.minecraft.server.v1_7_R1.PathfinderGoalRandomStroll;
    import net.minecraft.server.v1_7_R1.PathfinderGoalSelector;
    import net.minecraft.server.v1_7_R1.World;
    
    import org.bukkit.Bukkit;
    import org.bukkit.craftbukkit.v1_7_R1.util.UnsafeList;
    import org.bukkit.entity.EntityType;
    public class CustomEntityPig extends EntityPig{
    public CustomEntityPig(World world) {
    super(world);
    try {
    Field bField = PathfinderGoalSelector.class.getDeclaredField("b");
    bField.setAccessible(true);
    Field cField = PathfinderGoalSelector.class.getDeclaredField("c");
    cField.setAccessible(true);
    bField.set(goalSelector, new UnsafeList<PathfinderGoalSelector>());
    bField.set(targetSelector, new UnsafeList<PathfinderGoalSelector>());
    cField.set(goalSelector, new UnsafeList<PathfinderGoalSelector>());
    cField.set(targetSelector, new UnsafeList<PathfinderGoalSelector>());
    } catch (Exception exc) {
    exc.printStackTrace();
    // This means that the name of one of the fields changed names or declaration and will have to be re-examined.
    }
    this.goalSelector.a(0, new PathfinderGoalFloat(this));
    this.goalSelector.a(2, new PathfinderGoalMeleeAttack(this, EntityHuman.class, 1.0D, false));
    this.goalSelector.a(4, new PathfinderGoalMeleeAttack(this, EntitySkeleton.class, 1.0D, true));
    this.goalSelector.a(5, new PathfinderGoalMoveTowardsRestriction(this, 1.0D));
    this.goalSelector.a(7, new PathfinderGoalRandomStroll(this, 1.0D));
    this.goalSelector.a(8, new PathfinderGoalLookAtPlayer(this, EntityHuman.class, 8.0F));
    this.goalSelector.a(8, new PathfinderGoalRandomLookaround(this));
    this.targetSelector.a(1, new PathfinderGoalHurtByTarget(this, true));
    this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityHuman.class, 0, true, false, null));
    this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntitySkeleton.class, 0, false, false, null));
    }
    @Override
    protected void aD() {
    super.aD();
    this.getAttributeInstance(GenericAttributes.e).setValue(10.0D); // Original 3.0D
    }
    }
    I used this tutorial to make it:
    https://bukkit.org/threads/how-to-override-default-minecraft-mobs.216788/

    Another small and maybe stupid question, how do I spawn these?
    World.spawn(loc, CustomEntityPig.class) doesn't work

    Thanks, Jeroen
     
  2. Offline

    Abstract97

    Have you made sure that you have registered the entity? Can you please show all of your other classes that have referenced this class.
     
Thread Status:
Not open for further replies.

Share This Page