NpcSpawner lib - spawn basic NPCs

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

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

    Maro

    You'll just have to wait until it gets updated.
     
  2. Offline

    jeremytrains

    Found this on google:

    Trying it now. If you notice, the PlayerManager and ItemInWorldManager (which have changed) are listed in there.

    After fixing all of that (above), and changing the lookAtPoint method variable from ay to az, I was able to completely update the library. The only problem is the rename method. Can somebody take a look at that? The method d(net.minecraft.server.v1_4_6.Entity) does not exist in 1.4.6.

    Just found a GREAT website for us. This github repo shows all of the mappings for the craftbukkit-*.jar from version to version. The link is: https://github.com/agaricusb/MinecraftRemapping.

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

    zta192

    If anyone is having issues with the NPC's head not moving in 1.4.6, change the
    Code:
    getEntity().yaw = (float) (newYaw - 90);
    getEntity().pitch = (float) newPitch;
    ((EntityPlayer) getEntity()).ay = (float) (newYaw - 90);
    to
    Code:
    getEntity().yaw = (float) (newYaw - 90);
    getEntity().pitch = (float) newPitch;
    ((EntityPlayer) getEntity()).az = (float) (newYaw - 90);
     
  4. Offline

    jeremytrains

    A few more fixes for 1.4.6:

    The rename method in the NPCManager class is now:
    Code:
    public void rename(String id, String name) {
            if (name.length() > 16) { // Check and nag if name is too long, spawn NPC anyway with shortened name.
                String tmp = name.substring(0, 16);
                server.getLogger().log(Level.WARNING, "NPCs can't have names longer than 16 characters,");
                server.getLogger().log(Level.WARNING, name + " has been shortened to " + tmp);
                name = tmp;
            }
            HumanNPC npc = (HumanNPC) getNPC(id);
            npc.setName(name);
            BWorld b = getBWorld(npc.getBukkitEntity().getLocation().getWorld());
            WorldServer s = b.getWorldServer();
            try {
                Method m = s.getClass().getDeclaredMethod("b", new Class[] {Entity.class}); //jeremytrains: d -> b
                m.setAccessible(true);
                m.invoke(s, npc.getEntity());
                m = s.getClass().getDeclaredMethod("a", new Class[] {Entity.class}); //jeremytrains: c -> a
                m.setAccessible(true);
                m.invoke(s, npc.getEntity());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            s.everyoneSleeping();
        }
    Also, I was looking at the updateEquipment() method in the HumanNPC class, and I think I found a bug. In that method you have the for loop:

    Code:
    for (int i = 0; i < previousEquipment.length; i++) {
                net.minecraft.server.v1_4_6.ItemStack previous = previousEquipment[i];
                net.minecraft.server.v1_4_6.ItemStack current = ((EntityPlayer)getEntity()).getEquipment(i);
                Logger.getLogger("Minecraft").info("current=" + current);
                if (current == null) { continue; }
             
                if (!net.minecraft.server.v1_4_6.ItemStack.equals(previous, current) || (previous != null && !previous.equals(current))) {
                    Logger.getLogger("Minecraft").info("changed");
                    NPCUtils.sendPacketNearby(getBukkitEntity().getLocation(), new Packet5EntityEquipment(getEntity().id, i, current));
                    ++changes;
                } else {
                    Logger.getLogger("Minecraft").info("same as old: new=" + current.toString() + " & old=" + previous.toString());
                }
            }
    (I put in some debugging statements)

    The line in particular that I think might have to be changed is
    Code:
    if (current == null) { continue; }
    That line prevents a packet from being sent if the item in hand was cleared. For example:
    The NPC is holding dirt. I set the item in hand to Material.AIR. The updateEquipment() method is called. It sees that there was a change in the item in hand (dirt -> null). But, since you have the line above in the code - if an item in hand (or armor as well) is removed and set to null, the updateEquipment method does not send the packet telling the clients the equipment has been updated.

    Does that make sense, or am I missing something?


    EDIT: I have found a solution. I got this method to work well as my updateEquipment() method. The problem was is that getEquipment(int) contains item in hand, but getEquipment() does not. The for loop got item in hand, but then the item in hand slot in the previousEquipment array is always the helmet, because previousEquipment.length = 5 and getEquipment().length = 4.

    The solution:

    Code:
    /** Updated by jeremytrains */
        public void updateEquipment() {
            /*
            for (int i = 0; i < previousEquipment.length; i++) {
                net.minecraft.server.ItemStack previous = previousEquipment[i];
                net.minecraft.server.ItemStack current = ((EntityPlayer)getEntity()).getEquipment(i);
                if (previous != current) {
                    NPCUtils.sendPacketNearby(getBukkitEntity().getLocation(), new Packet5EntityEquipment(getEntity().id, i, current));
                    previousEquipment[i] = current;
                }
            }
            */
           
            /**/
            int changes = 0;
            net.minecraft.server.v1_4_6.ItemStack[] newI = new net.minecraft.server.v1_4_6.ItemStack[previousEquipment.length];
            for (int i = 0; i < previousEquipment.length; i++) {
                net.minecraft.server.v1_4_6.ItemStack previous = previousEquipment[i];
                net.minecraft.server.v1_4_6.ItemStack current = ((EntityPlayer)getEntity()).getEquipment(i);
                newI[i] = current;
                if (current == null) {
                    if (previous != null) {
                        NPCUtils.sendPacketNearby(getBukkitEntity().getLocation(), new Packet5EntityEquipment(getEntity().id, i, current));
                        ++changes;
                    }
                } else {
                    if (!net.minecraft.server.v1_4_6.ItemStack.equals(previous, current) || (previous != null && !previous.equals(current))) {
                        NPCUtils.sendPacketNearby(getBukkitEntity().getLocation(), new Packet5EntityEquipment(getEntity().id, i, current));
                        ++changes;
                    }
                }
            }
           
            if (changes > 0) {
                previousEquipment = newI;
            }
            /**/
        }
     
  5. Offline

    Maro

    Could someone please upload a fully updated version of the library? I would be so greatful if someone could do that.
     
  6. Offline

    zta192

    Here is the 1.4.6 update https://dl.dropbox.com/s/89vod1cew2w6u9j/NPCLib 1.4.6.zip?dl=1

    Did you get the problem fixed where NPC's aren't damageable? I am currently have the same issue.

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

    Neodork

    My NPCs are getting damaged just fine.. I'm using: this fork.
     
  8. Offline

    Tormus

    I have the same problem =/

    This version doesn't work for bukkit 1.4.6 =/
     
  9. Offline

    Neodork

    Woops, screwed up the repository with my push. All fixed now.
     
  10. Offline

    jeremytrains

    Neodork
    I was unable to find any differences between the github repo you use and this one, other than optimization of code. If there any specific method I can call to say "make this Entity damagable"? When I punch an NPC, EntityDamage events are not even being called.
     
  11. Offline

    Tormus

    Thank you but NPCs are not getting damages + item in hand is invisible. Have you a solution please ?
     
  12. Offline

    Neodork

    Mhhh haven't been able to locate the problem of the health freezing yet. But I did an update on the ActAsHurt method. The EntityDamageByEntityEvent gets called perfectly fine for me. The only problem is that after the NPC receives his first damage his health will somehow freeze. I'll look into it.

    The item in hand shows fine for me, you might want to call the UpdateEquipment method. Or increase the range for the packet.
     
  13. Are you sure the noDamageTicks are decreased properly?
     
    Neodork likes this.
  14. Offline

    IDragonfire

    These problem also appear with the topcat library. Some server admins means, that these happesn only in world guard regions ...
    Atm I can't reproduce it, maybe some plugin conflicts?
     
  15. Offline

    Neodork

    Thanks for pointing that out to me! Fixed now, if anyone finds something to be incorrect feel free to pull request.
     
  16. Offline

    viruzin

    Hey guys, what is wrong with my code? Is not creating the npc, just returning a message "/ npc" which is the name of the command.Please help! I'm trying for almost a week!

    Code:
    import org.bukkit.Location;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
     
    import org.bukkit.entity.Player;
     
    import org.bukkit.plugin.java.JavaPlugin;
    import com.mortella.plugin.NPCManager;
    import com.mortella.plugin.entity.*;
     
    public class MyPlugin extends JavaPlugin{
        public MyPlugin plugin;
        NPCManager m;
     
     
     
        public void onEnable(){
            plugin = this;
            m = new NPCManager(this);
         
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
     
            Player player = (Player) sender;
            Location l = player.getLocation();
         
            if(cmd.getName().equalsIgnoreCase("npc")){
              m.spawnHumanNPC("test", l);
            }
            return true;
        }
    }
     
  17. Offline

    Neodork

    You need to register to register the commands correctly. Have a look here.
     
  18. Offline

    viruzin

    I found the error :D . I put another plugin with the same command :p . Now is working, thanks for the reply :) .
     
  19. Offline

    Baba43

    Where to download?
     
  20. Offline

    viruzin

  21. Offline

    Baba43

    Thanks.. is it possible to change the skin?
     
  22. Offline

    viruzin

    I think to change the skin you need to use the spoutcraft .. :(
     
  23. Offline

    Baba43

    Also I don't get how to add any handlers to my NPCs.. It seems that this lib is differet to the examples of the start post.
     
  24. Offline

    Neodork

    It is different the OP is outdated. You should explore the code and initialize the manager onEnable.
     
  25. Offline

    Woobie

    This might be kind of a stupid question, but how do I add the classes to my project? Do I have to manually copy & paste the code in the class file, or is there an easier way to do this?
     
  26. Offline

    jeremytrains

    What I did is (I am using Eclipse IDE) manually copy and paste the .java files into my workspace.
     
  27. Offline

    Jameshobbs

    So I have a plan to add npcs as shop keepers.

    Is there a way to prevent the NPCs from despawning? (I have not tested the lib yet, but I had issues when spawning normal mobs that I'd keep track of despawning when no one was around).

    If this is not an issue then if I were to keep track of the unique IDs of the NPCs could I use that to monitor which NPC is interacted with (particularly after a server restart ... such that after a server restart, does the NPC still exist with the same unique ID)?

    Thanks for this lib, it could prove useful for a number of my plugins.
     
  28. Offline

    jeremytrains

    Usually, after a chunk unloads, so do the NPCs. This library automatically re-spawns the NPCs in a chunk when it loads. Each NPC has an ID (a string) that you can set. What I do is make this a String of a Unique number. If you do this, you can (on EntityTargetEvents) check if it is an NPC, get the NPC by its id (stored by your plugin), and then from there you have an action.

    Neodork zta192 and anybody else,
    Also, would the owner of this plugin right now be able to create a BukkitDev page for this? It will be easier to manage downloads and discussions. I will make the page if I have permissions and nobody else would like, just let me/someone else know who to add as authors and former authors.
     
  29. Nice! Any way to let a NPC attack an entity?
     
  30. black_ixx
    I've been doing this in one of my plugins. You have to code the AI for that yourself.
     
Thread Status:
Not open for further replies.

Share This Page