Skip to content

Commit

Permalink
Add Formatter.ValidPlayerName method which takes into account the suf…
Browse files Browse the repository at this point in the history
…fixes defined by the authentication services

Also fix in GUI the right click player in players table -> Kick showing 'kicked by console' twice in kick message
  • Loading branch information
UnknownShadow200 committed Nov 3, 2021
1 parent d650dc1 commit 35878c8
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 10 deletions.
4 changes: 2 additions & 2 deletions GUI/Window/Window.Main.cs
Expand Up @@ -46,7 +46,7 @@ public partial class Window : Form {
void tsPlayer_Voice_Click(object sender, EventArgs e) { PlayerCmd("Voice"); }
void tsPlayer_Whois_Click(object sender, EventArgs e) { PlayerCmd("WhoIs"); }
void tsPlayer_Ban_Click(object sender, EventArgs e) { PlayerCmd("Ban"); }
void tsPlayer_Kick_Click(object sender, EventArgs e) { PlayerCmd("Kick", "", " You have been kicked by the console."); }
void tsPlayer_Kick_Click(object sender, EventArgs e) { PlayerCmd("Kick"); }
void tsPlayer_Promote_Click(object sender, EventArgs e) { PlayerCmd("SetRank", "+up ", ""); }
void tsPlayer_Demote_Click(object sender, EventArgs e) { PlayerCmd("SetRank", "-down ", ""); }

Expand Down Expand Up @@ -80,7 +80,7 @@ public partial class Window : Form {
void tsMap_Physics5_Click(object sender, EventArgs e) { LevelCmd("Physics", "", " 5"); }
void tsMap_Save_Click(object sender, EventArgs e) { LevelCmd("Save"); }
void tsMap_Unload_Click(object sender, EventArgs e) { LevelCmd("Unload"); }
void tsMap_Reload_Click(object sender, EventArgs e) { LevelCmd("Reload", "", ""); }
void tsMap_Reload_Click(object sender, EventArgs e) { LevelCmd("Reload"); }



Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Commands/Economy/CmdBalance.cs
Expand Up @@ -28,7 +28,7 @@ public sealed class CmdBalance : Command2 {
public override void Use(Player p, string message, CommandData data) {
if (CheckSuper(p, message, "player name")) return;
if (message.Length == 0) message = p.name;
if (!Formatter.ValidName(p, message, "player")) return;
if (!Formatter.ValidPlayerName(p, message)) return;

int matches = 1;
Player who = PlayerInfo.FindMatches(p, message, out matches);
Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Commands/Information/CmdSeen.cs
Expand Up @@ -29,7 +29,7 @@ public sealed class CmdSeen : Command2 {
if (p.IsSuper) { SuperRequiresArgs(p, "player name"); return; }
message = p.name;
}
if (!Formatter.ValidName(p, message, "player")) return;
if (!Formatter.ValidPlayerName(p, message)) return;

int matches;
Player pl = PlayerInfo.FindMatches(p, message, out matches);
Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Commands/Information/CmdWhois.cs
Expand Up @@ -36,7 +36,7 @@ public sealed class CmdWhois : Command2 {
if (p.IsSuper) { SuperRequiresArgs(p, "player name"); return; }
message = p.name;
}
if (!Formatter.ValidName(p, message, "player")) return;
if (!Formatter.ValidPlayerName(p, message)) return;

int matches;
Player who = PlayerInfo.FindMatches(p, message, out matches);
Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Commands/Maintenance/CmdInfoSwap.cs
Expand Up @@ -46,7 +46,7 @@ public sealed class CmdInfoSwap : Command2 {
}

static string GetName(Player p, string name) {
if (!Formatter.ValidName(p, name, "player")) return null;
if (!Formatter.ValidPlayerName(p, name)) return null;
if (PlayerInfo.FindExact(name) != null) {
p.Message("\"{0}\" must be offline to use &T/InfoSwap", name); return null;
}
Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Commands/Moderation/ModActionCmd.cs
Expand Up @@ -148,7 +148,7 @@ public static class ModActionCmd {
/// and requires a confirmation message for non-existent players. </summary>
internal static string FindName(Player p, string action, string cmd,
string cmdSuffix, string name, ref string reason) {
if (!Formatter.ValidName(p, name, "player")) return null;
if (!Formatter.ValidPlayerName(p, name)) return null;
string match = MatchName(p, ref name);
string confirmed = IsConfirmed(reason);
if (confirmed != null) reason = confirmed;
Expand Down
4 changes: 2 additions & 2 deletions MCGalaxy/Player/PlayerInfo.cs
Expand Up @@ -71,7 +71,7 @@ public static class PlayerInfo
/// <returns> A Player instance if exactly one match was found </returns>
public static Player FindMatches(Player pl, string name, out int matches, bool _useless = false) {
matches = 0;
if (!Formatter.ValidName(pl, name, "player")) return null;
if (!Formatter.ValidPlayerName(pl, name)) return null;

// Try to exactly match name first (because names have + at end)
Player exact = FindExact(name);
Expand All @@ -82,7 +82,7 @@ public static class PlayerInfo
}

public static string FindMatchesPreferOnline(Player p, string name) {
if (!Formatter.ValidName(p, name, "player")) return null;
if (!Formatter.ValidPlayerName(p, name)) return null;
int matches;
Player target = FindMatches(p, name, out matches);

Expand Down
17 changes: 16 additions & 1 deletion MCGalaxy/util/Formatting/Formatter.cs
Expand Up @@ -18,6 +18,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using MCGalaxy.Authentication;
using MCGalaxy.Blocks;
using MCGalaxy.Commands;

Expand Down Expand Up @@ -73,11 +74,25 @@ public static class Formatter
public static void MessageNeedMinPerm(Player p, string action, LevelPermission perm) {
p.Message("Only {0}&S{1}", Group.GetColoredName(perm), action);
}


public static bool ValidName(Player p, string name, string type) {
const string alphabet = Player.USERNAME_ALPHABET + "+"; // compatibility with ClassiCubeAccountPlus
if (name.Length > 0 && name.ContainsAllIn(alphabet)) return true;
return IsValidName(p, name, type, alphabet);
}

public static bool ValidPlayerName(Player p, string name) {
string alphabet = Player.USERNAME_ALPHABET + "+"; // compatibility with ClassiCubeAccountPlus

foreach (AuthService service in AuthService.Services)
{
alphabet += service.Config.NameSuffix;
}
return IsValidName(p, name, "player", alphabet);
}

static bool IsValidName(Player p, string name, string type, string alphabet) {
if (name.Length > 0 && name.ContainsAllIn(alphabet)) return true;
p.Message("\"{0}\" is not a valid {1} name.", name, type);
return false;
}
Expand Down

0 comments on commit 35878c8

Please sign in to comment.