[LIB] CitizensAPI - Create NPCs quickly and easily [MC 1.8.x]

Discussion in 'Resources' started by fullwall, Feb 6, 2013.

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

    fullwall

    Ultimate_n00b - make the NPC have the Inventory trait and refresh its contents every so often.
    Code:
    npc.addTrait(Inventory.class);npc.addTrait(Equipment.class);
     
  2. Offline

    Ultimate_n00b

    Yeah I figured it out, you had some helpful people in IRC.
     
  3. Offline

    TrilisconPvP

    fullwall i'm making a Sleepers plugin. When a player logs out it will create an NPC (Sleeper). If the sleeper get's killed it will drop the player that logged off's inventory and if the player didn't die i need to remove the Sleeper. can you help me? this is what i have so far not much

    Code:java
    1. package pureplugins.sleepers;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import net.citizensnpcs.api.CitizensAPI;
    6. import net.citizensnpcs.api.npc.NPC;
    7. import net.citizensnpcs.api.npc.NPCRegistry;
    8.  
    9. import org.bukkit.Bukkit;
    10. import org.bukkit.ChatColor;
    11. import org.bukkit.entity.EntityType;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.event.EventHandler;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.event.player.PlayerJoinEvent;
    16. import org.bukkit.event.player.PlayerQuitEvent;
    17. import org.bukkit.inventory.Inventory;
    18. import org.bukkit.plugin.java.JavaPlugin;
    19.  
    20.  
    21. public class Sleepers extends JavaPlugin implements Listener{
    22.  
    23. HashMap<Player, Inventory> PlayerLoadout = new HashMap<Player, Inventory>();
    24. NPCRegistry registry = CitizensAPI.getNPCRegistry();
    25.  
    26. @Override
    27. public void onEnable(){
    28. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    29. }
    30.  
    31. @Override
    32. public void onDisable(){
    33.  
    34. }
    35.  
    36. @EventHandler
    37. public void CreateSleeper(PlayerQuitEvent evt){
    38. Player player = evt.getPlayer();
    39. String name = player.getName();
    40. Inventory load = player.getInventory();
    41. PlayerLoadout.put(player, load);
    42. NPC npc = registry.createNPC(EntityType.PLAYER, ChatColor.DARK_RED+"[s]"+name);
    43. npc.spawn(player.getLocation());
    44. npc.setProtected(false);
    45.  
    46. }
    47.  
    48. @EventHandler
    49. public void RemoveSleeper(PlayerJoinEvent evt){
    50. Player player = evt.getPlayer();
    51. String name = player.getName();
    52. PlayerLoadout.remove(player);
    53. }
    54.  
    55. }[/s]
     
    Kyorax likes this.
  4. Offline

    Ultimate_n00b

    Store the NPC in the HashMap (possibly make an object to hold the NPC and the inventory?) then call the .remove() or .destroy() method when he logs in.
     
  5. Offline

    TrilisconPvP

    Ultimate_n00b how would i store the npc in a hashmap? can i get some code?
     
  6. Offline

    Ultimate_n00b

    You posted this in the citizens thread, so I helped you with the citizens part of it. If you want help coding, I suggest watching a few videos or reading some books. Going the the Plugin Development sub-forum works as well I suppose.
     
    Goblom likes this.
  7. Offline

    ThePowerPlay

    @Ultimate_n00b u know so much, can you help me: How can i make the NPC Jump or Sneak or Leftclick(hit)
     
  8. Offline

    fullwall

    ThePowerPlay - you can make the NPC jump using bukkit -
    Code:
    npc.getEntity().setVelocity(npc.getEntity().getVelocity().add(0,0.4,0));
    The animations you mentioned are available through PlayerAnimation in the Citizens2 project.
     
  9. Offline

    HeavyMine13

    How can I make a Villager that: 1.Does not move 2.On right click executes code?
     
  10. Offline

    fullwall

    HeavyMine13

    Code:
    NPC npc =CitizensAPI.getNPCRegistry().createNPC(EntityType.VILLAGER, "name");
    npc.spawn(loc);
     
    @EventHandler
    public void onRightClick(NPCRightClickEvent event) {
       //code
    }
     
    
     
    HeavyMine13 likes this.
  11. Offline

    LCastr0

    Is there a way to make it not disappear the mob's nametag? Like, when we create a player's npc, it keeps the nametag even if you're not looking, but creating with the api only shows the name when you're looking
     
  12. Offline

    Ultimate_n00b

    npc.getBukkitEntity().setCustomNameVisible(true) LCastr0
     
  13. Offline

    LCastr0

    Ultimate_n00b Thanks!

    Ultimate_n00b one last question... After I reload/restar the server, the nametag disappears... is there a way to make it doesn't disappear after the restart?

    Ultimate_n00b any ideas? :(

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

    Ultimate_n00b

    LCastr0 Please stop spam tagging me. As well, have you tried to set it every time the server starts up? You could always create a trait.
     
  15. Offline

    fullwall

    LCastr0 - if you're using a current version of Citizens, you can call
    Code:
    npc.data().setPersistent(NPC.NAMEPLATE_VISIBLE_METADATA, true);
     
  16. Offline

    LCastr0

    Don't work, stays removing the nametag after the server reload, or after the player leaves the server.

    How would I use a trait to make it save the nametags?
     
  17. Offline

    fullwall

    LCastr0 - try with a value of false.
     
  18. Offline

    LCastr0

    Nope, still disappearing.
     
  19. Offline

    Scizzr

    fullwall - Been using this since the old days, but just now got started with messing with it's API due to custom needs. I'm making a pets plugin and using Citizens as the entity management plugin, since I would rather not have to rewrite what's already been done. What are your thoughts on this idea?

    I don't want the global NPCRegistry (ie: /npc list) to be polluted with tons of pet IDs. What I've done is made a new registry for each player - as needed - with the registry name being their player name, and am putting their pets there and deregistering all of their pets when they summon a new one. Is this a good way to go about my desired goal?

    Here's some sample code:
    Code:
    NPCRegistry registry = CitizensAPI.getNamedNPCRegistry(player.getName());
    if (registry == null) {
        registry = CitizensAPI.createNamedNPCRegistry(player.getName(), new PetDataStore());
    } else {
        registry.deregisterAll();
    }
     
    NPC npc = registry.createNPC(EntityType.BAT, "Pet Bat");
    npc.spawn(player.getLocation());
    npc.getNavigator().setTarget((Entity)player, false);
    npc.setFlyable(true);
    
    Also as a side question, is there a way to list their pets in-game (such as "/npc list <registryname>" or "/npc list --registry <registryname>")? If not, that would be a nice addition.

    Finally, I've made a small pull request to fix "/npc rename [name]" not translating color codes such as &2 and whatnot. https://github.com/CitizensDev/Citizens2/pull/27

    Thanks! :)
     
  20. Offline

    fullwall

    Scizzr - thanks for your PR and feature request, I'll implement it tonight (probably in the --registry form as there's some args in the other place already). Your way looks like a good way to do it, bear in mind that you will need to call the data store methods if you don't want it to be in-memory. I'm looking for a good way to simplify that process so if you have any API suggestions I'm happy to do them.

    @LCastro - http://wiki.citizensnpcs.co/API#Creating_a_Trait
     
  21. Offline

    Scizzr

    Yeah, I figured that as much and just made a custom data store that essentially does nothing. The reason is that I'm deregistering all of the pets in the player's datastore when they pick a new one, so there's no need to have them save.
    Code:
    public class PetDataStore implements NPCDataStore {
        @Override
        public void clearData(NPC npc) {}
     
        @Override
        public int createUniqueNPCId(NPCRegistry registry) {
            return 0;
        }
     
        @Override
        public void loadInto(NPCRegistry registry) {}
     
        @Override
        public void saveToDisk() {}
     
        @Override
        public void saveToDiskImmediate() {}
     
        @Override
        public void store(NPC npc) {}
     
        @Override
        public void storeAll(NPCRegistry registry) {}
    }
    
    In the end, all I had to do was make a new NPCRegistry for each player, and a custom PetDataStore(extends NPCDataStore) for each PetRegistry so that I didn't save the pets to disk. Was pretty easy to work from the place where I needed custom functionality, so I can't really suggest any API changes for that. :)
     
  22. Offline

    LCastr0

    I've seen it already, but it just made me more confused. How can I set a trait for it's name??

    Nvm, fixed it! I did a repeater that makes all the npcs in a config have the nametag back :D

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

    ThePowerPlay

    fullwall How can i get the player in the PlayerAnimate?
     
  24. Offline

    fullwall

    ThePowerPlay - use
    Code:
    (Player)npc.getEntity()
    if you know that the NPC is a player.
     
  25. Offline

    ThePowerPlay

    fullwall


    I meaned that when a player in a radius of 5 Blocks sneaks or Jumps, the npc also sneaks or Jumps.
    Can you help me with this? c:
     
  26. Offline

    fullwall

    Sneaking:
    Code:
    PlayerAnimation.SNEAK.animate((Player)npc.getEntity());
    Jumping:
    Code:
    npc.getEntity().setVelocity(npc.getEntity().getVelocity().add(0,0.42,0));
     
  27. Offline

    guitargun

    fullwall
    I want my npc to walk a course I made with some locations points. I stored it all in a Locations[] when I go through this array my npc onyl picks a random point and then stops. any ideas? how I can set this course. Also when the npc had reach the end I goes back or just goes to the beginn point
     
  28. fullwall
    if I use
    Code:java
    1. npc.getNavigator().setTarget(Bukkit.getWorlds().get(0).getSpawnLocation()); // walk to a point

    The NPC doesn't walk to the location, he gets teleported but I want him to walk,
    What am I doing wrong?
     
  29. Offline

    fullwall

    guitargun - probably an error with your code. Can you pastie it?
    bluegru - that means that the NPC couldn't find a path to the target.
     
  30. There's definitely a way,
    Is there a max range for that?

    Because it works if the location is not so far away
     
Thread Status:
Not open for further replies.

Share This Page