Spit Out Number Of BotsSolved

Spit out the number of bots in Chat via a server command.  That way, we can put it in Timed Execute to show the number anytime we want to.

Alternatively, have a constant label somewhere on the screen that shows the number, configurable with font size and color and position.

using System.Collections.Generic;
using UnityEngine;

namespace Oxide.Plugins
{
    [Info("NPC Counter", "bmgjet", "1.0.0")]
    public class ChatNPCCounter : RustPlugin
    {
        public int CountNPCs()
        {
            Dictionary<ulong, Vector3> NPCPlayers = new Dictionary<ulong, Vector3>();
            int i = 0;
            foreach (var npcs in GameObject.FindObjectsOfType<NPCPlayerApex>()) 
            {
                if(!NPCPlayers.ContainsKey(npcs.userID))
                NPCPlayers.Add(npcs.userID, npcs.transform.position);
            }
            foreach (var npcs in GameObject.FindObjectsOfType<HumanNPC>()) 
            {
                if (!NPCPlayers.ContainsKey(npcs.userID))
                    NPCPlayers.Add(npcs.userID, npcs.transform.position);
            }
            foreach (var npcs in GameObject.FindObjectsOfType<HumanNPCNew>()) 
            {
                if (!NPCPlayers.ContainsKey(npcs.userID))
                    NPCPlayers.Add(npcs.userID, npcs.transform.position);
            }
            foreach (var npcs in GameObject.FindObjectsOfType<ScientistNPC>()) 
            {
                if (!NPCPlayers.ContainsKey(npcs.userID))
                    NPCPlayers.Add(npcs.userID, npcs.transform.position);
            }
            foreach (var npcs in GameObject.FindObjectsOfType<ScientistNPCNew>()) 
            {
                if (!NPCPlayers.ContainsKey(npcs.userID))
                    NPCPlayers.Add(npcs.userID, npcs.transform.position);
            }
            foreach (var npcs in GameObject.FindObjectsOfType<NPCMurderer>())
            {
                if (!NPCPlayers.ContainsKey(npcs.userID))
                    NPCPlayers.Add(npcs.userID, npcs.transform.position);
            }
            foreach (var npcs in GameObject.FindObjectsOfType<NPCPlayer>())
            {
                if (!NPCPlayers.ContainsKey(npcs.userID))
                    NPCPlayers.Add(npcs.userID, npcs.transform.position);
            }
            return NPCPlayers.Count;
        }

        [ChatCommand("npccount")]
        private void DoCount(BasePlayer player)
        {
            if (player.IsAdmin){player.ChatMessage(CountNPCs().ToString());}
        }

        [ConsoleCommand("npccount")]
        private void Cmd(ConsoleSystem.Arg arg)
        {
            if (arg.IsAdmin){PrintWarning(CountNPCs().ToString());}
        }
    }
}

 

Entering "npccount" command in the console does not show the count in Chat, which is what I need this plugin to do.

How about this then.
Console command npccountall will send "X Bots are active!" to all players that are alive and not asleep.

Chat command /npccount will send that message just to the person that sent the request.
They will need to have permission ChatNPCCounter.use since its not a command you want to use often because it could be used to spam the server with.

 

using System.Collections.Generic;
using UnityEngine;

namespace Oxide.Plugins
{
    [Info("NPC Counter", "bmgjet", "1.0.0")]
    public class ChatNPCCounter : RustPlugin
    {
        #region Initialization
        private const string permCount = "ChatNPCCounter.use";

        private void Init()
        {
            permission.RegisterPermission(permCount, this);
            lang.RegisterMessages(new Dictionary<string, string>
            {
                ["NPCMessage"] = "{0} Bots are active!"
            }, this);
        }
        #endregion

        #region Commands

        [ChatCommand("npccount")]
        private void DoCount(BasePlayer player)
        {
          if (player.IPlayer.HasPermission(permCount)){ player.ChatMessage(string.Format(lang.GetMessage("NPCMessage", this, player.IPlayer.Id), CountNPCs().ToString())); }
        }

