NpcSpawner lib - spawn basic NPCs

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

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

    Redecouverte

    NpcSpawner lib - spawn basic NPCs
    version 1.6

    *this is a temporary fix to spawn NPCs with bukkit*

    -> Add craftbukkit as a lib to your project
    -> Add the classes from the zip to your project

    You may now spawn npcs like that:

    Code:
    import redecouverte.npcspawner.BasicHumanNpc;
    import redecouverte.npcspawner.BasicHumanNpcList;
    import redecouverte.npcspawner.NpcSpawner;
    
        public BasicHumanNpcList HumanNPCList;
    
        this.HumanNPCList = new BasicHumanNpcList();
    
    
                Player player = (Player) sender;
                Location l = player.getLocation();
    
    
                // create npc-id npc-name
                if (subCommand.equals("create")) {
                    if (args.length < 3) {
                        return false;
                    }
    
                    if (this.HumanNPCList.get(args[1]) != null) {
                        player.sendMessage("This npc-id is already in use.");
                        return true;
                    }
    
                    BasicHumanNpc hnpc = NpcSpawner.SpawnBasicHumanNpc(args[1], args[2], player.getWorld(), l.getX(), l.getY(), l.getZ(), l.getYaw(), l.getPitch());
                    this.HumanNPCList.put(args[1], hnpc);
    
                    ItemStack is = new ItemStack(Material.BOOKSHELF);
                    is.setAmount(1);
                    hnpc.getBukkitEntity().setItemInHand(is);
    
    
                // attackme npc-id
                } else if (subCommand.equals("attackme")) {
    
                    if (args.length < 2) {
                        return false;
                    }
    
                    BasicHumanNpc npc = this.HumanNPCList.get(args[1]);
                    if (npc != null) {
                        npc.attackLivingEntity(player);
                        return true;
                    }
    
                // move npc-id
                } else if (subCommand.equals("move")) {
                    if (args.length < 2) {
                        return false;
                    }
    
                    BasicHumanNpc npc = this.HumanNPCList.get(args[1]);
                    if (npc != null) {
                        npc.moveTo(l.getX(), l.getY(), l.getZ(), l.getYaw(), l.getPitch());
                        return true;
                    }
    
                // spawnpig
                } else if (subCommand.equals("spawnpig")) {
                    NpcSpawner.SpawnMob(MobType.PIG, player.getWorld(), l.getX(), l.getY(), l.getZ());
                }

    Example event callbacks:

    Code:
    import org.bukkit.event.entity.*;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.HumanEntity;
    import redecouverte.npcspawner.BasicHumanNpc;
    import redecouverte.npcspawner.NpcEntityTargetEvent;
    import redecouverte.npcspawner.NpcEntityTargetEvent.NpcTargetReason;
    import redecouverte.npcspawner.NpcSpawner;
    
        @Override
        public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
    
            if (event.getEntity() instanceof HumanEntity) {
                BasicHumanNpc npc = parent.HumanNPCList.getBasicHumanNpc(event.getEntity());
    
                if (npc != null && event.getDamager() instanceof Player) {
    
                    Player p = (Player) event.getDamager();
                    p.sendMessage("<" + npc.getName() + "> Don't hit me so much :P");
    
                    NpcSpawner.RemoveBasicHumanNpc(npc);
                    parent.HumanNPCList.remove(npc.getUniqueId());
    
                    event.setCancelled(true);
    
                }
    
            }
        }
    
        @Override
        public void onEntityTarget(EntityTargetEvent event) {
    
            if (event instanceof NpcEntityTargetEvent) {
                NpcEntityTargetEvent nevent = (NpcEntityTargetEvent)event;
    
                BasicHumanNpc npc = parent.HumanNPCList.getBasicHumanNpc(event.getEntity());
    
                if (npc != null && event.getTarget() instanceof Player) {
                    if (nevent.getNpcReason() == NpcTargetReason.CLOSEST_PLAYER) {
                        Player p = (Player) event.getTarget();
                        p.sendMessage("<" + npc.getName() + "> Hello friend, I'm an NPC!");
                        event.setCancelled(true);
    
                    } else if (nevent.getNpcReason() == NpcTargetReason.NPC_RIGHTCLICKED) {
                        Player p = (Player) event.getTarget();
                        p.sendMessage("<" + npc.getName() + "> You right-clicked me!");
                        event.setCancelled(true);
    
                    } else if (nevent.getNpcReason() == NpcTargetReason.NPC_BOUNCED) {
                        Player p = (Player) event.getTarget();
                        p.sendMessage("<" + npc.getName() + "> Stop bouncing on me!");
                        event.setCancelled(true);
                    }
                }
            }
    
        }
    Events
    - Player attacks / left clicks npc:
    onEntityDamageByEntity(EntityDamageByEntityEvent event)

    - Player right clicks npc:
    onEntityTarget(EntityTargetEvent event)
    event will be an instance of NpcEntityTargetEvent, with getNpcReason() == NpcTargetReason.NPC_RIGHTCLICKED

    - Player bounces against npc;
    onEntityTarget(EntityTargetEvent event)
    event will be an instance of NpcEntityTargetEvent, with getNpcReason() == NpcTargetReason.NPC_BOUNCED

    - Player gets near npc: *experimental*, not yet tested how it acts for lots of players passing by
    onEntityTarget(EntityTargetEvent event)
    event will be an instance of NpcEntityTargetEvent, with getNpcReason() == NpcTargetReason.CLOSEST_PLAYER

    Features
    + supports setItemInHand(), moveTo(), attackLivingEntity() and animateArmSwing()
    + has its own inventory [untested]

    npcspawner1.5.png


    Changelog:
    1.6:
    - renamed namespace to comply with bukkit policy
    1.5:
    - removed BasicAnimalNpc (its obsolete now)
    - added right-clicked event to BasicHumanNpc
    - added bounced event to BasicHumanNpc
    - added nearest entity event to BasicHumanNpc
    - added SpawnMob() function to spawn normal mobs
    - updated examples and example plugin
    1.41:
    - optimized function parameters
    1.4:
    - added unique ids to npc and list classes
    - added moveTo() to both npc types
    - added attackLivingEntity() and animateArmSwing() to human npc
    - updated example
    1.3:
    - the spawn functions now also take yaw and pitch
    - the spawn functions now take doubles instead of ints for coords
    1.2:
    - fixed BasicHumanNpcList and BasicAnimalNpcList
    - updated examples
    1.1
    - fixed EntityTargetEvent event for BasicAnimalNpc
    - added BasicHumanNpc
    1.0
    - initial release
     

    Attached Files:

  2. Offline

    highz

    Lol this is very nice!
    I gonna take a look at this
     
  3. Offline

    Redecouverte

    thanks :)

    I finally managed to display the names above the head :)
     
  4. Offline

    Crash

    You're a lifesaver !
    Thanks a lot !
     
  5. Offline

    Jimpi

    thanks dude, trying to use your fix, to finally get a port of the npcs-plugin. I dont have the time for developing temporary-solutions... Glad that you got time.

    Cheers
    Jimpi
     
  6. Offline

    Redecouverte

    nice, this will be awesome :)
    --- merged: Feb 4, 2011 8:43 PM ---
    next version will feature right-click and target event for humannpc! (release in 1hour) :)
    --- merged: Feb 4, 2011 10:23 PM ---
    1.5 is online! 2 new events!
     
  7. Offline

    Derek Peterson

    Nice! I don't imagine we have any control over the npcs skin?
     
  8. Offline

    Redecouverte

    which skin to display is decided by the client, and fully depends on the name (so give it an existing player name and it will have its skin)
    currently this cannot be influenced from server-side
     
  9. Offline

    unkzdomain

    Nice. Would be awesome-r if we could change the skin and create custom mobs, but if there's no way to intercept the call for the skin, I guess there's nothing you can do about that. [​IMG]
     
  10. Offline

    Redecouverte

    what I could do right now, is allowing npcs to use animal/monster skins, but then they would have no name displayed above their head, which is not that useful (you couldn't tell if its a normal animal/monster or npc)
     
  11. Offline

    Tannz0rz

    Excellent work! I might just have to put this to use and will give you some [hopefully] positive results.
     
  12. Offline

    Nylez

    So i cant get this to work. Also, eclipse reports your example to be full of errors. Any way to fix this?
     
  13. Offline

    Redecouverte

    make sure you set the package names right ("redecouverte.npcspawner" and "redecouverte.basicnpcs" and add craftbukkit and bukkit as lib
    --- merged: Feb 9, 2011 9:39 PM ---
    if it still doesn't work, feel free to post the errors here, and i'll take a look at them
     
  14. Offline

    MatCat

    Has inventory been tested? How many items can an NPC hold?
     
  15. Offline

    Waffletastic

    How exactly do we install this? I am confused. and also does it use ingame commands to spawn npcs?
     
  16. Offline

    Redecouverte

    i did a few tests with it and it seems to work, its basically identically with a player's inventory

    this is a library for developers only, so that they can build upon it
    it has a few commands, but these are intended for testing only (there's also no save function included)
     
  17. Offline

    connorcpu

    If anyone's interested for their NPCs, I've invested in another account so that you all can have a police officer skin :D
    Just name the npc "Police__Officer" (Yes, that is two underscores) The clients will automatically use this skin :D
     
    efstajas and DKDunnings like this.
  18. Offline

    Rurikar22

    Is there anyway you can not have the NPC name above there head, or better yet let us use a field to get the NPC skin from a users name and then just give it a completely new name. For example, connor here made an account to have a Police Officer, but could you make it so it uses that players skin and then I can give it a seperate name like "Policeman bob"
     
  19. Offline

    QQCucumber

    Nope.
     
  20. Offline

    PrivateAlpha

    actually it *might* be possible with player.setDisplayname().... thats a big *might* though
     
  21. Offline

    connorcpu

    Skins are handled by the client based on the human entity's display name, so it would require a client mod to force the client to use a skin other than the one associated with the display name.
     
  22. Offline

    kronflux

    I'm having a really awkawrdly difficult time getting this to run. it compiles just fine, but then when I go to actually use it, the only command that works is "/bnpc" (using the example plugin)
    which proceeds to print out "/bnpc" to the chat, in red.
    and when I use "/bnpc create 1 name" it crashes the server entirely! not even an error, the server just disappears. I'd love if someone could send me a compile of this. I just need it to spawn some dummy players temporarily :p
     
  23. Offline

    kovarex

    It works correctly, but why isn't this part of the standard library?
     
  24. Offline

    petterroea

    You can bee looking forward to a war plugin arriving soon from me
     
  25. Offline

    Dere011

    Thank you !
     
  26. Offline

    capsup

    Is there an actual way to make these npc's move like any other entity in the game such as the zombies, or will I have to come up with an algorithm that calculates that path and makes extensive use of moveTo?
     
  27. Offline

    Redecouverte

    currently there is only moveTo()

    depending on when bukkit releases its own npc functions i will add this in about 10 days
     
  28. Offline

    capsup

    Okay, cool. I wanted to make a kind of guard npc with this, just wanted to know if my job had been made even easier, hehe. Thanks for this library though.
     
  29. Offline

    Gesh

    Making a Guard Plugin using this, thanks, working on movement at the moment, got any tips?
     
  30. Offline

    exolius

    How would I make one of the npcs follow me, I have it so they move 1 block away from my position with the command "/bnpc move ID", but from there im stuck
     
Thread Status:
Not open for further replies.

Share This Page