Skip to content

Commit

Permalink
Add support for greedy strings
Browse files Browse the repository at this point in the history
  • Loading branch information
virustotalop committed Aug 28, 2024
1 parent be8cfb3 commit ad9303f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public static CloudArgument create(@NotNull Function<String, CommandComponent.Bu
} else {
builder.required();
}
if (d.isGreedy()) {
builder.parser(StringParser.greedyStringParser());
}
return builder.build();
});
}
Expand Down Expand Up @@ -96,11 +99,13 @@ public CloudArgument(@NotNull Function<CloudData, CommandComponent> function) {
}

public <T extends CommandComponent> T argument(@NotNull String argName) {
return this.argument(argName, false);
return this.argument(argName, false, false);
}

public <T extends CommandComponent> T argument(@NotNull String argName, boolean optional) {
public <T extends CommandComponent> T argument(@NotNull String argName,
boolean optional,
boolean greedy) {
Objects.requireNonNull(argName);
return (T) this.function.apply(new CloudData(argName, optional));
return (T) this.function.apply(new CloudData(argName, optional, greedy));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ public class CloudData {

private final String argumentName;
private final boolean optional;
private final boolean greedy;

public CloudData(String argumentName, boolean optional) {
public CloudData(String argumentName,
boolean optional,
boolean greedy) {
this.argumentName = argumentName;
this.optional = optional;
this.greedy = greedy;
}

public String getArgumentName() {
Expand All @@ -33,4 +37,8 @@ public String getArgumentName() {
public boolean isOptional() {
return this.optional;
}

public boolean isGreedy() {
return this.greedy;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ public void registerGuiAliasCommand(@NotNull String guiName,
String metaKey = "command_" + argName;
context.optional(argName).ifPresent(value -> {
String metadataValue = EntityManager.get().isPlayer(value) ?
EntityManager.get().createPlayerWrapper(value).getName() :
String.valueOf(value);
metadata.put(metaKey, metadataValue);
EntityManager.get().createPlayerWrapper(value).getName() :
String.valueOf(value);
metadata.put(metaKey, metadataValue);
});
}
GuiManager.get().openGui(playerWrapper, guiName, metadata);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,11 @@ private List<CommandComponent> loadCommandArguments(ConfigurationSection section
ConfigurationSection keySec = section.getConfigurationSection(key);
String type = keySec.getString("type");
boolean optional = keySec.getBoolean("optional");
boolean greedy = keySec.getBoolean("greedy");
Optional<CloudArgument> opt = CloudArgument.fromType(type);
if (opt.isPresent()) {
CloudArgument arg = opt.get();
args.add(arg.argument(keyName, optional));
args.add(arg.argument(keyName, optional, greedy));
} else {
DynamicGui.get().getLogger().error("Invalid argument type %s", type);
}
Expand Down

0 comments on commit ad9303f

Please sign in to comment.