Solved NullPointerException when spawning custom MagmaCube

Discussion in 'Plugin Help/Development/Requests' started by TheKiller53, Jul 22, 2015.

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

    TheKiller53

    Hello, I have recently started work on a custom bosses plugin. I created a custom Zombie the other day with no problems, however now after creating a custom magma cube I get a NPE whenever spawning it in with the spawn egg

    My Main / Listener:
    Code:
    package com.mclaurence.CustomBoss;
    
    import static org.bukkit.ChatColor.GOLD;
    import static org.bukkit.ChatColor.GREEN;
    import net.minecraft.server.v1_7_R4.EntityMagmaCube;
    import net.minecraft.server.v1_7_R4.NBTTagCompound;
    import net.minecraft.server.v1_7_R4.NBTTagList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.craftbukkit.v1_7_R4.inventory.CraftItemStack;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.MagmaCube;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.event.entity.EntitySpawnEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import com.mclaurence.CustomBoss.Bosses.CustomEntityMagma;
    
    public class Main extends JavaPlugin implements Listener{
       
        public static ItemStack Test;
       
        public void onEnable(){
           
            NMSUtil nmsu = new NMSUtil();
            nmsu.registerEntity("Test", 62, EntityMagmaCube.class, CustomEntityMagma.class);
           
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
           
            Test = new ItemStack(Material.MONSTER_EGG, 1, (short) 62);
            ItemMeta TestMeta = Test.getItemMeta();
           
            TestMeta.setDisplayName(GREEN + "Test");
            Test.setItemMeta(TestMeta);
           
            Test = addGlow(Test);
        }
       
        public void onDisable(){
           
        }
       
        @EventHandler
        public void onPlayerSpawnBoss(PlayerInteractEvent event){
            if(event.getAction() == Action.RIGHT_CLICK_BLOCK){
               
                if(event.getPlayer().getItemInHand() == Test && event.getPlayer().getItemInHand().getItemMeta().equals(Test.getItemMeta())){
                   
                }
            }
        }
       
        @EventHandler
        public void BossDeath(EntityDeathEvent event){
            if(event.getEntity().getKiller() instanceof Player){
               
                if(event.getEntity() instanceof MagmaCube){
                   
                    if(event.getEntity().getCustomName() == null){
                        return;
                    }
                    if(event.getEntity().getCustomName().equalsIgnoreCase(GREEN + "Test")){
                       
                        event.getEntity().getWorld().createExplosion(event.getEntity().getLocation(), 2.0F);
                       
                        Bukkit.broadcastMessage("" + event.getEntity().getKiller().getDisplayName() + " has defeated " + GOLD + "The Test");
                        }
                    }
                }
            }
       
        @EventHandler
        public void BossSpawn(EntitySpawnEvent event){
            if(event.getEntity() instanceof MagmaCube){
               
                if(((LivingEntity) event.getEntity()).getCustomName() == null){
                    return;
                }
               
                if(((LivingEntity) event.getEntity()).getCustomName().equalsIgnoreCase(GREEN + "Test")){
                   
                    getServer().getWorld(event.getEntity().getWorld().getName()).strikeLightning(event.getEntity().getLocation());
                    }
                }
            }
       
        public static ItemStack addGlow(ItemStack itemstack){
            net.minecraft.server.v1_7_R4.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemstack);
            NBTTagCompound tag = null;
           
            if(!nmsStack.hasTag()){
                tag = new NBTTagCompound();
                nmsStack.setTag(tag);
            }
            if(tag == null) tag = nmsStack.getTag();
           
            NBTTagList enchantment = new NBTTagList();
            tag.set("ench", enchantment);
            nmsStack.setTag(tag);
            return CraftItemStack.asCraftMirror(nmsStack);
        }
       
        @EventHandler
        public void onJoin(PlayerJoinEvent event){
            if(event.getPlayer().isOp()){
                event.getPlayer().getInventory().addItem(Test);
            }
        }
    }
    
    My Custom MagmaCube class:
    Code:
    package com.mclaurence.CustomBoss.Bosses;
    
    import net.minecraft.server.v1_7_R4.EntityMagmaCube;
    import net.minecraft.server.v1_7_R4.GenericAttributes;
    import net.minecraft.server.v1_7_R4.Item;
    import net.minecraft.server.v1_7_R4.World;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.craftbukkit.v1_7_R4.CraftWorld;
    import org.bukkit.craftbukkit.v1_7_R4.entity.CraftLivingEntity;
    import org.bukkit.entity.MagmaCube;
    import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
    
    public class CustomEntityMagma extends EntityMagmaCube{
       
        public CustomEntityMagma(World world){
            super(world);
        }
       
        @Override
        protected void aD(){
            super.aD();
           
            this.setCustomName(ChatColor.GREEN + "Test");
            this.getAttributeInstance(GenericAttributes.e).setValue(1.0D); //5X damage than normal mob
            this.getAttributeInstance(GenericAttributes.maxHealth).setValue(2000.0D); //50 Hearts
            this.getAttributeInstance(GenericAttributes.d).setValue(1.0D); //Set speed
            this.setSize(1000);    //Make the MagmaCube big
        }
       
        protected Item getLoot(){
            return null;
        }
       
        public static MagmaCube spawn(Location loc){
            World mcWorld = (World) ((CraftWorld) loc.getWorld()).getHandle();
            final CustomEntityMagma customEntity = new CustomEntityMagma(mcWorld);
           
            customEntity.setLocation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
            ((CraftLivingEntity) customEntity.getBukkitEntity()).setRemoveWhenFarAway(false);
            mcWorld.addEntity(customEntity, SpawnReason.CUSTOM);
            return (MagmaCube) customEntity.getBukkitEntity();
        }
    }
    
    The Error Report:
    Code:
    [17:19:01 WARN]: Skipping Entity with id 62
    [17:19:01 WARN]: java.lang.reflect.InvocationTargetException
    [17:19:01 WARN]:        at sun.reflect.GeneratedConstructorAccessor70.newInstanc
    e(Unknown Source)
    [17:19:01 WARN]:        at sun.reflect.DelegatingConstructorAccessorImpl.newInst
    ance(Unknown Source)
    [17:19:01 WARN]:        at java.lang.reflect.Constructor.newInstance(Unknown Sou
    rce)
    [17:19:01 WARN]:        at net.minecraft.server.v1_7_R4.EntityTypes.a(SourceFile
    :166)
    [17:19:01 WARN]:        at net.minecraft.server.v1_7_R4.ItemMonsterEgg.spawnCrea
    ture(ItemMonsterEgg.java:108)
    [17:19:01 WARN]:        at net.minecraft.server.v1_7_R4.ItemMonsterEgg.a(ItemMon
    sterEgg.java:97)
    [17:19:01 WARN]:        at net.minecraft.server.v1_7_R4.ItemMonsterEgg.interactW
    ith(ItemMonsterEgg.java:37)
    [17:19:01 WARN]:        at net.minecraft.server.v1_7_R4.ItemStack.placeItem(Item
    Stack.java:102)
    [17:19:01 WARN]:        at net.minecraft.server.v1_7_R4.PlayerInteractManager.in
    teract(PlayerInteractManager.java:398)
    [17:19:01 WARN]:        at net.minecraft.server.v1_7_R4.PlayerConnection.a(Playe
    rConnection.java:662)
    [17:19:01 WARN]:        at net.minecraft.server.v1_7_R4.PacketPlayInBlockPlace.a
    (PacketPlayInBlockPlace.java:50)
    [17:19:01 WARN]:        at net.minecraft.server.v1_7_R4.PacketPlayInBlockPlace.h
    andle(PacketPlayInBlockPlace.java:86)
    [17:19:01 WARN]:        at net.minecraft.server.v1_7_R4.NetworkManager.a(Network
    Manager.java:186)
    [17:19:01 WARN]:        at net.minecraft.server.v1_7_R4.ServerConnection.c(Serve
    rConnection.java:81)
    [17:19:01 WARN]:        at net.minecraft.server.v1_7_R4.MinecraftServer.v(Minecr
    aftServer.java:734)
    [17:19:01 WARN]:        at net.minecraft.server.v1_7_R4.DedicatedServer.v(Dedica
    tedServer.java:289)
    [17:19:01 WARN]:        at net.minecraft.server.v1_7_R4.MinecraftServer.u(Minecr
    aftServer.java:584)
    [17:19:01 WARN]:        at net.minecraft.server.v1_7_R4.MinecraftServer.run(Mine
    craftServer.java:490)
    [17:19:01 WARN]:        at net.minecraft.server.v1_7_R4.ThreadServerApplication.
    run(SourceFile:628)
    [17:19:01 WARN]: Caused by: java.lang.NullPointerException
    [17:19:01 WARN]:        at com.mclaurence.CustomBoss.Bosses.CustomEntityMagma.aD
    (CustomEntityMagma.java:26)
    [17:19:01 WARN]:        at net.minecraft.server.v1_7_R4.EntityLiving.<init>(Enti
    tyLiving.java:97)
    [17:19:01 WARN]:        at net.minecraft.server.v1_7_R4.EntityInsentient.<init>(
    EntityInsentient.java:39)
    [17:19:01 WARN]:        at net.minecraft.server.v1_7_R4.EntitySlime.<init>(Entit
    ySlime.java:19)
    [17:19:01 WARN]:        at net.minecraft.server.v1_7_R4.EntityMagmaCube.<init>(S
    ourceFile:10)
    [17:19:01 WARN]:        at com.mclaurence.CustomBoss.Bosses.CustomEntityMagma.<i
    nit>(CustomEntityMagma.java:18)
    [17:19:01 WARN]:        ... 19 more
    [17:19:01 WARN]: Skipping Entity with id 62
    >
    I do also have a NMSUtil class however that does not directly effect my Custom Magma so I wont upload it.
    Any help is appreciated greatly :)
    - Thanks
     
Thread Status:
Not open for further replies.

Share This Page