Switched from float to double in economical stuff

This commit is contained in:
Acrobot 2012-04-19 15:49:48 +02:00
parent 9f63103ff3
commit fce5508aa3
2 changed files with 17 additions and 18 deletions

View File

@ -15,10 +15,10 @@ public class Economy {
return economy.hasAccount(uLongName.getName(p)); return economy.hasAccount(uLongName.getName(p));
} }
public static void add(String name, float amount) { public static void add(String name, double amount) {
String account = Config.getString(Property.SERVER_ECONOMY_ACCOUNT); String account = Config.getString(Property.SERVER_ECONOMY_ACCOUNT);
if (!account.isEmpty()) { if (!account.isEmpty()) {
float tax = getTax(Property.TAX_AMOUNT, amount); double tax = getTax(Property.TAX_AMOUNT, amount);
economy.add(account, tax); economy.add(account, tax);
amount = amount - tax; amount = amount - tax;
} }
@ -26,25 +26,25 @@ public class Economy {
economy.add(uLongName.getName(name), amount); economy.add(uLongName.getName(name), amount);
} }
public static void addServer(String name, float amount) { public static void addServer(String name, double amount) {
String account = Config.getString(Property.SERVER_ECONOMY_ACCOUNT); String account = Config.getString(Property.SERVER_ECONOMY_ACCOUNT);
if (!account.isEmpty()) { if (!account.isEmpty()) {
float tax = getTax(Property.SERVER_TAX_AMOUNT, amount); double tax = getTax(Property.SERVER_TAX_AMOUNT, amount);
economy.add(account, tax); economy.add(account, tax);
amount = amount - tax; amount = amount - tax;
} }
economy.add(uLongName.getName(name), amount); economy.add(uLongName.getName(name), amount);
} }
public static float getTax(Property tax, float price) { public static double getTax(Property tax, double price) {
return (Config.getFloat(tax) / 100F) * price; return (Config.getFloat(tax) / 100F) * price;
} }
public static void subtract(String name, float amount) { public static void subtract(String name, double amount) {
economy.subtract(uLongName.getName(name), amount); economy.subtract(uLongName.getName(name), amount);
} }
public static boolean hasEnough(String name, float amount) { public static boolean hasEnough(String name, double amount) {
return economy.hasEnough(uLongName.getName(name), amount); return economy.hasEnough(uLongName.getName(name), amount);
} }

View File

@ -24,23 +24,20 @@ public class Shop {
public final ItemStack stock; public final ItemStack stock;
public int stockAmount; public int stockAmount;
public float buyPrice;
public float sellPrice;
public final String owner; public final String owner;
public final Sign sign; public final Sign sign;
public Shop(ChestObject chest, boolean buy, Sign sign, ItemStack... itemStacks) { public Shop(ChestObject chest, Sign sign, ItemStack... itemStacks) {
this.stock = itemStacks[0]; this.stock = itemStacks[0];
this.durability = stock.getDurability(); this.durability = stock.getDurability();
this.chest = chest; this.chest = chest;
this.buyPrice = (buy ? uSign.buyPrice(sign.getLine(2)) : -1);
this.sellPrice = (!buy ? uSign.sellPrice(sign.getLine(2)) : -1);
this.owner = sign.getLine(0); this.owner = sign.getLine(0);
this.stockAmount = uSign.itemAmount(sign.getLine(1)); this.stockAmount = uSign.itemAmount(sign.getLine(1));
this.sign = sign; this.sign = sign;
} }
public void buy(Player player) { public void buy(Player player) {
double buyPrice = uSign.buyPrice(sign.getLine(2));
if (chest == null && !isAdminShop()) { if (chest == null && !isAdminShop()) {
player.sendMessage(Config.getLocal(Language.NO_CHEST_DETECTED)); player.sendMessage(Config.getLocal(Language.NO_CHEST_DETECTED));
return; return;
@ -55,7 +52,7 @@ public class Shop {
} }
String playerName = player.getName(); String playerName = player.getName();
if (!Economy.hasEnough(playerName, buyPrice)) { if (!Economy.hasEnough(playerName, buyPrice)) {
int items = calculateItemAmount(Economy.balance(playerName), true); int items = calculateItemAmount(Economy.balance(playerName), buyPrice);
if (!Config.getBoolean(Property.ALLOW_PARTIAL_TRANSACTIONS) || items < 1) { if (!Config.getBoolean(Property.ALLOW_PARTIAL_TRANSACTIONS) || items < 1) {
player.sendMessage(Config.getLocal(Language.NOT_ENOUGH_MONEY)); player.sendMessage(Config.getLocal(Language.NOT_ENOUGH_MONEY));
return; return;
@ -105,7 +102,7 @@ public class Shop {
} }
uInventory.add(player.getInventory(), stock, stockAmount); uInventory.add(player.getInventory(), stock, stockAmount);
Logging.logTransaction(true, this, player); Logging.logTransaction(true, this, buyPrice, player);
player.updateInventory(); player.updateInventory();
if (Config.getBoolean(Property.SHOW_TRANSACTION_INFORMATION_OWNER)) { if (Config.getBoolean(Property.SHOW_TRANSACTION_INFORMATION_OWNER)) {
@ -120,6 +117,8 @@ public class Shop {
} }
public void sell(Player player) { public void sell(Player player) {
double sellPrice = uSign.sellPrice(sign.getLine(2));
if (chest == null && !isAdminShop()) { if (chest == null && !isAdminShop()) {
player.sendMessage(Config.getLocal(Language.NO_CHEST_DETECTED)); player.sendMessage(Config.getLocal(Language.NO_CHEST_DETECTED));
return; return;
@ -137,7 +136,7 @@ public class Shop {
boolean accountExists = !account.isEmpty() && Economy.hasAccount(account); boolean accountExists = !account.isEmpty() && Economy.hasAccount(account);
if (accountExists && !Economy.hasEnough(account, sellPrice)) { if (accountExists && !Economy.hasEnough(account, sellPrice)) {
int items = calculateItemAmount(Economy.balance(account), false); int items = calculateItemAmount(Economy.balance(account), sellPrice);
if (!Config.getBoolean(Property.ALLOW_PARTIAL_TRANSACTIONS) || items < 1) { if (!Config.getBoolean(Property.ALLOW_PARTIAL_TRANSACTIONS) || items < 1) {
player.sendMessage(Config.getLocal(Language.NOT_ENOUGH_MONEY_SHOP)); player.sendMessage(Config.getLocal(Language.NOT_ENOUGH_MONEY_SHOP));
return; return;
@ -180,7 +179,7 @@ public class Shop {
} }
uInventory.remove(player.getInventory(), stock, stockAmount, durability); uInventory.remove(player.getInventory(), stock, stockAmount, durability);
Logging.logTransaction(false, this, player); Logging.logTransaction(false, this, sellPrice, player);
player.updateInventory(); player.updateInventory();
if (Config.getBoolean(Property.SHOW_TRANSACTION_INFORMATION_OWNER)) { if (Config.getBoolean(Property.SHOW_TRANSACTION_INFORMATION_OWNER)) {
@ -217,8 +216,8 @@ public class Shop {
return chest.fits(stock, stockAmount, durability); return chest.fits(stock, stockAmount, durability);
} }
private int calculateItemAmount(double money, boolean buy) { private int calculateItemAmount(double money, double basePrice) {
return (int) Math.floor(money / ((buy ? buyPrice : sellPrice) / stockAmount)); return (int) Math.floor(money / (basePrice / stockAmount));
} }
private void sendMessageToOwner(String msg) { private void sendMessageToOwner(String msg) {