Custom Entity Class Thingy

Discussion in 'Plugin Development' started by Jacek, Aug 26, 2011.

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

    Jacek

    Based on this post http://forums.bukkit.org/threads/stop-a-mob-from-moving-completely.33393/#post-614382 I have been trying to use this method to make skeletons shoot fire arrows and also ultimately I want to extend their view range. I have been stuck with this one error for about two hours now, it seems that

    Code:
    mcWorld.addEntity(new BloodMoonEntitySkeleton(mcWorld), SpawnReason.NATURAL);
    Causes a NPE, removing the entity worked fine but putting the new one back fails for some reason.

    The full code is

    Code:
        public void onCreatureSpawn(CreatureSpawnEvent event){
            if (event.isCancelled()) return;
    
            Location location = event.getLocation();
            CreatureType creatureType = event.getCreatureType();
            World world = location.getWorld();
    
            net.minecraft.server.World mcWorld = (net.minecraft.server.World) (((CraftWorld) world).getHandle());
            net.minecraft.server.Entity mcEntity = (((CraftEntity) event.getEntity()).getHandle());
    
        //    if (plugin.bloodMoonWorlds.contains(world.getName()) == false) return;
    
            if (creatureType == CreatureType.SKELETON && mcEntity instanceof BloodMoonEntitySkeleton == false){
                mcWorld.removeEntity((net.minecraft.server.EntitySkeleton) mcEntity);
                mcWorld.addEntity(new BloodMoonEntitySkeleton(mcWorld), SpawnReason.NATURAL);
            }
    
            //world.spawnCreature(location, creatureType);
        }
    Any tips would be great :)
     
  2. Offline

    Afforess

    Post the NPE.
     
  3. Offline

    Jacek

    When a skeleton spawns

    Code:
    java.lang.NullPointerException
        at net.minecraft.server.EntityTypes.a(SourceFile:104)
        at net.minecraft.server.Packet24MobSpawn.<init>(SourceFile:29)
        at net.minecraft.server.EntityTrackerEntry.b(EntityTrackerEntry.java:261)
        at net.minecraft.server.EntityTrackerEntry.b(EntityTrackerEntry.java:191)
        at net.minecraft.server.EntityTrackerEntry.scanPlayers(EntityTrackerEntry.java:221)
        at net.minecraft.server.EntityTracker.a(EntityTracker.java:83)
        at net.minecraft.server.EntityTracker.a(EntityTracker.java:66)
        at net.minecraft.server.EntityTracker.track(EntityTracker.java:55)
        at net.minecraft.server.WorldManager.a(WorldManager.java:16)
        at net.minecraft.server.World.c(World.java:887)
        at net.minecraft.server.WorldServer.c(WorldServer.java:105)
        at net.minecraft.server.World.addEntity(World.java:880)
        at uk.co.jacekk.bukkit.BloodMoonEntityListener.onCreatureSpawn(BloodMoonEntityListener.java:50)
        at org.bukkit.plugin.java.JavaPluginLoader$67.execute(JavaPluginLoader.java:712)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:58)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:338)
        at org.bukkit.craftbukkit.event.CraftEventFactory.callCreatureSpawnEvent(CraftEventFactory.java:234)
        at net.minecraft.server.World.addEntity(World.java:855)
        at org.bukkit.craftbukkit.CraftWorld.spawnCreature(CraftWorld.java:327)
        at com.sk89q.commandbook.commands.FunCommands.spawnMob(FunCommands.java:69)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at com.sk89q.minecraft.util.commands.CommandsManager.executeMethod(CommandsManager.java:343)
        at com.sk89q.minecraft.util.commands.CommandsManager.execute(CommandsManager.java:256)
        at com.sk89q.commandbook.CommandBookPlugin.onCommand(CommandBookPlugin.java:245)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:35)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:129)
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:352)
        at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:737)
        at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:701)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:694)
        at net.minecraft.server.Packet3Chat.a(Packet3Chat.java:33)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:89)
        at org.getspout.spout.SpoutNetServerHandler.a(SpoutNetServerHandler.java:434)
        at net.minecraft.server.NetworkListenThread.a(SourceFile:105)
        at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:454)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:363)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:422)
     
  4. Offline

    Afforess

  5. Offline

    Jacek

    Okay, so I was way off then ;)

    Is the a() method you are suggesting to use the one from here https://github.com/Bukkit/mc-dev/blob/master/net/minecraft/server/EntityTypes.java#L15 because that is private :s

    Thanks for explaining the problem though, I honestly don't think I would have ever got there.
     
  6. Offline

    Crash

  7. Offline

    Afforess

    You can use reflection to call private and protected methods, as well as access private or protected fields. I do this all the time. ;)
     
  8. Offline

    Jacek

    /me Goes to google that

    Thanks :D

    I didn't, I'm somewhat of a Java noob, but thanks for the tip.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 19, 2016
  9. Offline

    Afforess

    Join us on Esper net IRC. I am usually in #bukkitdev and always in #spoutdev.
     
  10. Offline

    Jacek

    I have it working now actually ! Plus I would annoy your IRC types with my many typos :D

    Thanks again.

    Also, so not be selfish...

    This is the code I used

    Code:
            try {
                Class[] args = new Class[3];
                args[0] = Class.class;
                args[1] = String.class;
                args[2] = int.class;
    
                Method a = net.minecraft.server.EntityTypes.class.getDeclaredMethod("a", args);
                a.setAccessible(true);
                a.invoke(a, BloodMoonEntitySkeleton.class, "Skeleton", 51);
            } catch (Exception e){
                e.printStackTrace();
            }
    Seems to work, maybe wrong in some technical way.

    It seems you can basically do anything too, so far I have fire arrows and increased fire rate.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 19, 2016
  11. Offline

    Afforess

    @Jacek

    Looks perfect. Make sure you add a link to your plugin when you are done :p
     
  12. Offline

    Jacek

    Will do :D
     
  13. Offline

    Jacek

Thread Status:
Not open for further replies.

Share This Page