Atomicize balance field in SimpleUserData

This commit is contained in:
Allink 2023-05-20 05:21:36 +01:00
parent 0e31bea33f
commit bdd8c1fb68
No known key found for this signature in database

View File

@ -15,6 +15,7 @@ import org.jetbrains.annotations.Nullable;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;
public class SimpleUserData implements UserData public class SimpleUserData implements UserData
{ {
@ -26,7 +27,7 @@ public class SimpleUserData implements UserData
private boolean frozen; private boolean frozen;
private boolean canInteract; private boolean canInteract;
private boolean caged; private boolean caged;
private long balance; private AtomicLong balance;
private boolean transactionsFrozen; private boolean transactionsFrozen;
public SimpleUserData(final Player player) public SimpleUserData(final Player player)
@ -56,7 +57,7 @@ public class SimpleUserData implements UserData
this.frozen = frozen; this.frozen = frozen;
this.canInteract = canInteract; this.canInteract = canInteract;
this.caged = caged; this.caged = caged;
this.balance = balance; this.balance = new AtomicLong(balance);
this.transactionsFrozen = transactionsFrozen; this.transactionsFrozen = transactionsFrozen;
} }
@ -215,24 +216,24 @@ public class SimpleUserData implements UserData
@Override @Override
public long getBalance() public long getBalance()
{ {
return balance; return balance.get();
} }
@Override @Override
public void addToBalance(long amount) public void addToBalance(long amount)
{ {
this.balance += amount; this.balance.addAndGet(amount);
} }
@Override @Override
public void removeFromBalance(long amount) public void removeFromBalance(long amount)
{ {
this.balance -= amount; this.balance.addAndGet(-amount);
} }
@Override @Override
public void setBalance(long newBalance) public void setBalance(long newBalance)
{ {
this.balance = newBalance; this.balance.set(newBalance);
} }
} }