Get rid of the code duplication between title/model/skin/titlecolor/nick/color commands.

This commit is contained in:
UnknownShadow200 2016-09-10 21:50:08 +10:00
parent 2ff6d047e8
commit e2404c46fa
9 changed files with 182 additions and 221 deletions

View File

@ -19,13 +19,10 @@ using System;
using MCGalaxy.Bots;
namespace MCGalaxy.Commands.CPE {
public class CmdModel : Command {
public class CmdModel : EntityPropertyCmd {
public override string name { get { return "model"; } }
public override string shortcut { get { return "setmodel"; } }
public override string type { get { return CommandTypes.Other; } }
public override bool museumUsable { get { return true; } }
public override LevelPermission defaultRank { get { return LevelPermission.AdvBuilder; } }
public override CommandPerm[] ExtraPerms {
get { return new[] { new CommandPerm(LevelPermission.Operator, "+ can change the model of others") }; }
@ -35,63 +32,46 @@ namespace MCGalaxy.Commands.CPE {
}
public override void Use(Player p, string message) {
if (CheckSuper(p, message, "player or bot name")) return;
if (message == "") message = "humanoid";
Player who = p;
PlayerBot bot = null;
bool isBot = message.CaselessStarts("bot ");
string[] args = message.SplitSpaces(isBot ? 3 : 2);
string model = null;
if (args[0].CaselessEq("-own")) {
if (Player.IsSuper(p)) { SuperRequiresArgs(p, "player name"); return; }
args[0] = p.name;
if (args.Length == 1) message = "humanoid";
if (message.IndexOf(' ') == -1) {
message = "-own " + message;
message = message.TrimEnd();
}
UseBotOrPlayer(p, message, "model");
}
if (isBot && args.Length > 2) {
bot = PlayerBot.FindMatchesPreferLevel(p, args[1]);
if (bot == null) return;
model = args[2];
} else if (args.Length > 1) {
isBot = false;
who = PlayerInfo.FindMatches(p, args[0]);
if (who == null) return;
model = args.Length >= 2 ? args[1] : "humanoid";
} else {
isBot = false;
who = p;
if (who == null) { SuperRequiresArgs(p, "player name"); return; }
model = message;
}
protected override void SetBotData(Player p, PlayerBot bot, string[] args) {
string model = GetModel(p, args, 2);
bot.model = model;
Entities.UpdateModel(bot.id, model, bot.level, null);
Chat.MessageLevel(bot.level, "Bot " + bot.ColoredName + "'s %Smodel was changed to a &c" + model);
BotsFile.UpdateBot(bot);
}
protected override void SetPlayerData(Player p, Player who, string[] args) {
string model = GetModel(p, args, 1);
who.model = model;
Entities.UpdateModel(who.id, model, who.level, who);
if (p != who) {
Player.GlobalMessage(who, who.ColoredName + "'s %Smodel was changed to a &c" + model);
} else {
Player.Message(who, "Changed your own model to a &c" + model);
}
if (model != "humanoid") {
Server.models.AddOrReplace(who.name, model);
} else {
Server.models.Remove(who.name);
}
Server.models.Save();
}
static string GetModel(Player p, string[] args, int i) {
string model = args.Length > i ? args[i] : "humanoid";
model = model.ToLower();
model = model.Replace(':', '|'); // since many assume : is for scale instead of |.
if (p != null && who != null && who.Rank > p.Rank) {
MessageTooHighRank(p, "change the model of", true); return;
}
if ((isBot || who != p) && !CheckExtraPerm(p)) { MessageNeedExtra(p, "change the model of others."); return; }
if (isBot) {
bot.model = model;
Entities.UpdateModel(bot.id, model, bot.level, null);
Chat.MessageLevel(bot.level, "Bot " + bot.ColoredName + "'s %Smodel was changed to a &c" + model);
BotsFile.UpdateBot(bot);
} else {
who.model = model;
Entities.UpdateModel(who.id, model, who.level, who);
if (p != who)
Player.GlobalMessage(who, who.ColoredName + "'s %Smodel was changed to a &c" + model);
else
Player.Message(who, "Changed your own model to a &c" + model);
if (model != "humanoid")
Server.models.AddOrReplace(who.name, model);
else
Server.models.Remove(who.name);
Server.models.Save();
}
return model;
}
public override void Help(Player p) {

View File

@ -19,74 +19,61 @@ using System;
using MCGalaxy.Bots;
namespace MCGalaxy.Commands.CPE {
public class CmdSkin : Command {
public class CmdSkin : EntityPropertyCmd {
public override string name { get { return "skin"; } }
public override string shortcut { get { return ""; } }
public override string type { get { return CommandTypes.Other; } }
public override bool museumUsable { get { return true; } }
public override LevelPermission defaultRank { get { return LevelPermission.Operator; } }
public override CommandPerm[] ExtraPerms {
get { return new[] { new CommandPerm(LevelPermission.Operator, "+ can change the skin of others") }; }
}
public override void Use(Player p, string message) {
if (CheckSuper(p, message, "player or bot name")) return;
if (message == "") message = p.truename;
if (message.IndexOf(' ') == -1) {
message = "-own " + message;
message = message.TrimEnd();
}
UseBotOrPlayer(p, message, "skin");
}
Player who = p;
PlayerBot bot = null;
bool isBot = message.CaselessStarts("bot ");
string[] args = message.SplitSpaces(isBot ? 3 : 2);
string skin = null;
protected override void SetBotData(Player p, PlayerBot bot, string[] args) {
string skin = GetSkin(p, args, 2, bot.name);
if (skin == null) return;
bot.SkinName = skin;
Chat.MessageLevel(bot.level, "Bot " + bot.ColoredName + "'s %Sskin was changed to &c" + skin);
if (isBot && args.Length > 2) {
bot = PlayerBot.FindMatchesPreferLevel(p, args[1]);
if (bot == null) return;
skin = args[2];
} else if (args.Length >= 2) {
isBot = false;
who = PlayerInfo.FindMatches(p, args[0]);
if (who == null) return;
skin = args.Length >= 2 ? args[1] : who.truename;
bot.GlobalDespawn();
bot.GlobalSpawn();
BotsFile.UpdateBot(bot);
}
protected override void SetPlayerData(Player p, Player who, string[] args) {
string skin = GetSkin(p, args, 1, who.truename);
if (skin == null) return;
who.skinName = skin;
Entities.GlobalDespawn(who, true);
Entities.GlobalSpawn(who, true);
if (p != who) {
Player.GlobalMessage(who, who.ColoredName + "'s %Sskin was changed to &c" + skin);
} else {
isBot = false;
who = p;
if (who == null) { SuperRequiresArgs(p, "player name"); return; }
skin = message;
Player.Message(who, "Changed your own skin to &c" + skin);
}
if (!Formatter.ValidName(p, skin, "skin")) return;
if (p != null && who != null && who.Rank > p.Rank) {
MessageTooHighRank(p, "change the skin of", true); return;
if (skin == who.truename) {
Server.skins.Remove(who.name);
} else {
Server.skins.AddOrReplace(who.name, skin);
}
if ((isBot || who != p) && !CheckExtraPerm(p)) { MessageNeedExtra(p, "change the skin of others."); return; }
Server.skins.Save();
}
static string GetSkin(Player p, string[] args, int i, string defSkin) {
string skin = args.Length > i ? args[i] : defSkin;
if (!Formatter.ValidName(p, skin, "skin")) return null;
if (skin[0] == '+')
skin = "http://skins.minecraft.net/MinecraftSkins/" + skin.Substring(1) + ".png";
if (isBot) {
bot.SkinName = skin;
bot.GlobalDespawn();
bot.GlobalSpawn();
Chat.MessageLevel(bot.level, "Bot " + bot.ColoredName + "'s %Sskin was changed to &c" + skin);
BotsFile.UpdateBot(bot);
} else {
who.skinName = skin;
Entities.GlobalDespawn(who, true);
Entities.GlobalSpawn(who, true);
if (p != who)
Player.GlobalMessage(who, who.ColoredName + "'s %Sskin was changed to &c" + skin);
else
Player.Message(who, "Changed your own skin to &c" + skin);
if (skin == who.truename)
Server.skins.Remove(who.name);
else
Server.skins.AddOrReplace(who.name, skin);
Server.skins.Save();
}
return skin;
}
public override void Help(Player p) {

View File

@ -19,11 +19,10 @@ using MCGalaxy.Bots;
using MCGalaxy.SQL;
namespace MCGalaxy.Commands {
public class CmdColor : Command {
public class CmdColor : EntityPropertyCmd {
public override string name { get { return "color"; } }
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 override CommandPerm[] ExtraPerms {
get { return new[] { new CommandPerm(LevelPermission.Operator, "+ can change the color of others") }; }
@ -32,42 +31,21 @@ namespace MCGalaxy.Commands {
get { return new[] { new CommandAlias("colour"), new CommandAlias("xcolor", "-own") }; }
}
public override void Use(Player p, string message) {
if (message == "") { Help(p); return; }
string[] args = message.Split(' ');
if (args[0].CaselessEq("-own")) {
if (Player.IsSuper(p)) { SuperRequiresArgs(p, "player name"); return; }
args[0] = p.name;
}
public override void Use(Player p, string message) { UseBotOrPlayer(p, message, "color"); }
Player who = null;
PlayerBot bot = null;
bool isBot = message.CaselessStarts("bot ");
if (isBot) bot = PlayerBot.FindMatchesPreferLevel(p, args[1]);
else who = PlayerInfo.FindMatches(p, args[0]);
if (bot == null && who == null) return;
if (p != null && who != null && who.Rank > p.Rank) {
MessageTooHighRank(p, "change the color of", true); return;
}
if ((isBot || who != p) && !CheckExtraPerm(p)) { MessageNeedExtra(p, "change the color of others."); return; }
if (isBot) SetBotColor(p, bot, args);
else SetColor(p, who, args);
}
static void SetBotColor(Player p, PlayerBot bot, string[] args) {
string color = args.Length == 2 ? "&1" : Colors.Parse(args[2]);
protected override void SetBotData(Player p, PlayerBot bot, string[] args) {
string color = args.Length > 2 ? Colors.Parse(args[2]) : "&1";
if (color == "") { Player.Message(p, "There is no color \"" + args[2] + "\"."); return; }
Chat.MessageLevel(bot.level, "Bot " + bot.ColoredName + "'s %Scolor was set to "
+ color + Colors.Name(color));
bot.color = color;
bot.GlobalDespawn();
bot.GlobalSpawn();
BotsFile.UpdateBot(bot);
}
static void SetColor(Player p, Player who, string[] args) {
protected override void SetPlayerData(Player p, Player who, string[] args) {
if (args.Length == 1) {
Player.SendChatFrom(who, who.ColoredName + " %Shad their color removed.", false);
who.color = who.group.color;
@ -80,6 +58,7 @@ namespace MCGalaxy.Commands {
who.color = color;
Database.Execute("UPDATE Players SET color = @1 WHERE Name = @0", who.name, color);
}
Entities.GlobalDespawn(who, true);
Entities.GlobalSpawn(who, true);
who.SetPrefix();
@ -88,7 +67,7 @@ namespace MCGalaxy.Commands {
public override void Help(Player p) {
Player.Message(p, "%T/color [player] [color]");
Player.Message(p, "%HSets the nick color of that player");
Player.Message(p, "%HIf no [color] is given, reverts to player's rank color.");
Player.Message(p, "%H If [color] is not given, reverts to player's rank color.");
Player.Message(p, "%H/color bot [bot] [color]");
Player.Message(p, "%TSets the name color of that bot.");
Player.Message(p, "%HTo see a list of all colors, use /help colors.");

View File

@ -20,11 +20,10 @@ using MCGalaxy;
using MCGalaxy.Bots;
namespace MCGalaxy.Commands {
public class CmdNick : Command {
public class CmdNick : EntityPropertyCmd {
public override string name { get { return "nick"; } }
public override string shortcut { get { return "nickname"; } }
public override string type { get { return CommandTypes.Chat; } }
public override bool museumUsable { get { return true; } }
public override LevelPermission defaultRank { get { return LevelPermission.Operator; } }
public override CommandPerm[] ExtraPerms {
get { return new[] { new CommandPerm(LevelPermission.Operator, "+ can change the nick of other players") }; }
@ -32,31 +31,9 @@ namespace MCGalaxy.Commands {
public override CommandAlias[] Aliases {
get { return new[] { new CommandAlias("xnick", "-own") }; }
}
public override void Use(Player p, string message) { UseBotOrPlayer(p, message, "nick"); }
public override void Use(Player p, string message) {
if (message == "") { Help(p); return; }
bool isBot = message.CaselessStarts("bot ");
string[] args = message.SplitSpaces(isBot ? 3 : 2);
if (args[0].CaselessEq("-own")) {
if (Player.IsSuper(p)) { SuperRequiresArgs(p, "player name"); return; }
args[0] = p.name;
}
Player who = null;
PlayerBot bot = null;
if (isBot) bot = PlayerBot.FindMatchesPreferLevel(p, args[1]);
else who = PlayerInfo.FindMatches(p, args[0]);
if (bot == null && who == null) return;
if (p != null && who != null && who.Rank > p.Rank) {
MessageTooHighRank(p, "change the nick of", true); return;
}
if ((isBot || who != p) && !CheckExtraPerm(p)) { MessageNeedExtra(p, "change the nick of others."); return; }
if (isBot) SetBotNick(p, bot, args);
else SetNick(p, who, args);
}
static void SetBotNick(Player p, PlayerBot bot, string[] args) {
protected override void SetBotData(Player p, PlayerBot bot, string[] args) {
string newName = args.Length > 2 ? args[2] : "";
if (newName == "") {
bot.DisplayName = bot.name;
@ -73,7 +50,7 @@ namespace MCGalaxy.Commands {
BotsFile.UpdateBot(bot);
}
static void SetNick(Player p, Player who, string[] args) {
protected override void SetPlayerData(Player p, Player who, string[] args) {
string newName = args.Length > 1 ? args[1] : "";
if (newName == "") {
who.DisplayName = who.truename;
@ -92,7 +69,7 @@ namespace MCGalaxy.Commands {
public override void Help(Player p) {
Player.Message(p, "%T/nick [player] [nick]");
Player.Message(p, "%HSets the nick of that player.");
Player.Message(p, "%HIf no [nick] is given, reverts player's nick to their original name.");
Player.Message(p, "%H If [nick] is not given, reverts [player]'s nick to their account name.");
Player.Message(p, "%T/nick bot [bot] [name]");
Player.Message(p, "%HSets the name shown above that bot in game.");
Player.Message(p, "%H If [name] is \"empty\", the bot will not have a name shown.");

View File

@ -16,13 +16,12 @@
permissions and limitations under the Licenses.
*/
using MCGalaxy.SQL;
namespace MCGalaxy.Commands {
public class CmdTColor : Command {
namespace MCGalaxy.Commands {
public class CmdTColor : EntityPropertyCmd {
public override string name { get { return "tcolor"; } }
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.Admin; } }
public override CommandPerm[] ExtraPerms {
get { return new[] { new CommandPerm(LevelPermission.Operator, "+ can change the title color of others") }; }
@ -31,24 +30,9 @@ namespace MCGalaxy.Commands {
get { return new[] { new CommandAlias("tcolour"), new CommandAlias("xtcolor", "-own") }; }
}
public override void Use(Player p, string message) {
if (message == "") { Help(p); return; }
string[] args = message.Split(' ');
if (args[0].CaselessEq("-own")) {
if (Player.IsSuper(p)) { SuperRequiresArgs(p, "player name"); return; }
args[0] = p.name;
}
public override void Use(Player p, string message) { UsePlayer(p, message, "title color"); }
Player who = PlayerInfo.FindMatches(p, args[0]);
if (who == null) return;
if (p != null && who.Rank > p.Rank) {
MessageTooHighRank(p, "change the title color of", true); return;
}
if (who != p && !CheckExtraPerm(p)) { MessageNeedExtra(p, "change the title color of others."); return; }
SetTColor(p, who, args);
}
static void SetTColor(Player p, Player who, string[] args) {
protected override void SetPlayerData(Player p, Player who, string[] args) {
if (args.Length == 1) {
Player.SendChatFrom(who, who.ColoredName + " %Shad their title color removed.", false);
who.titlecolor = "";
@ -65,9 +49,9 @@ namespace MCGalaxy.Commands {
}
public override void Help(Player p) {
Player.Message(p, "%T/tcolor <player> [color]");
Player.Message(p, "%T/tcolor [player] [color]");
Player.Message(p, "%HSets the title color of [player]");
Player.Message(p, "%HIf no [color] is specified, title color is removed.");
Player.Message(p, "%H If [color] is not given, title color is removed.");
Player.Message(p, "%HTo see a list of all colors, use /help colors.");
}
}

View File

@ -18,12 +18,10 @@
using MCGalaxy.SQL;
namespace MCGalaxy.Commands {
public class CmdTitle : Command {
public class CmdTitle : EntityPropertyCmd {
public override string name { get { return "title"; } }
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.Admin; } }
public override CommandPerm[] ExtraPerms {
get { return new[] { new CommandPerm(LevelPermission.Admin, "+ can change the title of others") }; }
@ -32,27 +30,11 @@ namespace MCGalaxy.Commands {
get { return new[] { new CommandAlias("xtitle", "-own") }; }
}
public override void Use(Player p, string message) {
if (message == "") { Help(p); return; }
string[] args = message.SplitSpaces(2);
if (args[0].CaselessEq("-own")) {
if (Player.IsSuper(p)) { SuperRequiresArgs(p, "player name"); return; }
args[0] = p.name;
}
public override void Use(Player p, string message) { UsePlayer(p, message, "title"); }
Player who = PlayerInfo.FindMatches(p, args[0]);
if (who == null) return;
if (p != null && who.Rank > p.Rank) {
MessageTooHighRank(p, "change the title of", true); return;
}
if (who != p && !CheckExtraPerm(p)) { MessageNeedExtra(p, "change the title of others."); return; }
SetTitle(p, who, args);
}
static void SetTitle(Player p, Player who, string[] args) {
protected override void SetPlayerData(Player p, Player who, string[] args) {
string title = args.Length > 1 ? args[1] : "";
if (title != "")
title = title.Replace("[", "").Replace("]", "");
title = title.Replace("[", "").Replace("]", "");
if (title.Length >= 20) { Player.Message(p, "Title must be under 20 letters."); return; }
if (title == "") {
@ -67,9 +49,9 @@ namespace MCGalaxy.Commands {
}
public override void Help(Player p) {
Player.Message(p, "%T/title <player> [title]");
Player.Message(p, "%HSets the title of <player>");
Player.Message(p, "%HIf no [title] is given, removes player's title.");
Player.Message(p, "%T/title [player] [title]");
Player.Message(p, "%HSets the title of [player]");
Player.Message(p, "%H If [title] is not given, removes [player]'s title.");
}
}
}

View File

@ -0,0 +1,70 @@
/*
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 abstract class EntityPropertyCmd : Command {
public override bool museumUsable { get { return true; } }
protected void UseBotOrPlayer(Player p, string message, string type) {
if (message == "") { Help(p); return; }
bool isBot = message.CaselessStarts("bot ");
string[] args = message.SplitSpaces(isBot ? 3 : 2);
if (!CheckOwn(p, args, "player or bot name")) return;
Player who = null;
PlayerBot bot = null;
if (isBot) bot = PlayerBot.FindMatchesPreferLevel(p, args[1]);
else who = PlayerInfo.FindMatches(p, args[0]);
if (bot == null && who == null) return;
if (p != null && who != null && who.Rank > p.Rank) {
MessageTooHighRank(p, "change the " + type + " of", true); return;
}
if ((isBot || p != who) && !CheckExtraPerm(p)) { MessageNeedExtra(p, "change the " + type + " of others."); return; }
if (isBot) SetBotData(p, bot, args);
else SetPlayerData(p, who, args);
}
protected void UsePlayer(Player p, string message, string type) {
if (message == "") { Help(p); return; }
string[] args = message.SplitSpaces(2);
if (!CheckOwn(p, args, "player name")) return;
Player who = PlayerInfo.FindMatches(p, args[0]);
if (who == null) return;
if (p != null && who.Rank > p.Rank) {
MessageTooHighRank(p, "change the " + type + " of", true); return;
}
if (p != who && !CheckExtraPerm(p)) { MessageNeedExtra(p, "change the " + type + " of others."); return; }
SetPlayerData(p, who, args);
}
bool CheckOwn(Player p, string[] args, string type) {
if (args[0].CaselessEq("-own")) {
if (Player.IsSuper(p)) { SuperRequiresArgs(p, type); return false; }
args[0] = p.name;
}
return true;
}
protected virtual void SetBotData(Player p, PlayerBot bot, string[] args) { }
protected virtual void SetPlayerData(Player p, Player who, string[] args) { }
}
}

View File

@ -16,6 +16,7 @@
permissions and limitations under the Licenses.
*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
@ -159,7 +160,7 @@ PRIMARY KEY(player)
Database.Execute("UPDATE Players SET Money=@0 WHERE Name=@1", money, name);
}
public static Item[] Items = { new ColorItem(), new TitleColorItem(),
public static List<Item> Items = new List<Item>() { new ColorItem(), new TitleColorItem(),
new TitleItem(), new RankItem(), new LevelItem(), new LoginMessageItem(),
new LogoutMessageItem(), new BlocksItem(), new QueueLevelItem(),
new InfectMessageItem(), new NickItem(), new ReviveItem(),

View File

@ -221,6 +221,7 @@
<Compile Include="Commands\Economy\CmdStore.cs" />
<Compile Include="Commands\Economy\CmdTake.cs" />
<Compile Include="Commands\Economy\MoneyCmd.cs" />
<Compile Include="Commands\EntityPropertyCmd.cs" />
<Compile Include="Commands\Fun\Cmd8Ball.cs" />
<Compile Include="Commands\Fun\CmdAka.cs" />
<Compile Include="Commands\Fun\CmdCountdown.cs" />