Skip to content

Commit

Permalink
Fixed precision of doubles not being correct causing incorrect mathem…
Browse files Browse the repository at this point in the history
…atical operations (#1293)
  • Loading branch information
OmerBenGera committed Aug 4, 2022
1 parent 92da107 commit 1f589d5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
Expand Up @@ -3,6 +3,7 @@
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.hooks.EconomyProvider;
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
import com.bgsoftware.superiorskyblock.core.Precision;
import com.google.common.base.Preconditions;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.economy.EconomyResponse;
Expand All @@ -11,6 +12,7 @@
import org.bukkit.plugin.RegisteredServiceProvider;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class EconomyProvider_Vault implements EconomyProvider {

Expand All @@ -30,7 +32,8 @@ public static boolean isCompatible() {
@Override
public BigDecimal getBalance(SuperiorPlayer superiorPlayer) {
Preconditions.checkNotNull(superiorPlayer, "superiorPlayer parameter cannot be null.");
return BigDecimal.valueOf(getMoneyInBank(superiorPlayer.asOfflinePlayer()));
return BigDecimal.valueOf(getMoneyInBank(superiorPlayer.asOfflinePlayer()))
.setScale(3, RoundingMode.HALF_DOWN);
}

@Override
Expand All @@ -39,11 +42,15 @@ public EconomyResult depositMoney(SuperiorPlayer superiorPlayer, double amount)
OfflinePlayer offlinePlayer = superiorPlayer.asOfflinePlayer();
double moneyBeforeDeposit = getMoneyInBank(offlinePlayer);
EconomyResponse economyResponse = econ.depositPlayer(offlinePlayer, amount);
double moneyInTransaction = getMoneyInBank(offlinePlayer) - moneyBeforeDeposit;
double moneyInTransaction = Precision.round(getMoneyInBank(offlinePlayer) - moneyBeforeDeposit, 3);

String errorMessage = moneyInTransaction == amount ? economyResponse.errorMessage :
moneyInTransaction == 0 ? "You have exceed the limit of your bank" : "";

Bukkit.broadcastMessage("Deposit Money");
Bukkit.broadcastMessage("Transaction Money: " + moneyInTransaction);
Bukkit.broadcastMessage("New Balance: " + getMoneyInBank(offlinePlayer));

return new EconomyResult(errorMessage, moneyInTransaction);
}

Expand All @@ -53,19 +60,23 @@ public EconomyResult withdrawMoney(SuperiorPlayer superiorPlayer, double amount)
OfflinePlayer offlinePlayer = superiorPlayer.asOfflinePlayer();
double moneyBeforeWithdraw = getMoneyInBank(offlinePlayer);
EconomyResponse economyResponse = econ.withdrawPlayer(offlinePlayer, amount);
double moneyInTransaction = moneyBeforeWithdraw - getMoneyInBank(offlinePlayer);
double moneyInTransaction = Precision.round(moneyBeforeWithdraw - getMoneyInBank(offlinePlayer), 3);

String errorMessage = moneyInTransaction == amount ? economyResponse.errorMessage :
moneyInTransaction == 0 ? "Couldn't process the transaction" : "";

Bukkit.broadcastMessage("Withdraw Money");
Bukkit.broadcastMessage("Transaction Money: " + moneyInTransaction);
Bukkit.broadcastMessage("New Balance: " + getMoneyInBank(offlinePlayer));

return new EconomyResult(errorMessage, moneyInTransaction);
}

private double getMoneyInBank(OfflinePlayer offlinePlayer) {
if (!econ.hasAccount(offlinePlayer))
econ.createPlayerAccount(offlinePlayer);

return econ.getBalance(offlinePlayer);
return Precision.round(econ.getBalance(offlinePlayer), 3);
}

}
20 changes: 20 additions & 0 deletions src/main/java/com/bgsoftware/superiorskyblock/core/Precision.java
@@ -0,0 +1,20 @@
package com.bgsoftware.superiorskyblock.core;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Precision {

private Precision() {

}

public static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();

BigDecimal bd = new BigDecimal(Double.toString(value));
bd = bd.setScale(places, RoundingMode.HALF_DOWN);
return bd.doubleValue();
}

}
Expand Up @@ -282,7 +282,7 @@ private void decreaseBalance(BigDecimal amount) {
}

private void increaseBalance(BigDecimal amount) {
this.balance.updateAndGet(bigDecimal -> bigDecimal.add(amount).setScale(2, RoundingMode.HALF_DOWN));
this.balance.updateAndGet(bigDecimal -> bigDecimal.add(amount).setScale(3, RoundingMode.HALF_DOWN));
IslandsDatabaseBridge.saveBankBalance(island);
}

Expand Down

0 comments on commit 1f589d5

Please sign in to comment.