Merge pull request #21 from meiamsome/patch-2

Fixed some potential bugs in the economy section
This commit is contained in:
Andrzej Pomirski 2012-08-24 11:11:52 -07:00
commit 918b728767
2 changed files with 29 additions and 20 deletions

View File

@ -65,7 +65,7 @@ public class NumberUtil {
} }
/** /**
* Rounds the number up to two digit points (Can be inaccurate due to using decimal-points) * Rounds the number up to two decimal points (Can be inaccurate due to using decimal-points)
* *
* @param number Number to round * @param number Number to round
* @return Rounded number * @return Rounded number
@ -73,6 +73,15 @@ public class NumberUtil {
public static double roundUp(double number) { public static double roundUp(double number) {
return Math.ceil(number * 100) / 100; return Math.ceil(number * 100) / 100;
} }
/**
* Rounds the number down to two decimal points
*
* @param number Number to round
* @return Rounded number
*/
public static double roundDown(double number) {
return Math.floor(number * 100) / 100;
}
/** /**
* Converts the number (in seconds) to timer-like format, like 2:00 (minutes:seconds) * Converts the number (in seconds) to timer-like format, like 2:00 (minutes:seconds)

View File

@ -17,42 +17,42 @@ public class Economy {
return !p.isEmpty() && economy.hasAccount(uName.getName(p)); return !p.isEmpty() && economy.hasAccount(uName.getName(p));
} }
public static String getServerAccountName() {
return Config.getString(Property.SERVER_ECONOMY_ACCOUNT);
}
public static boolean isServerAccount(String acc) {
return getServerAccountName().equals(acc);
}
public static void add(String name, double amount) { public static void add(String name, double amount) {
if (!hasAccount(name)) { Property taxAmount = isServerAccount(name) ? Property.SERVER_TAX_AMOUNT : Property.TAX_AMOUNT;
return;
}
String serverAccount = Config.getString(Property.SERVER_ECONOMY_ACCOUNT); double tax = getTax(taxAmount, amount);
Property taxAmount = name.equals(serverAccount) ? Property.SERVER_TAX_AMOUNT : Property.TAX_AMOUNT; if (tax != 0) {
if (!serverAccount().isEmpty()) {
if (Config.getFloat(taxAmount) != 0) { economy.add(getServerAccountName(), tax);
double tax = getTax(taxAmount, amount);
if (!serverAccount.isEmpty()) {
economy.add(serverAccount, tax);
} }
amount -= tax; amount -= tax;
} }
economy.add(uName.getName(name), roundUp(amount)); if (name.isEmpty()) return;
economy.add(uName.getName(name), amount);
} }
public static double getTax(Property tax, double price) { public static double getTax(Property tax, double price) {
return (Config.getFloat(tax) / 100F) * price; return roundDown((Config.getFloat(tax) / 100F) * price);
} }
public static void subtract(String name, double amount) { public static void subtract(String name, double amount) {
if (!hasAccount(name)) { if (name.isEmpty()) return;
return;
}
economy.subtract(uName.getName(name), roundUp(amount)); economy.subtract(uName.getName(name), roundUp(amount));
} }
public static boolean hasEnough(String name, double amount) { public static boolean hasEnough(String name, double amount) {
if (!hasAccount(name)) { if (isServerAccount(name)) {
return true; return true;
} }
return economy.hasEnough(uName.getName(name), roundUp(amount)); return economy.hasEnough(uName.getName(name), roundUp(amount));
} }