mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-24 00:51:34 +01:00
Use a Supplier in Argument#setDefaultValue, fix all-optional syntaxes missing arguments
This commit is contained in:
parent
b29217e076
commit
45d3b7158c
@ -21,6 +21,7 @@ import org.jglrxavpok.hephaistos.nbt.NBTCompound;
|
|||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated renamed to {@link CommandContext}
|
* @deprecated renamed to {@link CommandContext}
|
||||||
@ -296,14 +297,17 @@ public class Arguments {
|
|||||||
this.args.clear();
|
this.args.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void retrieveDefaultValues(@Nullable Map<String, Object> defaultValuesMap) {
|
protected void retrieveDefaultValues(@Nullable Map<String, Supplier<Object>> defaultValuesMap) {
|
||||||
if (defaultValuesMap == null)
|
if (defaultValuesMap == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for (Map.Entry<String, Object> entry : defaultValuesMap.entrySet()) {
|
for (Map.Entry<String, Supplier<Object>> entry : defaultValuesMap.entrySet()) {
|
||||||
final String key = entry.getKey();
|
final String key = entry.getKey();
|
||||||
if (!args.containsKey(key))
|
if (!args.containsKey(key)) {
|
||||||
this.args.put(key, entry.getValue());
|
final var supplier = entry.getValue();
|
||||||
|
this.args.put(key, supplier.get());
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a command which has suggestion/auto-completion.
|
* Represents a command which has suggestion/auto-completion.
|
||||||
@ -157,14 +158,14 @@ public class Command {
|
|||||||
|
|
||||||
// the 'args' array starts by all the required arguments, followed by the optional ones
|
// the 'args' array starts by all the required arguments, followed by the optional ones
|
||||||
List<Argument<?>> requiredArguments = new ArrayList<>();
|
List<Argument<?>> requiredArguments = new ArrayList<>();
|
||||||
Map<String, Object> defaultValuesMap = new HashMap<>();
|
Map<String, Supplier<Object>> defaultValuesMap = new HashMap<>();
|
||||||
boolean optionalBranch = false;
|
boolean optionalBranch = false;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (Argument<?> argument : args) {
|
for (Argument<?> argument : args) {
|
||||||
final boolean isLast = ++i == args.length;
|
final boolean isLast = ++i == args.length;
|
||||||
if (argument.isOptional()) {
|
if (argument.isOptional()) {
|
||||||
// Set default value
|
// Set default value
|
||||||
defaultValuesMap.put(argument.getId(), argument.getDefaultValue());
|
defaultValuesMap.put(argument.getId(), (Supplier<Object>) argument.getDefaultValue());
|
||||||
|
|
||||||
if (!optionalBranch && !requiredArguments.isEmpty()) {
|
if (!optionalBranch && !requiredArguments.isEmpty()) {
|
||||||
// First optional argument, create a syntax with current cached arguments
|
// First optional argument, create a syntax with current cached arguments
|
||||||
|
@ -176,6 +176,7 @@ public class CommandDispatcher {
|
|||||||
// Empty syntax found
|
// Empty syntax found
|
||||||
final CommandSyntax syntax = optionalSyntax.get();
|
final CommandSyntax syntax = optionalSyntax.get();
|
||||||
|
|
||||||
|
parsedCommand.syntax = syntax;
|
||||||
parsedCommand.executor = syntax.getExecutor();
|
parsedCommand.executor = syntax.getExecutor();
|
||||||
parsedCommand.context = new CommandContext(input);
|
parsedCommand.context = new CommandContext(input);
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a syntax in {@link Command}
|
* Represents a syntax in {@link Command}
|
||||||
@ -18,14 +19,14 @@ public class CommandSyntax {
|
|||||||
private CommandCondition commandCondition;
|
private CommandCondition commandCondition;
|
||||||
private CommandExecutor executor;
|
private CommandExecutor executor;
|
||||||
|
|
||||||
private final Map<String, Object> defaultValuesMap;
|
private final Map<String, Supplier<Object>> defaultValuesMap;
|
||||||
private final Argument<?>[] args;
|
private final Argument<?>[] args;
|
||||||
|
|
||||||
private final boolean suggestion;
|
private final boolean suggestion;
|
||||||
|
|
||||||
protected CommandSyntax(@Nullable CommandCondition commandCondition,
|
protected CommandSyntax(@Nullable CommandCondition commandCondition,
|
||||||
@NotNull CommandExecutor commandExecutor,
|
@NotNull CommandExecutor commandExecutor,
|
||||||
@Nullable Map<String, Object> defaultValuesMap,
|
@Nullable Map<String, Supplier<Object>> defaultValuesMap,
|
||||||
@NotNull Argument<?>... args) {
|
@NotNull Argument<?>... args) {
|
||||||
this.commandCondition = commandCondition;
|
this.commandCondition = commandCondition;
|
||||||
this.executor = commandExecutor;
|
this.executor = commandExecutor;
|
||||||
@ -85,7 +86,7 @@ public class CommandSyntax {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
protected Map<String, Object> getDefaultValuesMap() {
|
protected Map<String, Supplier<Object>> getDefaultValuesMap() {
|
||||||
return defaultValuesMap;
|
return defaultValuesMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,6 +11,8 @@ import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An argument is meant to be parsed when added into a {@link Command}'s syntax with {@link Command#addSyntax(CommandExecutor, Argument[])}.
|
* An argument is meant to be parsed when added into a {@link Command}'s syntax with {@link Command#addSyntax(CommandExecutor, Argument[])}.
|
||||||
* <p>
|
* <p>
|
||||||
@ -28,7 +30,7 @@ public abstract class Argument<T> {
|
|||||||
|
|
||||||
private ArgumentCallback callback;
|
private ArgumentCallback callback;
|
||||||
|
|
||||||
private T defaultValue;
|
private Supplier<T> defaultValue;
|
||||||
|
|
||||||
private SuggestionCallback suggestionCallback;
|
private SuggestionCallback suggestionCallback;
|
||||||
|
|
||||||
@ -173,18 +175,13 @@ public abstract class Argument<T> {
|
|||||||
return defaultValue != null;
|
return defaultValue != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the default value of this argument.
|
|
||||||
*
|
|
||||||
* @return the argument default value, null if the argument is not optional
|
|
||||||
*/
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public T getDefaultValue() {
|
public Supplier<T> getDefaultValue() {
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the default value of the argument.
|
* Sets the default value supplier of the argument.
|
||||||
* <p>
|
* <p>
|
||||||
* A non-null value means that the argument can be put at the end of a syntax
|
* A non-null value means that the argument can be put at the end of a syntax
|
||||||
* to act as an optional one.
|
* to act as an optional one.
|
||||||
@ -193,11 +190,21 @@ public abstract class Argument<T> {
|
|||||||
* @return 'this' for chaining
|
* @return 'this' for chaining
|
||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
public Argument<T> setDefaultValue(@Nullable T defaultValue) {
|
public Argument<T> setDefaultValue(@Nullable Supplier<T> defaultValue) {
|
||||||
this.defaultValue = defaultValue;
|
this.defaultValue = defaultValue;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated use {@link #setDefaultValue(Supplier)}
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
@Deprecated
|
||||||
|
public Argument<T> setDefaultValue(@Nullable T defaultValue) {
|
||||||
|
this.defaultValue = () -> defaultValue;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public SuggestionCallback getSuggestionCallback() {
|
public SuggestionCallback getSuggestionCallback() {
|
||||||
return suggestionCallback;
|
return suggestionCallback;
|
||||||
|
Loading…
Reference in New Issue
Block a user