Reduce code duplication in /hug /eat /roll /high5, make them automatically work properly when a level has roleplay/level-only chat enabled.

This commit is contained in:
UnknownShadow200 2016-08-27 17:27:22 +10:00
parent 5371872c0a
commit 42bd448dc2
5 changed files with 52 additions and 60 deletions

View File

@ -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; }
@ -39,12 +35,7 @@ namespace MCGalaxy.Commands {
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, "<Level>" + p.ColoredName + " %S" + action, false);
} else {
Player.SendChatFrom(p, p.ColoredName + " %S" + action, false);
if (!TryMessage(p, p.ColoredName + " %S" + action)) return;
p.NextEat = DateTime.UtcNow.AddSeconds(10);
if (Economy.Enabled) {
p.money -= 1; p.OnMoneyChanged();
}
p.CheckForMessageSpam();
}
static string[] defMessages = { "guzzled a grape", "chewed a cherry", "ate an avocado" };

View File

@ -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 {
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) {

View File

@ -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) {
@ -36,8 +31,8 @@ namespace MCGalaxy.Commands {
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) {

View File

@ -1,7 +1,5 @@
/*
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
@ -16,31 +14,50 @@
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, "<Level>" + 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>");
Player.Message(p, "%HHigh five someone :D");
Player.Message(p, "%T/high5 [player]");
Player.Message(p, "%HHigh five someone! :D");
}
}
}

View File

@ -174,7 +174,6 @@
<Compile Include="Commands\Chat\CmdEat.cs" />
<Compile Include="Commands\Chat\CmdEmote.cs" />
<Compile Include="Commands\Chat\CmdGlobalCLS.cs" />
<Compile Include="Commands\Chat\CmdHigh5.cs" />
<Compile Include="Commands\Chat\CmdHug.cs" />
<Compile Include="Commands\Chat\CmdIgnore.cs" />
<Compile Include="Commands\Chat\CmdInbox.cs" />
@ -193,6 +192,7 @@
<Compile Include="Commands\Chat\CmdVote.cs" />
<Compile Include="Commands\Chat\CmdVoteResults.cs" />
<Compile Include="Commands\Chat\CmdWhisper.cs" />
<Compile Include="Commands\Chat\MessageCmd.cs" />
<Compile Include="Commands\CmdOverseer.cs" />
<Compile Include="Commands\Command.cs" />
<Compile Include="Commands\Command.Helpers.cs" />