mirror of
https://github.com/filoghost/ChestCommands.git
synced 2024-11-22 10:05:17 +01:00
Separate number parsing and refactoring
This commit is contained in:
parent
0f999dbb5d
commit
1da614aa29
@ -17,8 +17,9 @@ package me.filoghost.chestcommands.action;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import me.filoghost.chestcommands.bridge.BarAPIBridge;
|
||||
import me.filoghost.chestcommands.parser.FormatException;
|
||||
import me.filoghost.chestcommands.parser.NumberParser;
|
||||
import me.filoghost.chestcommands.util.FormatUtils;
|
||||
import me.filoghost.chestcommands.util.Utils;
|
||||
|
||||
public class DragonBarAction extends Action {
|
||||
|
||||
@ -37,9 +38,14 @@ public class DragonBarAction extends Action {
|
||||
message = action;
|
||||
|
||||
String[] split = action.split("\\|", 2); // Max of 2 pieces
|
||||
if (split.length > 1 && Utils.isValidPositiveInteger(split[0].trim())) {
|
||||
seconds = Integer.parseInt(split[0].trim());
|
||||
message = split[1].trim();
|
||||
if (split.length > 1) {
|
||||
try {
|
||||
seconds = NumberParser.getStrictlyPositiveInteger(split[0].trim());
|
||||
message = split[1].trim();
|
||||
} catch (FormatException ex) {
|
||||
// Ignore
|
||||
// TODO: notify with message
|
||||
}
|
||||
}
|
||||
|
||||
message = FormatUtils.addColors(message);
|
||||
|
@ -18,7 +18,8 @@ import org.bukkit.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import me.filoghost.chestcommands.bridge.EconomyBridge;
|
||||
import me.filoghost.chestcommands.util.Utils;
|
||||
import me.filoghost.chestcommands.parser.FormatException;
|
||||
import me.filoghost.chestcommands.parser.NumberParser;
|
||||
|
||||
public class GiveMoneyAction extends Action {
|
||||
|
||||
@ -33,12 +34,11 @@ public class GiveMoneyAction extends Action {
|
||||
}
|
||||
|
||||
private void parseMoney(String action) {
|
||||
if (!Utils.isValidPositiveDouble(action)) {
|
||||
try {
|
||||
moneyToGive = NumberParser.getStrictlyPositiveDouble(action);
|
||||
} catch (FormatException e) {
|
||||
errorMessage = ChatColor.RED + "Invalid money amount: " + action;
|
||||
return;
|
||||
}
|
||||
errorMessage = null;
|
||||
moneyToGive = Double.parseDouble(action);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -18,7 +18,6 @@ import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import me.filoghost.chestcommands.util.MaterialsHelper;
|
||||
import me.filoghost.chestcommands.util.Utils;
|
||||
import me.filoghost.chestcommands.util.Preconditions;
|
||||
|
||||
public class ItemStackParser {
|
||||
@ -44,14 +43,7 @@ public class ItemStackParser {
|
||||
String[] splitAmount = input.split(",");
|
||||
|
||||
if (splitAmount.length > 1) {
|
||||
|
||||
if (!Utils.isValidInteger(splitAmount[1])) {
|
||||
throw new FormatException("invalid amount \"" + splitAmount[1] + "\"");
|
||||
}
|
||||
|
||||
int amount = Integer.parseInt(splitAmount[1]);
|
||||
if (amount <= 0) throw new FormatException("invalid amount \"" + splitAmount[1] + "\"");
|
||||
this.amount = amount;
|
||||
this.amount = NumberParser.getStrictlyPositiveInteger(splitAmount[1], "invalid amount \"" + splitAmount[1] + "\"");
|
||||
|
||||
// Only keep the first part as input
|
||||
input = splitAmount[0];
|
||||
@ -63,15 +55,7 @@ public class ItemStackParser {
|
||||
String[] splitByColons = input.split(":");
|
||||
|
||||
if (splitByColons.length > 1) {
|
||||
|
||||
if (!Utils.isValidShort(splitByColons[1])) {
|
||||
throw new FormatException("invalid data value \"" + splitByColons[1] + "\"");
|
||||
}
|
||||
|
||||
short dataValue = Short.parseShort(splitByColons[1]);
|
||||
if (dataValue < 0) {
|
||||
throw new FormatException("invalid data value \"" + splitByColons[1] + "\"");
|
||||
}
|
||||
short dataValue = NumberParser.getPositiveShort(splitByColons[1], "invalid data value \"" + splitByColons[1] + "\"");
|
||||
|
||||
this.explicitDataValue = true;
|
||||
this.dataValue = dataValue;
|
||||
|
@ -0,0 +1,75 @@
|
||||
/*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package me.filoghost.chestcommands.parser;
|
||||
|
||||
public class NumberParser {
|
||||
|
||||
public static double getStrictlyPositiveDouble(String input) throws FormatException {
|
||||
return getStrictlyPositiveDouble(input, "number must be a valid decimal and greater than 0");
|
||||
}
|
||||
|
||||
public static double getStrictlyPositiveDouble(String input, String errorMessage) throws FormatException {
|
||||
double value = getDouble(input, errorMessage);
|
||||
check(value > 0.0, errorMessage);
|
||||
return value;
|
||||
}
|
||||
|
||||
private static double getDouble(String input, String errorMessage) throws FormatException {
|
||||
try {
|
||||
return Double.parseDouble(input);
|
||||
} catch (NumberFormatException ex) {
|
||||
throw new FormatException(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
public static short getPositiveShort(String input, String errorMessage) throws FormatException {
|
||||
short value = getShort(input, errorMessage);
|
||||
check(value >= 0, errorMessage);
|
||||
return value;
|
||||
}
|
||||
|
||||
public static short getShort(String input, String errorMessage) throws FormatException {
|
||||
try {
|
||||
return Short.parseShort(input);
|
||||
} catch (NumberFormatException ex) {
|
||||
throw new FormatException(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
public static int getStrictlyPositiveInteger(String input) throws FormatException {
|
||||
return getStrictlyPositiveInteger(input, "number must be a valid integer and greater than 0");
|
||||
}
|
||||
|
||||
public static int getStrictlyPositiveInteger(String input, String errorMessage) throws FormatException {
|
||||
int value = getInteger(input, errorMessage);
|
||||
check(value > 0, errorMessage);
|
||||
return value;
|
||||
}
|
||||
|
||||
public static int getInteger(String input, String errorMessage) throws FormatException {
|
||||
try {
|
||||
return Integer.parseInt(input);
|
||||
} catch (NumberFormatException ex) {
|
||||
throw new FormatException(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
private static void check(boolean expression, String errorMessage) throws FormatException {
|
||||
if (!expression) {
|
||||
throw new FormatException(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -18,47 +18,12 @@ import java.util.*;
|
||||
|
||||
public final class Utils {
|
||||
|
||||
private Utils() {
|
||||
}
|
||||
private Utils() {}
|
||||
|
||||
public static int makePositive(int i) {
|
||||
return i < 0 ? 0 : i;
|
||||
}
|
||||
|
||||
public static boolean isValidInteger(String input) {
|
||||
try {
|
||||
Integer.parseInt(input);
|
||||
return true;
|
||||
} catch (NumberFormatException ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isValidPositiveInteger(String input) {
|
||||
try {
|
||||
return Integer.parseInt(input) > 0;
|
||||
} catch (NumberFormatException ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isValidShort(String input) {
|
||||
try {
|
||||
Short.parseShort(input);
|
||||
return true;
|
||||
} catch (NumberFormatException ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isValidPositiveDouble(String input) {
|
||||
try {
|
||||
return Double.parseDouble(input) > 0.0;
|
||||
} catch (NumberFormatException ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isClassLoaded(String name) {
|
||||
try {
|
||||
Class.forName(name);
|
||||
|
Loading…
Reference in New Issue
Block a user