        [ConsoleCommand("npccountall")]
        private void Cmd2(ConsoleSystem.Arg arg)
        {
            if (arg.IsAdmin)
            {
                Broadcast("NPCMessage", CountNPCs().ToString());
            }
        }
        #endregion

        #region Helpers
        public int CountNPCs()
        {
            Dictionary<ulong, Vector3> NPCPlayers = new Dictionary<ulong, Vector3>();
            foreach (var npcs in GameObject.FindObjectsOfType<NPCPlayerApex>())
            {
                if (!NPCPlayers.ContainsKey(npcs.userID))
                    NPCPlayers.Add(npcs.userID, npcs.transform.position);
            }
            foreach (var npcs in GameObject.FindObjectsOfType<HumanNPC>())
            {
                if (!NPCPlayers.ContainsKey(npcs.userID))
                    NPCPlayers.Add(npcs.userID, npcs.transform.position);
            }
            foreach (var npcs in GameObject.FindObjectsOfType<HumanNPCNew>())
            {
                if (!NPCPlayers.ContainsKey(npcs.userID))
                    NPCPlayers.Add(npcs.userID, npcs.transform.position);
            }
            foreach (var npcs in GameObject.FindObjectsOfType<ScientistNPC>())
            {
                if (!NPCPlayers.ContainsKey(npcs.userID))
                    NPCPlayers.Add(npcs.userID, npcs.transform.position);
            }
            foreach (var npcs in GameObject.FindObjectsOfType<ScientistNPCNew>())
            {
                if (!NPCPlayers.ContainsKey(npcs.userID))
                    NPCPlayers.Add(npcs.userID, npcs.transform.position);
            }
            foreach (var npcs in GameObject.FindObjectsOfType<NPCMurderer>())
            {
                if (!NPCPlayers.ContainsKey(npcs.userID))
                    NPCPlayers.Add(npcs.userID, npcs.transform.position);
            }
            foreach (var npcs in GameObject.FindObjectsOfType<NPCPlayer>())
            {
                if (!NPCPlayers.ContainsKey(npcs.userID))
                    NPCPlayers.Add(npcs.userID, npcs.transform.position);
            }
            return NPCPlayers.Count;
        }
        private void Broadcast(string key, params object[] args)
        {
            foreach (var player in BasePlayer.allPlayerList)
            {
                if (player.IsAlive() && !player.IsSleeping())
                {
                    player.ChatMessage(string.Format(lang.GetMessage(key, this, player.IPlayer.Id), args));
                }
            }
        }
        #endregion
    }
}

That worked.  gj

bmgjet

How about this then.
Console command npccountall will send "X Bots are active!" to all players that are alive and not asleep.

Chat command /npccount will send that message just to the person that sent the request.
They will need to have permission ChatNPCCounter.use since its not a command you want to use often because it could be used to spam the server with.

 

using System.Collections.Generic;
using UnityEngine;

namespace Oxide.Plugins
{
    [Info("NPC Counter", "bmgjet", "1.0.0")]
    public class ChatNPCCounter : RustPlugin
    {
        #region Initialization
        private const string permCount = "ChatNPCCounter.use";

        private void Init()
        {
            permission.RegisterPermission(permCount, this);
            lang.RegisterMessages(new Dictionary<string, string>
            {
                ["NPCMessage"] = "{0} Bots are active!"
            }, this);
        }
        #endregion

        #region Commands

        [ChatCommand("npccount")]
        private void DoCount(BasePlayer player)
        {
          if (player.IPlayer.HasPermission(permCount)){ player.ChatMessage(string.Format(lang.GetMessage("NPCMessage", this, player.IPlayer.Id), CountNPCs().ToString())); }
        }

        [ConsoleCommand("npccountall")]
        private void Cmd2(ConsoleSystem.Arg arg)
        {
            if (arg.IsAdmin)
            {
                Broadcast("NPCMessage", CountNPCs().ToString());
            }
        }
        #endregion

        #region Helpers
        public int CountNPCs()
        {
            Dictionary<ulong, Vector3> NPCPlayers = new Dictionary<ulong, Vector3>();
            foreach (var npcs in GameObject.FindObjectsOfType<NPCPlayerApex>())
            {
                if (!NPCPlayers.ContainsKey(npcs.userID))
                    NPCPlayers.Add(npcs.userID, npcs.transform.position);
            }
            foreach (var npcs in GameObject.FindObjectsOfType<HumanNPC>())
            {
                if (!NPCPlayers.ContainsKey(npcs.userID))
                    NPCPlayers.Add(npcs.userID, npcs.transform.position);
            }
            foreach (var npcs in GameObject.FindObjectsOfType<HumanNPCNew>())
            {
                if (!NPCPlayers.ContainsKey(npcs.userID))
                    NPCPlayers.Add(npcs.userID, npcs.transform.position);
            }
            foreach (var npcs in GameObject.FindObjectsOfType<ScientistNPC>())
            {
                if (!NPCPlayers.ContainsKey(npcs.userID))
                    NPCPlayers.Add(npcs.userID, npcs.transform.position);
            }
            foreach (var npcs in GameObject.FindObjectsOfType<ScientistNPCNew>())
            {
                if (!NPCPlayers.ContainsKey(npcs.userID))
                    NPCPlayers.Add(npcs.userID, npcs.transform.position);
            }
            foreach (var npcs in GameObject.FindObjectsOfType<NPCMurderer>())
            {
                if (!NPCPlayers.ContainsKey(npcs.userID))
                    NPCPlayers.Add(npcs.userID, npcs.transform.position);
            }
            foreach (var npcs in GameObject.FindObjectsOfType<NPCPlayer>())
            {
                if (!NPCPlayers.ContainsKey(npcs.userID))
                    NPCPlayers.Add(npcs.userID, npcs.transform.position);
            }
            return NPCPlayers.Count;
        }
        private void Broadcast(string key, params object[] args)
        {
            foreach (var player in BasePlayer.allPlayerList)
            {
                if (player.IsAlive() && !player.IsSleeping())
                {
                    player.ChatMessage(string.Format(lang.GetMessage(key, this, player.IPlayer.Id), args));
                }
            }
        }
        #endregion
    }
}

One foreach is enough, and all of those Object types are derived from NPCPlayer, you also forgot about HTNPlayer which isn't derived from NPCPlayer.

using System;
using System.Collections.Generic;

namespace Oxide.Plugins
{
    [Info("Count NPC", "bsdinis", "0.0.1")]
    class CountNPC : RustPlugin
    {
        [ChatCommand("countnpc")]
        void CountNPCsChat(BasePlayer player) => player.ChatMessage(Count());
        [ConsoleCommand("countnpc")]
        void CountNPCs(ConsoleSystem.Arg arg) => SendReply(arg, Count());
        string Count()
        {
            int count = 0;
            string output = string.Empty;
            Dictionary<string, int> NPCs = new Dictionary<string, int>();
            foreach (var entity in BaseNetworkable.serverEntities)
            {
                var npc = entity as NPCPlayer;
                var htn = entity as HTNPlayer;
                if (npc != null)
                {
                    count++;
                    if (!NPCs.ContainsKey(npc.ShortPrefabName)) NPCs.Add(npc.ShortPrefabName, 1);
                    else NPCs[npc.ShortPrefabName] += 1;
                }
                if (htn != null)
                {
                    count++;
                    if (!NPCs.ContainsKey(htn.LootPanelName)) NPCs.Add(htn.LootPanelName, 1);
                    else NPCs[htn.LootPanelName] += 1;
                }
            }
            foreach (var kvp in NPCs) output = String.Format(output + "\n{0,-35} |   {1,-10}", kvp.Key, kvp.Value);
            output = String.Format("\n{0,-35} | {1,-10}", "     Name:", "Amount:" + output + $"\n\tThere are currently {count} NPCs.");
            return output;
        }
    }
}​
Locked automatically