Citizens api

Discussion in 'Plugin Development' started by DevManABCD, Jul 2, 2014.

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

    DevManABCD

    Hey, i want to use citizens api on my plugin, its my code:

    Code:java
    1. import net.citizensnpcs.api.CitizensAPI;
    2. import net.citizensnpcs.api.npc.NPC;
    3. import net.citizensnpcs.api.npc.NPCRegistry;
    4. import net.citizensnpcs.api.trait.TraitInfo;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.Location;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Entity;
    11. import org.bukkit.entity.EntityType;
    12. import org.bukkit.entity.Giant;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.event.EventHandler;
    15. import org.bukkit.event.Listener;
    16. import org.bukkit.event.player.PlayerJoinEvent;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18.  
    19.  
    20.  
    21. public class Main extends JavaPlugin implements Listener {
    22.  
    23. public void onEnable() {
    24. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    25. }
    26.  
    27. @EventHandler
    28. public void onJoin(PlayerJoinEvent e) {
    29. Player p = e.getPlayer();
    30. NPCRegistry registry = CitizensAPI.getNPCRegistry();
    31. NPC npc = registry.createNPC(EntityType.PLAYER, "MyNPC");
    32. npc.spawn(p.getLocation());
    33. }
    34.  
    35.  
    36.  
    37.  
    38.  
    39.  
    40.  
    41. }



    Plugin didnt output any errors, when i join npc doesnt exists at my location, its strange because i registered my npc, still citizens says:
    [Citizens] Loaded 0 NPCs.

    Is my code wrong? Why npc doesnt spawning?
     
  2. maybe try a delayed task that spawns the npc 5 seconds after you join the game. Again this still may not fix it but it might be worth a try.
     
  3. Offline

    DevManABCD


    Thanks! It works ;-)

    Maybe you know why entity name returns null?

    Code:java
    1. @EventHandler
    2. public void onJoin(PlayerJoinEvent e) {
    3. final Player p = e.getPlayer();
    4. new BukkitRunnable() {
    5.  
    6. @Override
    7. public void run() {
    8. NPCRegistry registry = CitizensAPI.getNPCRegistry();
    9. NPC npc = registry.createNPC(EntityType.PLAYER, "MyNPC");
    10. npc.spawn(p.getLocation());
    11. }
    12.  
    13. }.runTaskLater(this, 90);
    14. }
    15.  
    16. @EventHandler
    17. public void onClick(EntityDamageByEntityEvent e) {
    18. if(e.getEntity()instanceof Player) {
    19. Player p = (Player) e.getDamager();
    20. if(e.getEntity().getType() == EntityType.PLAYER) {
    21. p.sendMessage("xxx + " + e.getEntityType().getName());
    22.  
    23. }
    24. }
    25. }



    I just want to do, somehing like when player hits npc named MyNPC.

    p.sendMessage("xxx + " + e.getEntityType().getName());

    returns "xxx + null".
     
  4. Code:java
    1. @EventHandler(priority = EventPriority.MONITOR)
    2. public void onPlayerHitEntity(EntityDamageByEntityEvent e) {
    3.  
    4. Entity damager = e.getDamager();
    5. Entity damagee = e.getEntity();
    6.  
    7. if(damager instanceof Player && damagee instanceof Zombie) { //if the damager is a player and entity is a zombie for example
    8. Player player = (Player) damager;
    9. Zombie zombie = (Zombie) damagee;
    10.  
    11. player.sendMessage(zombie.getCustomName());
    12. }
    13.  
    14.  
    15. }


    this will work every time a player hits a zombie. You may want to check if the entity is an npc first before doing this as it will apply for every time you hit that mob type.

    It will return null if the entity does not have a custom name so you might want to check that.
     
  5. Offline

    DevManABCD

    Thanks, it was useful. Maybe you know how to remove an event? I seraching this all time, and still cant find. Its last needed thing ;p

    remove npc* soz

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
Thread Status:
Not open for further replies.

Share This Page