Allow using @number as a shortcut for the rule by that number in [reason] for /kick /warn /ban (thanks yorkiebarz).

This commit is contained in:
UnknownShadow200 2016-08-25 00:22:26 +10:00
parent 0436384446
commit 1d98b8703e
5 changed files with 94 additions and 14 deletions

View File

@ -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 <player> [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.");
}
}
}

View File

@ -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 <player> [message]");
Player.Message(p, "%T/kick <player> [reason]");
Player.Message(p, "%HKicks a player.");
Player.Message(p, "%HFor [reason], @number can be used as a shortcut for that rule.");
}
}
}

View File

@ -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> <reason>");
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.");
}
}
}

View File

@ -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<int, string> 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<int, string> GetRuleSections() {
Dictionary<int, string> sections = new Dictionary<int, string>();
if (!File.Exists("text/rules.txt")) return sections;
List<string> rules = CP437Reader.ReadAllLines("text/rules.txt");
foreach (string rule in rules)
AddRule(rule, sections);
return sections;
}
static void AddRule(string rule, Dictionary<int, string> 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;
}
}
}
}
}

View File

@ -333,6 +333,7 @@
<Compile Include="Commands\Moderation\CmdXJail.cs" />
<Compile Include="Commands\Moderation\CmdXmute.cs" />
<Compile Include="Commands\Moderation\CmdZone.cs" />
<Compile Include="Commands\Moderation\ModActionCmd.cs" />
<Compile Include="Commands\Moderation\RankCmd.cs" />
<Compile Include="Commands\other\CmdAgree.cs" />
<Compile Include="Commands\other\CmdAscend.cs" />