[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

    bluegru - yes, there's a max range configurable both in the settings and in the pathfinder config.
     
  2. fullwall
    thx,
    and is it possible to make the NPC attackable?
    So that the player can kill the NPC
     
  3. Offline

    LCastr0

    I'm having a problem with the nametags again... The runnable throws a lot of errors, so I decided to make it again. How can I set the nametag for a NPC everytime the server loads? Also, how would I make it not disappear after a player leaves the server?
    I don't know how to use traits, and the tutorial on the wiki is very confusing. If someone could help me, I would be very happy.

    #Edit: Nevermind, fixed! :D
     
  4. Offline

    guitargun

    fullwall
    sorry for late answer but I found the problem. now I use an different approach wich I like better.
     
  5. guitargun
    You could post your solution,
    Other may have the same problem
     
  6. Offline

    fullwall

    guitargun - yes, simply use npc.data().set(NPC.DEFAULT_PROTECTED_METADATA, false);
     
  7. Offline

    Serold

    fullwall
    My Citizens plugin doesn't work!
    I tried to re-install it already, didn't work.

    http://pastebin.com/7aU7RBST

    It says I have to downgrade or something, yesterday evening it did work.
    Today I joined my MC-Server, and it didn't work.
    I didn't upgrade anything :O

    Any solutions?
     
  8. Offline

    guitargun

    bluegru
    I made the npc walk in a random direction in a set radius
    Code:java
    1. NPCRegistry registry = CitizensAPI.getNPCRegistry();
    2. final NPC npc = registry.getNPC(npclist.get(npcname).getEntity());
    3. final Location startFrom = npcloc.get(npcname);
    4. final Random rndGen = new Random();
    5. final int[] values = new int[3];
    6.  
    7.  
    8. int id = Bukkit.getScheduler().scheduleSyncRepeatingTask(Bukkit.getPluginManager().getPlugin("NPCWalker"), new Runnable(){
    9. public void run(){
    10. for(int i = 0; i < 3; i++) {
    11. values[I] = rndGen.nextInt(4) + 1;
    12.  
    13. }
    14. Location output = startFrom.clone();
    15. Random x = new Random();
    16. int y = x.nextInt(3) + 1;
    17. switch(y){
    18. case 1:
    19. output.add(values[0], 0, values[2]);
    20. break;
    21. case 2:
    22. output.subtract(values[0], 0, values[2]);
    23. break;
    24. case 3:
    25. int k = values[0] - values[0]*2;
    26. output.add(k, 0, values[2]);
    27. break;
    28. case 4:
    29. int l = values[2] - values[2]*2;
    30. output.add(values[0], 0, l);
    31. break;
    32. }
    33. npc.getNavigator().setTarget(output);
    34. npcloc.put(npc, output);
    35. }
    36. }, 0L , 200L);
    37. movementsched.put(npc, id);
    38. }[/I]
     
    Last edited: Feb 6, 2015
  9. Offline

    fullwall

    Serold - get a more recent dev build of Citizens.
     
  10. Offline

    Serold

    I got it working now.

    I got another question, is there a possibility that the NPC Nametags can be hided?
     
  11. Offline

    fullwall

    Serold - /npc name or using the API entity.setCustomNameVisible(false). Note that this does not work for Players.
     
  12. Offline

    Serold

    That doesn't work for me..

    When I do /Npc Name
    it says, Nameplate visibility toggled
     
  13. Offline

    fullwall

    Serold - if it's not a Player, then the nameplate will be hidden if you're looking away.
     
  14. Offline

    tacticalsk8er

    NPCs spawn in via the API don't save:
    Code:
    [01:13:17] [Server thread/ERROR]: Error occurred while disabling Citizens v2.0.12-SNAPSHOT (build 1106) (Is it up to date?)
    java.lang.IllegalStateException: no implementation set
        at net.citizensnpcs.api.CitizensAPI.getImplementation(CitizensAPI.java:70) ~[?:?]
        at net.citizensnpcs.api.CitizensAPI.getPlugin(CitizensAPI.java:110) ~[?:?]
        at net.citizensnpcs.api.npc.AbstractNPC$1.set(AbstractNPC.java:60) ~[?:?]
        at net.citizensnpcs.api.npc.SimpleMetadataStore.get(SimpleMetadataStore.java:34) ~[?:?]
        at net.citizensnpcs.api.npc.AbstractNPC.save(AbstractNPC.java:341) ~[?:?]
        at net.citizensnpcs.npc.CitizensNPC.save(CitizensNPC.java:121) ~[?:?]
        at net.citizensnpcs.api.npc.SimpleNPCDataStore.store(SimpleNPCDataStore.java:80) ~[?:?]
        at net.citizensnpcs.api.npc.SimpleNPCDataStore.storeAll(SimpleNPCDataStore.java:86) ~[?:?]
        at net.citizensnpcs.Citizens.onDisable(Citizens.java:247) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:252) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3039jnks]
        at org.bukkit.plugin.java.JavaPluginLoader.disablePlugin(JavaPluginLoader.java:348) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3039jnks]
        at org.bukkit.plugin.SimplePluginManager.disablePlugin(SimplePluginManager.java:423) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3039jnks]
        at org.bukkit.plugin.SimplePluginManager.disablePlugins(SimplePluginManager.java:416) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3039jnks]
        at org.bukkit.craftbukkit.v1_7_R2.CraftServer.disablePlugins(CraftServer.java:390) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3039jnks]
        at net.minecraft.server.v1_7_R2.MinecraftServer.stop(MinecraftServer.java:377) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3039jnks]
        at net.minecraft.server.v1_7_R2.MinecraftServer.run(MinecraftServer.java:490) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3039jnks]
        at net.minecraft.server.v1_7_R2.ThreadServerApplication.run(SourceFile:618) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3039jnks]
     
  15. Offline

    TVXtrem

    Very cool API, but is it possible for a citizen to lay in a bed? That would be so awesome :)
     
  16. Offline

    fullwall

    tacticalsk8er - you need to let Citizens2 load before you start calling its methods.
    TVXtrem - yes, if you depend on Citizens2 you can call PlayerAnimation methods.
     
  17. Offline

    TVXtrem

    fullwall Yes, but if you call the sleep animation method, the npc is bugging through the floor. :/
     
  18. Offline

    fullwall

    TVXtrem - you should move the NPC up one block and then try the animation.
     
  19. Offline

    LCastr0

    How to set an NPC velocity?

    Anyone?

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

    fullwall

    LCastr0 - npc.getEntity().setVelocity(/*velocity*/)
     
  21. Offline

    LCastr0

    It's just spawning it, not going anywhere.
     
  22. Offline

    fullwall

  23. Offline

    LCastr0

    Nevermind, it is changing, but when the npc gets to the floor, it stops. How can I change it?
    Nevermind
     
  24. Offline

    LegitJava

    fullwall So, what's the status on Citizens for 1.7.8 with all these new Mojang skin changes? Will it still be functional just without skins?
     
  25. Offline

    elementalgodz11

    fullwall
    I have spawned an NPC using your api but for some reason, players can't damage it even after setting NPCDamageByEntityEvent and NPCDamageEvent to false. Any ideas what I'm doing wrong?

    code (open)
    Code:java
    1. package dkHc.listeners;
    2.  
    3. import java.util.List;
    4.  
    5. import net.citizensnpcs.api.CitizensAPI;
    6. import net.citizensnpcs.api.event.NPCDamageByBlockEvent;
    7. import net.citizensnpcs.api.event.NPCDamageByEntityEvent;
    8. import net.citizensnpcs.api.event.NPCDamageEvent;
    9. import net.citizensnpcs.api.npc.NPC;
    10. import net.citizensnpcs.api.npc.NPCRegistry;
    11.  
    12. import org.bukkit.Bukkit;
    13. import org.bukkit.entity.Entity;
    14. import org.bukkit.entity.EntityType;
    15. import org.bukkit.entity.Player;
    16. import org.bukkit.event.EventHandler;
    17. import org.bukkit.event.Listener;
    18. import org.bukkit.event.player.PlayerJoinEvent;
    19. import org.bukkit.event.player.PlayerQuitEvent;
    20.  
    21. import dkHc.main.Main;
    22.  
    23. public class QuitListener implements Listener {
    24.  
    25. public Main plugin;
    26. public QuitListener(Main instance) {
    27. this.plugin = instance;
    28. }
    29.  
    30. @EventHandler
    31. public void onDamage(NPCDamageEvent event) {
    32. event.setCancelled(false);
    33. }
    34.  
    35. @EventHandler
    36. public void onDamage(NPCDamageByEntityEvent event) {
    37. event.setCancelled(false);
    38. }
    39.  
    40. @EventHandler
    41. public void onDamage(NPCDamageByBlockEvent event) {
    42. event.setCancelled(false);
    43. }
    44.  
    45. NPCRegistry registry;
    46. NPC npc;
    47.  
    48. public void removeNpc(Player player) {
    49.  
    50. registry = CitizensAPI.getNPCRegistry();
    51.  
    52. npc.despawn();
    53. npc.destroy();
    54.  
    55. registry.deregister(npc);
    56.  
    57. }
    58.  
    59. public void spawnNPC(Player player) {
    60.  
    61. registry = CitizensAPI.getNPCRegistry();
    62.  
    63. npc = registry.createNPC(EntityType.PLAYER, player.getDisplayName());
    64. npc.spawn(player.getLocation());
    65. npc.setProtected(false);
    66.  
    67. int radius = Main.settingsManager.getWarps().getInt("Nearby_Player_Radius");
    68. List<Entity> near = player.getNearbyEntities(radius, radius, radius);
    69.  
    70. for (Entity entities : near) {
    71.  
    72. if (entities instanceof Player) {
    73.  
    74. Player players = (Player) entities;
    75.  
    76. players.sendMessage("NPC of " + player.getName() + " has been created.");
    77. System.out.println("NPC of " + player.getName() + " has been created.");
    78.  
    79. }
    80.  
    81. }
    82.  
    83. }
    84.  
    85. @EventHandler
    86. public void onQuit(PlayerQuitEvent event) {
    87.  
    88. final Player player = event.getPlayer();
    89.  
    90. event.setQuitMessage(null);
    91.  
    92. spawnNPC(player);
    93.  
    94. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    95. public void run() {
    96. removeNpc(player);
    97. }
    98. }, plugin.getConfig().getInt("Npc.RemoveTime") * 20L);
    99.  
    100. }
    101.  
    102. @EventHandler
    103. public void onJoin(PlayerJoinEvent event) {
    104.  
    105. removeNpc(event.getPlayer());
    106.  
    107. }
    108.  
    109. }
     
  26. Offline

    fullwall

    LegitJava - yep, that's correct. Player NPCs will just have steve skins.
    @elementalgoz11 - hm, that's strange. First of all, setProtected(false) should make them damageable without needing to listen for any events. Have you verified that the NPCs have their protected metadata set to false?
     
  27. Offline

    LCastr0

    How to make them rotate?
     
  28. Offline

    ledship

    Im trying to make a simple NPC spawn but its not working, heres my code:

    Code:java
    1.  
    2. import java.util.List;
    3. import java.util.logging.Level;
    4. import java.util.logging.Logger;
    5. import net.citizensnpcs.api.CitizensAPI;
    6. import net.citizensnpcs.api.npc.NPC;
    7. import net.citizensnpcs.api.npc.NPCRegistry;
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.Server;
    11. import org.bukkit.World;
    12. import org.bukkit.entity.EntityType;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.plugin.Plugin;
    15. import org.bukkit.plugin.PluginManager;
    16. import org.bukkit.plugin.java.JavaPlugin;
    17.  
    18. public class Main
    19. extends JavaPlugin
    20. implements Listener
    21. {
    22. NPCRegistry registry = CitizensAPI.getNPCRegistry();
    23.  
    24. public void onEnable()
    25. {
    26. if ((getServer().getPluginManager().getPlugin("Citizens") == null) || (!getServer().getPluginManager().getPlugin("Citizens").isEnabled()))
    27. {
    28. getLogger().log(Level.SEVERE, "Citizens 2.0 not found or not enabled");
    29. getServer().getPluginManager().disablePlugin(this);
    30. return;
    31. }
    32. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    33. }
    34.  
    35. public void onDisable() {
    36.  
    37. }
    38.  
    39. public void CreateNPC1()
    40. {
    41. NPC npc = this.registry.createNPC(EntityType.CREEPER, ChatColor.DARK_RED + "Server1");
    42. npc.spawn(((World)Bukkit.getWorlds().get(0)).getSpawnLocation());
    43. npc.setProtected(false);
    44. }
    45. }


    heres my error log:
    Code:
    [17:51:50] [Server thread/ERROR]: Could not load 'plugins\LedsCraftNPCs.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.IllegalStateException: no implementation set
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[spigot.jar:git-Spigot-1434]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:329) ~[spigot.jar:git-Spigot-1434]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) [spigot.jar:git-Spigot-1434]
        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.loadPlugins(CraftServer.java:362) [spigot.jar:git-Spigot-1434]
        at net.minecraft.server.v1_7_R3.DedicatedServer.init(DedicatedServer.java:135) [spigot.jar:git-Spigot-1434]
        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:450) [spigot.jar:git-Spigot-1434]
        at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [spigot.jar:git-Spigot-1434]
    Caused by: java.lang.IllegalStateException: no implementation set
        at net.citizensnpcs.api.CitizensAPI.getImplementation(CitizensAPI.java:70) ~[?:?]
        at net.citizensnpcs.api.CitizensAPI.getNPCRegistry(CitizensAPI.java:103) ~[?:?]
        at com.ledscraft.plugins.hub.npc.Main.<init>(Main.java:23) ~[?:?]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.7.0_45]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.7.0_45]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.7.0_45]
        at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.7.0_45]
        at java.lang.Class.newInstance(Unknown Source) ~[?:1.7.0_45]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:52) ~[spigot.jar:git-Spigot-1434]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:127) ~[spigot.jar:git-Spigot-1434]
        ... 6 more
     
  29. Offline

    fullwall

    ledship - although you are checking that Citizens exists correctly, your npcRegistry code is being run before that check.
    SkillSam - looks like the horse is not an NPC?
     
  30. Offline

    TwerkinCraft

    how would i register an NPCClickEvent, it doesn't seem to work.
     
Thread Status:
Not open for further replies.

Share This Page