From 42bd448dc2d98b128c8425c85e63c13b30f99937 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 27 Aug 2016 17:27:22 +1000 Subject: [PATCH] Reduce code duplication in /hug /eat /roll /high5, make them automatically work properly when a level has roleplay/level-only chat enabled. --- Commands/Chat/CmdEat.cs | 24 +++------ Commands/Chat/CmdHug.cs | 18 ++----- Commands/Chat/CmdRoll.cs | 13 ++--- Commands/Chat/{CmdHigh5.cs => MessageCmd.cs} | 55 +++++++++++++------- MCGalaxy_.csproj | 2 +- 5 files changed, 52 insertions(+), 60 deletions(-) rename Commands/Chat/{CmdHigh5.cs => MessageCmd.cs} (52%) diff --git a/Commands/Chat/CmdEat.cs b/Commands/Chat/CmdEat.cs index 9ba9ae3d4..c3c965429 100644 --- a/Commands/Chat/CmdEat.cs +++ b/Commands/Chat/CmdEat.cs @@ -21,12 +21,8 @@ using System.IO; namespace MCGalaxy.Commands { - public sealed class CmdEat : Command { + public sealed class CmdEat : MessageCmd { public override string name { get { return "eat"; } } - public override string shortcut { get { return ""; } } - public override string type { get { return CommandTypes.Chat; } } - public override bool museumUsable { get { return true; } } - public override LevelPermission defaultRank { get { return LevelPermission.Guest; } } public override void Use(Player p, string message) { if (p == null) { MessageInGameOnly(p); return; } @@ -38,13 +34,8 @@ namespace MCGalaxy.Commands { if (Economy.Enabled && p.money < 1) { Player.Message(p, "You need to have at least 1 &3" + Server.moneys + " %Sto purchase a snack."); return; - } - if (p.muted) { Player.Message(p, "You cannot use this command while muted."); return; } - - p.NextEat = DateTime.UtcNow.AddSeconds(10); - if (Economy.Enabled) { - p.money -= 1; p.OnMoneyChanged(); } + if (!File.Exists("text/eatmessages.txt")) { File.WriteAllLines("text/eatmessages.txt", defMessages); } @@ -54,12 +45,11 @@ namespace MCGalaxy.Commands { if (actions.Count > 0) action = actions[new Random().Next(actions.Count)]; - if (!p.level.worldChat) { - Chat.GlobalChatLevel(p, "" + p.ColoredName + " %S" + action, false); - } else { - Player.SendChatFrom(p, p.ColoredName + " %S" + action, false); - } - p.CheckForMessageSpam(); + if (!TryMessage(p, p.ColoredName + " %S" + action)) return; + p.NextEat = DateTime.UtcNow.AddSeconds(10); + if (Economy.Enabled) { + p.money -= 1; p.OnMoneyChanged(); + } } static string[] defMessages = { "guzzled a grape", "chewed a cherry", "ate an avocado" }; diff --git a/Commands/Chat/CmdHug.cs b/Commands/Chat/CmdHug.cs index 2626f6857..ee6d3ccdd 100644 --- a/Commands/Chat/CmdHug.cs +++ b/Commands/Chat/CmdHug.cs @@ -15,16 +15,10 @@ or implied. See the Licenses for the specific language governing permissions and limitations under the Licenses. */ -using System; -namespace MCGalaxy.Commands { - - public class CmdHug : Command { +namespace MCGalaxy.Commands { + public class CmdHug : MessageCmd { public override string name { get { return "hug"; } } - public override string shortcut { get { return ""; } } - public override string type { get { return CommandTypes.Chat; } } - public override bool museumUsable { get { return true; } } - public override LevelPermission defaultRank { get { return LevelPermission.Guest; } } public override void Use(Player p, string message) { if (message == "") { Help(p); return; } @@ -42,10 +36,7 @@ namespace MCGalaxy.Commands { if (args[1] == "loving" || args[1] == "creepy" || args[1] == "friendly" || args[1] == "deadly") type = args[1]; } - if (type == null) { - Player.GlobalMessage(p, giver + " %Shugged " + who.ColoredName + "."); - p.CheckForMessageSpam(); return; - } + if (type == null) { TryMessageAction(p, args[0], "{0} %Shugged {1}.", false); return; } if (type == "deadly") { if (p != null && p.Rank < LevelPermission.Operator) { @@ -56,8 +47,7 @@ namespace MCGalaxy.Commands { } who.HandleDeath(Block.rock, " died from a %cdeadly hug."); } - Player.GlobalMessage(p, giver + " %Sgave " + who.ColoredName + " %Sa " + type + " hug."); - p.CheckForMessageSpam(); + TryMessageAction(p, args[0], "{0} %Sgave {1} %Sa " + type + " hug.", false); return; } public override void Help(Player p) { diff --git a/Commands/Chat/CmdRoll.cs b/Commands/Chat/CmdRoll.cs index a285dc1d6..9ef2c41d5 100644 --- a/Commands/Chat/CmdRoll.cs +++ b/Commands/Chat/CmdRoll.cs @@ -18,13 +18,8 @@ using System; namespace MCGalaxy.Commands { - public sealed class CmdRoll : Command { - + public sealed class CmdRoll : MessageCmd { public override string name { get { return "roll"; } } - public override string shortcut { get { return ""; } } - public override string type { get { return CommandTypes.Chat; } } - public override bool museumUsable { get { return true; } } - public override LevelPermission defaultRank { get { return LevelPermission.Operator; } } public CmdRoll() { } public override void Use(Player p, string message) { @@ -35,9 +30,9 @@ namespace MCGalaxy.Commands { if (!int.TryParse(args[0], out min)) min = 1; if (args.Length == 1 || !int.TryParse(args[1], out max)) max = 7; if (min > max) { int a = min; min = max; max = a; } - - Player.GlobalMessage(p, p.ColoredName + " %Srolled a &a" + rand.Next(min, max + 1) + " %S(" + min + "|" + max + ")"); - p.CheckForMessageSpam(); + + string msg = p.ColoredName + " %Srolled a &a" + rand.Next(min, max + 1) + " %S(" + min + "|" + max + ")"; + TryMessage(p, msg); } public override void Help(Player p) { diff --git a/Commands/Chat/CmdHigh5.cs b/Commands/Chat/MessageCmd.cs similarity index 52% rename from Commands/Chat/CmdHigh5.cs rename to Commands/Chat/MessageCmd.cs index 3bf4abd2a..49ac09468 100644 --- a/Commands/Chat/CmdHigh5.cs +++ b/Commands/Chat/MessageCmd.cs @@ -1,46 +1,63 @@ -/* - Copyright 2011 MCForge - - Written by GamezGalaxy (hypereddie10) +/* + 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. -*/ + */ namespace MCGalaxy.Commands { - public sealed class CmdHigh5 : Command { - public override string name { get { return "high5"; } } + + public abstract class MessageCmd : Command { public override string shortcut { get { return ""; } } public override string type { get { return CommandTypes.Chat; } } public override bool museumUsable { get { return true; } } public override LevelPermission defaultRank { get { return LevelPermission.Guest; } } - public CmdHigh5() { } - public override void Use(Player p, string message) { - if (message == "") { Help(p); return; } - Player who = PlayerInfo.FindMatches(p, message); - if (who == null) return; - if (p != null && p.muted) { Player.Message(p, "Cannot use /high5 while muted."); return; } + protected bool TryMessageAction(Player p, string target, string message, bool messageWho) { + if (target == "") { Help(p); return false; } + Player who = PlayerInfo.FindMatches(p, target); + if (who == null) return false; string giver = (p == null) ? "(console)" : p.ColoredName; - Player.Message(who, giver + " just highfived you"); - Player.GlobalMessage(p, giver + " %Sjust highfived " + who.ColoredName); + if (!TryMessage(p, string.Format(message, giver, who.ColoredName))) return false; + if (messageWho) + Player.Message(who, string.Format(message, giver, "you")); + return true; + } + + protected bool TryMessage(Player p, string message) { + if (p != null && p.muted) { Player.Message(p, "Cannot use /{0} while muted.", name); return false; } + + if (p.level.worldChat) { + Player.SendChatFrom(p, message, false); + } else { + Chat.GlobalChatLevel(p, "" + message, false); + } p.CheckForMessageSpam(); + return true; + } + } + + public sealed class CmdHigh5 : MessageCmd { + public override string name { get { return "high5"; } } + + public override void Use(Player p, string message) { + TryMessageAction(p, message, "{0} %Sjust highfived {1}", true); } public override void Help(Player p) { - Player.Message(p, "%T/high5 "); - Player.Message(p, "%HHigh five someone :D"); + Player.Message(p, "%T/high5 [player]"); + Player.Message(p, "%HHigh five someone! :D"); } } } diff --git a/MCGalaxy_.csproj b/MCGalaxy_.csproj index 3b00b9721..d0e89d379 100644 --- a/MCGalaxy_.csproj +++ b/MCGalaxy_.csproj @@ -174,7 +174,6 @@ - @@ -193,6 +192,7 @@ +