Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make account a bit smarter #6597

Merged
merged 1 commit into from
Apr 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/com/palmergames/bukkit/towny/object/economy/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public Account(String name, World world) {
* @param reason The reason for adding.
* @return boolean indicating success.
*/
public boolean deposit(double amount, String reason) {
public synchronized boolean deposit(double amount, String reason) {
if (addMoney(amount)) {
notifyObserversDeposit(this, amount, reason);
if (TownySettings.getBoolean(ConfigNodes.ECO_CLOSED_ECONOMY_ENABLED))
Expand All @@ -87,7 +87,7 @@ public boolean deposit(double amount, String reason) {
* @param reason The reason for subtracting.
* @return boolean indicating success.
*/
public boolean withdraw(double amount, String reason) {
public synchronized boolean withdraw(double amount, String reason) {
if (subtractMoney(amount)) {
notifyObserversWithdraw(this, amount, reason);
if (TownySettings.getBoolean(ConfigNodes.ECO_CLOSED_ECONOMY_ENABLED))
Expand All @@ -111,13 +111,13 @@ public boolean payTo(double amount, EconomyHandler collector, String reason) {
return payTo(amount, collector.getAccount(), reason);
}

protected boolean payToServer(double amount, String reason) {
protected synchronized boolean payToServer(double amount, String reason) {
notifyObserversDeposit(EconomyAccount.SERVER_ACCOUNT, amount, reason);
// Put it back into the server.
return TownyEconomyHandler.addToServer(amount, getBukkitWorld());
}

protected boolean payFromServer(double amount, String reason) {
protected synchronized boolean payFromServer(double amount, String reason) {
notifyObserversWithdraw(EconomyAccount.SERVER_ACCOUNT, amount, reason);
// Remove it from the server economy.
return TownyEconomyHandler.subtractFromServer(amount, getBukkitWorld());
Expand All @@ -131,7 +131,7 @@ protected boolean payFromServer(double amount, String reason) {
* @param reason The reason for the pay.
* @return boolean indicating success.
*/
public boolean payTo(double amount, Account collector, String reason) {
public synchronized boolean payTo(double amount, Account collector, String reason) {

if (amount > getHoldingBalance()) {
return false;
Expand Down Expand Up @@ -172,7 +172,7 @@ public boolean setBalance(double amount, String reason) {
}
}

public double getHoldingBalance() {
public synchronized double getHoldingBalance() {
return getHoldingBalance(true);
}

Expand All @@ -182,7 +182,7 @@ public double getHoldingBalance() {
* @param setCache when True the account will have its cachedbalance set.
* @return The amount in this account.
*/
public double getHoldingBalance(boolean setCache) {
public synchronized double getHoldingBalance(boolean setCache) {
double balance = TownyEconomyHandler.getBalance(getName(), getBukkitWorld());
if (setCache)
cachedBalance.setBalance(balance);
Expand All @@ -195,7 +195,7 @@ public double getHoldingBalance(boolean setCache) {
* @param amount currency to check for
* @return true if there is enough.
*/
public boolean canPayFromHoldings(double amount) {
public synchronized boolean canPayFromHoldings(double amount) {
return TownyEconomyHandler.hasEnough(getName(), amount, getBukkitWorld());
}

Expand Down Expand Up @@ -308,6 +308,8 @@ void setBalance(double _balance) {
}

void updateCache() {
time = System.currentTimeMillis();

if (!TownySettings.isEconomyAsync()) // Some economy plugins don't handle things async, luckily we have a config option for this such case.
setBalance(getHoldingBalance());
else
Expand All @@ -334,7 +336,7 @@ public double getCachedBalance() {
* @param refreshIfStale when true, if the cache is stale it will update.
* @return balance {@link Double} which is from a {@link CachedBalance#balance}.
*/
public double getCachedBalance(boolean refreshIfStale) {
public synchronized double getCachedBalance(boolean refreshIfStale) {
if (refreshIfStale && cachedBalance.isStale())
cachedBalance.updateCache();

Expand Down