Skip to content

Commit

Permalink
Minor API tidyup
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Nov 24, 2022
1 parent 1b9ef7c commit e04ca53
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 74 deletions.
12 changes: 4 additions & 8 deletions MCGalaxy/Commands/building/CmdBrush.cs
Expand Up @@ -38,27 +38,23 @@ public sealed class CmdBrush : Command2 {
BrushFactory brush = BrushFactory.Find(args[0]);

if (IsListCommand(args[0])) {
List(p);
BrushFactory.List(p);
} else if (brush == null) {
p.Message("No brush found with name \"{0}\".", args[0]);
List(p);
BrushFactory.List(p);
} else {
p.Message("Set your brush to: " + brush.Name);
p.BrushName = brush.Name;
p.DefaultBrushArgs = args.Length > 1 ? args[1] : "";
}
}

internal static void List(Player p) {
p.Message("&HAvailable brushes: &f" + BrushFactory.Brushes.Join(b => b.Name));
}

public override void Help(Player p) {
p.Message("&T/Brush [name] <default brush args>");
p.Message("&HSets your current brush to the brush with that name.");
p.Message("&T/Help Brush [name]");
p.Message("&HOutputs the help for the brush with that name.");
List(p);
BrushFactory.List(p);
p.Message("&H- If \"skip\" is used for a block name, " +
"existing blocks in the map will not be replaced by this block.");
}
Expand All @@ -67,7 +63,7 @@ public sealed class CmdBrush : Command2 {
BrushFactory brush = BrushFactory.Find(message);
if (brush == null) {
p.Message("No brush found with name \"{0}\".", message);
List(p);
BrushFactory.List(p);
} else {
p.MessageLines(brush.Help);
}
Expand Down
7 changes: 6 additions & 1 deletion MCGalaxy/Drawing/BrushFactories/BrushFactory.cs
Expand Up @@ -44,11 +44,16 @@ public abstract class BrushFactory
};

public static BrushFactory Find(string name) {
foreach (BrushFactory entry in Brushes) {
foreach (BrushFactory entry in Brushes)
{
if (entry.Name.CaselessEq(name)) return entry;
}
return null;
}

public static void List(Player p) {
p.Message("&HAvailable brushes: &f" + Brushes.Join(b => b.Name));
}
}

public struct BrushArgs
Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Drawing/BrushFactories/ReplaceBrushBrushes.cs
Expand Up @@ -51,7 +51,7 @@ public class ReplaceBrushBrushFactory : BrushFactory
BrushFactory factory = BrushFactory.Find(parts[1]);
if (factory == null) {
p.Message("No brush found with name \"{0}\".", parts[1]);
CmdBrush.List(p); return null;
BrushFactory.List(p); return null;
}

