Skip to content

Commit

Permalink
feat: server commands added
Browse files Browse the repository at this point in the history
  • Loading branch information
Scoppio committed Nov 16, 2024
1 parent 231657f commit a4c6b6a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private JComboBox<String> createOptionalEnumComboBox(OptionalEnumArgument<?> enu
comboBox.setSelectedItem("-");
}
for (var arg : enumArg.getEnumType().getEnumConstants()) {
comboBox.addItem(arg.ordinal() + ": " + arg.toString());
comboBox.addItem(arg.ordinal() + ": " + arg);
}
if (enumArg.getValue() != null) {
comboBox.setSelectedItem(enumArg.getValue().ordinal() + ": " + enumArg.getValue().toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public String getHelpHtml() {
this.getHelp()
.replaceAll("<", "&lt;")
.replaceAll(">", "&gt;")
.replaceAll(" ", "| ")
.replaceAll(LONG_WHITESPACE, "| ")
.replaceAll(NEWLINE, "<br>")+
"</html>";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package megamek.server.commands.arguments;

import megamek.client.ui.Messages;

import java.util.Arrays;

public class OptionalEnumArgument<E extends Enum<E>> extends EnumArgument<E> {

public OptionalEnumArgument(String name, String description, Class<E> enumType) {
super(name, description, enumType, null);
}

@Override
public void parse(String input) throws IllegalArgumentException {
if (input == null) {
return;
}
try {
value = enumType.getEnumConstants()[Integer.parseInt(input)];
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(getName() + " must be one of: " + getEnumConstantsString());
}
}

public boolean isPresent() {
return value != null;
}

public boolean isEmpty() {
return value == null;
}

private String getEnumConstantsString() {
var sb = new StringBuilder();
for (int i = 0; i < enumType.getEnumConstants().length; i++) {
sb.append(i).append(": ").append(enumType.getEnumConstants()[i]);
if (i < enumType.getEnumConstants().length - 1) {
sb.append(", ");
}
}
return sb.toString();
}

@Override
public String getHelp() {
return getDescription() +
" [" + getEnumConstantsString() + "] " +
(defaultValue != null ? " [default: " + defaultValue + "]. " : ". ") +
Messages.getString("Gamemaster.cmd.params.optional");
}

}

0 comments on commit a4c6b6a

Please sign in to comment.