Fixed command range argument

This commit is contained in:
Felix Cravic 2020-07-11 00:38:39 +02:00
parent 17d638d460
commit e8cd68bc3b
3 changed files with 65 additions and 40 deletions

View File

@ -1,13 +1,10 @@
package net.minestom.server.command.builder.arguments.minecraft; package net.minestom.server.command.builder.arguments.minecraft;
import net.minestom.server.command.builder.arguments.Argument;
import net.minestom.server.utils.math.FloatRange; import net.minestom.server.utils.math.FloatRange;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class ArgumentFloatRange extends Argument<FloatRange> { public class ArgumentFloatRange extends ArgumentRange<FloatRange> {
public static final int FORMAT_ERROR = -1;
public ArgumentFloatRange(String id) { public ArgumentFloatRange(String id) {
super(id); super(id);
@ -20,7 +17,14 @@ public class ArgumentFloatRange extends Argument<FloatRange> {
return SUCCESS; // Is a single number return SUCCESS; // Is a single number
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
String[] split = value.split(Pattern.quote("..")); String[] split = value.split(Pattern.quote(".."));
if (split.length == 2) { if (split.length == 1) {
try {
Float.valueOf(split[0]); // min
return SUCCESS;
} catch (NumberFormatException e2) {
return FORMAT_ERROR;
}
} else if (split.length == 2) {
try { try {
Float.valueOf(split[0]); // min Float.valueOf(split[0]); // min
Float.valueOf(split[1]); // max Float.valueOf(split[1]); // max
@ -37,18 +41,31 @@ public class ArgumentFloatRange extends Argument<FloatRange> {
@Override @Override
public FloatRange parse(String value) { public FloatRange parse(String value) {
if (value.contains("..")) { if (value.contains("..")) {
final int index = value.indexOf('.');
String[] split = value.split(Pattern.quote("..")); String[] split = value.split(Pattern.quote(".."));
final float min = Float.valueOf(split[0]);
final float max = Float.valueOf(split[1]); final float min;
final float max;
if (index == 0) {
// Format ..NUMBER
min = Float.MIN_VALUE;
max = Float.valueOf(split[0]);
} else {
if (split.length == 2) {
// Format NUMBER..NUMBER
min = Float.valueOf(split[0]);
max = Float.valueOf(split[1]);
} else {
// Format NUMBER..
min = Float.valueOf(split[0]);
max = Float.MAX_VALUE;
}
}
return new FloatRange(min, max); return new FloatRange(min, max);
} else { } else {
final float number = Float.valueOf(value); final float number = Float.valueOf(value);
return new FloatRange(number, number); return new FloatRange(number, number);
} }
} }
@Override
public int getConditionResult(FloatRange value) {
return SUCCESS;
}
} }

View File

@ -1,13 +1,10 @@
package net.minestom.server.command.builder.arguments.minecraft; package net.minestom.server.command.builder.arguments.minecraft;
import net.minestom.server.command.builder.arguments.Argument;
import net.minestom.server.utils.math.IntRange; import net.minestom.server.utils.math.IntRange;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class ArgumentIntRange extends Argument<IntRange> { public class ArgumentIntRange extends ArgumentRange<IntRange> {
public static final int FORMAT_ERROR = -1;
public ArgumentIntRange(String id) { public ArgumentIntRange(String id) {
super(id); super(id);
@ -20,7 +17,14 @@ public class ArgumentIntRange extends Argument<IntRange> {
return SUCCESS; // Is a single number return SUCCESS; // Is a single number
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
String[] split = value.split(Pattern.quote("..")); String[] split = value.split(Pattern.quote(".."));
if (split.length == 2) { if (split.length == 1) {
try {
Integer.valueOf(split[0]); // min
return SUCCESS;
} catch (NumberFormatException e2) {
return FORMAT_ERROR;
}
} else if (split.length == 2) {
try { try {
Integer.valueOf(split[0]); // min Integer.valueOf(split[0]); // min
Integer.valueOf(split[1]); // max Integer.valueOf(split[1]); // max
@ -37,18 +41,31 @@ public class ArgumentIntRange extends Argument<IntRange> {
@Override @Override
public IntRange parse(String value) { public IntRange parse(String value) {
if (value.contains("..")) { if (value.contains("..")) {
final int index = value.indexOf('.');
String[] split = value.split(Pattern.quote("..")); String[] split = value.split(Pattern.quote(".."));
final int min = Integer.valueOf(split[0]);
final int max = Integer.valueOf(split[1]); final int min;
final int max;
if (index == 0) {
// Format ..NUMBER
min = Integer.MIN_VALUE;
max = Integer.valueOf(split[0]);
} else {
if (split.length == 2) {
// Format NUMBER..NUMBER
min = Integer.valueOf(split[0]);
max = Integer.valueOf(split[1]);
} else {
// Format NUMBER..
min = Integer.valueOf(split[0]);
max = Integer.MAX_VALUE;
}
}
return new IntRange(min, max); return new IntRange(min, max);
} else { } else {
final int number = Integer.valueOf(value); final int number = Integer.valueOf(value);
return new IntRange(number, number); return new IntRange(number, number);
} }
} }
@Override
public int getConditionResult(IntRange value) {
return SUCCESS;
}
} }

View File

@ -2,25 +2,16 @@ package net.minestom.server.command.builder.arguments.minecraft;
import net.minestom.server.command.builder.arguments.Argument; import net.minestom.server.command.builder.arguments.Argument;
// FIXME: cannot make the minecraft:range identifier working public abstract class ArgumentRange<T> extends Argument<T> {
public class ArgumentRange extends Argument<Float> {
public ArgumentRange(String id, boolean allowSpace, boolean useRemaining) { public static final int FORMAT_ERROR = -1;
super(id, allowSpace, useRemaining);
public ArgumentRange(String id) {
super(id);
} }
@Override @Override
public int getCorrectionResult(String value) { public int getConditionResult(T value) {
return 0; return SUCCESS;
}
@Override
public Float parse(String value) {
return null;
}
@Override
public int getConditionResult(Float value) {
return 0;
} }
} }