refactor: check for finite float/double
This commit is contained in:
@@ -184,25 +184,31 @@ public class CommandContext {
|
||||
}
|
||||
}
|
||||
|
||||
public Double getDouble (boolean required) throws CommandException {
|
||||
public Double getDouble (boolean required, boolean allowInfinite) throws CommandException {
|
||||
final String string = getString(false, required, "double");
|
||||
|
||||
if (string.isEmpty()) return null;
|
||||
|
||||
try {
|
||||
return Double.parseDouble(string);
|
||||
final double parsedDouble = Double.parseDouble(string);
|
||||
|
||||
if (!Double.isFinite(parsedDouble) && !allowInfinite) throw new NumberFormatException();
|
||||
else return parsedDouble;
|
||||
} catch (NumberFormatException e) {
|
||||
throw new CommandException(Component.text("Invalid double"));
|
||||
}
|
||||
}
|
||||
|
||||
public Float getFloat (boolean required) throws CommandException {
|
||||
public Float getFloat (boolean required, boolean allowInfinite) throws CommandException {
|
||||
final String string = getString(false, required, "float");
|
||||
|
||||
if (string.isEmpty()) return null;
|
||||
|
||||
try {
|
||||
return Float.parseFloat(string);
|
||||
final float parsedFloat = Float.parseFloat(string);
|
||||
|
||||
if (!Float.isFinite(parsedFloat) && !allowInfinite) throw new NumberFormatException();
|
||||
else return parsedFloat;
|
||||
} catch (NumberFormatException e) {
|
||||
throw new CommandException(Component.text("Invalid float"));
|
||||
}
|
||||
|
||||
@@ -405,7 +405,7 @@ public class MusicCommand extends Command {
|
||||
|
||||
final Bot bot = context.bot;
|
||||
|
||||
final float pitch = context.getFloat(true);
|
||||
final float pitch = context.getFloat(true, false);
|
||||
|
||||
bot.music.pitch = pitch;
|
||||
|
||||
@@ -421,7 +421,7 @@ public class MusicCommand extends Command {
|
||||
final Bot bot = context.bot;
|
||||
final Song currentSong = bot.music.currentSong;
|
||||
|
||||
final float speed = context.getFloat(true);
|
||||
final float speed = context.getFloat(true, false);
|
||||
|
||||
if (speed > 5) throw new CommandException(Component.text("Too fast!"));
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ public class TimestampUtilities {
|
||||
|
||||
if (hourString != null) time += (long) Integer.parseInt(hourString) * 60 * 60 * 1000;
|
||||
time += (long) Integer.parseInt(minuteString) * 60 * 1000;
|
||||
time += Double.parseDouble(secondString) * 1000.0;
|
||||
time += (long) (Double.parseDouble(secondString) * 1000.0);
|
||||
|
||||
return time;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user