Solved Citizens API Assistance

Discussion in 'Spigot Plugin Help' started by doublehelix457, May 6, 2017.

  1. So I have a peculiar problem. My goal is to get the number of existing non-player NPC's in realtime. One method I tried is:

    Code (Text):

    File save_file = CitizensAPI.getPlugin().getResource(getDataFolder(), "saves.yml");
    FileConfiguration conf = YamlConfiguration.loadConfiguration(save_file);

    int counter = 0;

    for(int x=0;x<=conf.getInt("last-created-npc-id";x++) counter++;

    return counter;
     
    Problem is, this file is saved on disable, and I dont want to reload the config EVERY time I call this method if someone creates an NPC ingame.

    Another Method I thought of but haven't tried is using dispatchCommand(), to print out /npc list and then get the result from the console.


    I would prefer to check for ALL NPC's with a certain trait, but the number of ALL NPCs would be fine too.

    All help is appreciated. Till then:

    Code (Text):

    boolean waiting = true;
    int coffee_cups = 4;
    while(waiting){
        coffee_cups++;
    }
     
    #1 doublehelix457, May 6, 2017
    Last edited: May 6, 2017
  2. Welp, After hours of troubleshooting, I figured out a solution to get the total of existing NPCs. I basically checked the source code for how the author got a number of npcs with the command /npc list. Here is my method ready for implementation.

    Code (Text):

    public int getTotalNPCs(){
        NPCRegistry regis = CitizensAPI.getNPCRegistry();
        if(regis == null){
            return -1;
        }
         List<NPC> npcs = new ArrayList<NPC>();
         
        for(NPC get : regis.sorted()) npcs.add(get);

      return npcs.size();
    }
    I hope this helps anyone who encountered this problem. Unless everyone already figured it out and didn't tell me :/

    I'm always late to the party...
     
    • Useful Useful x 1