Minestom/src/main/java/net/minestom/server/command/builder/arguments/minecraft/ArgumentFloatRange.java

76 lines
2.3 KiB
Java
Raw Normal View History

package net.minestom.server.command.builder.arguments.minecraft;
import net.minestom.server.utils.math.FloatRange;
import java.util.regex.Pattern;
2020-07-11 14:16:36 +02:00
/**
* Represent an argument which will give you an {@link FloatRange}
* Chat format: ..3, 3.., 5..10, 15
2020-07-11 14:16:36 +02:00
*/
2020-07-11 00:38:39 +02:00
public class ArgumentFloatRange extends ArgumentRange<FloatRange> {
public ArgumentFloatRange(String id) {
super(id);
}
@Override
public int getCorrectionResult(String value) {
try {
Float.valueOf(value);
return SUCCESS; // Is a single number
} catch (NumberFormatException e) {
String[] split = value.split(Pattern.quote(".."));
2020-07-11 00:38:39 +02:00
if (split.length == 1) {
try {
2020-07-11 14:16:36 +02:00
Float.valueOf(split[0]);
2020-07-11 00:38:39 +02:00
return SUCCESS;
} catch (NumberFormatException e2) {
return FORMAT_ERROR;
}
} else if (split.length == 2) {
try {
Float.valueOf(split[0]); // min
Float.valueOf(split[1]); // max
return SUCCESS;
} catch (NumberFormatException e2) {
return FORMAT_ERROR;
}
} else {
return FORMAT_ERROR;
}
}
}
@Override
public FloatRange parse(String value) {
if (value.contains("..")) {
2020-07-11 00:38:39 +02:00
final int index = value.indexOf('.');
String[] split = value.split(Pattern.quote(".."));
2020-07-11 00:38:39 +02:00
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);
} else {
final float number = Float.valueOf(value);
return new FloatRange(number);
}
}
}