Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
Fix causes with virtual accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
Flibio committed Mar 3, 2017
1 parent 236f395 commit f7e9ef1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ name=EconomyLite
owner=Flibio
inceptionYear=2015
currentYear=2017
version=2.10.1
version=2.10.2
apiVersion=5.0.0-SNAPSHOT
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ public Map<Currency, BigDecimal> getBalances(Set<Context> contexts) {
public TransactionResult setBalance(Currency currency, BigDecimal amount, Cause cause, Set<Context> contexts) {
// Check if the new balance is in bounds
if (amount.compareTo(BigDecimal.ZERO) == -1 || amount.compareTo(BigDecimal.valueOf(999999999)) == 1) {
return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_SPACE, TransactionTypes.DEPOSIT);
return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_SPACE, TransactionTypes.DEPOSIT, cause);
}
if (virtualService.setBalance(name, amount, currency, cause)) {
return resultAndEvent(this, amount, currency, ResultType.SUCCESS, TransactionTypes.DEPOSIT);
return resultAndEvent(this, amount, currency, ResultType.SUCCESS, TransactionTypes.DEPOSIT, cause);
} else {
return resultAndEvent(this, amount, currency, ResultType.FAILED, TransactionTypes.DEPOSIT);
return resultAndEvent(this, amount, currency, ResultType.FAILED, TransactionTypes.DEPOSIT, cause);
}
}

Expand All @@ -117,9 +117,9 @@ public Map<Currency, TransactionResult> resetBalances(Cause cause, Set<Context>
for (Currency currency : currencyService.getCurrencies()) {
if (virtualService.accountExists(name, currency, cause)) {
if (virtualService.setBalance(name, getDefaultBalance(currency), currency, cause)) {
results.put(currency, resultAndEvent(this, getBalance(currency), currency, ResultType.SUCCESS, TransactionTypes.WITHDRAW));
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));
results.put(currency, resultAndEvent(this, getBalance(currency), currency, ResultType.FAILED, TransactionTypes.WITHDRAW, cause));
}
}
}
Expand All @@ -129,9 +129,9 @@ public Map<Currency, TransactionResult> resetBalances(Cause cause, Set<Context>
@Override
public TransactionResult resetBalance(Currency currency, Cause cause, Set<Context> contexts) {
if (virtualService.setBalance(name, getDefaultBalance(currency), currency, cause)) {
return resultAndEvent(this, BigDecimal.ZERO, currency, ResultType.SUCCESS, TransactionTypes.WITHDRAW);
return resultAndEvent(this, BigDecimal.ZERO, currency, ResultType.SUCCESS, TransactionTypes.WITHDRAW, cause);
} else {
return resultAndEvent(this, BigDecimal.ZERO, currency, ResultType.FAILED, TransactionTypes.WITHDRAW);
return resultAndEvent(this, BigDecimal.ZERO, currency, ResultType.FAILED, TransactionTypes.WITHDRAW, cause);
}
}

Expand All @@ -140,12 +140,12 @@ public TransactionResult deposit(Currency currency, BigDecimal amount, Cause cau
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);
return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_SPACE, TransactionTypes.DEPOSIT, cause);
}
if (virtualService.deposit(name, amount, currency, cause)) {
return resultAndEvent(this, amount, currency, ResultType.SUCCESS, TransactionTypes.DEPOSIT);
return resultAndEvent(this, amount, currency, ResultType.SUCCESS, TransactionTypes.DEPOSIT, cause);
} else {
return resultAndEvent(this, amount, currency, ResultType.FAILED, TransactionTypes.DEPOSIT);
return resultAndEvent(this, amount, currency, ResultType.FAILED, TransactionTypes.DEPOSIT, cause);
}
}

Expand All @@ -154,15 +154,15 @@ public TransactionResult withdraw(Currency currency, BigDecimal amount, Cause ca
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);
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);
return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_SPACE, TransactionTypes.WITHDRAW, cause);
}
if (virtualService.withdraw(name, amount, currency, cause)) {
return resultAndEvent(this, amount, currency, ResultType.SUCCESS, TransactionTypes.WITHDRAW);
return resultAndEvent(this, amount, currency, ResultType.SUCCESS, TransactionTypes.WITHDRAW, cause);
} else {
return resultAndEvent(this, amount, currency, ResultType.FAILED, TransactionTypes.WITHDRAW);
return resultAndEvent(this, amount, currency, ResultType.FAILED, TransactionTypes.WITHDRAW, cause);
}
}

Expand All @@ -171,17 +171,17 @@ public TransferResult transfer(Account to, Currency currency, BigDecimal amount,
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);
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);
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);
return resultAndEvent(this, amount, currency, ResultType.SUCCESS, to, cause);
} else {
return resultAndEvent(this, amount, currency, ResultType.FAILED, to);
return resultAndEvent(this, amount, currency, ResultType.FAILED, to, cause);
}
}

Expand All @@ -196,15 +196,15 @@ public Set<Context> getActiveContexts() {
}

private TransactionResult resultAndEvent(Account account, BigDecimal amount, Currency currency, ResultType resultType,
TransactionType transactionType) {
TransactionType transactionType, Cause cause) {
TransactionResult result = new LiteTransactionResult(account, amount, currency, resultType, transactionType);
Sponge.getEventManager().post(new LiteEconomyTransactionEvent(result));
Sponge.getEventManager().post(new LiteEconomyTransactionEvent(result, cause));
return result;
}

private TransferResult resultAndEvent(Account account, BigDecimal amount, Currency currency, ResultType resultType, Account toWho) {
private TransferResult resultAndEvent(Account account, BigDecimal amount, Currency currency, ResultType resultType, Account toWho, Cause cause) {
TransferResult result = new LiteTransferResult(account, amount, currency, resultType, toWho);
Sponge.getEventManager().post(new LiteEconomyTransactionEvent(result));
Sponge.getEventManager().post(new LiteEconomyTransactionEvent(result, cause));
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public LiteEconomyTransactionEvent(TransactionResult result) {
this.result = result;
}

public LiteEconomyTransactionEvent(TransactionResult result, Cause cause) {
this.result = result;
this.cause = cause;
}

public LiteEconomyTransactionEvent(TransactionResult result, UUID user, Cause cause) {
this.result = result;
this.cause = cause.with(NamedCause.of("economylite:player", user));
Expand Down

0 comments on commit f7e9ef1

Please sign in to comment.