NpcSpawner lib - spawn basic NPCs

Discussion in 'Resources' started by Redecouverte, Feb 3, 2011.

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

    okami35

    Thank you for your answer, but the method getHandle doesn't existe for the type Entity...
     
  2. Offline

    jeremytrains

    Was stumped on the same problem actually! The getEntity() method returns a CraftPlayer, not an NPCEntity. (event AND nevent return a CraftPlayer). You can then create something like:

    Code:
    CraftPlayer npc = event.getEntity();
    
    and then use
    Code:
    [your_npc_manager].getNPC([your_npc_manager].getNPCIdFromEntity(event.getEntity()));
    where [your_npc_manager] is you NPCManager. This should work.
    The code above will give you the NPCEntity.
     
  3. Offline

    okami35

    Thank you it works perfectly.
     
  4. Offline

    rings_akira

    I'm new to this lib so propably this would be a dumb question.
    This npc entities are in reality Player entities from bukkit (extending that class) ?

    I ask this because in Spout you need a Player entity to do SpoutPlayer splayer = SpoutManager.getPlayer(player); and then be able to change skins etc
    But with npclib I get HumanEntity and I am unable to figure it out how to combine these two (Spout and npclib)
    I've tried getBukkitEntity() without any results.

    Thanks in advance
     
  5. Offline

    r3Fuze

    If you look a bit at this line you'll probally figure something out.
     
  6. Offline

    orange451

    Will this come out for Minecraft 1.0.0 soon?
    CraftBukkit changed the Entity.aa() method.
     
  7. Offline

    r3Fuze

    Just change aa to af (commit)
     
  8. Offline

    orange451

    Is there a way to fix the damaging?
    I've noticed that the NPC's still execute an EntityDamage event, but they do not take damage.
    I've tried settinging their health, but the player, visually, doesn't know they're killing an NPC.
     
  9. Offline

    Trc202

    Odd, every npc I have takes damage just fine (dies too). The only thing I have noticed they don't do is move backward when hit by something (a feature/bug I like).
     
  10. Offline

    orange451

    Maybe that's it...
     
  11. Offline

    Abrupt

    @Top_Cat
    consider adding this function to your NPCManager class, it's been useful to me:
    Code:Java
    1. public String getNPCIdFromEntity(NPCEntity e) {
    2. if(e instanceof EntityPlayer) {
    3. for(String i : npcs.keySet()) {
    4. if(npcs.get(i).getBukkitEntity().getEntityId() == ((EntityPlayer)e).getBukkitEntity().getEntityId()) {
    5. return i;
    6. }
    7. }
    8. }
    9. return null;
    10. }
     
  12. Offline

    Trc202

    It's already there.
     
  13. Offline

    Abrupt

    no it isn't. that's for regular Entities. NPCEntities can not be cast to regular Entities.
     
  14. Offline

    Top_Cat

    Yes, yes you can...
    Code:
    getNPCIdFromEntity(npcEntity.getBukkitEntity());
     
  15. Offline

    Abrupt

    oh, duh. I have no idea how I didn't think about that, I even used it in my function. my bad.
     
  16. Offline

    Top_Cat

    ***Notice***
    Spout update! Woot!
    With the latest source, available at https://github.com/Top-Cat/NPCLib you can get the spout player for an npc with which you can set the skin and cape. The method itself is safe on non-spout servers, however it will cause an error when you try and use the return. Therefore I recommend you check if spout is on the server and possibly create a soft dependency. You can check for spout reliably (as long as you have a soft depend) like this:

    Code:
    try {
        Class.forName("org.getspout.spout.Spout");
        //Spout on server
    } catch (ClassNotFoundException e) {
        //Spout not on server
    }
     
  17. Offline

    rings_akira

    First of all, thanks for the lib ;)
    I have some questions.

    1.- Top_cat, I've recently downloaded the last source and the cosntructor of NPCManager says that plugin must be cast to the type Server. Is this an error?
    Code:
    public NPCManager(JavaPlugin plugin) {
            server = BServer.getInstance((Server) plugin);
    .
    .
    .
    Also, does this new version require that I add the spoutapi to my project in Eclipse AND the one from spout?

    Until now, I've only needed the spoutapi one, and I've used snippets like
    Code:
    NPCEntity npc = plugin.nm.spawnNPC("MyName", location);
    SpoutPlayer splayer = SpoutManager.getPlayer(playerWhoTriggersEvent);
    splayer.setEntitySkin((LivingEntity) npc.getBukkitEntity(), "URL", EntitySkinType.DEFAULT);
    to set skins. What advantage does this last source provide?

    2.- I'vefound myself in need of a function to set the item in hand of my NPC's but suppotring wool color (damage/durability of the item). Adding this to NPCEntity.java

    Code:
    public void setItemInHand(Material m,short damage) {
            ((HumanEntity) getBukkitEntity()).setItemInHand(new ItemStack(m, 1,damage));
        }
    did "the trick" for me.

    Thanks

    For the first part of my previous point, I had to leave NPManager as in the source (without the cast) and add this to Bserver.java:
    Code:
    public static BServer getInstance(JavaPlugin pl) {
            if (ins == null) {
                ins = new BServer(pl);
            }
            return ins;
        }
        private BServer(JavaPlugin plugin) {
            //Getting neede structures
            server = plugin.getServer();
            try {
                cServer = (CraftServer) server;
                mcServer = cServer.getServer();
            } catch (Exception ex) {
                Logger.getLogger("Minecraft").log(Level.SEVERE, null, ex);
            }
            //end
        }
    that were on the previous version.

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

    Top_Cat

    Your change is wrong, just change
    server = BServer.getInstance((Server) plugin); -> server = BServer.getInstance();

    My mistake, but your fix is unnecessary

    Didn't think about that, but yes you need to import spout api (http://spout.in/pluginapidev) for this to compile. Although as mentioned spout is not needed on the server unless you're using these features.

    This code allows you to set the skin and clock of the npc not the player.

    As for 2, I've added that method (implemented slightly better) :)

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

    rings_akira

    Thank you Top_Cat for all the help :)
    I downloaded again your new source but since I've also upgraded my bukkit jar I feel there's been some changes on the bukkit code that are causing me some problems, I'll deal with that later.

    Refering to one of my previous issues, I did indeed add the spout api jar but, in your imports you do have:
    Code:
    import org.getspout.spout.player.SpoutCraftPlayer;
    import org.getspout.spoutapi.player.SpoutPlayer;
    and while the 2nd one resolves correctly, the first does not. So I have to add spoutapi AND spout jars and I'm wondering if that's correct.

    P.D.: Sorry about the "fix thing" in my first comment, I hope I didn't sound like a smart ass or something, or that I was implying a bad source code from your part :p (I'm not fluent in english as I'd like)
     
  20. Offline

    orange451

    Can you try to fix the damaging of NPC's?
    Since 1.0.0 bukkit, they no longer work :/
     
  21. Offline

    Top_Cat

    I just fixed and tested killing an NPC. Since my spout update they were in creative mode.
     
  22. Offline

    orange451

    You sir, have made my day.
    [EDIT]
    I downloaded the NPClib that you update 8 hours ago
    I still cannot amage my NPC's... I had to take out the SpoutPlayer stuff however, because I don't want to use spout in my plugin.
     
  23. Offline

    Top_Cat

    You can leave the code, it won't cause any errors if spout isn't on the server...
    If damaging doesn't work then another plugin is interfering or pvp is turned off. It works, so it's you not me.
     
    r3Fuze likes this.
  24. Offline

    orange451

    It's the only plugin I have in my server...
    Which worked fine before 1.0.0
     
  25. Offline

    Trc202

    Are you spawning the npc in spawn (pvp protected area)?
     
  26. Offline

    orange451

    Nope
    But it just randomely started working again, I didn't change anything...
    odd...
    Thanks again for the lib, top_cat :3
     
  27. Offline

    GarretSidzaka

    orange that error you and me talked about is still there and its crashing the shit out of my server.
     
  28. Offline

    Top_Cat

    Care to elaborate?
     
  29. Offline

    GarretSidzaka

    ill let orange explain it he much much smarter than me at code.
     
  30. Offline

    orange451

    I have, last time I asked you to fix it, remember? You told me to send my source code to you, and you never responded back...
    Code:
    2011-12-14 18:08:14 [SEVERE] java.lang.NullPointerException
    2011-12-14 18:08:14 [SEVERE]  at net.minecraft.server.World.getEntities(World.java:990)
    2011-12-14 18:08:14 [SEVERE]  at net.minecraft.server.Entity.move(Entity.java:417)
    2011-12-14 18:08:14 [SEVERE]  at net.minecraft.server.EntityItem.w_(EntityItem.java:64)
    2011-12-14 18:08:14 [SEVERE]  at net.minecraft.server.World.entityJoinedWorld(World.java:1253)
    2011-12-14 18:08:14 [SEVERE]  at net.minecraft.server.WorldServer.entityJoinedWorld(WorldServer.java:107)
    2011-12-14 18:08:14 [SEVERE]  at net.minecraft.server.World.playerJoinedWorld(World.java:1235)
    2011-12-14 18:08:14 [SEVERE]  at net.minecraft.server.World.tickEntities(World.java:1142)
    2011-12-14 18:08:14 [SEVERE]  at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:518)
    2011-12-14 18:08:14 [SEVERE]  at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:425)
    2011-12-14 18:08:14 [SEVERE]  at net.minecraft.server.ThreadServerApplication.run(SourceFile:457)
    2011-12-14 18:08:14 [SEVERE] Unexpected exception
    java.lang.NullPointerException
     at net.minecraft.server.World.getEntities(World.java:990)
     at net.minecraft.server.Entity.move(Entity.java:417)
     at net.minecraft.server.EntityItem.w_(EntityItem.java:64)
     at net.minecraft.server.World.entityJoinedWorld(World.java:1253)
     at net.minecraft.server.WorldServer.entityJoinedWorld(WorldServer.java:107)
     at net.minecraft.server.World.playerJoinedWorld(World.java:1235)
     at net.minecraft.server.World.tickEntities(World.java:1142)
     at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:518)
     at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:425)
     at net.minecraft.server.ThreadServerApplication.run(SourceFile:457)
     
Thread Status:
Not open for further replies.

Share This Page