Skip to content
This repository was archived by the owner on Jan 16, 2024. It is now read-only.
Open
Show file tree
Hide file tree
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
35 changes: 35 additions & 0 deletions src/main/java/io/github/flibio/economylite/LockUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* This file is part of EconomyLite, licensed under the MIT License (MIT). See the LICENSE file at the root of this project for more information.
*/
package io.github.flibio.economylite;

import java.util.concurrent.Semaphore;

public class LockUtils implements AutoCloseable {
static private Semaphore lock = new Semaphore(1);
static private long threadID = 0;
static private int numLock = 0;

public LockUtils() {
if (threadID > 0 && Thread.currentThread().getId() == threadID) {
numLock++;
return;
}
try {
lock.acquire();
threadID = Thread.currentThread().getId();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

@Override
public void close() {
if (numLock == 0) {
threadID = 0;
lock.release();
} else {
numLock--;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import io.github.flibio.economylite.CauseFactory;
import io.github.flibio.economylite.EconomyLite;
import io.github.flibio.economylite.LockUtils;
import io.github.flibio.economylite.api.CurrencyEconService;
import io.github.flibio.economylite.api.PlayerEconService;
import io.github.flibio.economylite.impl.economy.event.LiteEconomyTransactionEvent;
Expand Down Expand Up @@ -76,8 +77,10 @@ public boolean hasBalance(Currency currency, Set<Context> contexts) {

@Override
public BigDecimal getBalance(Currency currency, Set<Context> contexts) {
if (!hasBalance(currency, contexts)) {
playerService.setBalance(uuid, getDefaultBalance(currency), currency, CauseFactory.create("New Account"));
try (LockUtils lock = new LockUtils()) {
if (!hasBalance(currency, contexts)) {
playerService.setBalance(uuid, getDefaultBalance(currency), currency, CauseFactory.create("New Account"));
}
}
return playerService.getBalance(uuid, currency, CauseFactory.create("Get Balance"));
}
Expand All @@ -99,84 +102,96 @@ public TransactionResult setBalance(Currency currency, BigDecimal amount, Cause
if (amount.compareTo(BigDecimal.ZERO) == -1 || amount.compareTo(BigDecimal.valueOf(999999999)) == 1) {
return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_SPACE, TransactionTypes.DEPOSIT, cause);
}
if (playerService.setBalance(uuid, amount, currency, cause)) {
return resultAndEvent(this, amount, currency, ResultType.SUCCESS, TransactionTypes.DEPOSIT, cause);
} else {
return resultAndEvent(this, amount, currency, ResultType.FAILED, TransactionTypes.DEPOSIT, cause);
try (LockUtils lock = new LockUtils()) {
if (playerService.setBalance(uuid, amount, currency, cause)) {
return resultAndEvent(this, amount, currency, ResultType.SUCCESS, TransactionTypes.DEPOSIT, cause);
} else {
return resultAndEvent(this, amount, currency, ResultType.FAILED, TransactionTypes.DEPOSIT, cause);
}
}
}

@Override
public Map<Currency, TransactionResult> resetBalances(Cause cause, Set<Context> contexts) {
HashMap<Currency, TransactionResult> results = new HashMap<>();
for (Currency currency : currencyService.getCurrencies()) {
if (playerService.accountExists(uuid, currency, cause)) {
if (playerService.setBalance(uuid, getDefaultBalance(currency), currency, cause)) {
results.put(currency, resultAndEvent(this, getBalance(currency), currency, ResultType.SUCCESS, TransactionTypes.WITHDRAW, cause));
} else {
results.put(currency, resultAndEvent(this, getBalance(currency), currency, ResultType.FAILED, TransactionTypes.WITHDRAW, cause));
try (LockUtils lock = new LockUtils()) {
for (Currency currency : currencyService.getCurrencies()) {
if (playerService.accountExists(uuid, currency, cause)) {
if (playerService.setBalance(uuid, getDefaultBalance(currency), currency, cause)) {
results.put(currency, resultAndEvent(this, getBalance(currency), currency, ResultType.SUCCESS, TransactionTypes.WITHDRAW, cause));
} else {
results.put(currency, resultAndEvent(this, getBalance(currency), currency, ResultType.FAILED, TransactionTypes.WITHDRAW, cause));
}
}
}
return results;
}
return results;
}

@Override
public TransactionResult resetBalance(Currency currency, Cause cause, Set<Context> contexts) {
if (playerService.setBalance(uuid, getDefaultBalance(currency), currency, cause)) {
return resultAndEvent(this, BigDecimal.ZERO, currency, ResultType.SUCCESS, TransactionTypes.WITHDRAW, cause);
} else {
return resultAndEvent(this, BigDecimal.ZERO, currency, ResultType.FAILED, TransactionTypes.WITHDRAW, cause);
try (LockUtils lock = new LockUtils()) {
if (playerService.setBalance(uuid, getDefaultBalance(currency), currency, cause)) {
return resultAndEvent(this, BigDecimal.ZERO, currency, ResultType.SUCCESS, TransactionTypes.WITHDRAW, cause);
} else {
return resultAndEvent(this, BigDecimal.ZERO, currency, ResultType.FAILED, TransactionTypes.WITHDRAW, cause);
}
}
}

@Override
public TransactionResult deposit(Currency currency, BigDecimal amount, Cause cause, Set<Context> contexts) {
BigDecimal newBal = getBalance(currency).add(amount);
// Check if the new balance is in bounds
if (newBal.compareTo(BigDecimal.ZERO) == -1 || newBal.compareTo(BigDecimal.valueOf(999999999)) == 1) {
return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_SPACE, TransactionTypes.DEPOSIT, cause);
}
if (playerService.deposit(uuid, amount, currency, cause)) {
return resultAndEvent(this, amount, currency, ResultType.SUCCESS, TransactionTypes.DEPOSIT, cause);
} else {
return resultAndEvent(this, amount, currency, ResultType.FAILED, TransactionTypes.DEPOSIT, cause);
try (LockUtils lock = new LockUtils()) {
BigDecimal newBal = getBalance(currency).add(amount);
// Check if the new balance is in bounds
if (newBal.compareTo(BigDecimal.ZERO) == -1 || newBal.compareTo(BigDecimal.valueOf(999999999)) == 1) {
return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_SPACE, TransactionTypes.DEPOSIT, cause);
}
if (playerService.deposit(uuid, amount, currency, cause)) {
return resultAndEvent(this, amount, currency, ResultType.SUCCESS, TransactionTypes.DEPOSIT, cause);
} else {
return resultAndEvent(this, amount, currency, ResultType.FAILED, TransactionTypes.DEPOSIT, cause);
}
}
}

@Override
public TransactionResult withdraw(Currency currency, BigDecimal amount, Cause cause, Set<Context> contexts) {
BigDecimal newBal = getBalance(currency).subtract(amount);
// Check if the new balance is in bounds
if (newBal.compareTo(BigDecimal.ZERO) == -1) {
return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_FUNDS, TransactionTypes.WITHDRAW, cause);
}
if (newBal.compareTo(BigDecimal.valueOf(999999999)) == 1) {
return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_SPACE, TransactionTypes.WITHDRAW, cause);
}
if (playerService.withdraw(uuid, amount, currency, cause)) {
return resultAndEvent(this, amount, currency, ResultType.SUCCESS, TransactionTypes.WITHDRAW, cause);
} else {
return resultAndEvent(this, amount, currency, ResultType.FAILED, TransactionTypes.WITHDRAW, cause);
try (LockUtils lock = new LockUtils()) {
BigDecimal newBal = getBalance(currency).subtract(amount);
// Check if the new balance is in bounds
if (newBal.compareTo(BigDecimal.ZERO) == -1) {
return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_FUNDS, TransactionTypes.WITHDRAW, cause);
}
if (newBal.compareTo(BigDecimal.valueOf(999999999)) == 1) {
return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_SPACE, TransactionTypes.WITHDRAW, cause);
}
if (playerService.withdraw(uuid, amount, currency, cause)) {
return resultAndEvent(this, amount, currency, ResultType.SUCCESS, TransactionTypes.WITHDRAW, cause);
} else {
return resultAndEvent(this, amount, currency, ResultType.FAILED, TransactionTypes.WITHDRAW, cause);
}
}
}

@Override
public TransferResult transfer(Account to, Currency currency, BigDecimal amount, Cause cause, Set<Context> contexts) {
BigDecimal newBal = to.getBalance(currency).add(amount);
// Check if the new balance is in bounds
if (newBal.compareTo(BigDecimal.ZERO) == -1 || newBal.compareTo(BigDecimal.valueOf(999999999)) == 1) {
return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_SPACE, to, cause);
}
// Check if the account has enough funds
if (amount.compareTo(getBalance(currency)) == 1) {
return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_FUNDS, to, cause);
}
if (withdraw(currency, amount, cause).getResult().equals(ResultType.SUCCESS)
&& to.deposit(currency, amount, cause).getResult().equals(ResultType.SUCCESS)) {
return resultAndEvent(this, amount, currency, ResultType.SUCCESS, to, cause);
} else {
return resultAndEvent(this, amount, currency, ResultType.FAILED, to, cause);
try (LockUtils lock = new LockUtils()) {
BigDecimal newBal = to.getBalance(currency).add(amount);
// Check if the new balance is in bounds
if (newBal.compareTo(BigDecimal.ZERO) == -1 || newBal.compareTo(BigDecimal.valueOf(999999999)) == 1) {
return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_SPACE, to, cause);
}
// Check if the account has enough funds
if (amount.compareTo(getBalance(currency)) == 1) {
return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_FUNDS, to, cause);
}
if (withdraw(currency, amount, cause).getResult().equals(ResultType.SUCCESS)
&& to.deposit(currency, amount, cause).getResult().equals(ResultType.SUCCESS)) {
return resultAndEvent(this, amount, currency, ResultType.SUCCESS, to, cause);
} else {
return resultAndEvent(this, amount, currency, ResultType.FAILED, to, cause);
}
}
}

Expand Down Expand Up @@ -207,5 +222,4 @@ private TransferResult resultAndEvent(Account account, BigDecimal amount, Curren
public UUID getUniqueId() {
return uuid;
}

}
Loading