Properly set scale and rounding mode for divisions (Fixes #220)

This commit is contained in:
Phoenix616 2019-04-28 15:41:20 +01:00
parent f0bc277566
commit 8cf9934a32
3 changed files with 6 additions and 5 deletions

View File

@ -32,7 +32,7 @@ public class TaxModule implements Listener {
}
private static BigDecimal getTaxAmount(BigDecimal price, float taxAmount) {
return price.multiply(BigDecimal.valueOf(taxAmount)).divide(BigDecimal.valueOf(100), BigDecimal.ROUND_DOWN);
return price.multiply(BigDecimal.valueOf(taxAmount)).divide(BigDecimal.valueOf(100), Properties.PRICE_PRECISION, BigDecimal.ROUND_HALF_UP);
}
@EventHandler(priority = EventPriority.LOW)

View File

@ -196,7 +196,7 @@ public class PlayerInteract implements Listener {
if (Properties.SHIFT_SELLS_IN_STACKS && player.isSneaking() && price != PriceUtil.NO_PRICE && isAllowedForShift(action == buy)) {
int newAmount = adminShop ? InventoryUtil.getMaxStackSize(item) : getStackAmount(item, ownerInventory, player, action);
if (newAmount > 0) {
price = price.divide(BigDecimal.valueOf(amount), MathContext.UNLIMITED).multiply(BigDecimal.valueOf(newAmount));
price = price.divide(BigDecimal.valueOf(amount), Properties.PRICE_PRECISION, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(newAmount));
amount = newAmount;
}
}

View File

@ -3,6 +3,7 @@ package com.Acrobot.ChestShop.Listeners.PreTransaction;
import com.Acrobot.Breeze.Utils.InventoryUtil;
import com.Acrobot.Breeze.Utils.MaterialUtil;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Economy.Economy;
import com.Acrobot.ChestShop.Events.Economy.CurrencyAmountEvent;
import com.Acrobot.ChestShop.Events.Economy.CurrencyCheckEvent;
@ -40,7 +41,7 @@ public class PartialTransactionModule implements Listener {
Player client = event.getClient();
BigDecimal pricePerItem = event.getExactPrice().divide(BigDecimal.valueOf(InventoryUtil.countItems(event.getStock())));
BigDecimal pricePerItem = event.getExactPrice().divide(BigDecimal.valueOf(InventoryUtil.countItems(event.getStock())), Properties.PRICE_PRECISION, BigDecimal.ROUND_HALF_UP);
CurrencyAmountEvent currencyAmountEvent = new CurrencyAmountEvent(client);
ChestShop.callEvent(currencyAmountEvent);
@ -106,7 +107,7 @@ public class PartialTransactionModule implements Listener {
Player client = event.getClient();
UUID owner = event.getOwnerAccount().getUuid();
BigDecimal pricePerItem = event.getExactPrice().divide(BigDecimal.valueOf(InventoryUtil.countItems(event.getStock())));
BigDecimal pricePerItem = event.getExactPrice().divide(BigDecimal.valueOf(InventoryUtil.countItems(event.getStock())), Properties.PRICE_PRECISION, BigDecimal.ROUND_HALF_UP);
CurrencyAmountEvent currencyAmountEvent = new CurrencyAmountEvent(owner, client.getWorld());
ChestShop.callEvent(currencyAmountEvent);
@ -164,7 +165,7 @@ public class PartialTransactionModule implements Listener {
}
private static int getAmountOfAffordableItems(BigDecimal walletMoney, BigDecimal pricePerItem) {
return walletMoney.divide(pricePerItem, MathContext.UNLIMITED).setScale(0, RoundingMode.FLOOR).intValueExact();
return walletMoney.divide(pricePerItem, 0, RoundingMode.FLOOR).intValueExact();
}
private static ItemStack[] getItems(ItemStack[] stock, Inventory inventory) {