NMS Make pig attack players

Discussion in 'Spigot Plugin Development' started by Masafe, Mar 13, 2018.

  1. Hi i have an error when i try to make a pig attack entityhuman.

    Code (Text):
    Could not pass event CreatureSpawnEvent to FortisPvPCore v1.0.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at org.bukkit.craftbukkit.v1_11_R1.event.CraftEventFactory.callCreatureSpawnEvent(CraftEventFactory.java:283) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.World.addEntity(World.java:1038) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.WorldServer.addEntity(WorldServer.java:1065) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.ChunkRegionLoader.a(ChunkRegionLoader.java:509) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.MobSpawnerAbstract.c(MobSpawnerAbstract.java:113) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.TileEntityMobSpawner.F_(SourceFile:91) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.World.tickEntities(World.java:1498) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.WorldServer.tickEntities(WorldServer.java:618) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:814) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at java.lang.Thread.run(Thread.java:748) [?:1.8.0_151]
    Caused by: java.lang.NullPointerException
        at ovh.masafe.entities.CustomPig.initAttributes(CustomPig.java:68) ~[?:?]
        at net.minecraft.server.v1_11_R1.EntityLiving.<init>(EntityLiving.java:138) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.EntityInsentient.<init>(EntityInsentient.java:49) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.EntityCreature.<init>(EntityCreature.java:18) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.EntityAgeable.<init>(EntityAgeable.java:41) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.EntityAnimal.<init>(EntityAnimal.java:11) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.EntityPig.<init>(EntityPig.java:20) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at ovh.masafe.entities.CustomPig.<init>(CustomPig.java:40) ~[?:?]
        at ovh.masafe.entities.SpawnCustomEntity.MS(SpawnCustomEntity.java:27) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_151]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_151]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_151]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_151]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        ... 16 more
    Code (Text):

    public class CustomPig extends EntityPig{
          private static final Set<Item> by = com.google.common.collect.Sets.newHashSet(new Item[] { Items.CARROT, Items.POTATO, Items.BEETROOT });
        public CustomPig(World nmsworld)
        {
            super(nmsworld);
           
       
        }
        protected void r() {
            super.r();
             goalSelector.a(0, new PathfinderGoalFloat(this));
                goalSelector.a(1, new PathfinderGoalPanic(this, 1.25D));
                goalSelector.a(3, new PathfinderGoalBreed(this, 1.0D));
                goalSelector.a(4, new PathfinderGoalTempt(this, 1.2D, Items.CARROT_ON_A_STICK, false));
                goalSelector.a(4, new PathfinderGoalTempt(this, 1.2D, false, by));
                goalSelector.a(5, new PathfinderGoalFollowParent(this, 1.1D));
                goalSelector.a(6, new PathfinderGoalRandomStrollLand(this, 1.0D));
                goalSelector.a(7, new PathfinderGoalLookAtPlayer(this, EntityHuman.class, 6.0F));
                goalSelector.a(8, new PathfinderGoalRandomLookaround(this));
                dk();
          }
        protected void dk() {
            targetSelector.a(2, new PathfinderGoalNearestAttackableTarget<EntityHuman>(this, EntityHuman.class, true));
         
          }
       
        protected void initAttributes() {
            super.initAttributes();
            getAttributeInstance(GenericAttributes.maxHealth).setValue(10.0D);
            getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).setValue(100.75D);
            getAttributeInstance(GenericAttributes.ATTACK_DAMAGE).setValue(100.75D);
       
          }
       
       
    Code (Text):
    @EventHandler
        public void MS(CreatureSpawnEvent event) {
            if(event.getSpawnReason() == SpawnReason.SPAWNER) {
                Bukkit.broadcastMessage("spawner by spawner");
                if(event.getEntity().getType() == EntityType.PIG) {
                    if(Bukkit.getVersion().contains("1.11")) {

                        net.minecraft.server.v1_11_R1.WorldServer nmsworld =( (CraftWorld) event.getEntity().getWorld()).getHandle();
                        CustomPig cp = new CustomPig(nmsworld);
                        cp.setPosition(event.getLocation().getX(), event.getLocation().getY(), event.getLocation().getZ());
                        Bukkit.broadcastMessage("" + cp);
                    }else {
                        Bukkit.broadcastMessage("no 1.11");
                    }
                }else {
                    Bukkit.broadcastMessage("no spawn pig");
                }
            }
        }
     
  2. Try changing the attributes in the constructor. Because I have done this with my custom entities and it seems to work for me.
     
  3. But if I remove the atribute the pig will no attack
     
  4. What I mean is change the attributes in the constructor not removing them. Also try changing one attribute at a time and test it to see if it is just one of the attributes or something
     
  5. The attribute ATTACK_DAMAGE isn't registered to an EntityPig in vanilla so you cannot get its instance. The method getAttributeInstance attempts to get the attribute directly from the map and since it's not there it will return null. Register the new attribute using the method b() mc version 1_12_R1 first.
     
  6. in 1.11 is the same method?
     
  7. Not sure, but the nms class it's in is AttributeMapBase.
    It's the one that throws an illegalargumentexception "Attribute is already registered!"
    Should be b()
     

Share This Page