You really should use Visual Studio, it will help make your coding look so much cleaner and point out a lot of small mistakes that can be done when writing. Particularly will help when you set up all your references.
Would be useful to read through other plugins to see how they are laid out too. Generally the more recent ones are good and Wulf's are usually well written like Godmode.
Adding a cooldown to a command?
Discussion in 'Rust Development' started by Squire, Jun 23, 2017.
-
it appears when you reconnect the cooldown has gone, so i guess it needs a datafile
[DOUBLEPOST=1498394363][/DOUBLEPOST] -
Last edited by a moderator: Jun 25, 2017
-
Code:[ChatCommand("nuke")] void chatNuke(BasePlayer player, string command, string[] args) { if (permission.UserHasPermission(player.UserIDString, "nukeplayer.use")) { if (!Cooldowns.Contains(player.userID)) //Check if the player is listed in the HashSet "Cooldowns", if not continue { Cooldowns.Add(player.userID); //Add the player to the Cooldowns HashSet so they cannot run the command again until removed timer.Once(configFile.Cooldown, () => Cooldowns.Remove(player.userID)); //Create and start a timer for 10 seconds, when it expires remove the player from the Cooldowns HashSet } else SendReply(player, "Cooldown in effect."); //If the player is in the Cooldowns HashSet send this chat message to them } else { var foundPlayer = rust.FindPlayer(args[0]); if (foundPlayer == null) { PrintToChat(player, $"We couldn't find a player named {args[0]}"); return; } else { DoNuke(foundPlayer); } } }
If the player didn't get perm, you are checking for name then doing nuke.
You should put the nuke thing into the perm check. -
[DOUBLEPOST=1498412993][/DOUBLEPOST]added
DoNuke(foundPlayer);
SendReply(player, "nuked.");
}
}
}
and its not getting to this at ll after the command has been used. -
-
This is my personal method, as DateTime/TimeSpan variables work very well. Basically, I have a TimeSpan declared at the top, TimeSpans are the result of subtracting 2 date times. So, I log the time the player uses the command, then I check if their next time of use minus their last time is greater than the TimeSpan, if so I allow them to use the command.
Code:private TimeSpan cooldown = new TimeSpan(0, 1, 0); private Dictionary<BasePlayer, DateTime> useTimes = new Dictionary<BasePlayer, DateTime>(); [ChatCommand("test")] private void TestCommand(BasePlayer player, string command, string[] args) { if (useTimes.ContainsKey(player)) { if (DateTime.UtcNow - useTimes[player] > cooldown) { PrintToChat(player, "Your cooldown expired, you may use this command."); useTimes[player] = DateTime.UtcNow; return; } PrintToChat(player, $"Error, that command is on cooldown for {useTimes[player]}."); return; } useTimes.Add(player, DateTime.UtcNow); PrintToChat(player, "You have been added to the <i>useTimes</i> list, you will be able to use this command again in one hour."); // Chat command after this }
Last edited by a moderator: Jun 26, 2017 -
Well this is what was ment to be but now it cant find players
Code:Failed to call hook 'chatNuke' on plugin 'NukePlayer v1.0.0' (IndexOutOfRangeException: Array index is out of range.) at Oxide.Plugins.NukePlayer.chatNuke (.BasePlayer player, System.String command, System.String[] args) [0x00000] in <filename unknown>:0 at Oxide.Plugins.NukePlayer.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Plugins.CSharpPlugin.InvokeMethod (HookMethod method, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Core.Plugins.CSPlugin.OnCallHook (System.String name, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Core.Plugins.Plugin.CallHook (System.String hook, System.Object[] args) [0x00000] in <filename unknown>:0 Failed to call hook 'chatNuke' on plugin 'NukePlayer v1.0.0' (IndexOutOfRangeException: Array index is out of range.) at Oxide.Plugins.NukePlayer.chatNuke (.BasePlayer player, System.String command, System.String[] args) [0x00000] in <filename unknown>:0 at Oxide.Plugins.NukePlayer.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Plugins.CSharpPlugin.InvokeMethod (HookMethod method, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Core.Plugins.CSPlugin.OnCallHook (System.String name, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Core.Plugins.Plugin.CallHook (System.String hook, System.Object[] args) [0x00000] in <filename unknown>:0
Code:[ChatCommand("nuke")] void chatNuke(BasePlayer player, string command, string[] args) { if (permission.UserHasPermission(player.UserIDString, "nukeplayer.use")) { if (!Cooldowns.Contains(player.userID)) //Check if the player is listed in the HashSet "Cooldowns", if not continue { Cooldowns.Add(player.userID); //Add the player to the Cooldowns HashSet so they cannot run the command again until removed timer.Once(configFile.Cooldown, () => Cooldowns.Remove(player.userID)); //Create and start a timer for 10 seconds, when it expires remove the player from the Cooldowns HashSet } var foundPlayer = rust.FindPlayer(args[0]); if (foundPlayer == null) {PrintToChat(player, $"We couldn't find a player named {args[0]}"); return; } else { DoNuke(foundPlayer); SendReply(player, "nuked."); } } else { SendReply(player, "Cooldown in effect."); //If the player is in the Cooldowns HashSet send this chat message to them } }
-
-
yes and the problem i have is stacking the else, because if i put another } it falls outside that expression.
[DOUBLEPOST=1498467453][/DOUBLEPOST] -
No need, just use return.
-
-
-
Array index is out of range.
That line mean you are trying to use an index that you don't set specially at line args[0], You are trying to use the first arg sent by command but if you do only /nuke, there is no arg and that will display the error
The code on the picture is wrong the other one before is right -
secondly the partial /nuke <player> didnt work so i had to find another way, but you are right in it is working but only a round about way. -
-
blah, i give up.
-
And... there you have it, deviated from everyone elses examples.
TODO:
Make data rather than hash due to server restarts every 6 hours, hash will be lost.
Code:#region Core [ChatCommand("nuke")] void chatNuke(BasePlayer player, string command, string[] args) { if (permission.UserHasPermission(player.UserIDString, "nukeplayer.use")) { if (!Cooldowns.Contains(player.userID)) { Cooldowns.Add(player.userID); timer.Once(configFile.Cooldown, () => Cooldowns.Remove(player.userID)); } else { SendReply(player, "Preparing Nuke Systems 24 Hour Cooldown"); return; } var targetPlayer = covalence.Players.FindPlayer(args[0]); if (targetPlayer != null && targetPlayer.IsConnected) { var target = targetPlayer?.Object as BasePlayer; if (target != null) { DoNuke(target); } } } }
-