mirror of
https://github.com/IntellectualSites/PlotSquared.git
synced 2024-11-30 13:14:06 +01:00
Create DoubleFlag + extract common parsing stuff
This commit is contained in:
parent
408e1eb5d6
commit
aab911e8ef
@ -533,6 +533,7 @@ public enum Captions implements Caption {
|
|||||||
//<editor-fold desc="Flag category captions">
|
//<editor-fold desc="Flag category captions">
|
||||||
FLAG_CATEGORY_STRING("String Flags", "Flags"),
|
FLAG_CATEGORY_STRING("String Flags", "Flags"),
|
||||||
FLAG_CATEGORY_INTEGERS("Integer Flags", "Flags"),
|
FLAG_CATEGORY_INTEGERS("Integer Flags", "Flags"),
|
||||||
|
FLAG_CATEGORY_DOUBLES("Decimal Flags", "Flags"),
|
||||||
FLAG_CATEGORY_TELEPORT_DENY("Teleport Deny Flag", "Flags"),
|
FLAG_CATEGORY_TELEPORT_DENY("Teleport Deny Flag", "Flags"),
|
||||||
FLAG_CATEGORY_STRING_LIST("String List Flags", "Flags"),
|
FLAG_CATEGORY_STRING_LIST("String List Flags", "Flags"),
|
||||||
FLAG_CATEGORY_WEATHER("Weather Flags", "Flags"),
|
FLAG_CATEGORY_WEATHER("Weather Flags", "Flags"),
|
||||||
@ -620,7 +621,7 @@ public enum Captions implements Caption {
|
|||||||
FLAG_ERROR_LONG("Flag value must be a whole number (large numbers allowed)", "Flags"),
|
FLAG_ERROR_LONG("Flag value must be a whole number (large numbers allowed)", "Flags"),
|
||||||
FLAG_ERROR_PLOTBLOCKLIST("Flag value must be a block list", "Flags"),
|
FLAG_ERROR_PLOTBLOCKLIST("Flag value must be a block list", "Flags"),
|
||||||
FLAG_ERROR_INVALID_BLOCK("The provided value is not a valid block", "Flags"),
|
FLAG_ERROR_INVALID_BLOCK("The provided value is not a valid block", "Flags"),
|
||||||
FLAG_ERROR_PRICE("Flag value must be a positive number.", "Flags"),
|
FLAG_ERROR_DOUBLE("Flag value must be a decimal number.", "Flags"),
|
||||||
FLAG_ERROR_STRING("Flag value must be alphanumeric. Some special characters are allowed.", "Flags"),
|
FLAG_ERROR_STRING("Flag value must be alphanumeric. Some special characters are allowed.", "Flags"),
|
||||||
FLAG_ERROR_STRINGLIST("Flag value must be a string list", "Flags"),
|
FLAG_ERROR_STRINGLIST("Flag value must be a string list", "Flags"),
|
||||||
FLAG_ERROR_WEATHER("Flag must be a weather: 'rain' or 'sun'", "Flags"),
|
FLAG_ERROR_WEATHER("Flag must be a weather: 'rain' or 'sun'", "Flags"),
|
||||||
|
@ -18,7 +18,7 @@ public final class Flags {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override public String getValueDescription() {
|
@Override public String getValueDescription() {
|
||||||
return Captions.FLAG_ERROR_PRICE.getTranslated();
|
return Captions.FLAG_ERROR_DOUBLE.getTranslated();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
public static final StringListFlag BLOCKED_CMDS = new StringListFlag("blocked-cmds");
|
public static final StringListFlag BLOCKED_CMDS = new StringListFlag("blocked-cmds");
|
||||||
|
@ -0,0 +1,38 @@
|
|||||||
|
package com.github.intellectualsites.plotsquared.plot.flags.types;
|
||||||
|
|
||||||
|
import com.github.intellectualsites.plotsquared.plot.config.Caption;
|
||||||
|
import com.github.intellectualsites.plotsquared.plot.config.Captions;
|
||||||
|
import com.github.intellectualsites.plotsquared.plot.flags.FlagParseException;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
public abstract class DoubleFlag<F extends NumberFlag<Double, F>> extends NumberFlag<Double, F> {
|
||||||
|
|
||||||
|
|
||||||
|
protected DoubleFlag(@NotNull Double value, Double minimum, Double maximum, @NotNull Caption flagDescription) {
|
||||||
|
super(value, minimum, maximum, Captions.FLAG_CATEGORY_DOUBLES, flagDescription);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected DoubleFlag(@NotNull Double value, @NotNull Caption flagDescription) {
|
||||||
|
this(value, Double.MIN_VALUE, Double.MAX_VALUE, flagDescription);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public F merge(@NotNull Double newValue) {
|
||||||
|
return flagOf(getValue() + newValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public String getExample() {
|
||||||
|
return "12.175";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public String toString() {
|
||||||
|
return getValue().toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull @Override protected Double parseNumber(String input) throws FlagParseException {
|
||||||
|
try {
|
||||||
|
return Double.parseDouble(input);
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
throw new FlagParseException(this, input, Captions.FLAG_ERROR_DOUBLE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -3,40 +3,18 @@ package com.github.intellectualsites.plotsquared.plot.flags.types;
|
|||||||
import com.github.intellectualsites.plotsquared.plot.config.Caption;
|
import com.github.intellectualsites.plotsquared.plot.config.Caption;
|
||||||
import com.github.intellectualsites.plotsquared.plot.config.Captions;
|
import com.github.intellectualsites.plotsquared.plot.config.Captions;
|
||||||
import com.github.intellectualsites.plotsquared.plot.flags.FlagParseException;
|
import com.github.intellectualsites.plotsquared.plot.flags.FlagParseException;
|
||||||
import com.github.intellectualsites.plotsquared.plot.flags.PlotFlag;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
public abstract class IntegerFlag<F extends PlotFlag<Integer, F>> extends PlotFlag<Integer, F> {
|
public abstract class IntegerFlag<F extends NumberFlag<Integer, F>> extends NumberFlag<Integer, F> {
|
||||||
private final int minimum;
|
|
||||||
private final int maximum;
|
|
||||||
|
|
||||||
protected IntegerFlag(final int value, int minimum, int maximum, @NotNull Caption flagDescription) {
|
protected IntegerFlag(final int value, int minimum, int maximum, @NotNull Caption flagDescription) {
|
||||||
super(value, Captions.FLAG_CATEGORY_INTEGERS, flagDescription);
|
super(value, minimum, maximum, Captions.FLAG_CATEGORY_INTEGERS, flagDescription);
|
||||||
if (maximum < minimum) {
|
|
||||||
throw new IllegalArgumentException("Maximum may not be less than minimum:" + maximum + " < " + minimum);
|
|
||||||
}
|
|
||||||
this.minimum = minimum;
|
|
||||||
this.maximum = maximum;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected IntegerFlag(@NotNull Caption flagDescription) {
|
protected IntegerFlag(@NotNull Caption flagDescription) {
|
||||||
this(0, Integer.MIN_VALUE, Integer.MAX_VALUE, flagDescription);
|
this(0, Integer.MIN_VALUE, Integer.MAX_VALUE, flagDescription);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public F parse(@NotNull String input) throws FlagParseException {
|
|
||||||
int parsed;
|
|
||||||
try {
|
|
||||||
parsed = Integer.parseInt(input);
|
|
||||||
} catch (final Throwable throwable) {
|
|
||||||
throw new FlagParseException(this, input, Captions.FLAG_ERROR_INTEGER);
|
|
||||||
}
|
|
||||||
if (parsed < minimum || parsed > maximum) {
|
|
||||||
throw new FlagParseException(this, input, Captions.NOT_VALID_NUMBER); // TODO format Caption, provide valid range
|
|
||||||
}
|
|
||||||
return flagOf(parsed);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override public F merge(@NotNull Integer newValue) {
|
@Override public F merge(@NotNull Integer newValue) {
|
||||||
return flagOf(getValue() + newValue);
|
return flagOf(getValue() + newValue);
|
||||||
}
|
}
|
||||||
@ -48,4 +26,12 @@ public abstract class IntegerFlag<F extends PlotFlag<Integer, F>> extends PlotFl
|
|||||||
@Override public String getExample() {
|
@Override public String getExample() {
|
||||||
return "10";
|
return "10";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull @Override protected Integer parseNumber(String input) throws FlagParseException {
|
||||||
|
try {
|
||||||
|
return Integer.parseInt(input);
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
throw new FlagParseException(this, input, Captions.FLAG_ERROR_INTEGER);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,41 @@
|
|||||||
|
package com.github.intellectualsites.plotsquared.plot.flags.types;
|
||||||
|
|
||||||
|
import com.github.intellectualsites.plotsquared.plot.config.Caption;
|
||||||
|
import com.github.intellectualsites.plotsquared.plot.config.Captions;
|
||||||
|
import com.github.intellectualsites.plotsquared.plot.flags.FlagParseException;
|
||||||
|
import com.github.intellectualsites.plotsquared.plot.flags.PlotFlag;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
public abstract class NumberFlag<N extends Number & Comparable<N>, F extends PlotFlag<N, F>> extends PlotFlag<N, F> {
|
||||||
|
protected final N minimum;
|
||||||
|
protected final N maximum;
|
||||||
|
|
||||||
|
protected NumberFlag(@NotNull N value, N minimum, N maximum, @NotNull Caption flagCategory,
|
||||||
|
@NotNull Caption flagDescription) {
|
||||||
|
super(value, flagCategory, flagDescription);
|
||||||
|
if (maximum.compareTo(minimum) < 0) {
|
||||||
|
throw new IllegalArgumentException("Maximum may not be less than minimum:" + maximum + " < " + minimum);
|
||||||
|
}
|
||||||
|
this.minimum = minimum;
|
||||||
|
this.maximum = maximum;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public F parse(@NotNull String input) throws FlagParseException {
|
||||||
|
final N parsed = parseNumber(input);
|
||||||
|
if (parsed.compareTo(minimum) < 0 || parsed.compareTo(maximum) > 0) {
|
||||||
|
throw new FlagParseException(this, input,
|
||||||
|
Captions.NOT_VALID_NUMBER); // TODO format Caption, provide valid range
|
||||||
|
}
|
||||||
|
return flagOf(parsed);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse the raw string input to the number type.
|
||||||
|
* Throw a {@link FlagParseException} if the number couldn't be parsed.
|
||||||
|
*
|
||||||
|
* @param input the string to parse the number from.
|
||||||
|
* @return the parsed number.
|
||||||
|
*/
|
||||||
|
@NotNull protected abstract N parseNumber(String input) throws FlagParseException;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user