diff --git a/MCGalaxy/Commands/building/CmdBrush.cs b/MCGalaxy/Commands/building/CmdBrush.cs index ae0d62bef..927acfe60 100644 --- a/MCGalaxy/Commands/building/CmdBrush.cs +++ b/MCGalaxy/Commands/building/CmdBrush.cs @@ -36,19 +36,18 @@ public override void Use(Player p, string message, CommandData data) { 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 @@ public override void Help(Player p) { } 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); } } } diff --git a/MCGalaxy/Commands/building/CmdTransform.cs b/MCGalaxy/Commands/building/CmdTransform.cs index f2cfff118..325de2a73 100644 --- a/MCGalaxy/Commands/building/CmdTransform.cs +++ b/MCGalaxy/Commands/building/CmdTransform.cs @@ -33,24 +33,20 @@ public override void Use(Player p, string message, CommandData data) { 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 @@ public override void Help(Player p) { 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); } } } diff --git a/MCGalaxy/Drawing/BrushFactories/BrushFactory.cs b/MCGalaxy/Drawing/BrushFactories/BrushFactory.cs index be1ede1f0..4a4f0da90 100644 --- a/MCGalaxy/Drawing/BrushFactories/BrushFactory.cs +++ b/MCGalaxy/Drawing/BrushFactories/BrushFactory.cs @@ -43,6 +43,7 @@ public abstract class BrushFactory new ReplaceNotBrushBrushFactory(), new GridBrushFactory(), }; + public static BrushFactory Find(string name) { foreach (BrushFactory entry in Brushes) { @@ -51,6 +52,15 @@ public static BrushFactory Find(string name) { 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)); } diff --git a/MCGalaxy/Drawing/BrushFactories/ReplaceBrushBrushes.cs b/MCGalaxy/Drawing/BrushFactories/ReplaceBrushBrushes.cs index 3ed694d8e..bf7cf0cfa 100644 --- a/MCGalaxy/Drawing/BrushFactories/ReplaceBrushBrushes.cs +++ b/MCGalaxy/Drawing/BrushFactories/ReplaceBrushBrushes.cs @@ -48,11 +48,8 @@ protected Brush ParseArguments(BrushArgs args, ref BlockID target) { 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); diff --git a/MCGalaxy/Drawing/TransformFactories/TransformFactory.cs b/MCGalaxy/Drawing/TransformFactories/TransformFactory.cs index 1dbd50309..b8b7c5e76 100644 --- a/MCGalaxy/Drawing/TransformFactories/TransformFactory.cs +++ b/MCGalaxy/Drawing/TransformFactories/TransformFactory.cs @@ -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)); + } } }