Skip to content

Commit

Permalink
Change /brush and /transform to support partial name matching
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Jun 7, 2024
1 parent 0872171 commit cfbf37c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 46 deletions.
30 changes: 13 additions & 17 deletions MCGalaxy/Commands/building/CmdBrush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,18 @@ public sealed class CmdBrush : Command2
if (message.Length == 0) {
p.Message("Your current brush is: " + p.BrushName); return;
}

string[] args = message.SplitSpaces(2);
BrushFactory brush = BrushFactory.Find(args[0]);

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

BrushFactory brush = BrushFactory.FindMatch(p, args[0]);
if (brush == null) return;

p.Message("Set your brush to: " + brush.Name);
p.BrushName = brush.Name;
p.DefaultBrushArgs = args.Length > 1 ? args[1] : "";
}

public override void Help(Player p) {
Expand All @@ -62,13 +61,10 @@ public sealed class CmdBrush : Command2
}

public override void Help(Player p, string message) {
BrushFactory brush = BrushFactory.Find(message);
if (brush == null) {
p.Message("No brush found with name \"{0}\".", message);
BrushFactory.List(p);
} else {
p.MessageLines(brush.Help);
}
BrushFactory brush = BrushFactory.FindMatch(p, message);
if (brush == null) return;

p.MessageLines(brush.Help);
}
}
}
39 changes: 16 additions & 23 deletions MCGalaxy/Commands/building/CmdTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,42 +33,35 @@ public sealed class CmdTransform : Command2 {
if (message.Length == 0) {
p.Message("Your current transform is: " + p.Transform.Name); return;
}

string[] args = message.SplitSpaces(2);
TransformFactory transform = TransformFactory.Find(args[0]);

if (IsListAction(args[0])) {
List(p);
} else if (transform == null) {
p.Message("No transform found with name \"{0}\".", args[0]);
List(p);
} else {
p.Message("Set your transform to: " + transform.Name);
Transform instance = transform.Construct(p, args.Length == 1 ? "" : args[1]);
if (instance == null) return;
p.Transform = instance;
TransformFactory.List(p); return;
}
}

static void List(Player p) {
p.Message("&HAvailable transforms: &f" + TransformFactory.Transforms.Join(t => t.Name));

TransformFactory transform = TransformFactory.FindMatch(p, args[0]);
if (transform == null) return;

Transform instance = transform.Construct(p, args.Length == 1 ? "" : args[1]);
if (instance == null) return;

p.Message("Set your transform to: " + transform.Name);
p.Transform = instance;
}

public override void Help(Player p) {
p.Message("&T/Transform [name] <transform args>");
p.Message("&HSets your current transform to the transform with that name.");
p.Message("&T/Help Transform [name]");
p.Message("&HOutputs the help for the transform with that name.");
List(p);
TransformFactory.List(p);
}

public override void Help(Player p, string message) {
TransformFactory transform = TransformFactory.Find(message);
if (transform == null) {
p.Message("No transform found with name \"{0}\".", message);
List(p);
} else {
p.MessageLines(transform.Help);
}
TransformFactory transform = TransformFactory.FindMatch(p, message);
if (transform == null) return;

p.MessageLines(transform.Help);
}
}
}
10 changes: 10 additions & 0 deletions MCGalaxy/Drawing/BrushFactories/BrushFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public abstract class BrushFactory
new ReplaceNotBrushBrushFactory(), new GridBrushFactory(),
};


public static BrushFactory Find(string name) {
foreach (BrushFactory entry in Brushes)
{
Expand All @@ -51,6 +52,15 @@ public abstract class BrushFactory
return null;
}

public static BrushFactory FindMatch(Player p, string name) {
int matches;
BrushFactory match = Matcher.Find(p, name, out matches, Brushes,
null, b => b.Name, "brushes");

if (match == null && matches == 0) List(p);
return match;
}

public static void List(Player p) {
p.Message("&HAvailable brushes: &f" + Brushes.Join(b => b.Name));
}
Expand Down
7 changes: 2 additions & 5 deletions MCGalaxy/Drawing/BrushFactories/ReplaceBrushBrushes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,8 @@ public class ReplaceBrushBrushFactory : BrushFactory
if (parts.Length < 2) { p.MessageLines(Help); return null; }
if (!CommandParser.GetBlockIfAllowed(p, parts[0], "replace", out target)) return null;

BrushFactory factory = BrushFactory.Find(parts[1]);
if (factory == null) {
p.Message("No brush found with name \"{0}\".", parts[1]);
BrushFactory.List(p); return null;
}
BrushFactory factory = BrushFactory.FindMatch(p, parts[1]);
if (factory == null) return null;

args.Message = parts.Length > 2 ? parts[2] : "";
return factory.Construct(args);
Expand Down
17 changes: 16 additions & 1 deletion MCGalaxy/Drawing/TransformFactories/TransformFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,26 @@ public abstract class TransformFactory {
new RotateTransformFactory(),
};


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

public static TransformFactory FindMatch(Player p, string name) {
int matches;
TransformFactory match = Matcher.Find(p, name, out matches, Transforms,
null, t => t.Name, "transforms");

if (match == null && matches == 0) List(p);
return match;
}

public static void List(Player p) {
p.Message("&HAvailable transforms: &f" + Transforms.Join(t => t.Name));
}
}
}

0 comments on commit cfbf37c

Please sign in to comment.