Improve internal precision of calculations (Fixes #221)

This commit is contained in:
Phoenix616 2019-04-30 16:02:01 +01:00
parent 8cf9934a32
commit 6773f89221
2 changed files with 10 additions and 10 deletions

View File

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

View File

@ -41,7 +41,7 @@ public class PartialTransactionModule implements Listener {
Player client = event.getClient(); Player client = event.getClient();
BigDecimal pricePerItem = event.getExactPrice().divide(BigDecimal.valueOf(InventoryUtil.countItems(event.getStock())), Properties.PRICE_PRECISION, BigDecimal.ROUND_HALF_UP); BigDecimal pricePerItem = event.getExactPrice().divide(BigDecimal.valueOf(InventoryUtil.countItems(event.getStock())), MathContext.DECIMAL128);
CurrencyAmountEvent currencyAmountEvent = new CurrencyAmountEvent(client); CurrencyAmountEvent currencyAmountEvent = new CurrencyAmountEvent(client);
ChestShop.callEvent(currencyAmountEvent); ChestShop.callEvent(currencyAmountEvent);
@ -59,7 +59,7 @@ public class PartialTransactionModule implements Listener {
return; return;
} }
event.setExactPrice(pricePerItem.multiply(new BigDecimal(amountAffordable))); event.setExactPrice(pricePerItem.multiply(new BigDecimal(amountAffordable)).setScale(Properties.PRICE_PRECISION, BigDecimal.ROUND_HALF_UP));
event.setStock(getCountedItemStack(event.getStock(), amountAffordable)); event.setStock(getCountedItemStack(event.getStock(), amountAffordable));
} }
@ -72,7 +72,7 @@ public class PartialTransactionModule implements Listener {
return; return;
} }
event.setExactPrice(pricePerItem.multiply(BigDecimal.valueOf(possessedItemCount))); event.setExactPrice(pricePerItem.multiply(BigDecimal.valueOf(possessedItemCount)).setScale(Properties.PRICE_PRECISION, BigDecimal.ROUND_HALF_UP));
event.setStock(itemsHad); event.setStock(itemsHad);
} }
@ -85,7 +85,7 @@ public class PartialTransactionModule implements Listener {
} }
event.setStock(itemsFit); event.setStock(itemsFit);
event.setExactPrice(pricePerItem.multiply(BigDecimal.valueOf(possessedItemCount))); event.setExactPrice(pricePerItem.multiply(BigDecimal.valueOf(possessedItemCount)).setScale(Properties.PRICE_PRECISION, BigDecimal.ROUND_HALF_UP));
} }
UUID seller = event.getOwnerAccount().getUuid(); UUID seller = event.getOwnerAccount().getUuid();
@ -107,7 +107,7 @@ public class PartialTransactionModule implements Listener {
Player client = event.getClient(); Player client = event.getClient();
UUID owner = event.getOwnerAccount().getUuid(); UUID owner = event.getOwnerAccount().getUuid();
BigDecimal pricePerItem = event.getExactPrice().divide(BigDecimal.valueOf(InventoryUtil.countItems(event.getStock())), Properties.PRICE_PRECISION, BigDecimal.ROUND_HALF_UP); BigDecimal pricePerItem = event.getExactPrice().divide(BigDecimal.valueOf(InventoryUtil.countItems(event.getStock())), MathContext.DECIMAL128);
CurrencyAmountEvent currencyAmountEvent = new CurrencyAmountEvent(owner, client.getWorld()); CurrencyAmountEvent currencyAmountEvent = new CurrencyAmountEvent(owner, client.getWorld());
ChestShop.callEvent(currencyAmountEvent); ChestShop.callEvent(currencyAmountEvent);
@ -126,7 +126,7 @@ public class PartialTransactionModule implements Listener {
return; return;
} }
event.setExactPrice(pricePerItem.multiply(BigDecimal.valueOf(amountAffordable))); event.setExactPrice(pricePerItem.multiply(BigDecimal.valueOf(amountAffordable)).setScale(Properties.PRICE_PRECISION, BigDecimal.ROUND_HALF_UP));
event.setStock(getCountedItemStack(event.getStock(), amountAffordable)); event.setStock(getCountedItemStack(event.getStock(), amountAffordable));
} }
} }
@ -140,7 +140,7 @@ public class PartialTransactionModule implements Listener {
return; return;
} }
event.setExactPrice(pricePerItem.multiply(BigDecimal.valueOf(possessedItemCount))); event.setExactPrice(pricePerItem.multiply(BigDecimal.valueOf(possessedItemCount)).setScale(Properties.PRICE_PRECISION, BigDecimal.ROUND_HALF_UP));
event.setStock(itemsHad); event.setStock(itemsHad);
} }
@ -153,7 +153,7 @@ public class PartialTransactionModule implements Listener {
} }
event.setStock(itemsFit); event.setStock(itemsFit);
event.setExactPrice(pricePerItem.multiply(BigDecimal.valueOf(possessedItemCount))); event.setExactPrice(pricePerItem.multiply(BigDecimal.valueOf(possessedItemCount)).setScale(Properties.PRICE_PRECISION, BigDecimal.ROUND_HALF_UP));
} }
CurrencyHoldEvent currencyHoldEvent = new CurrencyHoldEvent(event.getExactPrice(), client); CurrencyHoldEvent currencyHoldEvent = new CurrencyHoldEvent(event.getExactPrice(), client);