diff --git a/Commands/Bots/CmdBotAI.cs b/Commands/Bots/CmdBotAI.cs index 008887e95..09c31b673 100644 --- a/Commands/Bots/CmdBotAI.cs +++ b/Commands/Bots/CmdBotAI.cs @@ -36,7 +36,7 @@ namespace MCGalaxy.Commands if (args.Length < 2) { Help(p); return; } string ai = args[1].ToLower(); - if (!Player.ValidName(ai)) { Player.Message(p, "Invalid AI name!"); return; } + if (!ValidName(p, ai, "bot AI")) return; if (ai == "hunt" || ai == "kill") { Player.Message(p, "Reserved for special AI."); return; } switch (args[0].ToLower()) { diff --git a/Commands/Bots/CmdBotAdd.cs b/Commands/Bots/CmdBotAdd.cs index 5da7aeda1..6e0f74c6e 100644 --- a/Commands/Bots/CmdBotAdd.cs +++ b/Commands/Bots/CmdBotAdd.cs @@ -28,7 +28,7 @@ namespace MCGalaxy.Commands { if (message == "") { Help(p); return; } if (p == null) { MessageInGameOnly(p); return; } - if (!Player.ValidName(message)) { Player.Message(p, "bot name " + message + " not valid!"); return; } + if (!ValidName(p, message, "bot")) return; PlayerBot bot = new PlayerBot(message, p.level, p.pos[0], p.pos[1], p.pos[2], p.rot[0], 0); PlayerBot.Add(bot); } diff --git a/Commands/CPE/CmdCustomColors.cs b/Commands/CPE/CmdCustomColors.cs index 6183a8157..63f522151 100644 --- a/Commands/CPE/CmdCustomColors.cs +++ b/Commands/CPE/CmdCustomColors.cs @@ -199,7 +199,7 @@ namespace MCGalaxy.Commands.CPE { 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 list [offset] %H- lists all custom colors."); Player.Message(p, "%T/customcolors edit [code] [name/fallback/hex]"); } diff --git a/Commands/CPE/CmdSkin.cs b/Commands/CPE/CmdSkin.cs index 331abb631..22749ee01 100644 --- a/Commands/CPE/CmdSkin.cs +++ b/Commands/CPE/CmdSkin.cs @@ -57,9 +57,7 @@ namespace MCGalaxy.Commands.CPE { skin = message; } - if (!Player.ValidName(skin)) { - Player.Message(p, "\"" + skin + "\" is not a valid skin name."); return; - } + if (!ValidName(p, skin, "skin")) return; if ((isBot || who != p) && !CheckExtraPerm(p)) { MessageNeedPerms(p, "can change the skin of other players."); return; } if (isBot) { diff --git a/Commands/Command.Helpers.cs b/Commands/Command.Helpers.cs index 9609a1144..c871ed006 100644 --- a/Commands/Command.Helpers.cs +++ b/Commands/Command.Helpers.cs @@ -89,6 +89,12 @@ namespace MCGalaxy { Player.Message(p, "Can only {0} players ranked below {1}", action, grp.ColoredName); } + protected static bool ValidName(Player p, string name, string type) { + if (Player.ValidName(name)) return true; + Player.Message(p, "\"{0}\" is not a valid {1} name.", name, type); + return false; + } + internal void MessageCannotUse(Player p) { var perms = GrpCommands.allowedCommands.Find(C => C.commandName == name); if (perms.disallow.Contains(p.group.Permission)) { diff --git a/Commands/Information/CmdWhois.cs b/Commands/Information/CmdWhois.cs index 984fe82a4..2985bcbfc 100644 --- a/Commands/Information/CmdWhois.cs +++ b/Commands/Information/CmdWhois.cs @@ -44,9 +44,7 @@ namespace MCGalaxy.Commands { if (matches == 1) { info = FromOnline(pl); } else { - if (!Player.ValidName(message)) { - Player.Message(p, "\"" + message + "\" is not a valid player name."); return; - } + if (!ValidName(p, message, "player")) return; Player.Message(p, "Searching database for the player.."); OfflinePlayer target = PlayerInfo.FindOfflineMatches(p, message); if (target == null) return; diff --git a/Commands/Moderation/CmdBan.cs b/Commands/Moderation/CmdBan.cs index c7c83a33c..14e939451 100644 --- a/Commands/Moderation/CmdBan.cs +++ b/Commands/Moderation/CmdBan.cs @@ -43,9 +43,7 @@ namespace MCGalaxy.Commands.Moderation { Player who = PlayerInfo.Find(args[0]); string target = who == null ? args[0] : who.name; - if (!Player.ValidName(target)) { - Player.Message(p, "Invalid name \"" + target + "\"."); return; - } + if (!ValidName(p, target, "player")) return; Group group = who == null ? Group.findPlayerGroup(args[0]) : who.group; if (!CheckPerms(target, group, p)) return; diff --git a/Commands/Moderation/CmdInfoSwap.cs b/Commands/Moderation/CmdInfoSwap.cs index d02da5ff2..bf3d7aa10 100644 --- a/Commands/Moderation/CmdInfoSwap.cs +++ b/Commands/Moderation/CmdInfoSwap.cs @@ -31,12 +31,8 @@ namespace MCGalaxy.Commands { public override void Use(Player p, string text) { string[] args = text.Split(' '); if (args.Length != 2) { Help(p); return; } - if (!Player.ValidName(args[0])) { - Player.SendMessage(p, "\"" + args[0] + "\" is not a valid player name."); return; - } - if (!Player.ValidName(args[1])) { - Player.SendMessage(p, "\"" + args[1] + "\" is not a valid player name."); return; - } + if (!ValidName(p, args[0], "player")) return; + if (!ValidName(p, args[1], "player")) return; if (PlayerInfo.FindExact(args[0]) != null) { Player.SendMessage(p, "\"" + args[0] + "\" must be offline to use /infoswap."); return; diff --git a/Commands/Moderation/CmdTempBan.cs b/Commands/Moderation/CmdTempBan.cs index 7ab9e87b9..ef789c33e 100644 --- a/Commands/Moderation/CmdTempBan.cs +++ b/Commands/Moderation/CmdTempBan.cs @@ -31,7 +31,7 @@ namespace MCGalaxy.Commands.Moderation { Player who = PlayerInfo.Find(args[0]); string target = who == null ? args[0] : who.name; - if (!Player.ValidName(target)) { Player.Message(p, "Invalid name \"" + target + "\"."); return; } + if (!ValidName(p, target, "player")) return; Group grp = who == null ? PlayerInfo.GetGroup(target) : who.group; if (p != null && grp.Permission >= p.group.Permission) { MessageTooHighRank(p, "temp ban", false); return; diff --git a/Commands/Moderation/CmdZone.cs b/Commands/Moderation/CmdZone.cs index b209b9f8d..e320b87b7 100644 --- a/Commands/Moderation/CmdZone.cs +++ b/Commands/Moderation/CmdZone.cs @@ -95,7 +95,7 @@ namespace MCGalaxy.Commands { } else { Help(p); return; } - if (!Player.ValidName(cpos.Owner)) { Player.Message(p, "INVALID NAME."); return; } + if (!ValidName(p, cpos.Owner, "player or rank")) return; cpos.x = 0; cpos.y = 0; cpos.z = 0; p.blockchangeObject = cpos; diff --git a/Commands/World/CmdCopyLVL.cs b/Commands/World/CmdCopyLVL.cs index b6383b080..ecf2ea64f 100644 --- a/Commands/World/CmdCopyLVL.cs +++ b/Commands/World/CmdCopyLVL.cs @@ -41,7 +41,7 @@ namespace MCGalaxy.Commands.World { } src = LevelInfo.FindMapMatches(p, src); if (src == null) return; - if (!Player.ValidName(dst)) { Player.Message(p, "\"" + dst + "\" is not a valid level name."); return; } + if (!ValidName(p, dst, "level")) return; if (LevelInfo.ExistsOffline(dst)) { Player.Message(p, "The level \"" + dst + "\" already exists."); return; } try { diff --git a/Commands/World/CmdDeleteLvl.cs b/Commands/World/CmdDeleteLvl.cs index d5c6be135..4e9f94ae9 100644 --- a/Commands/World/CmdDeleteLvl.cs +++ b/Commands/World/CmdDeleteLvl.cs @@ -38,7 +38,7 @@ namespace MCGalaxy.Commands.World { lvl.Unload(); } - if (!Player.ValidName(message)) { Player.Message(p, "\"" + message + "\" is not a valid level name."); return; } + if (!ValidName(p, message, "level")) return; if (lvl == Server.mainLevel) { Player.Message(p, "Cannot delete the main level."); return; } string map = LevelInfo.FindMapMatches(p, message); diff --git a/Commands/World/CmdLockdown.cs b/Commands/World/CmdLockdown.cs index 0887d3e36..99568c0de 100644 --- a/Commands/World/CmdLockdown.cs +++ b/Commands/World/CmdLockdown.cs @@ -39,9 +39,7 @@ namespace MCGalaxy.Commands { if (args[0].CaselessEq("map")) { args[1] = args[1].ToLower(); - if (!Player.ValidName(args[1])) { - Player.Message(p, "\"" + args[1] + "\" is not a valid level name."); return; - } + if (!ValidName(p, args[1], "level")) return; string path = "text/lockdown/map/" + args[1]; if (!File.Exists(path)) { diff --git a/Commands/World/CmdMain.cs b/Commands/World/CmdMain.cs index 488bc668e..aebdb285e 100644 --- a/Commands/World/CmdMain.cs +++ b/Commands/World/CmdMain.cs @@ -36,9 +36,7 @@ namespace MCGalaxy.Commands.World { PlayerActions.ChangeMap(p, Server.mainLevel.name); } else { if (!CheckExtraPerm(p)) { MessageNeedPerms(p, "change the main level"); return; } - if (!Player.ValidName(message)) { - Player.Message(p, "\"{0}\" is not a valid level name.", message); return; - } + if (!ValidName(p, message, "level")) return; string map = LevelInfo.FindMapMatches(p, message); if (map == null) return; diff --git a/Commands/World/CmdNewLvl.cs b/Commands/World/CmdNewLvl.cs index 14f430064..6f996ac06 100644 --- a/Commands/World/CmdNewLvl.cs +++ b/Commands/World/CmdNewLvl.cs @@ -43,9 +43,7 @@ namespace MCGalaxy.Commands.World { if (!MapGen.OkayAxis(y)) { Player.Message(p, "height must be divisible by 16, and >= 16"); return; } if (!MapGen.OkayAxis(z)) { Player.Message(p, "length must be divisible by 16, and >= to 16."); return; } - if (!Player.ValidName(name)) { - Player.Message(p, "Invalid name!"); return; - } + if (!ValidName(p, name, "level")) return; if (LevelInfo.ExistsOffline(name)) { Player.Message(p, "Level \"" + name + "\" already exists!"); return; } diff --git a/Commands/World/CmdRenameLvl.cs b/Commands/World/CmdRenameLvl.cs index e0ff9b60f..a449238a2 100644 --- a/Commands/World/CmdRenameLvl.cs +++ b/Commands/World/CmdRenameLvl.cs @@ -34,9 +34,7 @@ namespace MCGalaxy.Commands.World { Level lvl = LevelInfo.FindMatches(p, args[0]); if (lvl == null) return; string newName = args[1]; - if (!Player.ValidName(newName)) { - Player.Message(p, "\"" + newName + "\" is not a valid level name."); return; - } + if (!ValidName(p, newName, "level")) return; if (LevelInfo.ExistsOffline(newName)) { Player.Message(p, "Level already exists."); return; } if (lvl == Server.mainLevel) { Player.Message(p, "Cannot rename the main level."); return; } @@ -50,7 +48,7 @@ namespace MCGalaxy.Commands.World { public override void Help(Player p) { Player.Message(p, "%T/renamelvl "); - Player.Message(p, "%H- Renames to "); + Player.Message(p, "%HRenames to "); Player.Message(p, "%HNote: Portals going to will no longer work."); } } diff --git a/Commands/building/CmdCopy.cs b/Commands/building/CmdCopy.cs index bb8bd80a3..753095f84 100644 --- a/Commands/building/CmdCopy.cs +++ b/Commands/building/CmdCopy.cs @@ -164,9 +164,7 @@ namespace MCGalaxy.Commands.Building { } void SaveCopy(Player p, string file) { - if (!Player.ValidName(file)) { - Player.Message(p, "Bad file name"); return; - } + if (!ValidName(p, file, "saved copy")) return; if (!Directory.Exists("extra/savecopy")) Directory.CreateDirectory("extra/savecopy"); diff --git a/Commands/other/CmdReport.cs b/Commands/other/CmdReport.cs index 9ecb8b7e5..d45632af5 100644 --- a/Commands/other/CmdReport.cs +++ b/Commands/other/CmdReport.cs @@ -82,9 +82,7 @@ namespace MCGalaxy.Commands { Player.Message(p, "You need to provide a player's name."); return; } if (!CheckExtraPerm(p)) { MessageNeedPerms(p, "can view the details of a report."); return; } - if (!Player.ValidName(args[1])) { - Player.Message(p, "\"" + args[1] + "\" is not a valid player name."); return; - } + if (!ValidName(p, args[1], "player")) return; if (!File.Exists("extra/reported/" + args[1] + ".txt")) { Player.Message(p, "The player you specified has not been reported."); return; @@ -98,9 +96,7 @@ namespace MCGalaxy.Commands { Player.Message(p, "You need to provide a player's name."); return; } if (!CheckExtraPerm(p)) { MessageNeedPerms(p, "can delete reports."); return; } - if (!Player.ValidName(args[1])) { - Player.Message(p, "\"" + args[1] + "\" is not a valid player name."); return; - } + if (!ValidName(p, args[1], "player")) return; if (!File.Exists("extra/reported/" + args[1] + ".txt")) { Player.Message(p, "The player you specified has not been reported."); return; diff --git a/Player/Player.cs b/Player/Player.cs index 3654f6e1e..42cf0146c 100644 --- a/Player/Player.cs +++ b/Player/Player.cs @@ -681,8 +681,11 @@ Next: continue; } public static bool ValidName(string name) { - string allowedchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890._+"; - return name.All(ch => allowedchars.IndexOf(ch) != -1); + const string valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890._+"; + foreach (char c in name) { + if (valid.IndexOf(c) == -1) return false; + } + return true; } public static int GetBannedCount() {