Fix number handling bugs.

This commit is contained in:
AppleDash 2016-06-14 19:47:14 -04:00
parent e5cbb1dac3
commit 0b4581b23c
7 changed files with 39 additions and 10 deletions

View File

@ -1,5 +1,5 @@
group 'org.appledash'
version '0.5.0-SNAPSHOT'
version '0.5.2-SNAPSHOT'
apply plugin: 'java'

View File

@ -56,8 +56,7 @@ public class SaneEconomy extends JavaPlugin {
@Override
public void onDisable() {
vaultHook.unhook();
}
private void loadConfig() {

View File

@ -59,10 +59,10 @@ public class EconomyAdminCommand extends SaneEconomyCommand {
return;
}
double amount = NumberUtils.parsePositiveDouble(sAmount);
double amount = NumberUtils.parseAndFilter(sAmount);
if (amount == -1) {
MessageUtils.sendMessage(sender, "%s is not a positive number.", sAmount);
if (amount <= 0) {
MessageUtils.sendMessage(sender, "%s is not a positive number.", (amount == -1 ? sAmount : amount + ""));
return;
}

View File

@ -57,10 +57,10 @@ public class PayCommand extends SaneEconomyCommand {
}
String sAmount = args[1];
double amount = NumberUtils.parsePositiveDouble(sAmount);
double amount = NumberUtils.parseAndFilter(sAmount);
if (amount == -1) {
MessageUtils.sendMessage(sender, "%s is not a positive number.", sAmount);
if (amount <= 0) {
MessageUtils.sendMessage(sender, "%s is not a positive number.", (amount == -1 ? sAmount : amount + ""));
return;
}

View File

@ -1,6 +1,7 @@
package org.appledash.saneeconomy.economy;
import org.appledash.saneeconomy.economy.backend.EconomyStorageBackend;
import org.appledash.saneeconomy.utils.NumberUtils;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
@ -73,6 +74,8 @@ public class EconomyManager {
* @throws IllegalArgumentException If amount is negative
*/
public double addBalance(OfflinePlayer targetPlayer, double amount) {
amount = NumberUtils.filterAmount(amount);
if (amount < 0) {
throw new IllegalArgumentException("Cannot add a negative amount!");
}
@ -89,6 +92,8 @@ public class EconomyManager {
* @throws IllegalArgumentException If amount is negative
*/
public double subtractBalance(OfflinePlayer targetPlayer, double amount) {
amount = NumberUtils.filterAmount(amount);
if (amount < 0) {
throw new IllegalArgumentException("Cannot subtract a negative amount!");
}
@ -109,6 +114,8 @@ public class EconomyManager {
* @throws IllegalArgumentException If amount is negative
*/
public void setBalance(OfflinePlayer targetPlayer, double amount) {
amount = NumberUtils.filterAmount(amount);
if (amount < 0) {
throw new IllegalArgumentException("Cannot set balance to a negative value!");
}
@ -125,6 +132,8 @@ public class EconomyManager {
* @throws IllegalArgumentException If amount is negative
*/
public boolean transfer(OfflinePlayer fromPlayer, Player toPlayer, double amount) {
amount = NumberUtils.filterAmount(amount);
if (amount < 0) {
throw new IllegalArgumentException("Cannot transfer a negative amount!");
}

View File

@ -1,5 +1,8 @@
package org.appledash.saneeconomy.utils;
import com.google.common.base.Strings;
import org.appledash.saneeconomy.SaneEconomy;
/**
* Created by AppleDash on 6/14/2016.
* Blackjack is still best pony.
@ -7,6 +10,16 @@ package org.appledash.saneeconomy.utils;
public class NumberUtils {
public static double parsePositiveDouble(String sDouble) {
try {
if (Strings.isNullOrEmpty(sDouble)) {
throw new NumberFormatException();
}
sDouble = sDouble.trim();
if (sDouble.equalsIgnoreCase("nan") || sDouble.equalsIgnoreCase("infinity") || sDouble.equalsIgnoreCase("-infinity")) {
throw new NumberFormatException();
}
double doub = Double.valueOf(sDouble);
if (doub < 0) {
@ -18,4 +31,12 @@ public class NumberUtils {
return -1;
}
}
public static double filterAmount(double amount) {
return Double.valueOf(SaneEconomy.getInstance().getEconomyManager().getCurrency().getFormat().format(amount));
}
public static double parseAndFilter(String sDouble) {
return filterAmount(parsePositiveDouble(sDouble));
}
}

View File

@ -1,7 +1,7 @@
name: SaneEconomy
author: AppleDash
main: org.appledash.saneeconomy.SaneEconomy
version: 0.5.0
version: 0.5.2
softdepends: [Vault]
commands:
balance: