We've moved! Join us as we continue development on our new site at umod.org!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  1. I know that you can use [ChatCommand("thing")], but how can you make a Console Command?
     
  2. Code:
    [ConsoleCommand("thing")]
    void ThingCommand(ConsoleSystem.Arg arg)
    {
    
    }
     
    Garbage Collector likes this.
  3. So if I took this command
    Code:
    [ChatCommand("sclclear")]
            private void ClearSCLData(BasePlayer player, string cmd, string[] args)
            {
                if (!permission.UserHasPermission(player.UserIDString, permissionClear))
                {
                    player.ChatMessage(Lang("ErrorNoPermission", player.UserIDString));
                    return;
                }
                else
                {
                    storedData.SuspectLogs.Clear();
                }
            }
    and wanted to turn it into a console command I would change the attribute to ConsoleCommand but I know thats not all. What else would I need to change?
    Code:
    [ChatCommand("sclclear")]
            private void ClearSCLData(BasePlayer player, string cmd, string[] args)
            {
                if (!permission.UserHasPermission(player.UserIDString, permissionClear))
                {
                    player.ChatMessage(Lang("ErrorNoPermission", player.UserIDString));
                    return;
                }
                else
                {
                    storedData.SuspectLogs.Clear();
                }
            }
    
    I know baseplayer player wouldnt make any sense because its a console command
     
  4. This is the command:

    Code:
    [ConsoleCommand("sclclear")]
            void SCLClearConsole(ConsoleSystem.Arg arg)
            {
                storedData.SuspectLogs.Clear();
                PrintToConsole("Clearing all suspect logs...");
            }
    When used, this is the error

    Code:
    Failed to call hook 'SCLClearConsole' on plugin 'SuspiciousCombatlogs v0.1.0' (NullReferenceException: Object reference not set to an instance of an object)
      at Oxide.Plugins.SuspiciousCombatlogs.SCLClearConsole (ConsoleSystem+Arg arg) [0x00000] in <b8c1294f51c440e48f055c3eed4e5bd8>:0
      at Oxide.Plugins.SuspiciousCombatlogs.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x00317] in <b8c1294f51c440e48f055c3eed4e5bd8>:0
      at Oxide.Plugins.CSharpPlugin.InvokeMethod (Oxide.Core.Plugins.HookMethod method, System.Object[] args) [0x00079] in <e5ac390771dc411395a594de571775c7>:0
      at Oxide.Core.Plugins.CSPlugin.OnCallHook (System.String name, System.Object[] args) [0x000d8] in <fc2b4388d9974d719a0972b08cea0f17>:0
      at Oxide.Core.Plugins.Plugin.CallHook (System.String hook, System.Object[] args) [0x00060] in <fc2b4388d9974d719a0972b08cea0f17>:0
    It should be noted I dont know how to read errors like these
     
  5. Wulf

    Wulf Community Admin

    Your storedData or storedData.SuspectLogs is null.
     
  6. I think the hardest part will be taking this chat command from CombatLogsDb by mzrb
    Code:
    [ChatCommand("combatlog")]
            private void ChatCombatLog(BasePlayer player, string cmd, string[] args)
            {
                if (!permission.UserHasPermission(player.UserIDString, permissionUse))
                {
                    player.ChatMessage(Lang("ErrorNoPermission", player.UserIDString));
                    return;
                }
    
                if (args.Length == 0)
                {
                    player.ChatMessage(Lang("ErrorNameRequired", player.UserIDString));
                    return;
                }
    
                var adminInput = args[0].ToLower();
    
                var playerFound = new List<BasePlayer>();
                foreach (var t in BasePlayer.activePlayerList)
                {
                    if (t.UserIDString == adminInput)
                    {
                        playerFound.Clear();
                        playerFound.Add(t);
                        break;
                    }
                    else if (t.displayName.ToLower() == adminInput)
                    {
                        playerFound.Clear();
                        playerFound.Add(t);
                        break;
                    }
                    if (t.displayName.ToLower().Contains(adminInput))
                    {
                        playerFound.Add(t);
                    }
                }
    
                if (playerFound.Count == 0)
                {
                    player.ChatMessage(Lang("NoPlayersFound", player.UserIDString, adminInput));
                    return;
                }
                else if (playerFound.Count > 1)
                {
                    string msg = Lang("MultiplePlayersFound", player.UserIDString, adminInput) + ": \n";
                    foreach (var p in playerFound) msg += $"- {p.displayName} \n";
                    player.ChatMessage(msg);
                    return;
                }
    
                var suspect = playerFound[0];
    
                int rows = args.Length > 1 ? int.Parse(args[1]) : 20;
    
                var suspectLog = suspect.stats.combat.Get(rows);
    
                suspectLog = suspectLog.Replace("you", suspect.displayName);
    
                string[] numbers = Regex.Split(suspectLog, @"\D+");
                foreach (string value in numbers)
                {
                    if (!string.IsNullOrEmpty(value))
                    {
                        uint i = uint.Parse(value);
    
                        if (i < 1000) continue;
    
                        if (!storedData.Players.ContainsKey(i)) continue;
    
                        suspectLog = suspectLog.Replace(i.ToString(), GetNameFromId(storedData.Players[i].ToString()));
                    }
                }
    
                player.ConsoleMessage("\n");
                player.ChatMessage(Lang("CheckingCombatLogFor", player.UserIDString, suspect.displayName));
                player.ConsoleMessage(suspectLog);
                player.ConsoleMessage("\n");
    
                player.ChatMessage(Lang("CheckConsoleForData", player.UserIDString, suspect.displayName));
    
                storedData.SuspectLogs.Add(player.userID, suspectLog);
                SaveData();
    }
    and turning it into a console command for a server owner. I cant use anything with player or arg so as for that I'm clueless
    Also, even when the dictionary WASN'T null, the clear command would output the message "Clearing the suspect logs..." but when I went to the file location the json file was still full with whatever was in it. Same was the case when the server was wiped (though a method was made to clear the dictionary on wipe.

    in short, a whole lot of oopsie going on here.
     
  7. Console commands do not provide a player like chat commands do. The reasoning for this is because the player is not always there to be referenced with a console command. Console commands can be executed by console or RCON. So, to get the player you need to do something such as
    Code:
    var player = arg.Player();
    // Player is null from the ConsoleSystem.Arg - this means the command was ran via console/rcon, etc.
    if(player == null)
    {
        // Returning so the rest of the code is not ran beneath this
        return;
    }
    When you implement something similar to my example above, you will not get compilation errors or NullReferenceExceptions (NREs)
     
  8. Thank you, sir!