Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixes a class cast bug and an error in the argument parsing algorithm #6375

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions megamek/src/megamek/server/commands/ClientServerCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ protected boolean isGM(int connId) {
return server.getGameManager().getGame().getPlayer(connId).getGameMaster();
}

private void safeParseArgumentsAndRun(int connId, String[] args) {
protected void safeParseArgumentsAndRun(int connId, String[] args) {
try {
var parsedArguments = new Arguments(parseArguments(args));
var parsedArguments = new Arguments(parseArguments(args, defineArguments()));
runCommand(connId, parsedArguments);
} catch (IllegalArgumentException e) {
server.sendServerChat(connId, "Invalid arguments: " + e.getMessage() + "\nUsage: " + this.getHelp());
Expand Down Expand Up @@ -118,9 +118,7 @@ protected boolean isOutsideOfBoard(int connId, Arguments args) {


// Parses the arguments using the definition
private Map<String, Argument<?>> parseArguments(String[] args) {

List<Argument<?>> argumentDefinitions = defineArguments();
protected Map<String, Argument<?>> parseArguments(String[] args, List<Argument<?>> argumentDefinitions) {
Map<String, Argument<?>> parsedArguments = new HashMap<>();
List<String> positionalArguments = new ArrayList<>();

Expand Down
44 changes: 34 additions & 10 deletions megamek/src/megamek/server/commands/NukeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,30 @@ public List<Argument<?>> defineArguments() {
);
}

public List<Argument<?>> customArguments() {
return List.of(
new CoordXArgument("x", "The x-coordinate of the hex to nuke."),
new CoordYArgument("y", "The y-coordinate of the hex to nuke."),
new IntegerArgument("dmg", "The damage of the nuke.", 0, 1_000_000),
new IntegerArgument("deg", "The degredation of the nuke.", 0, 1_000_000),
new IntegerArgument("radius", "The secondary radius of the nuke.", 1, 1000),
new IntegerArgument("depth", "The crater depth of the nuke.", 0, 100)
);
}

@Override
protected void safeParseArgumentsAndRun(int connId, String[] args) {
try {
var parsedArguments = new Arguments(parseArguments(args, args.length == 4 ? defineArguments() : customArguments()));
runCommand(connId, parsedArguments);
} catch (IllegalArgumentException e) {
server.sendServerChat(connId, "Invalid arguments: " + e.getMessage() + "\nUsage: " + this.getHelp());
} catch (Exception e) {
server.sendServerChat(connId, "An error occurred while executing the command. Check the log for more information");
logger.error(errorMsg, e);
}
}

@Override
protected void runCommand(int connId, Arguments args) {
// Check to make sure nuking is allowed by game options!
Expand All @@ -64,11 +88,10 @@ protected void runCommand(int connId, Arguments args) {
return;
}

var typeOpt = ((OptionalIntegerArgument) args.get("type")).getValue();

if (typeOpt.isPresent()) {
if (args.hasArg("type")) {
//
try {
var typeOpt = ((OptionalIntegerArgument) args.get("type")).getValue();
int[] nuke = new int[]{
(int) args.get("x").getValue() - 1,
(int) args.get("y").getValue() - 1,
Expand All @@ -86,13 +109,14 @@ protected void runCommand(int connId, Arguments args) {
}
} else {
try {
int[] nuke = new int[6];
nuke[0] = ((IntegerArgument) args.get("x")).getValue() - 1;
nuke[1] = ((IntegerArgument) args.get("y")).getValue() - 1;
nuke[2] = ((OptionalIntegerArgument) args.get("dmg")).getValue().orElseThrow();
nuke[3] = ((OptionalIntegerArgument) args.get("deg")).getValue().orElseThrow();
nuke[4] = ((OptionalIntegerArgument) args.get("radius")).getValue().orElseThrow();
nuke[5] = ((OptionalIntegerArgument) args.get("depth")).getValue().orElseThrow();
int[] nuke = new int[]{
(int) args.get("x").getValue() - 1,
(int) args.get("y").getValue() - 1,
(int) args.get("dmg").getValue(),
(int) args.get("deg").getValue(),
(int) args.get("radius").getValue(),
(int) args.get("depth").getValue()
};

// is the hex on the board?
if (!gameManager.getGame().getBoard().contains(nuke[0], nuke[1])) {
Expand Down
Loading