From 1d98b8703e9568e42f2faa086d51bf36e0112f50 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 25 Aug 2016 00:22:26 +1000 Subject: [PATCH] Allow using @number as a shortcut for the rule by that number in [reason] for /kick /warn /ban (thanks yorkiebarz). --- Commands/Moderation/CmdBan.cs | 13 ++--- Commands/Moderation/CmdKick.cs | 10 ++-- Commands/Moderation/CmdWarn.cs | 9 ++-- Commands/Moderation/ModActionCmd.cs | 75 +++++++++++++++++++++++++++++ MCGalaxy_.csproj | 1 + 5 files changed, 94 insertions(+), 14 deletions(-) create mode 100644 Commands/Moderation/ModActionCmd.cs diff --git a/Commands/Moderation/CmdBan.cs b/Commands/Moderation/CmdBan.cs index 91ce50c69..2b136fa72 100644 --- a/Commands/Moderation/CmdBan.cs +++ b/Commands/Moderation/CmdBan.cs @@ -18,11 +18,9 @@ using System; namespace MCGalaxy.Commands.Moderation { - public sealed class CmdBan : Command { + public sealed class CmdBan : ModActionCmd { public override string name { get { return "ban"; } } public override string shortcut { get { return ""; } } - public override string type { get { return CommandTypes.Moderation; } } - public override bool museumUsable { get { return true; } } public override LevelPermission defaultRank { get { return LevelPermission.Operator; } } public CmdBan() { } @@ -37,11 +35,13 @@ namespace MCGalaxy.Commands.Moderation { } string[] args = message.SplitSpaces(2); - string reason = args.Length > 1 ? args[1] : Server.defaultBanMessage; + string reason = args.Length > 1 ? args[1] : Server.defaultBanMessage; + if (reason == "-") reason = "&c-"; + reason = GetReason(p, reason); + if (reason == null) return; string banReason = reason == "-" ? "" : " (" + reason + ")"; - if (reason == "-") reason = "&c-"; - Player who = PlayerInfo.Find(args[0]); + Player who = PlayerInfo.Find(args[0]); string target = who == null ? args[0] : who.name; if (!ValidName(p, target, "player")) return; Group group = who == null ? Group.findPlayerGroup(args[0]) : who.group; @@ -87,6 +87,7 @@ namespace MCGalaxy.Commands.Moderation { Player.Message(p, "%T/ban [reason]"); Player.Message(p, "%HBans a player without kicking them."); Player.Message(p, "%HAdd # before name to stealth ban."); + Player.Message(p, "%HFor [reason], @number can be used as a shortcut for that rule."); } } } diff --git a/Commands/Moderation/CmdKick.cs b/Commands/Moderation/CmdKick.cs index 268eb91ef..49eff9f3a 100644 --- a/Commands/Moderation/CmdKick.cs +++ b/Commands/Moderation/CmdKick.cs @@ -18,11 +18,9 @@ using System; namespace MCGalaxy.Commands { - public sealed class CmdKick : Command { + public sealed class CmdKick : ModActionCmd { public override string name { get { return "kick"; } } public override string shortcut { get { return "k"; } } - public override string type { get { return CommandTypes.Moderation; } } - public override bool museumUsable { get { return true; } } public override LevelPermission defaultRank { get { return LevelPermission.AdvBuilder; } } public override void Use(Player p, string message) { @@ -34,6 +32,9 @@ namespace MCGalaxy.Commands { if (args.Length > 1) message = args[1]; else if (p == null) message = "You were kicked by the console."; else message = "You were kicked by " + p.DisplayName + "."; + + message = GetReason(p, message); + if (message == null) return; if (p != null && p == who) { Player.Message(p, "You cannot kick yourself."); return; } if (p != null && who.Rank >= p.Rank) { @@ -48,8 +49,9 @@ namespace MCGalaxy.Commands { } public override void Help(Player p) { - Player.Message(p, "%T/kick [message]"); + Player.Message(p, "%T/kick [reason]"); Player.Message(p, "%HKicks a player."); + Player.Message(p, "%HFor [reason], @number can be used as a shortcut for that rule."); } } } diff --git a/Commands/Moderation/CmdWarn.cs b/Commands/Moderation/CmdWarn.cs index 2deb20205..11e2d1c2e 100644 --- a/Commands/Moderation/CmdWarn.cs +++ b/Commands/Moderation/CmdWarn.cs @@ -16,11 +16,9 @@ permissions and limitations under the Licenses. */ namespace MCGalaxy.Commands.Moderation { - public sealed class CmdWarn : Command { + public sealed class CmdWarn : ModActionCmd { public override string name { get { return "warn"; } } public override string shortcut { get { return ""; } } - public override string type { get { return CommandTypes.Moderation; } } - public override bool museumUsable { get { return true; } } public override LevelPermission defaultRank { get { return LevelPermission.Builder; } } public override void Use(Player p, string message) { @@ -28,6 +26,8 @@ namespace MCGalaxy.Commands.Moderation { string[] args = message.SplitSpaces(2); Player who = PlayerInfo.FindMatches(p, args[0]); string reason = args.Length == 1 ? "you know why." : args[1]; + reason = GetReason(p, reason); + if (reason == null) return; if (who == null) { WarnOffline(p, args); return; } if (who == p) { Player.Message(p, "you can't warn yourself"); return; } @@ -70,8 +70,9 @@ namespace MCGalaxy.Commands.Moderation { } public override void Help(Player p) { - Player.Message(p, "%T/warn "); + Player.Message(p, "%T/warn [player] [reason]"); Player.Message(p, "%HWarns a player. Players are kicked after 3 warnings."); + Player.Message(p, "%HFor [reason], @number can be used as a shortcut for that rule."); } } } diff --git a/Commands/Moderation/ModActionCmd.cs b/Commands/Moderation/ModActionCmd.cs new file mode 100644 index 000000000..d8eda551b --- /dev/null +++ b/Commands/Moderation/ModActionCmd.cs @@ -0,0 +1,75 @@ +/* + Copyright 2015 MCGalaxy + + Dual-licensed under the Educational Community License, Version 2.0 and + the GNU General Public License, Version 3 (the "Licenses"); you may + not use this file except in compliance with the Licenses. You may + obtain a copy of the Licenses at + + http://www.opensource.org/licenses/ecl2.php + http://www.gnu.org/licenses/gpl-3.0.html + + Unless required by applicable law or agreed to in writing, + software distributed under the Licenses are distributed on an "AS IS" + BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + or implied. See the Licenses for the specific language governing + permissions and limitations under the Licenses. + */ +using System; +using System.Collections.Generic; +using System.IO; + +namespace MCGalaxy.Commands { + public abstract class ModActionCmd : Command { + public override string type { get { return CommandTypes.Moderation; } } + public override bool museumUsable { get { return true; } } + + protected string GetReason(Player p, string reason) { + if (reason.Length == 0 || reason[0] != '@') return reason; + reason = reason.Substring(1); + int num; + if (!int.TryParse(reason, out num)) return "@" + reason; + + // Treat @num as a shortcut for rule #num + Dictionary sections = GetRuleSections(); + string rule; + if (sections.TryGetValue(num, out rule)) return rule; + + Player.Message(p, "No rule has number \"{0}\". Current rule numbers are: {1}", + num, sections.Keys.Join(n => n.ToString())); + return null; + } + + static Dictionary GetRuleSections() { + Dictionary sections = new Dictionary(); + if (!File.Exists("text/rules.txt")) return sections; + + List rules = CP437Reader.ReadAllLines("text/rules.txt"); + foreach (string rule in rules) + AddRule(rule, sections); + return sections; + } + + static void AddRule(string rule, Dictionary sections) { + int num = -1; + rule = Colors.StripColors(rule); + + for (int i = 0; i < rule.Length; i++) { + char c = rule[i]; + bool isNumber = c >= '0' && c <= '9'; + bool isLetter = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); + if (!isNumber && !isLetter) continue; + // Found start of a word, but didn't find a number - assume this is a non-numbered rule + if (isLetter && num == -1) return; + + if (isNumber) { // 1) Do not do X + if (num == -1) num = 0; + num *= 10; num += (c - '0'); + } else { + sections[num] = rule.Substring(i); + return; + } + } + } + } +} diff --git a/MCGalaxy_.csproj b/MCGalaxy_.csproj index 2ee79ff9f..fb352bbcf 100644 --- a/MCGalaxy_.csproj +++ b/MCGalaxy_.csproj @@ -333,6 +333,7 @@ +