NpcSpawner lib - spawn basic NPCs

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

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

    Top_Cat

    @desmin88 You can do that, can't you? Also why would you need a damage function, you can damage the other entity directly or use NPCEntity.damageEntity(Entity, ammount)
     
  2. Offline

    desmin88

    @Top_Cat
    No, you have to check if it's an instance of HumanEntity, then get the NPCID from the entity, then get the npc, and check if it's not null
    I mean't attack entity as in arm swings and damage according to what the npc has in its hand and firing arrows.
     
  3. Offline

    Qix

    For kekec's NPC Lib and the update to 1.7, change j.Q() in NPCManager to j.R()
     
  4. Offline

    desmin88

    @Kekec852
    Any change on a better pathing system? As of now, it just goes through blocks and does nothing.
     
  5. Offline

    Qix

    @Kekec852 Also, how would one go about straightening out the body after a rotation? I cannot get an NPC to be facing 180 yaw without the body being rotated (the head is fine, though).
     
  6. Offline

    Top_Cat

    @Qix j.Q() updated. For rotation I could never find a better way than rotating 45 degrees past and then rotating back.

    @desmin88 If you mean it teleports instead of pathfinding, it's probably bacause you're making it go to far or to somewhere it thinks is impossible to reach. It teleports to save your server from severe lag. But you can try increasing the maxIterations and it should have a longer reach, if not then a little more info about what you're asking one to do would be nice ;)


    Code:
    if (((CraftEntity) event.getEntity()).getHandle() instanceof NPCEntity) {
        System.out.println("This works");
    }
    Ok, or do you want me to implement a function for this?

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

    desmin88

    @Top_Cat
    Implement a function. That's a bit large to have to do multiple times.
    When doing a path finding, I have an NPC constantly path find to a location of x -3 and z -3 behind me. He just walks through blocks.
     
  8. Offline

    Qix

    @Top_Cat I tried that, but if I want the NPC to be perfectly at 180 yaw, it seems impossible; anything below 180 makes it turn clockwise, anything above 180 make it turn counter-clockwise. I can't get it to constantly turn clockwise to get it to 180+45. :/
     
  9. Offline

    desmin88

    @Qix
    Body rotation isn't determined serverside.
    It's an effect done by the client.
     
  10. Offline

    Qix

    desmin88 exactly; but turning it +45 then -45 will straighten the body out and then the head (usually). That's my issue, however. It's no biggie, just something I would have liked to fix :]
     
  11. Offline

    TAT

    What happens if you rotate over 4 rounds?
    1: +90
    2: +90
    3: +45
    4: -45

    I haven't tried it, but from what I have read until now, that sounds the correct way to do a turn around.
     
  12. Offline

    Qix

    I'll give that a try, @TAT.

    @TAT nope, still turned :/

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

    Top_Cat

    @Qix Are you leaving enough time between changes so the clients receive them?
     
  14. Offline

    killgoblen

    Ok, so where is the updated version of this? Is the one in the OP updated?
     
  15. Offline

    Top_Cat

    Please at least read the latest page of the thread:

     
  16. Offline

    killgoblen

    Ok so I tried this, and I managed to spawn an NPC on my local computer. However, when I tried the exact same code on a remote machine, no NPCs spawned. What am I doing wrong?

    Code:
    Code:
    package me.killgoblen.NPCTest;
    
    import java.util.logging.Logger;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class NPCTest extends JavaPlugin{
        Logger log = Logger.getLogger("Minecraft");
    
        public void onEnable(){
            NPCTestPL pl = new NPCTestPL(this);
            PluginManager pm = this.getServer().getPluginManager();
            pm.registerEvent(Event.Type.PLAYER_INTERACT, pl, Event.Priority.Normal, this);
        }
        public void onDisable(){
            log.info("Yup.");
        }
    
    }
    
    Code:
    package me.killgoblen.NPCTest;
    
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerListener;
    import org.martin.bukkit.npclib.NPCManager;
    
    public class NPCTestPL extends PlayerListener{
        public static NPCTest plugin;
        NPCManager NpcManager;
        int id = 0;
        public NPCTestPL(NPCTest instance){
            plugin = instance;
            NpcManager = new NPCManager(instance);
        }
    
        public void onPlayerInteract(PlayerInteractEvent event){
            if(event.getAction() == Action.RIGHT_CLICK_BLOCK ){
                NpcManager.spawnNPC("killgoblen", event.getClickedBlock().getLocation().add(0.5, 1, 0.5), Integer.toString(id));
                id++;
            }
        }
    
    }
    
    Am I supposed to add the Lib as a referenced library, or just add the classes directly to my project? I added them directly into my project, and, as I said, they spawned fine on my local computer, but not on my remote server. (Both of which have the same CraftBukkit and MC server versions, being 953 and 1.7.2)

    Sorry for my stupid questions, I'm new to this. :p
     
  17. Offline

    Top_Cat

    @killgoblen Questions on usage is cool, as we don't really have any guides written; I should probably do that...

    But yes, just put the code directly into your project. Also I tried that code on my server on 953 and it spawns an NPC just fine so the code isn't the issue. If you tell me a little more I may be able to suggest something but you should just check you've copied everything right.

    Top Cat :)
     
  18. Offline

    killgoblen

    So am I the only one that has this problem? When I spawn an NPC, its coordinates are apparently rounded to the next-lowest integer, so they are standing right on the corner of 4 blocks. I have it so that a command is supposed to spawn an NPC at the exact location of the person sending the command, but it just rounds to the next lowest integer for X, Y, and Z coords.


    P.S. On an unrelated note, I've not used a plugin that uses CraftBukkit as a library. Does that mean I will have to update my plugin with each MC update?

    P.P.S. How would I go about making sure NPCs are still there after the chunk is unloaded?

    P.P.P.S. (I really need to stop doing this.) I assume I should just use ChunkLoadEvent and check if an NPC is in the chunk?
     
  19. Offline

    Top_Cat

    @killgoblen
    1. Your code gets the middle of a block and adds 0.5 to get the corner.
    2. You only have to update as often as a craftbukkit method used in the code is changed, usually only big updates and we make the changes on the git pretty soon after.
    3. Chunks with NPCs in don't unload, they act like players and prevent the server from doing that.
     
  20. Offline

    killgoblen

    @Top_Cat

    I changed my code so that instead of getting a clicked block's location, it gets the player's location, but it still does the rounding.

    And I spawned an NPC and got far enough away for the chunk to unload, then when I came back, it was gone. Also, When I (The only one on the server other than NPCs) log out and back in, the NPCs are gone.
     
  21. Offline

    Top_Cat

    @killgoblen I fixed the rounding bug, but can't reproduce the unloading you're mentioning.
     
  22. Offline

    killgoblen

    @Top_Cat
    I haven't changed any of the code in the NPClib except for adding an import of CraftEntity in the NPCManager. Should I have changed more?

    And how did you fix the rounding?

    And thanks for being so helpful so far!
     
  23. Offline

    Top_Cat

    @killgoblen No problem.

    That import should have already existed and the fixed code is in the github repo :)
     
  24. Offline

    killgoblen

    Hm... Well the import didn't exist. In eclipse it gave me some error, and to fix it, I just imported it. Oh well. To the github! =D

    And out of curiosity, you know all of those methods in CraftBukkit that are just one letter, like a(), b(), etc.? Is there a place where I could find out what those do without digging through source code?
     
  25. Offline

    Top_Cat

    @killgoblen There is no way to without looking through source code as they are renamed to that by mojang and are the methods craftbukkit hasn't changed, which are the methods that aren't documented.
     
  26. Offline

    killgoblen

    Ok thanks.

    So now that I have the basics down, I have some more complicated questions. Is it possible to set an NPCs armor? And is it possible for only certain players to see certain NPCs?

    P.S. In NPCManager.java, I can confirm that there is definitely no import for org.bukkit.craftbukkit.entity. Just sayin' ;)
    And when you said you could not reproduce the NPCs despawning, I really don't get it. I just used the basic NPCManager.spawnNPC(Name Location ID) method and no other code related to NPCs, and they are despawning as I said. Do I need to add some other code?

    Also, I was just messing around with NPCs, and I kept getting this error in the console:

    Code:
    18:44:51 [SEVERE] java.util.ConcurrentModificationException
    18:44:51 [SEVERE]       at java.util.HashMap$HashIterator.nextEntry(HashMap.java:810)
    18:44:51 [SEVERE]       at java.util.HashMap$KeyIterator.next(HashMap.java:845)
    18:44:51 [SEVERE]       at org.martin.bukkit.npclib.NPCManager$1.run(NPCManager.java:33)
    18:44:51 [SEVERE]       at org.bukkit.craftbukkit.scheduler.CraftWorker.run(CraftWorker.java:34)
    18:44:51 [SEVERE]       at java.lang.Thread.run(Thread.java:636)
    
    It came up several times at once, at the same time in the console.
    I'm not quite sure what caused it, but there were no problems in-game. I thought it might have happened when I killed the NPC with my name, but I couldn't re-produce it.
     
  27. Is the most fluent way to make the NPC jump set the y + 1 then set the y back to normal?
     
  28. Offline

    Top_Cat

    @tips48 seems the only logical way to me.

    @killgoblen No idea why that import was missing , you've made me update the code twice now :p and I fell like I am going to need to fixed that error with some synchronisation or stopping it being asynchronous as it's normally caused by a put or remove while it's halfway through a loop. But I can only really fix things if they're reproducible so if you can show me some code that will always cause it I'll be able to find the issue.
     
  29. @Top_Cat oh... :/

    Ok, the biggest problem i have(not really a problem, a *feature request* maybe? :p) is that there is no built in method to make the npc look at a player. Is there any chance you could add that? Please! Or should i just write that myself?

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

    killgoblen

    @Top_Cat:

    I just used exactly the NPCLib code and a simple NPCManager.spawnNPC(), and here's what I've found:

    1. If there are NPCs in the world and the server is reloaded, I can only hit the NPCs once. After that, I simply can't damage them.
    2. Soon after reloading, if more than one NPC of the same name is killed within a short time period, that concurrentModification exception pops up.
    3. After reloading, it seems I have to wait a few seconds before I can damage any NPC.
    4. Upon reloading, there are a bunch of nags about Threads (Async tasks) not being shut down properly.
    5. NPCs despawning when the chunk is unloaded.

    Yup. That's what I've found so far. I don't think I need to give any source code, mine just makes an NPC with a new id each time. Nothing special.

    And also, it appears NPCs can't pathfind up hills. No big deal, I can work around that, but just sayin.

    Ok, so I have decided to make an attempt at making a new pathfinding algorithm. It will probably be extremely inefficient, but it should be a little better than the current one. I will make it follow the example found here. Yes, I'm using wikipedia. XD I'll report back here when I'm done. (Or when I give up! :p)
     
Thread Status:
Not open for further replies.

Share This Page