Solved Anti Default Mob Spawn

Discussion in 'Spigot Plugin Development' started by Gorobetz, Jun 7, 2017.

  1. So yeah Im getting an error message and it aint working whenever a mob spawns.

    Code:
    Code (Text):

    public class AntiDefaultMobs extends JavaPlugin implements Listener{
    @Override
        public void onEnable(){
        Bukkit.getPluginManager().registerEvents(this, this);
    }
    @EventHandler
    public void onMobsSpawn(EntitySpawnEvent e){
        if(!e.getEntity().getCustomName().isEmpty()){
            if(e.getEntity() instanceof LivingEntity){
            e.getEntity().isDead();
            }
        }
    }
    }
     
    Error Message:
    Code (Text):

     Could not pass event CreatureSpawnEvent to AntiDefaultMobs v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at org.bukkit.craftbukkit.v1_11_R1.event.CraftEventFactory.callCreatureSpawnEvent(CraftEventFactory.java:283) [spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.World.addEntity(World.java:1038) [spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.WorldServer.addEntity(WorldServer.java:1065) [spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.SpawnerCreature.a(SpawnerCreature.java:188) [spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.WorldServer.doTick(WorldServer.java:229) [spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:798) [spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    Caused by: java.lang.NullPointerException
            at com.LobsterTrap.antidefault.DefaultMobs.onMobsSpawn(DefaultMobs.java:17) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_131]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_131]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_131]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_131]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot.jar:git-Spigot-3fb9445-6e3cec8]
            ... 13 more
     
    That line that is triggering the console:
    Code (Text):

    if(!e.getEntity().getCustomName().isEmpty()){
     
    So yeah any better way to deny the spawing of default mobs or any way to fix this?
     
  2. what are you trying to do..?
     
  3. e.getEntity().getCustomName() will return null if it doesn't have a custom name, so you can't use isEmpty() on it.
    You should also be using e.setCancelled(true); to cancel the spawn. isDead() only tells you if it was killed.
     
    • Useful Useful x 1
  4. I checked / surfed the spigot forums and someone used isDead() to set the mob as dead, unless I read through their code to fast. What other ways can I kill it? Also, thanks c:
     
  5. Just changed it to setting the mobs health to 0, new error:
    Code (Text):

    [16:20:03 ERROR]: Could not pass event CreatureSpawnEvent to AntiDefaultMobs v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at org.bukkit.craftbukkit.v1_11_R1.event.CraftEventFactory.callCreatureSpawnEvent(CraftEventFactory.java:283) [spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.World.addEntity(World.java:1038) [spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.WorldServer.addEntity(WorldServer.java:1065) [spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.SpawnerCreature.a(SpawnerCreature.java:188) [spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.WorldServer.doTick(WorldServer.java:229) [spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:798) [spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    Caused by: java.lang.NullPointerException
            at com.LobsterTrap.antidefault.DefaultMobs.onMobsSpawn(DefaultMobs.java:18) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_131]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_131]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_131]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_131]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot.jar:git-Spigot-3fb9445-6e3cec8]
            ... 13 more
     
    Error code:
    Code (Text):

    if(e.getEntity().getCustomName().equals(null)){
     
    @iPyronic
     
    #5 Gorobetz, Jun 7, 2017
    Last edited: Jun 7, 2017
  6. You can forcibly remove a mob by using entity.remove() on it. It will not fire the death event nor drop items in that case.

    Your error is because you're still trying to use a method on a null 'object'. If something is null, that means there is no class instance to use. If there is no instance, it cannot call any methods because it doesn't have any methods. If you want to verify that an instance is or is not there first, you need to use either == or !=, not equals(). All class instances extend from the class java.lang.Object (which is where the .equals() method originally comes from).
     
    • Useful Useful x 1
  7. Mas

    Mas

    I think you may be getting a little bit confused. isDead() return a boolean - whether the entity is dead or not. It won't actually "do" anything to the mob.

    Also, you may want to consider using CreatureSpawnEvent, as EntitySpawnEvent will also trigger for entities such as items/snowballs etc.
     
  8. Ok thanks! Always useful.
     


  9. Check it with
    Code (Text):

    if(e.getEntity().getCustomName() == null){

    }
     

    instead.