args.Message = parts.Length > 2 ? parts[2] : "";
Expand Down
69 changes: 5 additions & 64 deletions MCGalaxy/util/Formatting/Matcher.cs
Expand Up @@ -98,7 +98,8 @@ public static class Matcher {
StringBuilder nameMatches = new StringBuilder();
const StringComparison comp = StringComparison.OrdinalIgnoreCase;

foreach (T item in items) {
foreach (T item in items)
{
if (filter != null && !filter(item)) continue;
string itemName = nameGetter(item);
if (itemName.Equals(name, comp)) { matches = 1; return item; }
Expand All @@ -114,75 +115,15 @@ public static class Matcher {

if (matches == 1) return match;
if (matches == 0) {
p.Message("No " + group + " match \"" + name + "\".");
} else {
OutputMulti(p, name, nameMatches, matches, group, limit);
}
return default(T);
}

/// <summary> Finds partial matches of 'name' against the names of the items in the 'items' enumerable. </summary>
/// <remarks> Outputs multiple matching entries, as 'items' enumerable may have multiple entries. </remarks>
/// <returns> If exactly one match, the matching list of items. </returns>
public static List<T> FindMulti<T>(Player p, string name, out int matches, IEnumerable items,
Predicate<T> filter, StringFormatter<T> nameGetter, string group, int limit = 5) {
List<T> matchItems = null; matches = 0;
StringBuilder nameMatches = new StringBuilder();
List<string> outputtedNames = new List<string>(limit);
string match = null;
const StringComparison comp = StringComparison.OrdinalIgnoreCase;

foreach (T item in items) {
if (filter != null && !filter(item)) continue;
string itemName = nameGetter(item);

// Found an exact name match - only output items now which exactly match
if (itemName.Equals(name, comp)) {
if (match == null || !name.Equals(match, comp))
matchItems = new List<T>();
matchItems.Add(item);

matches = 1; match = name;
continue;
}

if (itemName.IndexOf(name, comp) < 0) continue;
if (matches == 0) { // Found our first partial match - init the list
matchItems = new List<T>();
matchItems.Add(item);
match = itemName;
} else if (match != null && itemName.Equals(match, comp)) { // Found same partial match
matchItems.Add(item);
}

// We do not want to output the same name multiple times
if (outputtedNames.CaselessContains(itemName) || matches > (limit + 1)) continue;
matches++;

if (matches <= limit) {
nameMatches.Append(itemName).Append(", ");
} else if (matches == limit + 1) {
nameMatches.Append("(and more)").Append(", ");
}
outputtedNames.Add(itemName);
p.Message("No {0} match \"{1}\".", group, name); return default(T);
}

if (matches == 1) return matchItems;
if (matches == 0) {
p.Message("No " + group + " found for \"" + name + "\".");
} else {
OutputMulti(p, name, nameMatches, matches, "players", limit);
}
return null;
}

static void OutputMulti(Player p, string name, StringBuilder nameMatches,
int matches, string group, int limit = 5) {
string count = matches > limit ? limit + "+ " : matches + " ";
string names = nameMatches.ToString(0, nameMatches.Length - 2);

p.Message(count + group + " match \"" + name + "\":");
p.Message("{0}{1} match \"{2}\":", count, group, name);
p.Message(names);
return default(T);
}


Expand Down
222 changes: 222 additions & 0 deletions tatic bool CanEditAny(Player p) {
@@ -0,0 +1,222 @@
diff --git a/MCGalaxy/Bots/PlayerBot.cs b/MCGalaxy/Bots/PlayerBot.cs
index 907acff3..9b6bfafe 100644
--- a/MCGalaxy/Bots/PlayerBot.cs
+++ b/MCGalaxy/Bots/PlayerBot.cs
@@ -73,8 +73,7 @@ namespace MCGalaxy {
public static bool CanEditAny(Player p) {
if (LevelInfo.IsRealmOwner(p.level, p.name)) { return true; }
ItemPerms perms = CommandExtraPerms.Find("Bot", 1) ?? new ItemPerms(LevelPermission.Operator);
- if (perms.UsableBy(p.Rank)) { return true; }
- return false;
+ return perms.UsableBy(p);
}

public static void Add(PlayerBot bot, bool save = true) {
@@ -310,7 +309,7 @@ namespace MCGalaxy {

if (String.IsNullOrEmpty(ClickedOnText)) return;
ItemPerms perms = CommandExtraPerms.Find("About", 1) ?? new ItemPerms(LevelPermission.AdvBuilder);
- if (!perms.UsableBy(p.Rank)) return; //don't show bot's ClickedOnText if player isn't allowed to see message block contents
+ if (!perms.UsableBy(p)) return; //don't show bot's ClickedOnText if player isn't allowed to see message block contents
p.Message(" Clicked-on text: {0}", ClickedOnText);
}

diff --git a/MCGalaxy/Bots/ScriptFile.cs b/MCGalaxy/Bots/ScriptFile.cs
index 36f8a2c4..b291bd2e 100644
--- a/MCGalaxy/Bots/ScriptFile.cs
+++ b/MCGalaxy/Bots/ScriptFile.cs
@@ -66,7 +66,7 @@ namespace MCGalaxy.Bots {
}

CommandExtraPerms killPerms = CommandExtraPerms.Find("BotSet", 1);
- if (ins.Name.CaselessEq("kill") && !killPerms.UsableBy(p.Rank)) {
+ if (ins.Name.CaselessEq("kill") && !killPerms.UsableBy(p)) {
killPerms.MessageCannotUse(p); 
return null;
}
diff --git a/MCGalaxy/Chat/Chat.cs b/MCGalaxy/Chat/Chat.cs
index 85a0cf27..351baf55 100644
--- a/MCGalaxy/Chat/Chat.cs
+++ b/MCGalaxy/Chat/Chat.cs
@@ -90,7 +90,7 @@ namespace MCGalaxy {

static bool DeprecatedFilter(Player pl, object arg) { return false; } 
public static bool FilterRank(Player pl, object arg) { return pl.Rank == (LevelPermission)arg; }
- public static bool FilterPerms(Player pl, object arg) { return ((ItemPerms)arg).UsableBy(pl.Rank); }
+ public static bool FilterPerms(Player pl, object arg) { return ((ItemPerms)arg).UsableBy(pl); }
public static bool FilterPM(Player pl, object arg) { return pl == arg; }

public static ChatMessageFilter[] scopeFilters = new ChatMessageFilter[] {
diff --git a/MCGalaxy/Commands/Moderation/CmdPatrol.cs b/MCGalaxy/Commands/Moderation/CmdPatrol.cs
index ce15cc2a..813be3ac 100644
--- a/MCGalaxy/Commands/Moderation/CmdPatrol.cs
+++ b/MCGalaxy/Commands/Moderation/CmdPatrol.cs
@@ -51,8 +51,9 @@ namespace MCGalaxy.Commands.Moderation {
Player[] players = PlayerInfo.Online.Items;
DateTime cutoff = DateTime.UtcNow.AddSeconds(-15);

- foreach (Player target in players) {
- if (except.UsableBy(target.Rank) || !p.CanSee(target, data.Rank)) continue;
+ foreach (Player target in players) 
+ {
+ if (except.UsableBy(target) || !p.CanSee(target, data.Rank)) continue;
if (target == p || target.LastPatrol > cutoff) continue;
candidates.Add(target);
}
diff --git a/MCGalaxy/Commands/Moderation/CmdReview.cs b/MCGalaxy/Commands/Moderation/CmdReview.cs
index 52f0c007..1b3e0fad 100644
--- a/MCGalaxy/Commands/Moderation/CmdReview.cs
+++ b/MCGalaxy/Commands/Moderation/CmdReview.cs
@@ -70,7 +70,7 @@ namespace MCGalaxy.Commands.Moderation {

foreach (Player pl in PlayerInfo.GetOnlineCanSee(p, data.Rank)) 
{
- if (nextPerms.UsableBy(pl.Rank)) {
+ if (nextPerms.UsableBy(pl)) {
anyStaff = true; break;
}
}
diff --git a/MCGalaxy/Commands/building/CmdBrush.cs b/MCGalaxy/Commands/building/CmdBrush.cs
index ebe5ad38..35c3d57c 100644
--- a/MCGalaxy/Commands/building/CmdBrush.cs
+++ b/MCGalaxy/Commands/building/CmdBrush.cs
@@ -38,10 +38,10 @@ namespace MCGalaxy.Commands.Building {
BrushFactory brush = BrushFactory.Find(args[0]);

if (IsListCommand(args[0])) {
- List(p);
+ BrushFactory.OutputList(p);
} else if (brush == null) {
p.Message("No brush found with name \"{0}\".", args[0]);
- List(p);
+ BrushFactory.OutputList(p);
} else {
p.Message("Set your brush to: " + brush.Name);
p.BrushName = brush.Name;
@@ -49,16 +49,12 @@ namespace MCGalaxy.Commands.Building {
}
}

- internal static void List(Player p) {
- p.Message("&HAvailable brushes: &f" + BrushFactory.Brushes.Join(b => b.Name));
- }
- 
public override void Help(Player p) {
p.Message("&T/Brush [name] <default brush args>");
p.Message("&HSets your current brush to the brush with that name.");
p.Message("&T/Help Brush [name]");
p.Message("&HOutputs the help for the brush with that name.");
- List(p);
+ BrushFactory.OutputList(p);
p.Message("&H- If \"skip\" is used for a block name, " +
"existing blocks in the map will not be replaced by this block.");
}
@@ -67,7 +63,7 @@ namespace MCGalaxy.Commands.Building {
BrushFactory brush = BrushFactory.Find(message);
if (brush == null) {
p.Message("No brush found with name \"{0}\".", message);
- List(p);
+ BrushFactory.OutputList(p);
} else {
p.MessageLines(brush.Help);
}
diff --git a/MCGalaxy/Config/Permissions/ItemPerms.cs b/MCGalaxy/Config/Permissions/ItemPerms.cs
index 297324ef..a1f0e18a 100644
--- a/MCGalaxy/Config/Permissions/ItemPerms.cs
+++ b/MCGalaxy/Config/Permissions/ItemPerms.cs
@@ -47,6 +47,10 @@ namespace MCGalaxy
&& (Disallowed == null || !Disallowed.Contains(perm));
}

+ public bool UsableBy(Player p) {
+ return UsableBy(p.group.Permission);
+ }
+ 
public void Describe(StringBuilder builder) {
builder.Append(Group.GetColoredName(MinRank) + "&S+");

diff --git a/MCGalaxy/CorePlugin/ConnectHandler.cs b/MCGalaxy/CorePlugin/ConnectHandler.cs
index 96313173..ccfee0e9 100644
--- a/MCGalaxy/CorePlugin/ConnectHandler.cs
+++ b/MCGalaxy/CorePlugin/ConnectHandler.cs
@@ -32,7 +32,7 @@ namespace MCGalaxy.Core {
static void CheckReviewList(Player p) {
if (!p.CanUse("Review")) return;
ItemPerms checkPerms = CommandExtraPerms.Find("Review", 1);
- if (!checkPerms.UsableBy(p.Rank)) return;
+ if (!checkPerms.UsableBy(p)) return;

int count = Server.reviewlist.Count;
if (count == 0) return;
diff --git a/MCGalaxy/Database/Stats/OnlineStat.cs b/MCGalaxy/Database/Stats/OnlineStat.cs
index 532406f9..a466598e 100644
--- a/MCGalaxy/Database/Stats/OnlineStat.cs
+++ b/MCGalaxy/Database/Stats/OnlineStat.cs
@@ -115,7 +115,7 @@ namespace MCGalaxy.DB {

public static void IPLine(Player p, string name, string ip) {
ItemPerms seeIpPerms = CommandExtraPerms.Find("WhoIs", 1);
- if (!seeIpPerms.UsableBy(p.Rank)) return;
+ if (!seeIpPerms.UsableBy(p)) return;

string ipMsg = ip;
if (Server.bannedIP.Contains(ip)) ipMsg = "&8" + ip + ", which is banned";
diff --git a/MCGalaxy/Drawing/BrushFactories/BrushFactory.cs b/MCGalaxy/Drawing/BrushFactories/BrushFactory.cs
index b748f2f2..f3a63c9c 100644
--- a/MCGalaxy/Drawing/BrushFactories/BrushFactory.cs
+++ b/MCGalaxy/Drawing/BrushFactories/BrushFactory.cs
@@ -43,12 +43,18 @@ namespace MCGalaxy.Drawing.Brushes
new ReplaceNotBrushBrushFactory(),
};

+ 
public static BrushFactory Find(string name) {
- foreach (BrushFactory entry in Brushes) {
+ foreach (BrushFactory entry in Brushes) 
+ {
if (entry.Name.CaselessEq(name)) return entry;
}
return null;
}
+ 
+ public static void OutputList(Player p) {
+ p.Message("&HAvailable brushes: &f" + Brushes.Join(b => b.Name));
+ }
}

public struct BrushArgs 
diff --git a/MCGalaxy/Drawing/BrushFactories/ReplaceBrushBrushes.cs b/MCGalaxy/Drawing/BrushFactories/ReplaceBrushBrushes.cs
index 9facc855..d34fc815 100644
--- a/MCGalaxy/Drawing/BrushFactories/ReplaceBrushBrushes.cs
+++ b/MCGalaxy/Drawing/BrushFactories/ReplaceBrushBrushes.cs
@@ -51,7 +51,7 @@ namespace MCGalaxy.Drawing.Brushes
BrushFactory factory = BrushFactory.Find(parts[1]);
if (factory == null) {
p.Message("No brush found with name \"{0}\".", parts[1]);
- CmdBrush.List(p); return null;
+ BrushFactory.OutputList(p); return null;
}

args.Message = parts.Length > 2 ? parts[2] : "";
diff --git a/MCGalaxy/Player/Player.Login.cs b/MCGalaxy/Player/Player.Login.cs
index cb5f1895..7a9ebb23 100644
--- a/MCGalaxy/Player/Player.Login.cs
+++ b/MCGalaxy/Player/Player.Login.cs
@@ -105,7 +105,7 @@ namespace MCGalaxy
hidden = CanUse("Hide") && Server.hidden.Contains(name);
if (hidden) Message("&8Reminder: You are still hidden.");

- if (Chat.AdminchatPerms.UsableBy(Rank) && Server.Config.AdminsJoinSilently) {
+ if (Chat.AdminchatPerms.UsableBy(this) && Server.Config.AdminsJoinSilently) {
hidden = true; adminchat = true; 
}

@@ -256,7 +256,7 @@ namespace MCGalaxy
string altsMsg = "λNICK &Sis lately known as: " + alts.Join();

Chat.MessageFrom(p, altsMsg,
- (pl, obj) => pl.CanSee(p) && opchat.UsableBy(pl.Rank));
+ (pl, obj) => pl.CanSee(p) && opchat.UsableBy(pl));

//IRCBot.Say(temp, true); //Tells people in op channel on IRC
altsMsg = altsMsg.Replace("λNICK", name);
Expand Down

0 comments on commit e04ca53

Please sign in to comment.