Add /ccols edit, closes #185. (Thanks worldshowoff)

This commit is contained in:
UnknownShadow200 2016-06-16 09:48:18 +10:00
parent 3492e7f116
commit 4a31395a06
2 changed files with 96 additions and 43 deletions

View File

@ -35,13 +35,15 @@ namespace MCGalaxy.Commands.CPE {
switch (args[0].ToLower()) { switch (args[0].ToLower()) {
case "add": case "add":
case "create": case "create":
case "new":
AddHandler(p, args); break; AddHandler(p, args); break;
case "remove": case "remove":
case "delete": case "delete":
RemoveHandler(p, args); break; RemoveHandler(p, args); break;
case "list": case "list":
ListHandler(p, args, false); break; ListHandler(p, args, false); break;
case "edit":
case "modify":
EditHandler(p, args); break;
default: default:
Help(p); break; Help(p); break;
} }
@ -52,10 +54,10 @@ namespace MCGalaxy.Commands.CPE {
char code = args[1][0]; char code = args[1][0];
if (Colors.IsStandardColor(code)) { if (Colors.IsStandardColor(code)) {
Player.Message(p, code + " is a standard color code, and thus cannot be removed."); return; Player.Message(p, code + " is a standard color code, and thus cannot be replaced."); return;
} }
if (code <= ' ' || code > '~' || code == '%' || code == '&') { if (code <= ' ' || code > '~' || code == '%' || code == '&') {
Player.Message(p, code + " must be a standard ASCII character."); Player.Message(p, "{0} must be a standard ASCII character.", code);
Player.Message(p, "It also cannot be a space, percentage, or ampersand."); Player.Message(p, "It also cannot be a space, percentage, or ampersand.");
return; return;
} }
@ -63,33 +65,18 @@ namespace MCGalaxy.Commands.CPE {
char code2 = code; char code2 = code;
if (Colors.MapColor(ref code2)) { if (Colors.MapColor(ref code2)) {
Player.Message(p, "There is already a custom or server defined color with the code " + code + Player.Message(p, "There is already a custom or server defined color with the code " + code +
", you must either use a different code or use \"%T/ccols remove " + code + "%S\""); ", you must either use a different code or use \"%T/ccols remove " + code + "%S\"");
return; return;
} }
string name = args[2]; char fallback;
if (Colors.Parse(name) != "") { if (!CheckName(p, args[2]) || !CheckFallback(p, args[3], out fallback)
Player.Message(p, "There is already an existing standard or " + || !CheckHex(p, ref args[4])) return;
"custom color with the name \"" + name + "\"."); return;
}
char fallback = args[3][0];
if (!Colors.IsStandardColor(fallback)) {
Player.Message(p, fallback + " must be a standard color code."); return;
}
if (fallback >= 'A' && fallback <= 'F') fallback += ' ';
string hex = args[4];
if (hex.Length > 0 && hex[0] == '#')
hex = hex.Substring(1);
if (hex.Length != 6 || !IsValidHex(hex)) {
Player.Message(p, "\"#" + hex + "\" is not a valid hex color."); return;
}
CustomColor col = default(CustomColor); CustomColor col = default(CustomColor);
col.Code = code; col.Fallback = fallback; col.A = 255; col.Code = code; col.Fallback = fallback; col.A = 255;
col.Name = name; col.Name = args[2];
Color rgb = ColorTranslator.FromHtml("#" + hex); Color rgb = ColorTranslator.FromHtml("#" + args[4]);
col.R = rgb.R; col.G = rgb.G; col.B = rgb.B; col.R = rgb.R; col.G = rgb.G; col.B = rgb.B;
Colors.AddExtColor(col); Colors.AddExtColor(col);
Player.Message(p, "Successfully added a custom color."); Player.Message(p, "Successfully added a custom color.");
@ -100,11 +87,11 @@ namespace MCGalaxy.Commands.CPE {
char code = args[1][0]; char code = args[1][0];
if (Colors.IsStandardColor(code)) { if (Colors.IsStandardColor(code)) {
Player.Message(p, code + " is a standard color, and thus cannot be removed."); return; Player.Message(p,"{0} is a standard color, and thus cannot be removed.", code); return;
} }
if ((int)code >= 256 || Colors.ExtColors[code].Undefined) { if ((int)code >= 256 || Colors.ExtColors[code].Undefined) {
Player.Message(p, "There is no custom color with the code " + code + "."); Player.Message(p, "There is no custom color with the code {0}.", code);
Player.Message(p, "Use \"%T/ccols list\" %Sto see a list of custom colors."); Player.Message(p, "Use \"%T/ccols list\" %Sto see a list of custom colors.");
return; return;
} }
@ -123,8 +110,9 @@ namespace MCGalaxy.Commands.CPE {
if (index >= offset) { if (index >= offset) {
count++; count++;
const string format = "{4}{0} &{1}({2}){4} - %{1}, falls back to %{3}."; Server.s.Log(col.Fallback.ToString());
Player.SendMessage(p, String.Format(format, col.Name, col.Code, Hex(col), col.Fallback, Server.DefaultColor), false); const string format = "{4}{0} &{1}({2}){4} - %{1}, falls back to &{3}%{3}.";
Player.SendMessage(p, String.Format(format, col.Name, col.Code, col.Hex(), col.Fallback, Server.DefaultColor), false);
if (count >= 8 && !all) { if (count >= 8 && !all) {
const string helpFormat = "To see the next set of custom colors, type %T/ccols list {0}"; const string helpFormat = "To see the next set of custom colors, type %T/ccols list {0}";
@ -136,15 +124,70 @@ namespace MCGalaxy.Commands.CPE {
} }
} }
public override void Help(Player p) { void EditHandler(Player p, string[] args) {
Player.Message(p, "%T/ccols <add/remove/list>"); if (args.Length < 4) { Help(p); return; }
Player.Message(p, "%H/ccols add [code] [name] [fallback] [hex]");
Player.Message(p, "%H code is in ASCII. You cannot replace the standard color codes."); char code = args[1][0];
Player.Message(p, "%H fallback is the standard color code shown to non-supporting clients."); if (Colors.IsStandardColor(code)) {
Player.Message(p, "%H/ccols remove [code]"); Player.Message(p, "{0} is a standard color, and thus cannot be edited.", code); return;
Player.Message(p, "%H/ccols list [offset] - lists all custom color codes."); }
if ((int)code >= 256 || Colors.ExtColors[code].Undefined) {
Player.Message(p, "There is no custom color with the code {0}.", code);
Player.Message(p, "Use \"%T/ccols list\" %Sto see a list of custom colors.");
return;
}
CustomColor col = Colors.ExtColors[code];
char fallback;
switch (args[2]) {
case "name":
if (!CheckName(p, args[3])) return;
col.Name = args[3]; break;
case "fallback":
if (!CheckFallback(p, args[3], out fallback)) return;
col.Fallback = fallback; break;
case "hex":
case "color":
if (!CheckHex(p, ref args[3])) return;
Color rgb = ColorTranslator.FromHtml("#" + args[3]);
col.R = rgb.R; col.G = rgb.G; col.B = rgb.B;
break;
default:
Help(p); return;
}
Colors.AddExtColor(col);
Player.Message(p, "Successfully edited a custom color.");
} }
static bool CheckName(Player p, string arg) {
if (Colors.Parse(arg) != "") {
Player.Message(p, "There is already an existing standard or " +
"custom color with the name \"{0}\".", arg);
return false;
}
return true;
}
static bool CheckFallback(Player p, string arg, out char fallback) {
fallback = arg[0];
if (!Colors.IsStandardColor(fallback)) {
Player.Message(p, "{0} must be a standard color code.", fallback); return false;
}
if (fallback >= 'A' && fallback <= 'F') fallback += ' ';
return true;
}
static bool CheckHex(Player p, ref string arg) {
if (arg.Length > 0 && arg[0] == '#')
arg = arg.Substring(1);
if (arg.Length != 6 || !IsValidHex(arg)) {
Player.Message(p, "\"#{0}\" is not a valid hex color.", arg); return false;
}
return true;
}
static bool IsValidHex(string hex) { static bool IsValidHex(string hex) {
for (int i = 0; i < hex.Length; i++) { for (int i = 0; i < hex.Length; i++) {
if (!Colors.IsStandardColor(hex[i])) return false; if (!Colors.IsStandardColor(hex[i])) return false;
@ -152,8 +195,14 @@ namespace MCGalaxy.Commands.CPE {
return true; return true;
} }
static string Hex(CustomColor c) { public override void Help(Player p) {
return "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2"); Player.Message(p, "%T/customcolors add [code] [name] [fallback] [hex]");
Player.Message(p, "%H code is a single ascii character.");
Player.Message(p, "%H fallback is the color code shown to non-supporting clients.");
Player.Message(p, "%T/customcolors remove [code] %H- Removes that custom color.");
Player.Message(p, "%T/customcolors list [offset] %H - lists all custom colors.");
Player.Message(p, "%T/customcolors edit [code] [name/fallback/hex]");
} }
} }
} }

View File

@ -162,11 +162,11 @@ namespace MCGalaxy {
} }
public static bool MapColor(ref char color) { public static bool MapColor(ref char color) {
if (IsStandardColor(color)) { if (IsStandardColor(color)) {
if (color >= 'A' && color <= 'F') color += ' '; if (color >= 'A' && color <= 'F') color += ' ';
return true; return true;
} }
if (color == 's' || color == 'S') { color = Server.DefaultColor[1]; return true; } if (color == 's' || color == 'S') { color = Server.DefaultColor[1]; return true; }
if (color == 'h' || color == 'H') { color = Server.HelpDescriptionColor[1]; return true; } if (color == 'h' || color == 'H') { color = Server.HelpDescriptionColor[1]; return true; }
if (color == 't' || color == 'T') { color = Server.HelpSyntaxColor[1]; return true; } if (color == 't' || color == 'T') { color = Server.HelpSyntaxColor[1]; return true; }
@ -175,7 +175,7 @@ namespace MCGalaxy {
} }
public static string StripColours(string value) { public static string StripColours(string value) {
if (value.IndexOf('%') == -1 && value.IndexOf('&') == -1) if (value.IndexOf('%') == -1 && value.IndexOf('&') == -1)
return value; return value;
char[] output = new char[value.Length]; char[] output = new char[value.Length];
int usedChars = 0; int usedChars = 0;
@ -272,5 +272,9 @@ namespace MCGalaxy {
public string Name; public string Name;
public bool Undefined { get { return Fallback == '\0'; } } public bool Undefined { get { return Fallback == '\0'; } }
public string Hex() {
return "#" + R.ToString("X2") + G.ToString("X2") + B.ToString("X2");
}
} }
} }