From 0f961e4d47fca0c4126d281e816882ef04bead33 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 31 Dec 2025 11:17:20 +0000 Subject: [PATCH] Refactor command parsing to use ParsingContext and ArgumentBinder Replaced manual string parsing in several command definitions with standard ParsingContext methods. - Updated CreateObjectCommand to use `ConsumePreposition` and `GetRemainingText`. - Updated DigCommand to use `ConsumePreposition` and `GetRemainingText`. - Updated RenameCommand to use `Pop` and `GetRemainingText`. - Updated EditVerbCommand to use `Pop` and split logic. --- .../Commands/Creative/CreateObjectCommand.cs | 6 ++---- MooSharp/Commands/Commands/Creative/DigCommand.cs | 7 ++----- .../Commands/Commands/Creative/EditVerbCommand.cs | 10 ++++++++-- .../Commands/Commands/Creative/RenameCommand.cs | 13 ++++++------- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/MooSharp/Commands/Commands/Creative/CreateObjectCommand.cs b/MooSharp/Commands/Commands/Creative/CreateObjectCommand.cs index c5bd48f..d064b11 100644 --- a/MooSharp/Commands/Commands/Creative/CreateObjectCommand.cs +++ b/MooSharp/Commands/Commands/Creative/CreateObjectCommand.cs @@ -22,16 +22,14 @@ public class CreateObjectCommandDefinition : ICommandDefinition public string? TryCreateCommand(ParsingContext ctx, ArgumentBinder binder, out ICommand? command) { - var args = ctx.GetRemainingText().Trim(); - // Expected format: @create object "Name" or @create object Name - if (!args.StartsWith("object", StringComparison.OrdinalIgnoreCase)) + if (!binder.ConsumePreposition(ctx, "object")) { command = null; return "Usage: @create object \"Name\"."; } - var nameArg = args[6..].Trim().Trim('"', '\''); + var nameArg = ctx.GetRemainingText(); if (string.IsNullOrWhiteSpace(nameArg)) { diff --git a/MooSharp/Commands/Commands/Creative/DigCommand.cs b/MooSharp/Commands/Commands/Creative/DigCommand.cs index 0aec4f7..63c03db 100644 --- a/MooSharp/Commands/Commands/Creative/DigCommand.cs +++ b/MooSharp/Commands/Commands/Creative/DigCommand.cs @@ -22,11 +22,8 @@ public class DigCommandDefinition : ICommandDefinition public string? TryCreateCommand(ParsingContext ctx, ArgumentBinder binder, out ICommand? command) { - var args = ctx.GetRemainingText(); - - var name = args.StartsWith("to ", StringComparison.OrdinalIgnoreCase) - ? args[3..].Trim() - : args.Trim(); + binder.ConsumePreposition(ctx, "to"); + var name = ctx.GetRemainingText(); if (string.IsNullOrWhiteSpace(name)) { diff --git a/MooSharp/Commands/Commands/Creative/EditVerbCommand.cs b/MooSharp/Commands/Commands/Creative/EditVerbCommand.cs index 9b01bf6..aa7a04c 100644 --- a/MooSharp/Commands/Commands/Creative/EditVerbCommand.cs +++ b/MooSharp/Commands/Commands/Creative/EditVerbCommand.cs @@ -23,9 +23,15 @@ public class EditVerbCommandDefinition : ICommandDefinition public string? TryCreateCommand(ParsingContext ctx, ArgumentBinder binder, out ICommand? command) { - var args = ctx.GetRemainingText().Trim(); + var arg = ctx.Pop(); - var parts = args.Split(':'); + if (arg is null) + { + command = null; + return "Usage: @edit object:verbname"; + } + + var parts = arg.Split(':'); if (parts.Length != 2) { command = null; diff --git a/MooSharp/Commands/Commands/Creative/RenameCommand.cs b/MooSharp/Commands/Commands/Creative/RenameCommand.cs index 9939c78..d28eb6c 100644 --- a/MooSharp/Commands/Commands/Creative/RenameCommand.cs +++ b/MooSharp/Commands/Commands/Creative/RenameCommand.cs @@ -21,10 +21,7 @@ public class RenameCommandDefinition : ICommandDefinition public string? TryCreateCommand(ParsingContext ctx, ArgumentBinder binder, out ICommand? command) { - var args = ctx.GetRemainingText(); - var trimmed = args.Trim(); - - if (string.IsNullOrWhiteSpace(trimmed)) + if (ctx.IsFinished) { command = new RenameCommand { @@ -32,15 +29,17 @@ public class RenameCommandDefinition : ICommandDefinition Target = string.Empty, NewName = string.Empty }; + return null; } - var parts = trimmed.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + var target = ctx.Pop() ?? string.Empty; + var newName = ctx.GetRemainingText(); command = new RenameCommand { Player = ctx.Player, - Target = parts.ElementAtOrDefault(0) ?? string.Empty, - NewName = parts.ElementAtOrDefault(1) ?? string.Empty + Target = target, + NewName = newName }; return null;