How to check if Citizens NPC exists?

Discussion in 'Plugin Development' started by shohouku, Jan 30, 2020.

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

    shohouku

    How can I check if an NPC exists?

    I'm using citizens API.
     
  2. Online

    timtower Administrator Administrator Moderator

    @shohouku And how do you want to identify that NPC? Name? UUID? Location?
     
  3. Offline

    shohouku

    By name.
     
  4. Online

    timtower Administrator Administrator Moderator

    Find all entities, check if they are a NPC, check the name?
     
  5. Offline

    shohouku

    I've tried this code but it doesn't work.

    Code:
            for(Entity npc : Bukkit.getWorld("world").getEntities()) {
                if(CitizensAPI.getNPCRegistry().isNPC(npc)) {
                    if(npc.getName().equals("Shop")) {
                        System.out.println("Shop exists");
                    } else {
                        System.out.println("Shop doesn't exist");
                    }
                }
            }
     
  6. Online

    timtower Administrator Administrator Moderator

    Define "doesn't work"
    Have you tried printing out the names?
     
  7. Offline

    shohouku

    It doesn't detect if any npc's exist in the world and yes I've tried printing out the names.
     
  8. Online

    timtower Administrator Administrator Moderator

  9. Offline

    Sploon

    If the NPC name is colored, it won't match ("&5Shop" is not the same as "Shop").
    Try:
    Code:
    if(ChatColor.stripColor(npc.getName()).equals("Shop"))
     
Thread Status:
Not open for further replies.

Share This Page