Fix range parsing

This commit is contained in:
Németh Noel 2021-08-22 08:36:02 +02:00
parent 2dbf795d5c
commit 293786854b
4 changed files with 72 additions and 118 deletions

View File

@ -245,7 +245,7 @@ public class ArgumentEntity extends Argument<EntityFinder> {
break;
case "level":
try {
final IntRange level = ArgumentIntRange.staticParse(value);
final IntRange level = Argument.parse(new ArgumentIntRange(value));
entityFinder.setLevel(level);
} catch (ArgumentSyntaxException e) {
throw new ArgumentSyntaxException("Invalid level number", input, INVALID_ARGUMENT_VALUE);
@ -253,7 +253,7 @@ public class ArgumentEntity extends Argument<EntityFinder> {
break;
case "distance":
try {
final IntRange distance = ArgumentIntRange.staticParse(value);
final IntRange distance = Argument.parse(new ArgumentIntRange(value));
entityFinder.setDistance(distance);
} catch (ArgumentSyntaxException e) {
throw new ArgumentSyntaxException("Invalid level number", input, INVALID_ARGUMENT_VALUE);

View File

@ -1,66 +1,16 @@
package net.minestom.server.command.builder.arguments.minecraft;
import net.minestom.server.command.builder.NodeMaker;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
import net.minestom.server.utils.math.FloatRange;
import org.jetbrains.annotations.NotNull;
import java.util.regex.Pattern;
/**
* Represents an argument which will give you an {@link FloatRange}.
* <p>
* Example: ..3, 3.., 5..10, 15
*/
public class ArgumentFloatRange extends ArgumentRange<FloatRange> {
public class ArgumentFloatRange extends ArgumentRange<FloatRange, Float> {
public ArgumentFloatRange(String id) {
super(id);
}
@NotNull
@Override
public FloatRange parse(@NotNull String input) throws ArgumentSyntaxException {
try {
if (input.contains("..")) {
final int index = input.indexOf('.');
final String[] split = input.split(Pattern.quote(".."));
final float min;
final float max;
if (index == 0) {
// Format ..NUMBER
min = Float.MIN_VALUE;
max = Float.parseFloat(split[0]);
} else {
if (split.length == 2) {
// Format NUMBER..NUMBER
min = Float.parseFloat(split[0]);
max = Float.parseFloat(split[1]);
} else {
// Format NUMBER..
min = Float.parseFloat(split[0]);
max = Float.MAX_VALUE;
}
}
return new FloatRange(min, max);
} else {
final float number = Float.parseFloat(input);
return new FloatRange(number);
}
} catch (NumberFormatException e2) {
throw new ArgumentSyntaxException("Invalid number", input, FORMAT_ERROR);
}
}
@Override
public void processNodes(@NotNull NodeMaker nodeMaker, boolean executable) {
DeclareCommandsPacket.Node argumentNode = simpleArgumentNode(this, executable, false, false);
argumentNode.parser = "minecraft:float_range";
nodeMaker.addNodes(new DeclareCommandsPacket.Node[]{argumentNode});
super(id, Float.MIN_VALUE, Float.MAX_VALUE, Float::parseFloat, "minecraft:float_range", FloatRange::new);
}
@Override

View File

@ -1,76 +1,16 @@
package net.minestom.server.command.builder.arguments.minecraft;
import net.minestom.server.command.builder.NodeMaker;
import net.minestom.server.command.builder.arguments.Argument;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
import net.minestom.server.utils.math.IntRange;
import org.jetbrains.annotations.NotNull;
import java.util.regex.Pattern;
/**
* Represents an argument which will give you an {@link IntRange}.
* <p>
* Example: ..3, 3.., 5..10, 15
*/
public class ArgumentIntRange extends ArgumentRange<IntRange> {
public class ArgumentIntRange extends ArgumentRange<IntRange, Integer> {
public ArgumentIntRange(String id) {
super(id);
}
@NotNull
@Override
public IntRange parse(@NotNull String input) throws ArgumentSyntaxException {
return staticParse(input);
}
/**
* @deprecated use {@link Argument#parse(Argument)}
*/
@Deprecated
@NotNull
public static IntRange staticParse(@NotNull String input) throws ArgumentSyntaxException {
try {
if (input.contains("..")) {
final int index = input.indexOf('.');
final String[] split = input.split(Pattern.quote(".."));
final int min;
final int max;
if (index == 0) {
// Format ..NUMBER
min = Integer.MIN_VALUE;
max = Integer.parseInt(split[0]);
} else {
if (split.length == 2) {
// Format NUMBER..NUMBER
min = Integer.parseInt(split[0]);
max = Integer.parseInt(split[1]);
} else {
// Format NUMBER..
min = Integer.parseInt(split[0]);
max = Integer.MAX_VALUE;
}
}
return new IntRange(min, max);
} else {
final int number = Integer.parseInt(input);
return new IntRange(number);
}
} catch (NumberFormatException e2) {
throw new ArgumentSyntaxException("Invalid number", input, FORMAT_ERROR);
}
}
@Override
public void processNodes(@NotNull NodeMaker nodeMaker, boolean executable) {
DeclareCommandsPacket.Node argumentNode = simpleArgumentNode(this, executable, false, false);
argumentNode.parser = "minecraft:int_range";
nodeMaker.addNodes(new DeclareCommandsPacket.Node[]{argumentNode});
super(id, Integer.MIN_VALUE, Integer.MAX_VALUE, Integer::parseInt, "minecraft:int_range", IntRange::new);
}
@Override

View File

@ -1,17 +1,81 @@
package net.minestom.server.command.builder.arguments.minecraft;
import net.minestom.server.command.builder.NodeMaker;
import net.minestom.server.command.builder.arguments.Argument;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
import net.minestom.server.utils.math.FloatRange;
import org.jetbrains.annotations.NotNull;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.regex.Pattern;
/**
* Abstract class used by {@link ArgumentIntRange} and {@link ArgumentFloatRange}.
*
* @param <T> the type of the range
*/
public abstract class ArgumentRange<T> extends Argument<T> {
public abstract class ArgumentRange<T, N extends Number> extends Argument<T> {
public static final int FORMAT_ERROR = -1;
private final N min;
private final N max;
private final Function<String, N> parser;
private final String parserName;
private final BiFunction<N, N, T> rangeConstructor;
public ArgumentRange(String id) {
public ArgumentRange(@NotNull String id, N min, N max, Function<String, N> parser, String parserName, BiFunction<N, N, T> rangeConstructor) {
super(id);
this.min = min;
this.max = max;
this.parser = parser;
this.parserName = parserName;
this.rangeConstructor = rangeConstructor;
}
@NotNull
@Override
public T parse(@NotNull String input) throws ArgumentSyntaxException {
try {
final String[] split = input.split(Pattern.quote(".."), -1);
if (split.length == 2) {
final N min;
final N max;
if (split[0].length() == 0 && split[1].length() > 0) {
// Format ..NUMBER
min = this.min;
max = parser.apply(split[1]);
} else if (split[0].length() > 0 && split[1].length() == 0) {
// Format NUMBER..
min = parser.apply(split[0]);
max = this.max;
} else if (split[0].length() > 0) {
// Format NUMBER..NUMBER
min = parser.apply(split[0]);
max = parser.apply(split[1]);
} else {
// Format ..
throw new ArgumentSyntaxException("Invalid range format", input, FORMAT_ERROR);
}
return rangeConstructor.apply(min, max);
} else if (split.length == 1) {
final N number = parser.apply(input);
return rangeConstructor.apply(number, number);
}
} catch (NumberFormatException e2) {
throw new ArgumentSyntaxException("Invalid number", input, FORMAT_ERROR);
}
throw new ArgumentSyntaxException("Invalid range format", input, FORMAT_ERROR);
}
@Override
public void processNodes(@NotNull NodeMaker nodeMaker, boolean executable) {
DeclareCommandsPacket.Node argumentNode = simpleArgumentNode(this, executable, false, false);
argumentNode.parser = parserName;
nodeMaker.addNodes(new DeclareCommandsPacket.Node[]{argumentNode});
}
}