core: Improvements to number arguments (#255)
This commit is contained in:
parent
343be0bf67
commit
e109e639a1
8 changed files with 579 additions and 289 deletions
|
|
@ -59,7 +59,7 @@ public final class ByteArgument<C> extends CommandArgument<C, Byte> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new builder
|
* Create a new {@link Builder}.
|
||||||
*
|
*
|
||||||
* @param name Name of the argument
|
* @param name Name of the argument
|
||||||
* @param <C> Command sender type
|
* @param <C> Command sender type
|
||||||
|
|
@ -70,7 +70,7 @@ public final class ByteArgument<C> extends CommandArgument<C, Byte> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new required command argument
|
* Create a new required {@link ByteArgument}.
|
||||||
*
|
*
|
||||||
* @param name Argument name
|
* @param name Argument name
|
||||||
* @param <C> Command sender type
|
* @param <C> Command sender type
|
||||||
|
|
@ -81,7 +81,7 @@ public final class ByteArgument<C> extends CommandArgument<C, Byte> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new optional command argument
|
* Create a new optional {@link ByteArgument}.
|
||||||
*
|
*
|
||||||
* @param name Argument name
|
* @param name Argument name
|
||||||
* @param <C> Command sender type
|
* @param <C> Command sender type
|
||||||
|
|
@ -92,7 +92,7 @@ public final class ByteArgument<C> extends CommandArgument<C, Byte> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new required command argument with a default value
|
* Create a new optional {@link ByteArgument} with the specified default value.
|
||||||
*
|
*
|
||||||
* @param name Argument name
|
* @param name Argument name
|
||||||
* @param defaultNum Default num
|
* @param defaultNum Default num
|
||||||
|
|
@ -103,7 +103,7 @@ public final class ByteArgument<C> extends CommandArgument<C, Byte> {
|
||||||
final @NonNull String name,
|
final @NonNull String name,
|
||||||
final byte defaultNum
|
final byte defaultNum
|
||||||
) {
|
) {
|
||||||
return ByteArgument.<C>newBuilder(name).asOptionalWithDefault(Byte.toString(defaultNum)).build();
|
return ByteArgument.<C>newBuilder(name).asOptionalWithDefault(defaultNum).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -126,8 +126,8 @@ public final class ByteArgument<C> extends CommandArgument<C, Byte> {
|
||||||
|
|
||||||
public static final class Builder<C> extends CommandArgument.Builder<C, Byte> {
|
public static final class Builder<C> extends CommandArgument.Builder<C, Byte> {
|
||||||
|
|
||||||
private byte min = Byte.MIN_VALUE;
|
private byte min = ByteParser.DEFAULT_MINIMUM;
|
||||||
private byte max = Byte.MAX_VALUE;
|
private byte max = ByteParser.DEFAULT_MAXIMUM;
|
||||||
|
|
||||||
private Builder(final @NonNull String name) {
|
private Builder(final @NonNull String name) {
|
||||||
super(Byte.class, name);
|
super(Byte.class, name);
|
||||||
|
|
@ -155,6 +155,18 @@ public final class ByteArgument<C> extends CommandArgument<C, Byte> {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the command argument to be optional, with the specified default value.
|
||||||
|
*
|
||||||
|
* @param defaultValue default value
|
||||||
|
* @return this builder
|
||||||
|
* @see CommandArgument.Builder#asOptionalWithDefault(String)
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public @NonNull Builder<C> asOptionalWithDefault(final byte defaultValue) {
|
||||||
|
return (Builder<C>) this.asOptionalWithDefault(Byte.toString(defaultValue));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builder a new byte argument
|
* Builder a new byte argument
|
||||||
*
|
*
|
||||||
|
|
@ -171,6 +183,20 @@ public final class ByteArgument<C> extends CommandArgument<C, Byte> {
|
||||||
|
|
||||||
public static final class ByteParser<C> implements ArgumentParser<C, Byte> {
|
public static final class ByteParser<C> implements ArgumentParser<C, Byte> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constant for the default/unset minimum value.
|
||||||
|
*
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public static final byte DEFAULT_MINIMUM = Byte.MIN_VALUE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constant for the default/unset maximum value.
|
||||||
|
*
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public static final byte DEFAULT_MAXIMUM = Byte.MAX_VALUE;
|
||||||
|
|
||||||
private final byte min;
|
private final byte min;
|
||||||
private final byte max;
|
private final byte max;
|
||||||
|
|
||||||
|
|
@ -192,32 +218,17 @@ public final class ByteArgument<C> extends CommandArgument<C, Byte> {
|
||||||
) {
|
) {
|
||||||
final String input = inputQueue.peek();
|
final String input = inputQueue.peek();
|
||||||
if (input == null) {
|
if (input == null) {
|
||||||
return ArgumentParseResult.failure(new NoInputProvidedException(
|
return ArgumentParseResult.failure(new NoInputProvidedException(ByteParser.class, commandContext));
|
||||||
ByteParser.class,
|
|
||||||
commandContext
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
final byte value = Byte.parseByte(input);
|
final byte value = Byte.parseByte(input);
|
||||||
if (value < this.min || value > this.max) {
|
if (value < this.min || value > this.max) {
|
||||||
return ArgumentParseResult.failure(
|
return ArgumentParseResult.failure(new ByteParseException(input, this, commandContext));
|
||||||
new ByteParseException(
|
|
||||||
input,
|
|
||||||
this.min,
|
|
||||||
this.max,
|
|
||||||
commandContext
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
inputQueue.remove();
|
inputQueue.remove();
|
||||||
return ArgumentParseResult.success(value);
|
return ArgumentParseResult.success(value);
|
||||||
} catch (final Exception e) {
|
} catch (final Exception e) {
|
||||||
return ArgumentParseResult.failure(
|
return ArgumentParseResult.failure(new ByteParseException(input, this, commandContext));
|
||||||
new ByteParseException(
|
|
||||||
input,
|
|
||||||
this.min,
|
|
||||||
this.max,
|
|
||||||
commandContext
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -252,6 +263,28 @@ public final class ByteArgument<C> extends CommandArgument<C, Byte> {
|
||||||
return this.min;
|
return this.min;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get whether this parser has a maximum set.
|
||||||
|
* This will compare the parser's maximum to {@link #DEFAULT_MAXIMUM}.
|
||||||
|
*
|
||||||
|
* @return whether the parser has a maximum set
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public boolean hasMax() {
|
||||||
|
return this.max != DEFAULT_MAXIMUM;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get whether this parser has a minimum set.
|
||||||
|
* This will compare the parser's minimum to {@link #DEFAULT_MINIMUM}.
|
||||||
|
*
|
||||||
|
* @return whether the parser has a maximum set
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public boolean hasMin() {
|
||||||
|
return this.min != DEFAULT_MINIMUM;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -262,6 +295,8 @@ public final class ByteArgument<C> extends CommandArgument<C, Byte> {
|
||||||
|
|
||||||
private static final long serialVersionUID = -4724241304872989208L;
|
private static final long serialVersionUID = -4724241304872989208L;
|
||||||
|
|
||||||
|
private final ByteParser<?> parser;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new byte parse exception
|
* Construct a new byte parse exception
|
||||||
*
|
*
|
||||||
|
|
@ -269,30 +304,43 @@ public final class ByteArgument<C> extends CommandArgument<C, Byte> {
|
||||||
* @param min Minimum value
|
* @param min Minimum value
|
||||||
* @param max Maximum value
|
* @param max Maximum value
|
||||||
* @param context Command context
|
* @param context Command context
|
||||||
|
* @deprecated use {@link #ByteParseException(String, ByteParser, CommandContext)} instead
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public ByteParseException(
|
public ByteParseException(
|
||||||
final @NonNull String input,
|
final @NonNull String input,
|
||||||
final byte min,
|
final byte min,
|
||||||
final byte max,
|
final byte max,
|
||||||
final @NonNull CommandContext<?> context
|
final @NonNull CommandContext<?> context
|
||||||
) {
|
) {
|
||||||
super(
|
this(input, new ByteParser<>(min, max), context);
|
||||||
input,
|
}
|
||||||
min,
|
|
||||||
max,
|
/**
|
||||||
ByteParser.class,
|
* Create a new {@link ByteParseException}.
|
||||||
context
|
*
|
||||||
);
|
* @param input input string
|
||||||
|
* @param parser byte parser
|
||||||
|
* @param context command context
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public ByteParseException(
|
||||||
|
final @NonNull String input,
|
||||||
|
final @NonNull ByteParser<?> parser,
|
||||||
|
final @NonNull CommandContext<?> context
|
||||||
|
) {
|
||||||
|
super(input, parser.min, parser.max, ByteParser.class, context);
|
||||||
|
this.parser = parser;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasMin() {
|
public boolean hasMin() {
|
||||||
return this.getMin().byteValue() != Byte.MIN_VALUE;
|
return this.parser.hasMin();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasMax() {
|
public boolean hasMax() {
|
||||||
return this.getMax().byteValue() != Byte.MAX_VALUE;
|
return this.parser.hasMax();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,7 @@ public final class DoubleArgument<C> extends CommandArgument<C, Double> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new builder
|
* Create a new {@link Builder}.
|
||||||
*
|
*
|
||||||
* @param name Name of the argument
|
* @param name Name of the argument
|
||||||
* @param <C> Command sender type
|
* @param <C> Command sender type
|
||||||
|
|
@ -70,7 +70,7 @@ public final class DoubleArgument<C> extends CommandArgument<C, Double> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new required command argument
|
* Create a new required {@link DoubleArgument}.
|
||||||
*
|
*
|
||||||
* @param name Argument name
|
* @param name Argument name
|
||||||
* @param <C> Command sender type
|
* @param <C> Command sender type
|
||||||
|
|
@ -81,7 +81,7 @@ public final class DoubleArgument<C> extends CommandArgument<C, Double> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new optional command argument
|
* Create a new optional {@link DoubleArgument}.
|
||||||
*
|
*
|
||||||
* @param name Argument name
|
* @param name Argument name
|
||||||
* @param <C> Command sender type
|
* @param <C> Command sender type
|
||||||
|
|
@ -92,7 +92,7 @@ public final class DoubleArgument<C> extends CommandArgument<C, Double> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new required command argument with a default value
|
* Create a new optional {@link DoubleArgument} with the specified default value.
|
||||||
*
|
*
|
||||||
* @param name Argument name
|
* @param name Argument name
|
||||||
* @param defaultNum Default num
|
* @param defaultNum Default num
|
||||||
|
|
@ -103,7 +103,7 @@ public final class DoubleArgument<C> extends CommandArgument<C, Double> {
|
||||||
final @NonNull String name,
|
final @NonNull String name,
|
||||||
final double defaultNum
|
final double defaultNum
|
||||||
) {
|
) {
|
||||||
return DoubleArgument.<C>newBuilder(name).asOptionalWithDefault(Double.toString(defaultNum)).build();
|
return DoubleArgument.<C>newBuilder(name).asOptionalWithDefault(defaultNum).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -126,8 +126,8 @@ public final class DoubleArgument<C> extends CommandArgument<C, Double> {
|
||||||
|
|
||||||
public static final class Builder<C> extends CommandArgument.Builder<C, Double> {
|
public static final class Builder<C> extends CommandArgument.Builder<C, Double> {
|
||||||
|
|
||||||
private double min = Double.NEGATIVE_INFINITY;
|
private double min = DoubleParser.DEFAULT_MINIMUM;
|
||||||
private double max = Double.POSITIVE_INFINITY;
|
private double max = DoubleParser.DEFAULT_MAXIMUM;
|
||||||
|
|
||||||
private Builder(final @NonNull String name) {
|
private Builder(final @NonNull String name) {
|
||||||
super(Double.class, name);
|
super(Double.class, name);
|
||||||
|
|
@ -155,6 +155,18 @@ public final class DoubleArgument<C> extends CommandArgument<C, Double> {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the command argument to be optional, with the specified default value.
|
||||||
|
*
|
||||||
|
* @param defaultValue default value
|
||||||
|
* @return this builder
|
||||||
|
* @see CommandArgument.Builder#asOptionalWithDefault(String)
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public @NonNull Builder<C> asOptionalWithDefault(final double defaultValue) {
|
||||||
|
return (Builder<C>) this.asOptionalWithDefault(Double.toString(defaultValue));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builder a new double argument
|
* Builder a new double argument
|
||||||
*
|
*
|
||||||
|
|
@ -171,6 +183,20 @@ public final class DoubleArgument<C> extends CommandArgument<C, Double> {
|
||||||
|
|
||||||
public static final class DoubleParser<C> implements ArgumentParser<C, Double> {
|
public static final class DoubleParser<C> implements ArgumentParser<C, Double> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constant for the default/unset minimum value.
|
||||||
|
*
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public static final double DEFAULT_MINIMUM = Double.NEGATIVE_INFINITY;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constant for the default/unset maximum value.
|
||||||
|
*
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public static final double DEFAULT_MAXIMUM = Double.POSITIVE_INFINITY;
|
||||||
|
|
||||||
private final double min;
|
private final double min;
|
||||||
private final double max;
|
private final double max;
|
||||||
|
|
||||||
|
|
@ -192,30 +218,17 @@ public final class DoubleArgument<C> extends CommandArgument<C, Double> {
|
||||||
) {
|
) {
|
||||||
final String input = inputQueue.peek();
|
final String input = inputQueue.peek();
|
||||||
if (input == null) {
|
if (input == null) {
|
||||||
return ArgumentParseResult.failure(new NoInputProvidedException(
|
return ArgumentParseResult.failure(new NoInputProvidedException(DoubleParser.class, commandContext));
|
||||||
DoubleParser.class,
|
|
||||||
commandContext
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
final double value = Double.parseDouble(input);
|
final double value = Double.parseDouble(input);
|
||||||
if (value < this.min || value > this.max) {
|
if (value < this.min || value > this.max) {
|
||||||
return ArgumentParseResult.failure(new DoubleParseException(
|
return ArgumentParseResult.failure(new DoubleParseException(input, this, commandContext));
|
||||||
input,
|
|
||||||
this.min,
|
|
||||||
this.max,
|
|
||||||
commandContext
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
inputQueue.remove();
|
inputQueue.remove();
|
||||||
return ArgumentParseResult.success(value);
|
return ArgumentParseResult.success(value);
|
||||||
} catch (final Exception e) {
|
} catch (final Exception e) {
|
||||||
return ArgumentParseResult.failure(new DoubleParseException(
|
return ArgumentParseResult.failure(new DoubleParseException(input, this, commandContext));
|
||||||
input,
|
|
||||||
this.min,
|
|
||||||
this.max,
|
|
||||||
commandContext
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -242,6 +255,28 @@ public final class DoubleArgument<C> extends CommandArgument<C, Double> {
|
||||||
return this.min;
|
return this.min;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get whether this parser has a maximum set.
|
||||||
|
* This will compare the parser's maximum to {@link #DEFAULT_MAXIMUM}.
|
||||||
|
*
|
||||||
|
* @return whether the parser has a maximum set
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public boolean hasMax() {
|
||||||
|
return this.max != DEFAULT_MAXIMUM;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get whether this parser has a minimum set.
|
||||||
|
* This will compare the parser's minimum to {@link #DEFAULT_MINIMUM}.
|
||||||
|
*
|
||||||
|
* @return whether the parser has a maximum set
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public boolean hasMin() {
|
||||||
|
return this.min != DEFAULT_MINIMUM;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -249,6 +284,8 @@ public final class DoubleArgument<C> extends CommandArgument<C, Double> {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1764554911581976586L;
|
private static final long serialVersionUID = 1764554911581976586L;
|
||||||
|
|
||||||
|
private final DoubleParser<?> parser;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new double parse exception
|
* Construct a new double parse exception
|
||||||
*
|
*
|
||||||
|
|
@ -256,30 +293,43 @@ public final class DoubleArgument<C> extends CommandArgument<C, Double> {
|
||||||
* @param min Minimum value
|
* @param min Minimum value
|
||||||
* @param max Maximum value
|
* @param max Maximum value
|
||||||
* @param commandContext Command context
|
* @param commandContext Command context
|
||||||
|
* @deprecated use {@link #DoubleParseException(String, DoubleParser, CommandContext)} instead
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public DoubleParseException(
|
public DoubleParseException(
|
||||||
final @NonNull String input,
|
final @NonNull String input,
|
||||||
final double min,
|
final double min,
|
||||||
final double max,
|
final double max,
|
||||||
final @NonNull CommandContext<?> commandContext
|
final @NonNull CommandContext<?> commandContext
|
||||||
) {
|
) {
|
||||||
super(
|
this(input, new DoubleParser<>(min, max), commandContext);
|
||||||
input,
|
}
|
||||||
min,
|
|
||||||
max,
|
/**
|
||||||
DoubleParser.class,
|
* Create a new {@link DoubleParseException}.
|
||||||
commandContext
|
*
|
||||||
);
|
* @param input input string
|
||||||
|
* @param parser double parser
|
||||||
|
* @param commandContext command context
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public DoubleParseException(
|
||||||
|
final @NonNull String input,
|
||||||
|
final @NonNull DoubleParser<?> parser,
|
||||||
|
final @NonNull CommandContext<?> commandContext
|
||||||
|
) {
|
||||||
|
super(input, parser.min, parser.max, DoubleParser.class, commandContext);
|
||||||
|
this.parser = parser;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasMin() {
|
public boolean hasMin() {
|
||||||
return this.getMin().doubleValue() != Double.MIN_VALUE;
|
return this.parser.hasMin();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasMax() {
|
public boolean hasMax() {
|
||||||
return this.getMax().doubleValue() != Double.MAX_VALUE;
|
return this.parser.hasMax();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,7 @@ public final class FloatArgument<C> extends CommandArgument<C, Float> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new builder
|
* Create a new {@link Builder}.
|
||||||
*
|
*
|
||||||
* @param name Name of the argument
|
* @param name Name of the argument
|
||||||
* @param <C> Command sender type
|
* @param <C> Command sender type
|
||||||
|
|
@ -70,7 +70,7 @@ public final class FloatArgument<C> extends CommandArgument<C, Float> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new required command argument
|
* Create a new required {@link FloatArgument}.
|
||||||
*
|
*
|
||||||
* @param name Argument name
|
* @param name Argument name
|
||||||
* @param <C> Command sender type
|
* @param <C> Command sender type
|
||||||
|
|
@ -81,7 +81,7 @@ public final class FloatArgument<C> extends CommandArgument<C, Float> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new optional command argument
|
* Create a new optional {@link FloatArgument}.
|
||||||
*
|
*
|
||||||
* @param name Argument name
|
* @param name Argument name
|
||||||
* @param <C> Command sender type
|
* @param <C> Command sender type
|
||||||
|
|
@ -92,7 +92,7 @@ public final class FloatArgument<C> extends CommandArgument<C, Float> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new required command argument with a default value
|
* Create a new optional {@link FloatArgument} with the specified default value.
|
||||||
*
|
*
|
||||||
* @param name Argument name
|
* @param name Argument name
|
||||||
* @param defaultNum Default num
|
* @param defaultNum Default num
|
||||||
|
|
@ -103,7 +103,7 @@ public final class FloatArgument<C> extends CommandArgument<C, Float> {
|
||||||
final @NonNull String name,
|
final @NonNull String name,
|
||||||
final float defaultNum
|
final float defaultNum
|
||||||
) {
|
) {
|
||||||
return FloatArgument.<C>newBuilder(name).asOptionalWithDefault(Float.toString(defaultNum)).build();
|
return FloatArgument.<C>newBuilder(name).asOptionalWithDefault(defaultNum).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -126,8 +126,8 @@ public final class FloatArgument<C> extends CommandArgument<C, Float> {
|
||||||
|
|
||||||
public static final class Builder<C> extends CommandArgument.Builder<C, Float> {
|
public static final class Builder<C> extends CommandArgument.Builder<C, Float> {
|
||||||
|
|
||||||
private float min = Float.NEGATIVE_INFINITY;
|
private float min = FloatParser.DEFAULT_MINIMUM;
|
||||||
private float max = Float.POSITIVE_INFINITY;
|
private float max = FloatParser.DEFAULT_MAXIMUM;
|
||||||
|
|
||||||
private Builder(final @NonNull String name) {
|
private Builder(final @NonNull String name) {
|
||||||
super(Float.class, name);
|
super(Float.class, name);
|
||||||
|
|
@ -156,10 +156,17 @@ public final class FloatArgument<C> extends CommandArgument<C, Float> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builder a new float argument
|
* Sets the command argument to be optional, with the specified default value.
|
||||||
*
|
*
|
||||||
* @return Constructed argument
|
* @param defaultValue default value
|
||||||
|
* @return this builder
|
||||||
|
* @see CommandArgument.Builder#asOptionalWithDefault(String)
|
||||||
|
* @since 1.5.0
|
||||||
*/
|
*/
|
||||||
|
public @NonNull Builder<C> asOptionalWithDefault(final float defaultValue) {
|
||||||
|
return (Builder<C>) this.asOptionalWithDefault(Float.toString(defaultValue));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NonNull FloatArgument<C> build() {
|
public @NonNull FloatArgument<C> build() {
|
||||||
return new FloatArgument<>(this.isRequired(), this.getName(), this.min, this.max,
|
return new FloatArgument<>(this.isRequired(), this.getName(), this.min, this.max,
|
||||||
|
|
@ -171,6 +178,20 @@ public final class FloatArgument<C> extends CommandArgument<C, Float> {
|
||||||
|
|
||||||
public static final class FloatParser<C> implements ArgumentParser<C, Float> {
|
public static final class FloatParser<C> implements ArgumentParser<C, Float> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constant for the default/unset minimum value.
|
||||||
|
*
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public static final float DEFAULT_MINIMUM = Float.NEGATIVE_INFINITY;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constant for the default/unset maximum value.
|
||||||
|
*
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public static final float DEFAULT_MAXIMUM = Float.POSITIVE_INFINITY;
|
||||||
|
|
||||||
private final float min;
|
private final float min;
|
||||||
private final float max;
|
private final float max;
|
||||||
|
|
||||||
|
|
@ -192,30 +213,17 @@ public final class FloatArgument<C> extends CommandArgument<C, Float> {
|
||||||
) {
|
) {
|
||||||
final String input = inputQueue.peek();
|
final String input = inputQueue.peek();
|
||||||
if (input == null) {
|
if (input == null) {
|
||||||
return ArgumentParseResult.failure(new NoInputProvidedException(
|
return ArgumentParseResult.failure(new NoInputProvidedException(FloatParser.class, commandContext));
|
||||||
FloatParser.class,
|
|
||||||
commandContext
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
final float value = Float.parseFloat(input);
|
final float value = Float.parseFloat(input);
|
||||||
if (value < this.min || value > this.max) {
|
if (value < this.min || value > this.max) {
|
||||||
return ArgumentParseResult.failure(new FloatParseException(
|
return ArgumentParseResult.failure(new FloatParseException(input, this, commandContext));
|
||||||
input,
|
|
||||||
this.min,
|
|
||||||
this.max,
|
|
||||||
commandContext
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
inputQueue.remove();
|
inputQueue.remove();
|
||||||
return ArgumentParseResult.success(value);
|
return ArgumentParseResult.success(value);
|
||||||
} catch (final Exception e) {
|
} catch (final Exception e) {
|
||||||
return ArgumentParseResult.failure(new FloatParseException(
|
return ArgumentParseResult.failure(new FloatParseException(input, this, commandContext));
|
||||||
input,
|
|
||||||
this.min,
|
|
||||||
this.max,
|
|
||||||
commandContext
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -242,6 +250,28 @@ public final class FloatArgument<C> extends CommandArgument<C, Float> {
|
||||||
return this.min;
|
return this.min;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get whether this parser has a maximum set.
|
||||||
|
* This will compare the parser's maximum to {@link #DEFAULT_MAXIMUM}.
|
||||||
|
*
|
||||||
|
* @return whether the parser has a maximum set
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public boolean hasMax() {
|
||||||
|
return this.max != DEFAULT_MAXIMUM;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get whether this parser has a minimum set.
|
||||||
|
* This will compare the parser's minimum to {@link #DEFAULT_MINIMUM}.
|
||||||
|
*
|
||||||
|
* @return whether the parser has a maximum set
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public boolean hasMin() {
|
||||||
|
return this.min != DEFAULT_MINIMUM;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -249,6 +279,8 @@ public final class FloatArgument<C> extends CommandArgument<C, Float> {
|
||||||
|
|
||||||
private static final long serialVersionUID = -1162983846751812292L;
|
private static final long serialVersionUID = -1162983846751812292L;
|
||||||
|
|
||||||
|
private final FloatParser<?> parser;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new float parse exception
|
* Construct a new float parse exception
|
||||||
*
|
*
|
||||||
|
|
@ -256,30 +288,43 @@ public final class FloatArgument<C> extends CommandArgument<C, Float> {
|
||||||
* @param min Minimum value
|
* @param min Minimum value
|
||||||
* @param max Maximum value
|
* @param max Maximum value
|
||||||
* @param commandContext Command context
|
* @param commandContext Command context
|
||||||
|
* @deprecated use {@link #FloatParseException(String, FloatParser, CommandContext)} instead
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public FloatParseException(
|
public FloatParseException(
|
||||||
final @NonNull String input,
|
final @NonNull String input,
|
||||||
final float min,
|
final float min,
|
||||||
final float max,
|
final float max,
|
||||||
final @NonNull CommandContext<?> commandContext
|
final @NonNull CommandContext<?> commandContext
|
||||||
) {
|
) {
|
||||||
super(
|
this(input, new FloatParser<>(min, max), commandContext);
|
||||||
input,
|
}
|
||||||
min,
|
|
||||||
max,
|
/**
|
||||||
FloatParser.class,
|
* Create a new {@link FloatParseException}.
|
||||||
commandContext
|
*
|
||||||
);
|
* @param input input string
|
||||||
|
* @param parser float parser
|
||||||
|
* @param commandContext command context
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public FloatParseException(
|
||||||
|
final @NonNull String input,
|
||||||
|
final @NonNull FloatParser<?> parser,
|
||||||
|
final @NonNull CommandContext<?> commandContext
|
||||||
|
) {
|
||||||
|
super(input, parser.min, parser.max, FloatParser.class, commandContext);
|
||||||
|
this.parser = parser;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasMin() {
|
public boolean hasMin() {
|
||||||
return this.getMin().floatValue() != Float.MIN_VALUE;
|
return this.parser.hasMin();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasMax() {
|
public boolean hasMax() {
|
||||||
return this.getMax().floatValue() != Float.MAX_VALUE;
|
return this.parser.hasMax();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -55,18 +55,26 @@ public final class IntegerArgument<C> extends CommandArgument<C, Integer> {
|
||||||
final @NonNull String name,
|
final @NonNull String name,
|
||||||
final int min,
|
final int min,
|
||||||
final int max,
|
final int max,
|
||||||
final String defaultValue,
|
final @NonNull String defaultValue,
|
||||||
final @Nullable BiFunction<@NonNull CommandContext<C>, @NonNull String,
|
final @Nullable BiFunction<@NonNull CommandContext<C>, @NonNull String,
|
||||||
@NonNull List<@NonNull String>> suggestionsProvider,
|
@NonNull List<@NonNull String>> suggestionsProvider,
|
||||||
final @NonNull ArgumentDescription defaultDescription
|
final @NonNull ArgumentDescription defaultDescription
|
||||||
) {
|
) {
|
||||||
super(required, name, new IntegerParser<>(min, max), defaultValue, Integer.class, suggestionsProvider, defaultDescription);
|
super(
|
||||||
|
required,
|
||||||
|
name,
|
||||||
|
new IntegerParser<>(min, max),
|
||||||
|
defaultValue,
|
||||||
|
Integer.class,
|
||||||
|
suggestionsProvider,
|
||||||
|
defaultDescription
|
||||||
|
);
|
||||||
this.min = min;
|
this.min = min;
|
||||||
this.max = max;
|
this.max = max;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new builder
|
* Create a new {@link Builder}.
|
||||||
*
|
*
|
||||||
* @param name Name of the argument
|
* @param name Name of the argument
|
||||||
* @param <C> Command sender type
|
* @param <C> Command sender type
|
||||||
|
|
@ -77,7 +85,7 @@ public final class IntegerArgument<C> extends CommandArgument<C, Integer> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new required command argument
|
* Create a new required {@link IntegerArgument}.
|
||||||
*
|
*
|
||||||
* @param name Argument name
|
* @param name Argument name
|
||||||
* @param <C> Command sender type
|
* @param <C> Command sender type
|
||||||
|
|
@ -88,7 +96,7 @@ public final class IntegerArgument<C> extends CommandArgument<C, Integer> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new optional command argument
|
* Create a new optional {@link IntegerArgument}.
|
||||||
*
|
*
|
||||||
* @param name Argument name
|
* @param name Argument name
|
||||||
* @param <C> Command sender type
|
* @param <C> Command sender type
|
||||||
|
|
@ -99,18 +107,15 @@ public final class IntegerArgument<C> extends CommandArgument<C, Integer> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new required command argument with a default value
|
* Create a new required {@link IntegerArgument} with the specified default value.
|
||||||
*
|
*
|
||||||
* @param name Argument name
|
* @param name Argument name
|
||||||
* @param defaultNum Default num
|
* @param defaultNum Default value
|
||||||
* @param <C> Command sender type
|
* @param <C> Command sender type
|
||||||
* @return Created argument
|
* @return Created argument
|
||||||
*/
|
*/
|
||||||
public static <C> @NonNull CommandArgument<C, Integer> optional(
|
public static <C> @NonNull CommandArgument<C, Integer> optional(final @NonNull String name, final int defaultNum) {
|
||||||
final @NonNull String name,
|
return IntegerArgument.<C>newBuilder(name).asOptionalWithDefault(defaultNum).build();
|
||||||
final int defaultNum
|
|
||||||
) {
|
|
||||||
return IntegerArgument.<C>newBuilder(name).asOptionalWithDefault(Integer.toString(defaultNum)).build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -133,8 +138,8 @@ public final class IntegerArgument<C> extends CommandArgument<C, Integer> {
|
||||||
|
|
||||||
public static final class Builder<C> extends CommandArgument.Builder<C, Integer> {
|
public static final class Builder<C> extends CommandArgument.Builder<C, Integer> {
|
||||||
|
|
||||||
private int min = Integer.MIN_VALUE;
|
private int min = IntegerParser.DEFAULT_MINIMUM;
|
||||||
private int max = Integer.MAX_VALUE;
|
private int max = IntegerParser.DEFAULT_MAXIMUM;
|
||||||
|
|
||||||
private Builder(final @NonNull String name) {
|
private Builder(final @NonNull String name) {
|
||||||
super(Integer.class, name);
|
super(Integer.class, name);
|
||||||
|
|
@ -163,10 +168,17 @@ public final class IntegerArgument<C> extends CommandArgument<C, Integer> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builder a new integer argument
|
* Sets the command argument to be optional, with the specified default value.
|
||||||
*
|
*
|
||||||
* @return Constructed argument
|
* @param defaultValue default value
|
||||||
|
* @return this builder
|
||||||
|
* @see CommandArgument.Builder#asOptionalWithDefault(String)
|
||||||
|
* @since 1.5.0
|
||||||
*/
|
*/
|
||||||
|
public @NonNull Builder<C> asOptionalWithDefault(final int defaultValue) {
|
||||||
|
return (Builder<C>) this.asOptionalWithDefault(Integer.toString(defaultValue));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NonNull IntegerArgument<C> build() {
|
public @NonNull IntegerArgument<C> build() {
|
||||||
return new IntegerArgument<>(this.isRequired(), this.getName(), this.min, this.max,
|
return new IntegerArgument<>(this.isRequired(), this.getName(), this.min, this.max,
|
||||||
|
|
@ -178,6 +190,20 @@ public final class IntegerArgument<C> extends CommandArgument<C, Integer> {
|
||||||
|
|
||||||
public static final class IntegerParser<C> implements ArgumentParser<C, Integer> {
|
public static final class IntegerParser<C> implements ArgumentParser<C, Integer> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constant for the default/unset minimum value.
|
||||||
|
*
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public static final int DEFAULT_MINIMUM = Integer.MIN_VALUE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constant for the default/unset maximum value.
|
||||||
|
*
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public static final int DEFAULT_MAXIMUM = Integer.MAX_VALUE;
|
||||||
|
|
||||||
private final int min;
|
private final int min;
|
||||||
private final int max;
|
private final int max;
|
||||||
|
|
||||||
|
|
@ -242,30 +268,17 @@ public final class IntegerArgument<C> extends CommandArgument<C, Integer> {
|
||||||
) {
|
) {
|
||||||
final String input = inputQueue.peek();
|
final String input = inputQueue.peek();
|
||||||
if (input == null) {
|
if (input == null) {
|
||||||
return ArgumentParseResult.failure(new NoInputProvidedException(
|
return ArgumentParseResult.failure(new NoInputProvidedException(IntegerParser.class, commandContext));
|
||||||
IntegerParser.class,
|
|
||||||
commandContext
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
final int value = Integer.parseInt(input);
|
final int value = Integer.parseInt(input);
|
||||||
if (value < this.min || value > this.max) {
|
if (value < this.min || value > this.max) {
|
||||||
return ArgumentParseResult.failure(new IntegerParseException(
|
return ArgumentParseResult.failure(new IntegerParseException(input, this, commandContext));
|
||||||
input,
|
|
||||||
this.min,
|
|
||||||
this.max,
|
|
||||||
commandContext
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
inputQueue.remove();
|
inputQueue.remove();
|
||||||
return ArgumentParseResult.success(value);
|
return ArgumentParseResult.success(value);
|
||||||
} catch (final Exception e) {
|
} catch (final Exception e) {
|
||||||
return ArgumentParseResult.failure(new IntegerParseException(
|
return ArgumentParseResult.failure(new IntegerParseException(input, this, commandContext));
|
||||||
input,
|
|
||||||
this.min,
|
|
||||||
this.max,
|
|
||||||
commandContext
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -287,6 +300,28 @@ public final class IntegerArgument<C> extends CommandArgument<C, Integer> {
|
||||||
return this.max;
|
return this.max;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get whether this parser has a maximum set.
|
||||||
|
* This will compare the parser's maximum to {@link #DEFAULT_MAXIMUM}.
|
||||||
|
*
|
||||||
|
* @return whether the parser has a maximum set
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public boolean hasMax() {
|
||||||
|
return this.max != DEFAULT_MAXIMUM;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get whether this parser has a minimum set.
|
||||||
|
* This will compare the parser's minimum to {@link #DEFAULT_MINIMUM}.
|
||||||
|
*
|
||||||
|
* @return whether the parser has a maximum set
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public boolean hasMin() {
|
||||||
|
return this.min != DEFAULT_MINIMUM;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isContextFree() {
|
public boolean isContextFree() {
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -307,6 +342,8 @@ public final class IntegerArgument<C> extends CommandArgument<C, Integer> {
|
||||||
|
|
||||||
private static final long serialVersionUID = -6933923056628373853L;
|
private static final long serialVersionUID = -6933923056628373853L;
|
||||||
|
|
||||||
|
private final IntegerParser<?> parser;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new integer parse exception
|
* Construct a new integer parse exception
|
||||||
*
|
*
|
||||||
|
|
@ -314,30 +351,43 @@ public final class IntegerArgument<C> extends CommandArgument<C, Integer> {
|
||||||
* @param min Minimum value
|
* @param min Minimum value
|
||||||
* @param max Maximum value
|
* @param max Maximum value
|
||||||
* @param commandContext Command context
|
* @param commandContext Command context
|
||||||
|
* @deprecated use {@link #IntegerParseException(String, IntegerParser, CommandContext)} instead
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public IntegerParseException(
|
public IntegerParseException(
|
||||||
final @NonNull String input,
|
final @NonNull String input,
|
||||||
final int min,
|
final int min,
|
||||||
final int max,
|
final int max,
|
||||||
final @NonNull CommandContext<?> commandContext
|
final @NonNull CommandContext<?> commandContext
|
||||||
) {
|
) {
|
||||||
super(
|
this(input, new IntegerParser<>(min, max), commandContext);
|
||||||
input,
|
}
|
||||||
min,
|
|
||||||
max,
|
/**
|
||||||
IntegerParser.class,
|
* Create a new {@link IntegerParseException}.
|
||||||
commandContext
|
*
|
||||||
);
|
* @param input input string
|
||||||
|
* @param parser integer parser
|
||||||
|
* @param commandContext command context
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public IntegerParseException(
|
||||||
|
final @NonNull String input,
|
||||||
|
final @NonNull IntegerParser<?> parser,
|
||||||
|
final @NonNull CommandContext<?> commandContext
|
||||||
|
) {
|
||||||
|
super(input, parser.min, parser.max, IntegerParser.class, commandContext);
|
||||||
|
this.parser = parser;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasMin() {
|
public boolean hasMin() {
|
||||||
return this.getMin().intValue() != Integer.MIN_VALUE;
|
return this.parser.hasMin();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasMax() {
|
public boolean hasMax() {
|
||||||
return this.getMax().intValue() != Integer.MAX_VALUE;
|
return this.parser.hasMax();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,7 @@ public final class LongArgument<C> extends CommandArgument<C, Long> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new builder
|
* Create a new {@link Builder}.
|
||||||
*
|
*
|
||||||
* @param name Name of the argument
|
* @param name Name of the argument
|
||||||
* @param <C> Command sender type
|
* @param <C> Command sender type
|
||||||
|
|
@ -70,7 +70,7 @@ public final class LongArgument<C> extends CommandArgument<C, Long> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new required command argument
|
* Create a new required {@link LongArgument}.
|
||||||
*
|
*
|
||||||
* @param name Argument name
|
* @param name Argument name
|
||||||
* @param <C> Command sender type
|
* @param <C> Command sender type
|
||||||
|
|
@ -81,7 +81,7 @@ public final class LongArgument<C> extends CommandArgument<C, Long> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new optional command argument
|
* Create a new optional {@link LongArgument}.
|
||||||
*
|
*
|
||||||
* @param name Argument name
|
* @param name Argument name
|
||||||
* @param <C> Command sender type
|
* @param <C> Command sender type
|
||||||
|
|
@ -92,7 +92,7 @@ public final class LongArgument<C> extends CommandArgument<C, Long> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new required command argument with a default value
|
* Create a new optional {@link LongArgument} with the specified default value.
|
||||||
*
|
*
|
||||||
* @param name Argument name
|
* @param name Argument name
|
||||||
* @param defaultNum Default num
|
* @param defaultNum Default num
|
||||||
|
|
@ -103,7 +103,7 @@ public final class LongArgument<C> extends CommandArgument<C, Long> {
|
||||||
final @NonNull String name,
|
final @NonNull String name,
|
||||||
final long defaultNum
|
final long defaultNum
|
||||||
) {
|
) {
|
||||||
return LongArgument.<C>newBuilder(name).asOptionalWithDefault(Long.toString(defaultNum)).build();
|
return LongArgument.<C>newBuilder(name).asOptionalWithDefault(defaultNum).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -126,8 +126,8 @@ public final class LongArgument<C> extends CommandArgument<C, Long> {
|
||||||
|
|
||||||
public static final class Builder<C> extends CommandArgument.Builder<C, Long> {
|
public static final class Builder<C> extends CommandArgument.Builder<C, Long> {
|
||||||
|
|
||||||
private long min = Long.MIN_VALUE;
|
private long min = LongParser.DEFAULT_MINIMUM;
|
||||||
private long max = Long.MAX_VALUE;
|
private long max = LongParser.DEFAULT_MAXIMUM;
|
||||||
|
|
||||||
private Builder(final @NonNull String name) {
|
private Builder(final @NonNull String name) {
|
||||||
super(Long.class, name);
|
super(Long.class, name);
|
||||||
|
|
@ -156,10 +156,17 @@ public final class LongArgument<C> extends CommandArgument<C, Long> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builder a new long argument
|
* Sets the command argument to be optional, with the specified default value.
|
||||||
*
|
*
|
||||||
* @return Constructed argument
|
* @param defaultValue default value
|
||||||
|
* @return this builder
|
||||||
|
* @see CommandArgument.Builder#asOptionalWithDefault(String)
|
||||||
|
* @since 1.5.0
|
||||||
*/
|
*/
|
||||||
|
public @NonNull Builder<C> asOptionalWithDefault(final long defaultValue) {
|
||||||
|
return (Builder<C>) this.asOptionalWithDefault(Long.toString(defaultValue));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NonNull LongArgument<C> build() {
|
public @NonNull LongArgument<C> build() {
|
||||||
return new LongArgument<>(this.isRequired(), this.getName(), this.min,
|
return new LongArgument<>(this.isRequired(), this.getName(), this.min,
|
||||||
|
|
@ -171,6 +178,20 @@ public final class LongArgument<C> extends CommandArgument<C, Long> {
|
||||||
|
|
||||||
public static final class LongParser<C> implements ArgumentParser<C, Long> {
|
public static final class LongParser<C> implements ArgumentParser<C, Long> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constant for the default/unset minimum value.
|
||||||
|
*
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public static final long DEFAULT_MINIMUM = Long.MIN_VALUE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constant for the default/unset maximum value.
|
||||||
|
*
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public static final long DEFAULT_MAXIMUM = Long.MAX_VALUE;
|
||||||
|
|
||||||
private final long min;
|
private final long min;
|
||||||
private final long max;
|
private final long max;
|
||||||
|
|
||||||
|
|
@ -192,33 +213,60 @@ public final class LongArgument<C> extends CommandArgument<C, Long> {
|
||||||
) {
|
) {
|
||||||
final String input = inputQueue.peek();
|
final String input = inputQueue.peek();
|
||||||
if (input == null) {
|
if (input == null) {
|
||||||
return ArgumentParseResult.failure(new NoInputProvidedException(
|
return ArgumentParseResult.failure(new NoInputProvidedException(LongParser.class, commandContext));
|
||||||
LongParser.class,
|
|
||||||
commandContext
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
final long value = Long.parseLong(input);
|
final long value = Long.parseLong(input);
|
||||||
if (value < this.min || value > this.max) {
|
if (value < this.min || value > this.max) {
|
||||||
return ArgumentParseResult.failure(new LongParseException(
|
return ArgumentParseResult.failure(new LongParseException(input, this, commandContext));
|
||||||
input,
|
|
||||||
this.min,
|
|
||||||
this.max,
|
|
||||||
commandContext
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
inputQueue.remove();
|
inputQueue.remove();
|
||||||
return ArgumentParseResult.success(value);
|
return ArgumentParseResult.success(value);
|
||||||
} catch (final Exception e) {
|
} catch (final Exception e) {
|
||||||
return ArgumentParseResult.failure(new LongParseException(
|
return ArgumentParseResult.failure(new LongParseException(input, this, commandContext));
|
||||||
input,
|
|
||||||
this.min,
|
|
||||||
this.max,
|
|
||||||
commandContext
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the minimum value accepted by this parser
|
||||||
|
*
|
||||||
|
* @return Min value
|
||||||
|
*/
|
||||||
|
public long getMin() {
|
||||||
|
return this.min;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the maximum value accepted by this parser
|
||||||
|
*
|
||||||
|
* @return Max value
|
||||||
|
*/
|
||||||
|
public long getMax() {
|
||||||
|
return this.max;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get whether this parser has a maximum set.
|
||||||
|
* This will compare the parser's maximum to {@link #DEFAULT_MAXIMUM}.
|
||||||
|
*
|
||||||
|
* @return whether the parser has a maximum set
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public boolean hasMax() {
|
||||||
|
return this.max != DEFAULT_MAXIMUM;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get whether this parser has a minimum set.
|
||||||
|
* This will compare the parser's minimum to {@link #DEFAULT_MINIMUM}.
|
||||||
|
*
|
||||||
|
* @return whether the parser has a maximum set
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public boolean hasMin() {
|
||||||
|
return this.min != DEFAULT_MINIMUM;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isContextFree() {
|
public boolean isContextFree() {
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -239,6 +287,8 @@ public final class LongArgument<C> extends CommandArgument<C, Long> {
|
||||||
|
|
||||||
private static final long serialVersionUID = 4366856282301198232L;
|
private static final long serialVersionUID = 4366856282301198232L;
|
||||||
|
|
||||||
|
private final LongParser<?> parser;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new long parse exception
|
* Construct a new long parse exception
|
||||||
*
|
*
|
||||||
|
|
@ -246,30 +296,43 @@ public final class LongArgument<C> extends CommandArgument<C, Long> {
|
||||||
* @param min Minimum value
|
* @param min Minimum value
|
||||||
* @param max Maximum value
|
* @param max Maximum value
|
||||||
* @param commandContext Command context
|
* @param commandContext Command context
|
||||||
|
* @deprecated use {@link #LongParseException(String, LongParser, CommandContext)} instead
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public LongParseException(
|
public LongParseException(
|
||||||
final @NonNull String input,
|
final @NonNull String input,
|
||||||
final long min,
|
final long min,
|
||||||
final long max,
|
final long max,
|
||||||
final @NonNull CommandContext<?> commandContext
|
final @NonNull CommandContext<?> commandContext
|
||||||
) {
|
) {
|
||||||
super(
|
this(input, new LongParser<>(min, max), commandContext);
|
||||||
input,
|
}
|
||||||
min,
|
|
||||||
max,
|
/**
|
||||||
LongParser.class,
|
* Create a new {@link LongParseException}.
|
||||||
commandContext
|
*
|
||||||
);
|
* @param input input string
|
||||||
|
* @param parser long parser
|
||||||
|
* @param commandContext command context
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public LongParseException(
|
||||||
|
final @NonNull String input,
|
||||||
|
final @NonNull LongParser<?> parser,
|
||||||
|
final @NonNull CommandContext<?> commandContext
|
||||||
|
) {
|
||||||
|
super(input, parser.min, parser.max, LongParser.class, commandContext);
|
||||||
|
this.parser = parser;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasMin() {
|
public boolean hasMin() {
|
||||||
return this.getMin().longValue() != Long.MIN_VALUE;
|
return this.parser.hasMin();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasMax() {
|
public boolean hasMax() {
|
||||||
return this.getMax().longValue() != Long.MAX_VALUE;
|
return this.parser.hasMax();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,7 @@ public final class ShortArgument<C> extends CommandArgument<C, Short> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new builder
|
* Create a new {@link Builder}.
|
||||||
*
|
*
|
||||||
* @param name Name of the argument
|
* @param name Name of the argument
|
||||||
* @param <C> Command sender type
|
* @param <C> Command sender type
|
||||||
|
|
@ -70,7 +70,7 @@ public final class ShortArgument<C> extends CommandArgument<C, Short> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new required command argument
|
* Create a new required {@link ShortArgument}.
|
||||||
*
|
*
|
||||||
* @param name Argument name
|
* @param name Argument name
|
||||||
* @param <C> Command sender type
|
* @param <C> Command sender type
|
||||||
|
|
@ -81,7 +81,7 @@ public final class ShortArgument<C> extends CommandArgument<C, Short> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new optional command argument
|
* Create a new optional {@link ShortArgument}.
|
||||||
*
|
*
|
||||||
* @param name Argument name
|
* @param name Argument name
|
||||||
* @param <C> Command sender type
|
* @param <C> Command sender type
|
||||||
|
|
@ -92,18 +92,15 @@ public final class ShortArgument<C> extends CommandArgument<C, Short> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new required command argument with a default value
|
* Create a new required {@link ShortArgument} with the specified default value.
|
||||||
*
|
*
|
||||||
* @param name Argument name
|
* @param name Argument name
|
||||||
* @param defaultNum Default num
|
* @param defaultNum Default value
|
||||||
* @param <C> Command sender type
|
* @param <C> Command sender type
|
||||||
* @return Created argument
|
* @return Created argument
|
||||||
*/
|
*/
|
||||||
public static <C> @NonNull CommandArgument<C, Short> optional(
|
public static <C> @NonNull CommandArgument<C, Short> optional(final @NonNull String name, final short defaultNum) {
|
||||||
final @NonNull String name,
|
return ShortArgument.<C>newBuilder(name).asOptionalWithDefault(defaultNum).build();
|
||||||
final short defaultNum
|
|
||||||
) {
|
|
||||||
return ShortArgument.<C>newBuilder(name).asOptionalWithDefault(Short.toString(defaultNum)).build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -126,8 +123,8 @@ public final class ShortArgument<C> extends CommandArgument<C, Short> {
|
||||||
|
|
||||||
public static final class Builder<C> extends CommandArgument.Builder<C, Short> {
|
public static final class Builder<C> extends CommandArgument.Builder<C, Short> {
|
||||||
|
|
||||||
private short min = Short.MIN_VALUE;
|
private short min = ShortParser.DEFAULT_MINIMUM;
|
||||||
private short max = Short.MAX_VALUE;
|
private short max = ShortParser.DEFAULT_MAXIMUM;
|
||||||
|
|
||||||
private Builder(final @NonNull String name) {
|
private Builder(final @NonNull String name) {
|
||||||
super(Short.class, name);
|
super(Short.class, name);
|
||||||
|
|
@ -156,10 +153,17 @@ public final class ShortArgument<C> extends CommandArgument<C, Short> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builder a new short argument
|
* Sets the command argument to be optional, with the specified default value.
|
||||||
*
|
*
|
||||||
* @return Constructed argument
|
* @param defaultValue default value
|
||||||
|
* @return this builder
|
||||||
|
* @see CommandArgument.Builder#asOptionalWithDefault(String)
|
||||||
|
* @since 1.5.0
|
||||||
*/
|
*/
|
||||||
|
public @NonNull Builder<C> asOptionalWithDefault(final short defaultValue) {
|
||||||
|
return (Builder<C>) this.asOptionalWithDefault(Short.toString(defaultValue));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NonNull ShortArgument<C> build() {
|
public @NonNull ShortArgument<C> build() {
|
||||||
return new ShortArgument<>(this.isRequired(), this.getName(), this.min, this.max,
|
return new ShortArgument<>(this.isRequired(), this.getName(), this.min, this.max,
|
||||||
|
|
@ -171,6 +175,20 @@ public final class ShortArgument<C> extends CommandArgument<C, Short> {
|
||||||
|
|
||||||
public static final class ShortParser<C> implements ArgumentParser<C, Short> {
|
public static final class ShortParser<C> implements ArgumentParser<C, Short> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constant for the default/unset minimum value.
|
||||||
|
*
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public static final short DEFAULT_MINIMUM = Short.MIN_VALUE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constant for the default/unset maximum value.
|
||||||
|
*
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public static final short DEFAULT_MAXIMUM = Short.MAX_VALUE;
|
||||||
|
|
||||||
private final short min;
|
private final short min;
|
||||||
private final short max;
|
private final short max;
|
||||||
|
|
||||||
|
|
@ -192,30 +210,17 @@ public final class ShortArgument<C> extends CommandArgument<C, Short> {
|
||||||
) {
|
) {
|
||||||
final String input = inputQueue.peek();
|
final String input = inputQueue.peek();
|
||||||
if (input == null) {
|
if (input == null) {
|
||||||
return ArgumentParseResult.failure(new NoInputProvidedException(
|
return ArgumentParseResult.failure(new NoInputProvidedException(ShortParser.class, commandContext));
|
||||||
ShortParser.class,
|
|
||||||
commandContext
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
final short value = Short.parseShort(input);
|
final short value = Short.parseShort(input);
|
||||||
if (value < this.min || value > this.max) {
|
if (value < this.min || value > this.max) {
|
||||||
return ArgumentParseResult.failure(new ShortParseException(
|
return ArgumentParseResult.failure(new ShortParseException(input, this, commandContext));
|
||||||
input,
|
|
||||||
this.min,
|
|
||||||
this.max,
|
|
||||||
commandContext
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
inputQueue.remove();
|
inputQueue.remove();
|
||||||
return ArgumentParseResult.success(value);
|
return ArgumentParseResult.success(value);
|
||||||
} catch (final Exception e) {
|
} catch (final Exception e) {
|
||||||
return ArgumentParseResult.failure(new ShortParseException(
|
return ArgumentParseResult.failure(new ShortParseException(input, this, commandContext));
|
||||||
input,
|
|
||||||
this.min,
|
|
||||||
this.max,
|
|
||||||
commandContext
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -250,6 +255,28 @@ public final class ShortArgument<C> extends CommandArgument<C, Short> {
|
||||||
return this.min;
|
return this.min;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get whether this parser has a maximum set.
|
||||||
|
* This will compare the parser's maximum to {@link #DEFAULT_MAXIMUM}.
|
||||||
|
*
|
||||||
|
* @return whether the parser has a maximum set
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public boolean hasMax() {
|
||||||
|
return this.max != DEFAULT_MAXIMUM;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get whether this parser has a minimum set.
|
||||||
|
* This will compare the parser's minimum to {@link #DEFAULT_MINIMUM}.
|
||||||
|
*
|
||||||
|
* @return whether the parser has a maximum set
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public boolean hasMin() {
|
||||||
|
return this.min != DEFAULT_MINIMUM;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -257,6 +284,8 @@ public final class ShortArgument<C> extends CommandArgument<C, Short> {
|
||||||
|
|
||||||
private static final long serialVersionUID = -478674263339091032L;
|
private static final long serialVersionUID = -478674263339091032L;
|
||||||
|
|
||||||
|
private final ShortParser<?> parser;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new short parse exception
|
* Construct a new short parse exception
|
||||||
*
|
*
|
||||||
|
|
@ -264,30 +293,43 @@ public final class ShortArgument<C> extends CommandArgument<C, Short> {
|
||||||
* @param min Minimum value
|
* @param min Minimum value
|
||||||
* @param max Maximum value
|
* @param max Maximum value
|
||||||
* @param commandContext Command context
|
* @param commandContext Command context
|
||||||
|
* @deprecated use {@link #ShortParseException(String, ShortParser, CommandContext)} instead
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public ShortParseException(
|
public ShortParseException(
|
||||||
final @NonNull String input,
|
final @NonNull String input,
|
||||||
final short min,
|
final short min,
|
||||||
final short max,
|
final short max,
|
||||||
final @NonNull CommandContext<?> commandContext
|
final @NonNull CommandContext<?> commandContext
|
||||||
) {
|
) {
|
||||||
super(
|
this(input, new ShortParser<>(min, max), commandContext);
|
||||||
input,
|
}
|
||||||
min,
|
|
||||||
max,
|
/**
|
||||||
ShortParser.class,
|
* Create a new {@link ShortParseException}.
|
||||||
commandContext
|
*
|
||||||
);
|
* @param input input string
|
||||||
|
* @param parser short parser
|
||||||
|
* @param commandContext command context
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public ShortParseException(
|
||||||
|
final @NonNull String input,
|
||||||
|
final @NonNull ShortParser<?> parser,
|
||||||
|
final @NonNull CommandContext<?> commandContext
|
||||||
|
) {
|
||||||
|
super(input, parser.min, parser.max, ShortParser.class, commandContext);
|
||||||
|
this.parser = parser;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasMin() {
|
public boolean hasMin() {
|
||||||
return this.getMin().shortValue() != Short.MIN_VALUE;
|
return this.parser.hasMin();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasMax() {
|
public boolean hasMax() {
|
||||||
return this.getMax().shortValue() != Short.MAX_VALUE;
|
return this.parser.hasMax();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,7 @@ import cloud.commandframework.arguments.standard.ByteArgument;
|
||||||
import cloud.commandframework.arguments.standard.DoubleArgument;
|
import cloud.commandframework.arguments.standard.DoubleArgument;
|
||||||
import cloud.commandframework.arguments.standard.FloatArgument;
|
import cloud.commandframework.arguments.standard.FloatArgument;
|
||||||
import cloud.commandframework.arguments.standard.IntegerArgument;
|
import cloud.commandframework.arguments.standard.IntegerArgument;
|
||||||
|
import cloud.commandframework.arguments.standard.LongArgument;
|
||||||
import cloud.commandframework.arguments.standard.ShortArgument;
|
import cloud.commandframework.arguments.standard.ShortArgument;
|
||||||
import cloud.commandframework.arguments.standard.StringArgument;
|
import cloud.commandframework.arguments.standard.StringArgument;
|
||||||
import cloud.commandframework.arguments.standard.StringArrayArgument;
|
import cloud.commandframework.arguments.standard.StringArrayArgument;
|
||||||
|
|
@ -51,6 +52,7 @@ import com.mojang.brigadier.arguments.BoolArgumentType;
|
||||||
import com.mojang.brigadier.arguments.DoubleArgumentType;
|
import com.mojang.brigadier.arguments.DoubleArgumentType;
|
||||||
import com.mojang.brigadier.arguments.FloatArgumentType;
|
import com.mojang.brigadier.arguments.FloatArgumentType;
|
||||||
import com.mojang.brigadier.arguments.IntegerArgumentType;
|
import com.mojang.brigadier.arguments.IntegerArgumentType;
|
||||||
|
import com.mojang.brigadier.arguments.LongArgumentType;
|
||||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||||
import com.mojang.brigadier.builder.ArgumentBuilder;
|
import com.mojang.brigadier.builder.ArgumentBuilder;
|
||||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||||
|
|
@ -138,65 +140,66 @@ public final class CloudBrigadierManager<C, S> {
|
||||||
/* Map byte, short and int to IntegerArgumentType */
|
/* Map byte, short and int to IntegerArgumentType */
|
||||||
this.registerMapping(new TypeToken<ByteArgument.ByteParser<C>>() {
|
this.registerMapping(new TypeToken<ByteArgument.ByteParser<C>>() {
|
||||||
}, builder -> builder.to(argument -> {
|
}, builder -> builder.to(argument -> {
|
||||||
final boolean hasMin = argument.getMin() != Byte.MIN_VALUE;
|
|
||||||
final boolean hasMax = argument.getMax() != Byte.MAX_VALUE;
|
|
||||||
if (hasMin) {
|
|
||||||
return IntegerArgumentType.integer(argument.getMin(), argument.getMax());
|
return IntegerArgumentType.integer(argument.getMin(), argument.getMax());
|
||||||
} else if (hasMax) {
|
|
||||||
return IntegerArgumentType.integer(Byte.MIN_VALUE, argument.getMax());
|
|
||||||
} else {
|
|
||||||
return IntegerArgumentType.integer();
|
|
||||||
}
|
|
||||||
}));
|
}));
|
||||||
this.registerMapping(new TypeToken<ShortArgument.ShortParser<C>>() {
|
this.registerMapping(new TypeToken<ShortArgument.ShortParser<C>>() {
|
||||||
}, builder -> builder.to(argument -> {
|
}, builder -> builder.to(argument -> {
|
||||||
final boolean hasMin = argument.getMin() != Short.MIN_VALUE;
|
|
||||||
final boolean hasMax = argument.getMax() != Short.MAX_VALUE;
|
|
||||||
if (hasMin) {
|
|
||||||
return IntegerArgumentType.integer(argument.getMin(), argument.getMax());
|
return IntegerArgumentType.integer(argument.getMin(), argument.getMax());
|
||||||
} else if (hasMax) {
|
|
||||||
return IntegerArgumentType.integer(Short.MIN_VALUE, argument.getMax());
|
|
||||||
} else {
|
|
||||||
return IntegerArgumentType.integer();
|
|
||||||
}
|
|
||||||
}));
|
}));
|
||||||
this.registerMapping(new TypeToken<IntegerArgument.IntegerParser<C>>() {
|
this.registerMapping(new TypeToken<IntegerArgument.IntegerParser<C>>() {
|
||||||
}, builder -> builder.to(argument -> {
|
}, builder -> builder.to(argument -> {
|
||||||
final boolean hasMin = argument.getMin() != Integer.MIN_VALUE;
|
if (!argument.hasMin() && !argument.hasMax()) {
|
||||||
final boolean hasMax = argument.getMax() != Integer.MAX_VALUE;
|
|
||||||
if (hasMin) {
|
|
||||||
return IntegerArgumentType.integer(argument.getMin(), argument.getMax());
|
|
||||||
} else if (hasMax) {
|
|
||||||
return IntegerArgumentType.integer(Integer.MIN_VALUE, argument.getMax());
|
|
||||||
} else {
|
|
||||||
return IntegerArgumentType.integer();
|
return IntegerArgumentType.integer();
|
||||||
}
|
}
|
||||||
|
if (argument.hasMin() && !argument.hasMax()) {
|
||||||
|
return IntegerArgumentType.integer(argument.getMin());
|
||||||
|
} else if (!argument.hasMin()) {
|
||||||
|
// Brig uses Integer.MIN_VALUE and Integer.MAX_VALUE for default min/max
|
||||||
|
return IntegerArgumentType.integer(Integer.MIN_VALUE, argument.getMax());
|
||||||
|
}
|
||||||
|
return IntegerArgumentType.integer(argument.getMin(), argument.getMax());
|
||||||
}));
|
}));
|
||||||
/* Map float to FloatArgumentType */
|
/* Map float to FloatArgumentType */
|
||||||
this.registerMapping(new TypeToken<FloatArgument.FloatParser<C>>() {
|
this.registerMapping(new TypeToken<FloatArgument.FloatParser<C>>() {
|
||||||
}, builder -> builder.to(argument -> {
|
}, builder -> builder.to(argument -> {
|
||||||
final boolean hasMin = argument.getMin() != Float.NEGATIVE_INFINITY;
|
if (!argument.hasMin() && !argument.hasMax()) {
|
||||||
final boolean hasMax = argument.getMax() != Float.POSITIVE_INFINITY;
|
|
||||||
if (hasMin) {
|
|
||||||
return FloatArgumentType.floatArg(argument.getMin(), argument.getMax());
|
|
||||||
} else if (hasMax) {
|
|
||||||
return FloatArgumentType.floatArg(Float.NEGATIVE_INFINITY, argument.getMax());
|
|
||||||
} else {
|
|
||||||
return FloatArgumentType.floatArg();
|
return FloatArgumentType.floatArg();
|
||||||
}
|
}
|
||||||
|
if (argument.hasMin() && !argument.hasMax()) {
|
||||||
|
return FloatArgumentType.floatArg(argument.getMin());
|
||||||
|
} else if (!argument.hasMin()) {
|
||||||
|
// Brig uses -Float.MAX_VALUE and Float.MAX_VALUE for default min/max
|
||||||
|
return FloatArgumentType.floatArg(-Float.MAX_VALUE, argument.getMax());
|
||||||
|
}
|
||||||
|
return FloatArgumentType.floatArg(argument.getMin(), argument.getMax());
|
||||||
}));
|
}));
|
||||||
/* Map double to DoubleArgumentType */
|
/* Map double to DoubleArgumentType */
|
||||||
this.registerMapping(new TypeToken<DoubleArgument.DoubleParser<C>>() {
|
this.registerMapping(new TypeToken<DoubleArgument.DoubleParser<C>>() {
|
||||||
}, builder -> builder.to(argument -> {
|
}, builder -> builder.to(argument -> {
|
||||||
final boolean hasMin = argument.getMin() != Double.NEGATIVE_INFINITY;
|
if (!argument.hasMin() && !argument.hasMax()) {
|
||||||
final boolean hasMax = argument.getMax() != Double.POSITIVE_INFINITY;
|
|
||||||
if (hasMin) {
|
|
||||||
return DoubleArgumentType.doubleArg(argument.getMin(), argument.getMax());
|
|
||||||
} else if (hasMax) {
|
|
||||||
return DoubleArgumentType.doubleArg(Double.NEGATIVE_INFINITY, argument.getMax());
|
|
||||||
} else {
|
|
||||||
return DoubleArgumentType.doubleArg();
|
return DoubleArgumentType.doubleArg();
|
||||||
}
|
}
|
||||||
|
if (argument.hasMin() && !argument.hasMax()) {
|
||||||
|
return DoubleArgumentType.doubleArg(argument.getMin());
|
||||||
|
} else if (!argument.hasMin()) {
|
||||||
|
// Brig uses -Double.MAX_VALUE and Double.MAX_VALUE for default min/max
|
||||||
|
return DoubleArgumentType.doubleArg(-Double.MAX_VALUE, argument.getMax());
|
||||||
|
}
|
||||||
|
return DoubleArgumentType.doubleArg(argument.getMin(), argument.getMax());
|
||||||
|
}));
|
||||||
|
/* Map long parser to LongArgumentType */
|
||||||
|
this.registerMapping(new TypeToken<LongArgument.LongParser<C>>() {
|
||||||
|
}, builder -> builder.to(longParser -> {
|
||||||
|
if (!longParser.hasMin() && !longParser.hasMax()) {
|
||||||
|
return LongArgumentType.longArg();
|
||||||
|
}
|
||||||
|
if (longParser.hasMin() && !longParser.hasMax()) {
|
||||||
|
return LongArgumentType.longArg(longParser.getMin());
|
||||||
|
} else if (!longParser.hasMin()) {
|
||||||
|
// Brig uses Long.MIN_VALUE and Long.MAX_VALUE for default min/max
|
||||||
|
return LongArgumentType.longArg(Long.MIN_VALUE, longParser.getMax());
|
||||||
|
}
|
||||||
|
return LongArgumentType.longArg(longParser.getMin(), longParser.getMax());
|
||||||
}));
|
}));
|
||||||
/* Map boolean to BoolArgumentType */
|
/* Map boolean to BoolArgumentType */
|
||||||
this.registerMapping(new TypeToken<BooleanArgument.BooleanParser<C>>() {
|
this.registerMapping(new TypeToken<BooleanArgument.BooleanParser<C>>() {
|
||||||
|
|
@ -267,31 +270,18 @@ public final class CloudBrigadierManager<C, S> {
|
||||||
* @since 1.2.0
|
* @since 1.2.0
|
||||||
*/
|
*/
|
||||||
public void setNativeNumberSuggestions(final boolean nativeNumberSuggestions) {
|
public void setNativeNumberSuggestions(final boolean nativeNumberSuggestions) {
|
||||||
this.setNativeSuggestions(
|
this.setNativeSuggestions(new TypeToken<ByteArgument.ByteParser<C>>() {
|
||||||
new TypeToken<ByteArgument.ByteParser<C>>() {
|
}, nativeNumberSuggestions);
|
||||||
},
|
this.setNativeSuggestions(new TypeToken<ShortArgument.ShortParser<C>>() {
|
||||||
nativeNumberSuggestions
|
}, nativeNumberSuggestions);
|
||||||
);
|
this.setNativeSuggestions(new TypeToken<IntegerArgument.IntegerParser<C>>() {
|
||||||
this.setNativeSuggestions(
|
}, nativeNumberSuggestions);
|
||||||
new TypeToken<ShortArgument.ShortParser<C>>() {
|
this.setNativeSuggestions(new TypeToken<FloatArgument.FloatParser<C>>() {
|
||||||
},
|
}, nativeNumberSuggestions);
|
||||||
nativeNumberSuggestions
|
this.setNativeSuggestions(new TypeToken<DoubleArgument.DoubleParser<C>>() {
|
||||||
);
|
}, nativeNumberSuggestions);
|
||||||
this.setNativeSuggestions(
|
this.setNativeSuggestions(new TypeToken<LongArgument.LongParser<C>>() {
|
||||||
new TypeToken<IntegerArgument.IntegerParser<C>>() {
|
}, nativeNumberSuggestions);
|
||||||
},
|
|
||||||
nativeNumberSuggestions
|
|
||||||
);
|
|
||||||
this.setNativeSuggestions(
|
|
||||||
new TypeToken<FloatArgument.FloatParser<C>>() {
|
|
||||||
},
|
|
||||||
nativeNumberSuggestions
|
|
||||||
);
|
|
||||||
this.setNativeSuggestions(
|
|
||||||
new TypeToken<DoubleArgument.DoubleParser<C>>() {
|
|
||||||
},
|
|
||||||
nativeNumberSuggestions
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -361,8 +351,10 @@ public final class CloudBrigadierManager<C, S> {
|
||||||
* @param <K> cloud argument parser type
|
* @param <K> cloud argument parser type
|
||||||
* @since 1.5.0
|
* @since 1.5.0
|
||||||
*/
|
*/
|
||||||
public <K extends ArgumentParser<C, ?>> void registerMapping(final @NonNull TypeToken<K> parserType,
|
public <K extends ArgumentParser<C, ?>> void registerMapping(
|
||||||
final Consumer<BrigadierMappingBuilder<K, S>> configurer) {
|
final @NonNull TypeToken<K> parserType,
|
||||||
|
final Consumer<BrigadierMappingBuilder<K, S>> configurer
|
||||||
|
) {
|
||||||
final BrigadierMapping.BuilderImpl<C, K, S> builder = new BrigadierMapping.BuilderImpl<>();
|
final BrigadierMapping.BuilderImpl<C, K, S> builder = new BrigadierMapping.BuilderImpl<>();
|
||||||
configurer.accept(builder);
|
configurer.accept(builder);
|
||||||
this.mappers.put(GenericTypeReflector.erase(parserType.getType()), builder.build());
|
this.mappers.put(GenericTypeReflector.erase(parserType.getType()), builder.build());
|
||||||
|
|
|
||||||
|
|
@ -71,14 +71,14 @@ public final class LocationCoordinateParser<C> implements ArgumentParser<C, Loca
|
||||||
try {
|
try {
|
||||||
coordinate = input.isEmpty() ? 0 : Double.parseDouble(input);
|
coordinate = input.isEmpty() ? 0 : Double.parseDouble(input);
|
||||||
} catch (final Exception e) {
|
} catch (final Exception e) {
|
||||||
return ArgumentParseResult.failure(
|
return ArgumentParseResult.failure(new DoubleArgument.DoubleParseException(
|
||||||
new DoubleArgument.DoubleParseException(
|
|
||||||
input,
|
input,
|
||||||
Double.NEGATIVE_INFINITY,
|
new DoubleArgument.DoubleParser<>(
|
||||||
Double.POSITIVE_INFINITY,
|
DoubleArgument.DoubleParser.DEFAULT_MINIMUM,
|
||||||
|
DoubleArgument.DoubleParser.DEFAULT_MAXIMUM
|
||||||
|
),
|
||||||
commandContext
|
commandContext
|
||||||
)
|
));
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inputQueue.remove();
|
inputQueue.remove();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue