Change /brush and /transform to support partial name matching

This commit is contained in:
UnknownShadow200 2024-06-08 07:30:45 +10:00
parent 0872171cbb
commit cfbf37c812
5 changed files with 57 additions and 46 deletions

View File

@ -36,19 +36,18 @@ namespace MCGalaxy.Commands.Building
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) {
@ -62,13 +61,10 @@ namespace MCGalaxy.Commands.Building
}
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);
}
}
}

View File

@ -33,24 +33,20 @@ namespace MCGalaxy.Commands.Building {
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) {
@ -58,17 +54,14 @@ namespace MCGalaxy.Commands.Building {
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);
}
}
}

View File

@ -43,6 +43,7 @@ namespace MCGalaxy.Drawing.Brushes
new ReplaceNotBrushBrushFactory(), new GridBrushFactory(),
};
public static BrushFactory Find(string name) {
foreach (BrushFactory entry in Brushes)
{
@ -51,6 +52,15 @@ namespace MCGalaxy.Drawing.Brushes
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));
}

View File

@ -48,11 +48,8 @@ namespace MCGalaxy.Drawing.Brushes
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);

View File

@ -38,11 +38,26 @@ namespace MCGalaxy.Drawing.Transforms {
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));
}
}
}