From 40ec933ed81d11933726a0f5bc8238dfacf6115d Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Fri, 23 Oct 2020 03:02:33 +0200 Subject: [PATCH 01/12] Experimental design inspired by sponge --- .../org/minecraftoss/catacomb/Currency.java | 5 + .../catacomb/account/Account.java | 112 ++++++++++++++++++ .../catacomb/account/AccountContext.java | 5 + .../catacomb/transaction/Transaction.java | 21 ++++ .../transaction/TransactionCondition.java | 67 +++++++++++ .../transaction/TransactionManager.java | 15 +++ .../transaction/TransactionResult.java | 25 ++++ .../transaction/TransactionResultState.java | 9 ++ 8 files changed, 259 insertions(+) create mode 100644 catacomb-common-api/src/main/java/org/minecraftoss/catacomb/Currency.java create mode 100644 catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/Account.java create mode 100644 catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/AccountContext.java create mode 100644 catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/Transaction.java create mode 100644 catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionCondition.java create mode 100644 catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionManager.java create mode 100644 catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionResult.java create mode 100644 catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionResultState.java diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/Currency.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/Currency.java new file mode 100644 index 0000000..530ff66 --- /dev/null +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/Currency.java @@ -0,0 +1,5 @@ +package org.minecraftoss.catacomb; + +public interface Currency { + // TODO +} diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/Account.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/Account.java new file mode 100644 index 0000000..1ec19e2 --- /dev/null +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/Account.java @@ -0,0 +1,112 @@ +package org.minecraftoss.catacomb.account; + +import org.minecraftoss.catacomb.Currency; +import org.minecraftoss.catacomb.transaction.Transaction; +import org.minecraftoss.catacomb.transaction.TransactionCondition; +import org.minecraftoss.catacomb.transaction.TransactionManager; +import org.minecraftoss.catacomb.transaction.TransactionResult; + +import java.math.BigDecimal; +import java.util.Collections; +import java.util.Set; + +public interface Account { + + // TODO identification stuff + + BigDecimal getBalance(Currency currency); + + BigDecimal getInitialBalance(Currency currency); + + Set getActiveContexts(); + + TransactionResult resetBalance(Currency currency, Set contexts); + + default TransactionResult resetBalance(Currency currency) { + return resetBalance(currency, getActiveContexts()); + } + + TransactionResult transfer(Account to, Currency currency, BigDecimal amount, TransactionCondition condition, Set contexts); + + default TransactionResult transfer(Account to, Currency currency, BigDecimal amount, Set contexts) { + return transfer(to, currency, amount, TransactionCondition.always(), contexts); + } + + default TransactionResult transfer(Account to, Currency currency, BigDecimal amount, TransactionCondition condition) { + return transfer(to, currency, amount, condition, getActiveContexts()); + } + + default TransactionResult transfer(Account to, Currency currency, BigDecimal amount) { + return transfer(to, currency, amount, TransactionCondition.always()); + } + + default TransactionResult withdraw(Currency currency, BigDecimal amount, TransactionCondition condition, Set contexts) { + return transfer(infiniteAccount(), currency, amount, condition, contexts); + } + + default TransactionResult withdraw(Currency currency, BigDecimal amount, Set contexts) { + return withdraw(currency, amount, TransactionCondition.always(), contexts); + } + + default TransactionResult withdraw(Currency currency, BigDecimal amount, TransactionCondition condition) { + return withdraw(currency, amount, condition, getActiveContexts()); + } + + default TransactionResult withdraw(Currency currency, BigDecimal amount) { + return withdraw(currency, amount, TransactionCondition.always()); + } + + default TransactionResult deposit(Currency currency, BigDecimal amount, TransactionCondition condition, Set contexts) { + return infiniteAccount().transfer(this, currency, amount, condition, contexts); + } + + default TransactionResult deposit(Currency currency, BigDecimal amount, Set contexts) { + return deposit(currency, amount, TransactionCondition.always(), contexts); + } + + default TransactionResult deposit(Currency currency, BigDecimal amount, TransactionCondition condition) { + return deposit(currency, amount, condition, getActiveContexts()); + } + + default TransactionResult deposit(Currency currency, BigDecimal amount) { + return deposit(currency, amount, TransactionCondition.always()); + } + + TransactionManager getTransactionHandler(); + + static Account infiniteAccount() { + return new Account() { + @Override + public BigDecimal getBalance(Currency currency) { + return getInitialBalance(currency); + } + + @Override + public BigDecimal getInitialBalance(Currency currency) { + return BigDecimal.valueOf(Double.MAX_VALUE); + } + + @Override + public Set getActiveContexts() { + return Collections.emptySet(); + } + + @Override + public TransactionResult resetBalance(Currency currency, Set contexts) { + throw new UnsupportedOperationException("Cannot reset infinite account"); + } + + @Override + public TransactionResult transfer(Account to, Currency currency, BigDecimal amount, + TransactionCondition condition, Set contexts) { + Transaction transaction = to.getTransactionHandler().createTransaction(this, to, currency, amount, contexts); + return to.getTransactionHandler().handle(transaction, condition); + } + + @Override + public TransactionManager getTransactionHandler() { + throw new UnsupportedOperationException("No associated transaction handler for infinite account"); + } + }; + } +} diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/AccountContext.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/AccountContext.java new file mode 100644 index 0000000..0f44e8b --- /dev/null +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/AccountContext.java @@ -0,0 +1,5 @@ +package org.minecraftoss.catacomb.account; + +public interface AccountContext { + // TODO +} diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/Transaction.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/Transaction.java new file mode 100644 index 0000000..05586e5 --- /dev/null +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/Transaction.java @@ -0,0 +1,21 @@ +package org.minecraftoss.catacomb.transaction; + +import org.minecraftoss.catacomb.Currency; +import org.minecraftoss.catacomb.account.Account; +import org.minecraftoss.catacomb.account.AccountContext; + +import java.math.BigDecimal; +import java.util.Set; + +public interface Transaction { + + Account getAccountFrom(); + + Account getAccountTo(); + + Currency getCurrency(); + + BigDecimal getAmount(); + + Set getContexts(); +} diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionCondition.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionCondition.java new file mode 100644 index 0000000..3b58f16 --- /dev/null +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionCondition.java @@ -0,0 +1,67 @@ +package org.minecraftoss.catacomb.transaction; + +import java.util.function.Predicate; + +/** + * A TransactionCondition checks if a {@link Transaction} is valid or not. + * + *

As an example, if the balance of an account might not be below 0, + * a TransactionCondition can be used to check if the {@link Transaction} + * fulfills that requirement. + */ +@FunctionalInterface +public interface TransactionCondition extends Predicate { + + static TransactionCondition allOf(Iterable conditions) { + return transaction -> { + for (TransactionCondition condition : conditions) { + if (!condition.passes(transaction)) { + return false; + } + } + return true; + }; + } + + static TransactionCondition anyOf(Iterable conditions) { + return transaction -> { + for (TransactionCondition condition : conditions) { + if (condition.passes(transaction)) { + return true; + } + } + return false; + }; + } + + static TransactionCondition always() { + return transaction -> true; + } + + static TransactionCondition never() { + return transaction -> false; + } + + boolean passes(Transaction transaction); + + @Override + default boolean test(Transaction transaction) { + return passes(transaction); + } + + default TransactionCondition negate() { + return transaction -> !passes(transaction); + } + + default TransactionCondition and(TransactionCondition other) { + return transaction -> passes(transaction) && other.passes(transaction); + } + + default TransactionCondition or(TransactionCondition other) { + return transaction -> passes(transaction) || other.passes(transaction); + } + + default TransactionCondition xor(TransactionCondition other) { + return transaction -> passes(transaction) ^ other.passes(transaction); + } +} diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionManager.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionManager.java new file mode 100644 index 0000000..d0a16dc --- /dev/null +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionManager.java @@ -0,0 +1,15 @@ +package org.minecraftoss.catacomb.transaction; + +import org.minecraftoss.catacomb.Currency; +import org.minecraftoss.catacomb.account.Account; +import org.minecraftoss.catacomb.account.AccountContext; + +import java.math.BigDecimal; +import java.util.Set; + +public interface TransactionManager { + + TransactionResult handle(Transaction transaction, TransactionCondition condition); + + Transaction createTransaction(Account from, Account to, Currency currency, BigDecimal amount, Set contexts); +} diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionResult.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionResult.java new file mode 100644 index 0000000..74374d2 --- /dev/null +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionResult.java @@ -0,0 +1,25 @@ +package org.minecraftoss.catacomb.transaction; + +public class TransactionResult { + private final TransactionResultState transactionResultState; + private final Transaction transaction; + + private TransactionResult(TransactionResultState transactionResultState, Transaction transaction) { + this.transactionResultState = transactionResultState; + this.transaction = transaction; + } + + public static TransactionResult of(TransactionResultState transactionResultState, Transaction transaction) { + return new TransactionResult(transactionResultState, transaction); + } + + public TransactionResultState getTransactionResultState() { + return this.transactionResultState; + } + + public Transaction getTransaction() { + return this.transaction; + } + + // TODO track changes? +} diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionResultState.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionResultState.java new file mode 100644 index 0000000..69c4e6b --- /dev/null +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionResultState.java @@ -0,0 +1,9 @@ +package org.minecraftoss.catacomb.transaction; + +public interface TransactionResultState & TransactionResultState> { + + enum BuiltinTransactionResultState implements TransactionResultState { + ACCEPTED, + DECLINED + } +} From 40e65079970383609e3a696dcbaafaed237d4792 Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Fri, 23 Oct 2020 03:05:34 +0200 Subject: [PATCH 02/12] Don't use wildcard as generic type is allowed --- .../catacomb/transaction/TransactionResultState.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionResultState.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionResultState.java index 69c4e6b..516f469 100644 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionResultState.java +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionResultState.java @@ -1,6 +1,6 @@ package org.minecraftoss.catacomb.transaction; -public interface TransactionResultState & TransactionResultState> { +public interface TransactionResultState & TransactionResultState> { enum BuiltinTransactionResultState implements TransactionResultState { ACCEPTED, From 26ff80652788d013be07a91beee35354e9e7a01a Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Fri, 23 Oct 2020 03:17:25 +0200 Subject: [PATCH 03/12] Simplify Transaction creation --- .../catacomb/account/Account.java | 2 +- .../catacomb/transaction/Transaction.java | 5 ++ .../catacomb/transaction/TransactionImpl.java | 50 +++++++++++++++++++ .../transaction/TransactionManager.java | 9 ---- 4 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionImpl.java diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/Account.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/Account.java index 1ec19e2..bf6444b 100644 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/Account.java +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/Account.java @@ -99,7 +99,7 @@ public TransactionResult resetBalance(Currency currency, Set con @Override public TransactionResult transfer(Account to, Currency currency, BigDecimal amount, TransactionCondition condition, Set contexts) { - Transaction transaction = to.getTransactionHandler().createTransaction(this, to, currency, amount, contexts); + Transaction transaction = Transaction.of(this, to, currency, amount, contexts); return to.getTransactionHandler().handle(transaction, condition); } diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/Transaction.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/Transaction.java index 05586e5..781f5cf 100644 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/Transaction.java +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/Transaction.java @@ -5,10 +5,15 @@ import org.minecraftoss.catacomb.account.AccountContext; import java.math.BigDecimal; +import java.util.HashSet; import java.util.Set; public interface Transaction { + static Transaction of(Account accountFrom, Account accountTo, Currency currency, BigDecimal amount, Set contexts) { + return new TransactionImpl(accountFrom, accountTo, currency, amount, new HashSet<>(contexts)); + } + Account getAccountFrom(); Account getAccountTo(); diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionImpl.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionImpl.java new file mode 100644 index 0000000..c0304bf --- /dev/null +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionImpl.java @@ -0,0 +1,50 @@ +package org.minecraftoss.catacomb.transaction; + +import org.minecraftoss.catacomb.Currency; +import org.minecraftoss.catacomb.account.Account; +import org.minecraftoss.catacomb.account.AccountContext; + +import java.math.BigDecimal; +import java.util.Collections; +import java.util.Set; + +public class TransactionImpl implements Transaction { + private final Account accountFrom; + private final Account accountTo; + private final Currency currency; + private final BigDecimal amount; + private final Set contexts; + + public TransactionImpl(Account accountFrom, Account accountTo, Currency currency, BigDecimal amount, Set contexts) { + this.accountFrom = accountFrom; + this.accountTo = accountTo; + this.currency = currency; + this.amount = amount; + this.contexts = Collections.unmodifiableSet(contexts); + } + + @Override + public Account getAccountFrom() { + return this.accountFrom; + } + + @Override + public Account getAccountTo() { + return this.accountTo; + } + + @Override + public Currency getCurrency() { + return this.currency; + } + + @Override + public BigDecimal getAmount() { + return this.amount; + } + + @Override + public Set getContexts() { + return this.contexts; + } +} diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionManager.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionManager.java index d0a16dc..34c4b7e 100644 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionManager.java +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionManager.java @@ -1,15 +1,6 @@ package org.minecraftoss.catacomb.transaction; -import org.minecraftoss.catacomb.Currency; -import org.minecraftoss.catacomb.account.Account; -import org.minecraftoss.catacomb.account.AccountContext; - -import java.math.BigDecimal; -import java.util.Set; - public interface TransactionManager { TransactionResult handle(Transaction transaction, TransactionCondition condition); - - Transaction createTransaction(Account from, Account to, Currency currency, BigDecimal amount, Set contexts); } From 0d21698b8b0873a9d23546e0d904fa29724c73b5 Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Fri, 23 Oct 2020 03:30:01 +0200 Subject: [PATCH 04/12] Clean up --- .../transaction/TransactionCondition.java | 16 ++++++++-------- .../catacomb/transaction/TransactionResult.java | 8 ++++---- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionCondition.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionCondition.java index 3b58f16..94728a5 100644 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionCondition.java +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionCondition.java @@ -15,7 +15,7 @@ public interface TransactionCondition extends Predicate { static TransactionCondition allOf(Iterable conditions) { return transaction -> { for (TransactionCondition condition : conditions) { - if (!condition.passes(transaction)) { + if (!condition.isFulfilled(transaction)) { return false; } } @@ -26,7 +26,7 @@ static TransactionCondition allOf(Iterable condi static TransactionCondition anyOf(Iterable conditions) { return transaction -> { for (TransactionCondition condition : conditions) { - if (condition.passes(transaction)) { + if (condition.isFulfilled(transaction)) { return true; } } @@ -42,26 +42,26 @@ static TransactionCondition never() { return transaction -> false; } - boolean passes(Transaction transaction); + boolean isFulfilled(Transaction transaction); @Override default boolean test(Transaction transaction) { - return passes(transaction); + return isFulfilled(transaction); } default TransactionCondition negate() { - return transaction -> !passes(transaction); + return transaction -> !isFulfilled(transaction); } default TransactionCondition and(TransactionCondition other) { - return transaction -> passes(transaction) && other.passes(transaction); + return transaction -> isFulfilled(transaction) && other.isFulfilled(transaction); } default TransactionCondition or(TransactionCondition other) { - return transaction -> passes(transaction) || other.passes(transaction); + return transaction -> isFulfilled(transaction) || other.isFulfilled(transaction); } default TransactionCondition xor(TransactionCondition other) { - return transaction -> passes(transaction) ^ other.passes(transaction); + return transaction -> isFulfilled(transaction) ^ other.isFulfilled(transaction); } } diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionResult.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionResult.java index 74374d2..c673b6e 100644 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionResult.java +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionResult.java @@ -1,19 +1,19 @@ package org.minecraftoss.catacomb.transaction; public class TransactionResult { - private final TransactionResultState transactionResultState; + private final TransactionResultState transactionResultState; private final Transaction transaction; - private TransactionResult(TransactionResultState transactionResultState, Transaction transaction) { + private TransactionResult(TransactionResultState transactionResultState, Transaction transaction) { this.transactionResultState = transactionResultState; this.transaction = transaction; } - public static TransactionResult of(TransactionResultState transactionResultState, Transaction transaction) { + public static TransactionResult of(TransactionResultState transactionResultState, Transaction transaction) { return new TransactionResult(transactionResultState, transaction); } - public TransactionResultState getTransactionResultState() { + public TransactionResultState getTransactionResultState() { return this.transactionResultState; } From 5f262a46d62101b7fbf826146d2147c974d8f335 Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Fri, 23 Oct 2020 04:07:04 +0200 Subject: [PATCH 05/12] More work --- .../org/minecraftoss/catacomb/Capability.java | 33 ++++++++++ .../catacomb/CatacombService.java | 29 ++++++++ .../org/minecraftoss/catacomb/Currency.java | 5 -- .../catacomb/account/Account.java | 66 +++++++++++++++++-- .../catacomb/account/AccountManager.java | 8 +++ .../catacomb/currency/Currency.java | 5 ++ .../catacomb/currency/CurrencyManager.java | 6 ++ .../catacomb/transaction/Transaction.java | 2 +- .../catacomb/transaction/TransactionImpl.java | 2 +- 9 files changed, 143 insertions(+), 13 deletions(-) create mode 100644 catacomb-common-api/src/main/java/org/minecraftoss/catacomb/Capability.java create mode 100644 catacomb-common-api/src/main/java/org/minecraftoss/catacomb/CatacombService.java delete mode 100644 catacomb-common-api/src/main/java/org/minecraftoss/catacomb/Currency.java create mode 100644 catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/AccountManager.java create mode 100644 catacomb-common-api/src/main/java/org/minecraftoss/catacomb/currency/Currency.java create mode 100644 catacomb-common-api/src/main/java/org/minecraftoss/catacomb/currency/CurrencyManager.java diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/Capability.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/Capability.java new file mode 100644 index 0000000..62c1e2a --- /dev/null +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/Capability.java @@ -0,0 +1,33 @@ +package org.minecraftoss.catacomb; + +@SuppressWarnings("unused") +public enum Capability { + + /** + * Accounts that are provided by a third-party plugin. + * + *

If this capability isn't provided, third-party plugins + * can't use the implementation to have e.g. shared accounts. + */ + PLUGIN_ACCOUNTS, + + /** + * Different contexts for accounts. + * + *

If this capability isn't provided, third-party plugins + * can't use the implementation to have e.g. a bank account + * and a wallet for one player. Alternatively, {@link #PLUGIN_ACCOUNTS} + * could be used to achieve the same functionality in some cases. + */ + CONTEXTS, + + /** + * More than one currency. + * + *

If this capability isn't provided, third-party plugins + * can't use the implementation to have different currencies. + * Depending on the use case, {@link #PLUGIN_ACCOUNTS} could be + * used if provided. + */ + MULTIPLE_CURRENCIES +} diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/CatacombService.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/CatacombService.java new file mode 100644 index 0000000..e9f9b80 --- /dev/null +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/CatacombService.java @@ -0,0 +1,29 @@ +package org.minecraftoss.catacomb; + +import org.minecraftoss.catacomb.account.AccountManager; +import org.minecraftoss.catacomb.currency.CurrencyManager; +import org.minecraftoss.catacomb.transaction.TransactionManager; + +import java.util.Set; + +public interface CatacombService { + + default boolean queryCapabilities(Capability capability) { + return false; + } + + default boolean queryCapabilities(Set capabilities) { + for (Capability capability : capabilities) { + if (!queryCapabilities(capability)) { + return false; + } + } + return true; + } + + CurrencyManager getCurrencyManager(); + + TransactionManager getTransactionManager(); + + AccountManager getAccountManager(); +} diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/Currency.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/Currency.java deleted file mode 100644 index 530ff66..0000000 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/Currency.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.minecraftoss.catacomb; - -public interface Currency { - // TODO -} diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/Account.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/Account.java index bf6444b..7fb9428 100644 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/Account.java +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/Account.java @@ -1,9 +1,9 @@ package org.minecraftoss.catacomb.account; -import org.minecraftoss.catacomb.Currency; +import org.minecraftoss.catacomb.CatacombService; +import org.minecraftoss.catacomb.currency.Currency; import org.minecraftoss.catacomb.transaction.Transaction; import org.minecraftoss.catacomb.transaction.TransactionCondition; -import org.minecraftoss.catacomb.transaction.TransactionManager; import org.minecraftoss.catacomb.transaction.TransactionResult; import java.math.BigDecimal; @@ -26,53 +26,107 @@ default TransactionResult resetBalance(Currency currency) { return resetBalance(currency, getActiveContexts()); } + // transfer + TransactionResult transfer(Account to, Currency currency, BigDecimal amount, TransactionCondition condition, Set contexts); + default TransactionResult transfer(Account to, BigDecimal amount, TransactionCondition condition, Set contexts) { + return transfer(to, getCatacombService().getCurrencyManager().getDefaultCurrency(), amount, condition, contexts); + } + default TransactionResult transfer(Account to, Currency currency, BigDecimal amount, Set contexts) { return transfer(to, currency, amount, TransactionCondition.always(), contexts); } + default TransactionResult transfer(Account to, BigDecimal amount, Set contexts) { + return transfer(to, getCatacombService().getCurrencyManager().getDefaultCurrency(), amount, contexts); + } + default TransactionResult transfer(Account to, Currency currency, BigDecimal amount, TransactionCondition condition) { return transfer(to, currency, amount, condition, getActiveContexts()); } + default TransactionResult transfer(Account to, BigDecimal amount, TransactionCondition condition) { + return transfer(to, getCatacombService().getCurrencyManager().getDefaultCurrency(), amount, condition); + } + default TransactionResult transfer(Account to, Currency currency, BigDecimal amount) { return transfer(to, currency, amount, TransactionCondition.always()); } + default TransactionResult transfer(Account to, BigDecimal amount) { + return transfer(to, getCatacombService().getCurrencyManager().getDefaultCurrency(), amount); + } + + // withdraw + default TransactionResult withdraw(Currency currency, BigDecimal amount, TransactionCondition condition, Set contexts) { return transfer(infiniteAccount(), currency, amount, condition, contexts); } + default TransactionResult withdraw(BigDecimal amount, TransactionCondition condition, Set contexts) { + return withdraw(getCatacombService().getCurrencyManager().getDefaultCurrency(), amount, condition, contexts); + } + default TransactionResult withdraw(Currency currency, BigDecimal amount, Set contexts) { return withdraw(currency, amount, TransactionCondition.always(), contexts); } + default TransactionResult withdraw(BigDecimal amount, Set contexts) { + return withdraw(getCatacombService().getCurrencyManager().getDefaultCurrency(), amount, contexts); + } + default TransactionResult withdraw(Currency currency, BigDecimal amount, TransactionCondition condition) { return withdraw(currency, amount, condition, getActiveContexts()); } + default TransactionResult withdraw(BigDecimal amount, TransactionCondition condition) { + return withdraw(getCatacombService().getCurrencyManager().getDefaultCurrency(), amount, condition); + } + default TransactionResult withdraw(Currency currency, BigDecimal amount) { return withdraw(currency, amount, TransactionCondition.always()); } + default TransactionResult withdraw(BigDecimal amount) { + return withdraw(getCatacombService().getCurrencyManager().getDefaultCurrency(), amount); + } + + // deposit + default TransactionResult deposit(Currency currency, BigDecimal amount, TransactionCondition condition, Set contexts) { return infiniteAccount().transfer(this, currency, amount, condition, contexts); } + default TransactionResult deposit(BigDecimal amount, TransactionCondition condition, Set contexts) { + return deposit(getCatacombService().getCurrencyManager().getDefaultCurrency(), amount, condition, contexts); + } + default TransactionResult deposit(Currency currency, BigDecimal amount, Set contexts) { return deposit(currency, amount, TransactionCondition.always(), contexts); } + default TransactionResult deposit(BigDecimal amount, Set contexts) { + return deposit(getCatacombService().getCurrencyManager().getDefaultCurrency(), amount, contexts); + } + default TransactionResult deposit(Currency currency, BigDecimal amount, TransactionCondition condition) { return deposit(currency, amount, condition, getActiveContexts()); } + default TransactionResult deposit(BigDecimal amount, TransactionCondition condition) { + return deposit(getCatacombService().getCurrencyManager().getDefaultCurrency(), amount, condition); + } + default TransactionResult deposit(Currency currency, BigDecimal amount) { return deposit(currency, amount, TransactionCondition.always()); } - TransactionManager getTransactionHandler(); + default TransactionResult deposit(BigDecimal amount) { + return deposit(getCatacombService().getCurrencyManager().getDefaultCurrency(), amount); + } + + CatacombService getCatacombService(); static Account infiniteAccount() { return new Account() { @@ -100,12 +154,12 @@ public TransactionResult resetBalance(Currency currency, Set con public TransactionResult transfer(Account to, Currency currency, BigDecimal amount, TransactionCondition condition, Set contexts) { Transaction transaction = Transaction.of(this, to, currency, amount, contexts); - return to.getTransactionHandler().handle(transaction, condition); + return to.getCatacombService().getTransactionManager().handle(transaction, condition); } @Override - public TransactionManager getTransactionHandler() { - throw new UnsupportedOperationException("No associated transaction handler for infinite account"); + public CatacombService getCatacombService() { + throw new UnsupportedOperationException("No associated catacomb service for infinite account"); } }; } diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/AccountManager.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/AccountManager.java new file mode 100644 index 0000000..8767fe8 --- /dev/null +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/AccountManager.java @@ -0,0 +1,8 @@ +package org.minecraftoss.catacomb.account; + +import java.util.UUID; + +public interface AccountManager { + + Account getAccount(UUID uuid); +} diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/currency/Currency.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/currency/Currency.java new file mode 100644 index 0000000..994a497 --- /dev/null +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/currency/Currency.java @@ -0,0 +1,5 @@ +package org.minecraftoss.catacomb.currency; + +public interface Currency { + // TODO +} diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/currency/CurrencyManager.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/currency/CurrencyManager.java new file mode 100644 index 0000000..4b5cefe --- /dev/null +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/currency/CurrencyManager.java @@ -0,0 +1,6 @@ +package org.minecraftoss.catacomb.currency; + +public interface CurrencyManager { + + Currency getDefaultCurrency(); +} diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/Transaction.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/Transaction.java index 781f5cf..80924f7 100644 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/Transaction.java +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/Transaction.java @@ -1,6 +1,6 @@ package org.minecraftoss.catacomb.transaction; -import org.minecraftoss.catacomb.Currency; +import org.minecraftoss.catacomb.currency.Currency; import org.minecraftoss.catacomb.account.Account; import org.minecraftoss.catacomb.account.AccountContext; diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionImpl.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionImpl.java index c0304bf..af30972 100644 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionImpl.java +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionImpl.java @@ -1,6 +1,6 @@ package org.minecraftoss.catacomb.transaction; -import org.minecraftoss.catacomb.Currency; +import org.minecraftoss.catacomb.currency.Currency; import org.minecraftoss.catacomb.account.Account; import org.minecraftoss.catacomb.account.AccountContext; From 5492ccefa8eb6525e4e6f7c3190520af776de0da Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Sun, 25 Oct 2020 13:34:51 +0100 Subject: [PATCH 06/12] Work on contexts and bukkit stuff --- .../catacomb/bukkit/BukkitRequestContext.java | 20 +++ .../bukkit/BukkitRequestContextFactory.java | 25 +++ .../bukkit/BukkitRequestContextImpl.java | 21 +++ catacomb-bukkit/src/main/resources/plugin.yml | 2 +- .../catacomb/CatacombService.java | 7 +- .../catacomb/SimpleCatacombService.java | 40 +++++ .../catacomb/account/Account.java | 148 +++++++++--------- .../catacomb/account/AccountContext.java | 13 +- .../catacomb/account/AccountManager.java | 4 +- .../catacomb/account/InfiniteAccount.java | 56 +++++++ .../account/request/RequestContext.java | 34 ++++ .../request/RequestContextFactory.java | 8 + .../account/request/RequestContextImpl.java | 24 +++ .../catacomb/currency/CurrencyManager.java | 6 - .../catacomb/transaction/EconomyManager.java | 15 ++ .../catacomb/transaction/Transaction.java | 5 +- .../transaction/TransactionContext.java | 5 + .../catacomb/transaction/TransactionImpl.java | 9 +- .../transaction/TransactionManager.java | 6 - .../transaction/TransactionResultState.java | 9 -- .../result/BuiltinTransactionResultState.java | 6 + .../{ => result}/TransactionResult.java | 4 +- .../result/TransactionResultState.java | 5 + 23 files changed, 360 insertions(+), 112 deletions(-) create mode 100644 catacomb-bukkit-api/src/main/java/org/minecraftoss/catacomb/bukkit/BukkitRequestContext.java create mode 100644 catacomb-bukkit-api/src/main/java/org/minecraftoss/catacomb/bukkit/BukkitRequestContextFactory.java create mode 100644 catacomb-bukkit-api/src/main/java/org/minecraftoss/catacomb/bukkit/BukkitRequestContextImpl.java create mode 100644 catacomb-common-api/src/main/java/org/minecraftoss/catacomb/SimpleCatacombService.java create mode 100644 catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/InfiniteAccount.java create mode 100644 catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/request/RequestContext.java create mode 100644 catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/request/RequestContextFactory.java create mode 100644 catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/request/RequestContextImpl.java delete mode 100644 catacomb-common-api/src/main/java/org/minecraftoss/catacomb/currency/CurrencyManager.java create mode 100644 catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/EconomyManager.java create mode 100644 catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionContext.java delete mode 100644 catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionManager.java delete mode 100644 catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionResultState.java create mode 100644 catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/result/BuiltinTransactionResultState.java rename catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/{ => result}/TransactionResult.java (87%) create mode 100644 catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/result/TransactionResultState.java diff --git a/catacomb-bukkit-api/src/main/java/org/minecraftoss/catacomb/bukkit/BukkitRequestContext.java b/catacomb-bukkit-api/src/main/java/org/minecraftoss/catacomb/bukkit/BukkitRequestContext.java new file mode 100644 index 0000000..0ab9084 --- /dev/null +++ b/catacomb-bukkit-api/src/main/java/org/minecraftoss/catacomb/bukkit/BukkitRequestContext.java @@ -0,0 +1,20 @@ +package org.minecraftoss.catacomb.bukkit; + +import org.bukkit.plugin.Plugin; +import org.minecraftoss.catacomb.account.request.RequestContext; + +import java.util.UUID; + +public interface BukkitRequestContext extends RequestContext { + + static BukkitRequestContext pluginRequest(UUID accountId, Plugin requestId) { + return new BukkitRequestContextImpl(accountId, requestId); + } + + Plugin getRequestIdentifier(); + + @Override + default String getRequesterIdentifier() { + return getRequestIdentifier().getName(); + } +} diff --git a/catacomb-bukkit-api/src/main/java/org/minecraftoss/catacomb/bukkit/BukkitRequestContextFactory.java b/catacomb-bukkit-api/src/main/java/org/minecraftoss/catacomb/bukkit/BukkitRequestContextFactory.java new file mode 100644 index 0000000..9d00dab --- /dev/null +++ b/catacomb-bukkit-api/src/main/java/org/minecraftoss/catacomb/bukkit/BukkitRequestContextFactory.java @@ -0,0 +1,25 @@ +package org.minecraftoss.catacomb.bukkit; + +import org.bukkit.plugin.Plugin; +import org.minecraftoss.catacomb.account.request.RequestContext; +import org.minecraftoss.catacomb.account.request.RequestContextFactory; + +import java.util.UUID; + +public class BukkitRequestContextFactory implements RequestContextFactory { + + private final Plugin plugin; + + private BukkitRequestContextFactory(Plugin plugin) { + this.plugin = plugin; + } + + static BukkitRequestContextFactory forPlugin(Plugin plugin) { + return new BukkitRequestContextFactory(plugin); + } + + @Override + public RequestContext fromAccountIdentifier(UUID accountIdentifier) { + return BukkitRequestContext.pluginRequest(accountIdentifier, this.plugin); + } +} diff --git a/catacomb-bukkit-api/src/main/java/org/minecraftoss/catacomb/bukkit/BukkitRequestContextImpl.java b/catacomb-bukkit-api/src/main/java/org/minecraftoss/catacomb/bukkit/BukkitRequestContextImpl.java new file mode 100644 index 0000000..cdee2f6 --- /dev/null +++ b/catacomb-bukkit-api/src/main/java/org/minecraftoss/catacomb/bukkit/BukkitRequestContextImpl.java @@ -0,0 +1,21 @@ +package org.minecraftoss.catacomb.bukkit; + +import org.bukkit.plugin.Plugin; +import org.minecraftoss.catacomb.account.request.RequestContextImpl; + +import java.util.UUID; + +class BukkitRequestContextImpl extends RequestContextImpl implements BukkitRequestContext { + + private final Plugin requestIdentifier; + + BukkitRequestContextImpl(UUID accountIdentifier, Plugin requestIdentifier) { + super(accountIdentifier, requestIdentifier.getName()); + this.requestIdentifier = requestIdentifier; + } + + @Override + public Plugin getRequestIdentifier() { + return this.requestIdentifier; + } +} diff --git a/catacomb-bukkit/src/main/resources/plugin.yml b/catacomb-bukkit/src/main/resources/plugin.yml index 136f9e1..18460cb 100644 --- a/catacomb-bukkit/src/main/resources/plugin.yml +++ b/catacomb-bukkit/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ name: Catacomb version: @version@ -main: org.mincraftoss.catacomb.CatacombBukkit +main: org.minecraftoss.catacomb.CatacombBukkit api-version: 1.13 authors: [mbaxter, md678685] description: Totally not a replacement for Vault diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/CatacombService.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/CatacombService.java index e9f9b80..f0f6dee 100644 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/CatacombService.java +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/CatacombService.java @@ -1,8 +1,7 @@ package org.minecraftoss.catacomb; import org.minecraftoss.catacomb.account.AccountManager; -import org.minecraftoss.catacomb.currency.CurrencyManager; -import org.minecraftoss.catacomb.transaction.TransactionManager; +import org.minecraftoss.catacomb.transaction.EconomyManager; import java.util.Set; @@ -21,9 +20,7 @@ default boolean queryCapabilities(Set capabilities) { return true; } - CurrencyManager getCurrencyManager(); - - TransactionManager getTransactionManager(); + EconomyManager getEconomyManager(); AccountManager getAccountManager(); } diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/SimpleCatacombService.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/SimpleCatacombService.java new file mode 100644 index 0000000..f8a2de4 --- /dev/null +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/SimpleCatacombService.java @@ -0,0 +1,40 @@ +package org.minecraftoss.catacomb; + +import org.minecraftoss.catacomb.account.AccountManager; +import org.minecraftoss.catacomb.transaction.EconomyManager; + +import java.util.EnumSet; +import java.util.Set; + +public class SimpleCatacombService implements CatacombService { + private final EconomyManager economyManager; + private final AccountManager accountManager; + private final Set capabilities; + + public SimpleCatacombService(EconomyManager economyManager, + AccountManager accountManager, Set capabilities) { + this.economyManager = economyManager; + this.accountManager = accountManager; + this.capabilities = EnumSet.copyOf(capabilities); + } + + @Override + public boolean queryCapabilities(Capability capability) { + return this.capabilities.contains(capability); + } + + @Override + public boolean queryCapabilities(Set capabilities) { + return this.capabilities.containsAll(capabilities); + } + + @Override + public EconomyManager getEconomyManager() { + return this.economyManager; + } + + @Override + public AccountManager getAccountManager() { + return this.accountManager; + } +} diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/Account.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/Account.java index 7fb9428..f57fbc4 100644 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/Account.java +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/Account.java @@ -3,164 +3,166 @@ import org.minecraftoss.catacomb.CatacombService; import org.minecraftoss.catacomb.currency.Currency; import org.minecraftoss.catacomb.transaction.Transaction; +import org.minecraftoss.catacomb.transaction.TransactionContext; import org.minecraftoss.catacomb.transaction.TransactionCondition; -import org.minecraftoss.catacomb.transaction.TransactionResult; +import org.minecraftoss.catacomb.transaction.result.TransactionResult; import java.math.BigDecimal; -import java.util.Collections; import java.util.Set; +import java.util.UUID; public interface Account { - // TODO identification stuff + UUID getIdentifier(); - BigDecimal getBalance(Currency currency); + default BigDecimal getBalance(Currency currency, Set contexts) { + return getCatacombService().getEconomyManager().getBalance(getIdentifier(), currency, contexts); + } + + default BigDecimal getBalance(Currency currency) { + return getBalance(currency, getAccountContext().getDefaultContexts()); + } + + default BigDecimal getBalance() { + return getBalance(getAccountContext().getDefaultCurrency()); + } BigDecimal getInitialBalance(Currency currency); - Set getActiveContexts(); + default BigDecimal getInitialBalance() { + return getInitialBalance(getAccountContext().getDefaultCurrency()); + } - TransactionResult resetBalance(Currency currency, Set contexts); + // account reset + + TransactionResult resetBalance(Currency currency, Set contexts); default TransactionResult resetBalance(Currency currency) { - return resetBalance(currency, getActiveContexts()); + return resetBalance(currency, getAccountContext().getDefaultContexts()); + } + + default TransactionResult resetBalance() { + return resetBalance(getAccountContext().getDefaultCurrency()); } // transfer - TransactionResult transfer(Account to, Currency currency, BigDecimal amount, TransactionCondition condition, Set contexts); + default TransactionResult transfer(Account to, Currency currency, BigDecimal amount, TransactionCondition condition, Set contexts) { + Transaction transaction = Transaction.of(this, to, currency, amount, contexts); + TransactionCondition andDefault = condition.and(getAccountContext().getDefaultTransactionCondition()); + return getCatacombService().getEconomyManager().handle(transaction, andDefault); + } - default TransactionResult transfer(Account to, BigDecimal amount, TransactionCondition condition, Set contexts) { - return transfer(to, getCatacombService().getCurrencyManager().getDefaultCurrency(), amount, condition, contexts); + default TransactionResult transfer(Account to, BigDecimal amount, TransactionCondition condition, Set contexts) { + return transfer(to, getAccountContext().getDefaultCurrency(), amount, condition, contexts); } - default TransactionResult transfer(Account to, Currency currency, BigDecimal amount, Set contexts) { - return transfer(to, currency, amount, TransactionCondition.always(), contexts); + default TransactionResult transfer(Account to, Currency currency, BigDecimal amount, Set contexts) { + return transfer(to, currency, amount, getAccountContext().getDefaultTransactionCondition(), contexts); } - default TransactionResult transfer(Account to, BigDecimal amount, Set contexts) { - return transfer(to, getCatacombService().getCurrencyManager().getDefaultCurrency(), amount, contexts); + default TransactionResult transfer(Account to, BigDecimal amount, Set contexts) { + return transfer(to, getAccountContext().getDefaultCurrency(), amount, contexts); } default TransactionResult transfer(Account to, Currency currency, BigDecimal amount, TransactionCondition condition) { - return transfer(to, currency, amount, condition, getActiveContexts()); + return transfer(to, currency, amount, condition, getAccountContext().getDefaultContexts()); } default TransactionResult transfer(Account to, BigDecimal amount, TransactionCondition condition) { - return transfer(to, getCatacombService().getCurrencyManager().getDefaultCurrency(), amount, condition); + return transfer(to, getAccountContext().getDefaultCurrency(), amount, condition); } default TransactionResult transfer(Account to, Currency currency, BigDecimal amount) { - return transfer(to, currency, amount, TransactionCondition.always()); + return transfer(to, currency, amount, getAccountContext().getDefaultTransactionCondition()); } default TransactionResult transfer(Account to, BigDecimal amount) { - return transfer(to, getCatacombService().getCurrencyManager().getDefaultCurrency(), amount); + return transfer(to, getAccountContext().getDefaultCurrency(), amount); } // withdraw - default TransactionResult withdraw(Currency currency, BigDecimal amount, TransactionCondition condition, Set contexts) { + default TransactionResult withdraw(Currency currency, BigDecimal amount, TransactionCondition condition, Set contexts) { return transfer(infiniteAccount(), currency, amount, condition, contexts); } - default TransactionResult withdraw(BigDecimal amount, TransactionCondition condition, Set contexts) { - return withdraw(getCatacombService().getCurrencyManager().getDefaultCurrency(), amount, condition, contexts); + default TransactionResult withdraw(BigDecimal amount, TransactionCondition condition, Set contexts) { + return withdraw(getAccountContext().getDefaultCurrency(), amount, condition, contexts); } - default TransactionResult withdraw(Currency currency, BigDecimal amount, Set contexts) { - return withdraw(currency, amount, TransactionCondition.always(), contexts); + default TransactionResult withdraw(Currency currency, BigDecimal amount, Set contexts) { + return withdraw(currency, amount, getAccountContext().getDefaultTransactionCondition(), contexts); } - default TransactionResult withdraw(BigDecimal amount, Set contexts) { - return withdraw(getCatacombService().getCurrencyManager().getDefaultCurrency(), amount, contexts); + default TransactionResult withdraw(BigDecimal amount, Set contexts) { + return withdraw(getAccountContext().getDefaultCurrency(), amount, contexts); } default TransactionResult withdraw(Currency currency, BigDecimal amount, TransactionCondition condition) { - return withdraw(currency, amount, condition, getActiveContexts()); + return withdraw(currency, amount, condition, getAccountContext().getDefaultContexts()); } default TransactionResult withdraw(BigDecimal amount, TransactionCondition condition) { - return withdraw(getCatacombService().getCurrencyManager().getDefaultCurrency(), amount, condition); + return withdraw(getAccountContext().getDefaultCurrency(), amount, condition); } default TransactionResult withdraw(Currency currency, BigDecimal amount) { - return withdraw(currency, amount, TransactionCondition.always()); + return withdraw(currency, amount, getAccountContext().getDefaultTransactionCondition()); } default TransactionResult withdraw(BigDecimal amount) { - return withdraw(getCatacombService().getCurrencyManager().getDefaultCurrency(), amount); + return withdraw(getAccountContext().getDefaultCurrency(), amount); } // deposit - default TransactionResult deposit(Currency currency, BigDecimal amount, TransactionCondition condition, Set contexts) { + default TransactionResult deposit(Currency currency, BigDecimal amount, TransactionCondition condition, Set contexts) { return infiniteAccount().transfer(this, currency, amount, condition, contexts); } - default TransactionResult deposit(BigDecimal amount, TransactionCondition condition, Set contexts) { - return deposit(getCatacombService().getCurrencyManager().getDefaultCurrency(), amount, condition, contexts); + default TransactionResult deposit(BigDecimal amount, TransactionCondition condition, Set contexts) { + return deposit(getAccountContext().getDefaultCurrency(), amount, condition, contexts); } - default TransactionResult deposit(Currency currency, BigDecimal amount, Set contexts) { - return deposit(currency, amount, TransactionCondition.always(), contexts); + default TransactionResult deposit(Currency currency, BigDecimal amount, Set contexts) { + return deposit(currency, amount, getAccountContext().getDefaultTransactionCondition(), contexts); } - default TransactionResult deposit(BigDecimal amount, Set contexts) { - return deposit(getCatacombService().getCurrencyManager().getDefaultCurrency(), amount, contexts); + default TransactionResult deposit(BigDecimal amount, Set contexts) { + return deposit(getAccountContext().getDefaultCurrency(), amount, contexts); } default TransactionResult deposit(Currency currency, BigDecimal amount, TransactionCondition condition) { - return deposit(currency, amount, condition, getActiveContexts()); + return deposit(currency, amount, condition, getAccountContext().getDefaultContexts()); } default TransactionResult deposit(BigDecimal amount, TransactionCondition condition) { - return deposit(getCatacombService().getCurrencyManager().getDefaultCurrency(), amount, condition); + return deposit(getAccountContext().getDefaultCurrency(), amount, condition); } default TransactionResult deposit(Currency currency, BigDecimal amount) { - return deposit(currency, amount, TransactionCondition.always()); + return deposit(currency, amount, getAccountContext().getDefaultTransactionCondition()); } default TransactionResult deposit(BigDecimal amount) { - return deposit(getCatacombService().getCurrencyManager().getDefaultCurrency(), amount); + return deposit(getAccountContext().getDefaultCurrency(), amount); } CatacombService getCatacombService(); + /** + * The context of this account. While an account is uniquely identified + * by {@link #getIdentifier()}, this allows to provide different context + * to different users (e.g. third-party plugins). + * + *

Implementations of Catacomb might allow settings so different plugins + * use different {@link Currency}, different default {@link TransactionCondition}. + * @return the context of this account. + */ + AccountContext getAccountContext(); + static Account infiniteAccount() { - return new Account() { - @Override - public BigDecimal getBalance(Currency currency) { - return getInitialBalance(currency); - } - - @Override - public BigDecimal getInitialBalance(Currency currency) { - return BigDecimal.valueOf(Double.MAX_VALUE); - } - - @Override - public Set getActiveContexts() { - return Collections.emptySet(); - } - - @Override - public TransactionResult resetBalance(Currency currency, Set contexts) { - throw new UnsupportedOperationException("Cannot reset infinite account"); - } - - @Override - public TransactionResult transfer(Account to, Currency currency, BigDecimal amount, - TransactionCondition condition, Set contexts) { - Transaction transaction = Transaction.of(this, to, currency, amount, contexts); - return to.getCatacombService().getTransactionManager().handle(transaction, condition); - } - - @Override - public CatacombService getCatacombService() { - throw new UnsupportedOperationException("No associated catacomb service for infinite account"); - } - }; + return InfiniteAccount.INSTANCE; } } diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/AccountContext.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/AccountContext.java index 0f44e8b..0391bd1 100644 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/AccountContext.java +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/AccountContext.java @@ -1,5 +1,16 @@ package org.minecraftoss.catacomb.account; +import org.minecraftoss.catacomb.currency.Currency; +import org.minecraftoss.catacomb.transaction.TransactionCondition; +import org.minecraftoss.catacomb.transaction.TransactionContext; + +import java.util.Set; + public interface AccountContext { - // TODO + + TransactionCondition getDefaultTransactionCondition(); + + Currency getDefaultCurrency(); + + Set getDefaultContexts(); } diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/AccountManager.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/AccountManager.java index 8767fe8..1842671 100644 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/AccountManager.java +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/AccountManager.java @@ -1,8 +1,8 @@ package org.minecraftoss.catacomb.account; -import java.util.UUID; +import org.minecraftoss.catacomb.account.request.RequestContext; public interface AccountManager { - Account getAccount(UUID uuid); + Account getAccount(RequestContext context); } diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/InfiniteAccount.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/InfiniteAccount.java new file mode 100644 index 0000000..3dbdea2 --- /dev/null +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/InfiniteAccount.java @@ -0,0 +1,56 @@ +package org.minecraftoss.catacomb.account; + +import org.minecraftoss.catacomb.CatacombService; +import org.minecraftoss.catacomb.currency.Currency; +import org.minecraftoss.catacomb.transaction.Transaction; +import org.minecraftoss.catacomb.transaction.TransactionCondition; +import org.minecraftoss.catacomb.transaction.TransactionContext; +import org.minecraftoss.catacomb.transaction.result.TransactionResult; + +import java.math.BigDecimal; +import java.util.Set; +import java.util.UUID; + +enum InfiniteAccount implements Account { + INSTANCE; + + // TODO + private final UUID id = UUID.nameUUIDFromBytes(new byte[0]); + + @Override + public UUID getIdentifier() { + return this.id; + } + + @Override + public BigDecimal getBalance(Currency currency) { + return getInitialBalance(currency); + } + + @Override + public BigDecimal getInitialBalance(Currency currency) { + return BigDecimal.valueOf(Double.MAX_VALUE); + } + + @Override + public TransactionResult resetBalance(Currency currency, Set contexts) { + throw new UnsupportedOperationException("Cannot reset infinite account"); + } + + @Override + public TransactionResult transfer(Account to, Currency currency, BigDecimal amount, + TransactionCondition condition, Set contexts) { + Transaction transaction = Transaction.of(this, to, currency, amount, contexts); + return to.getCatacombService().getEconomyManager().handle(transaction, condition); + } + + @Override + public CatacombService getCatacombService() { + throw new UnsupportedOperationException("No associated catacomb service for infinite account"); + } + + @Override + public AccountContext getAccountContext() { + throw new UnsupportedOperationException("No associated account context"); + } +} diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/request/RequestContext.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/request/RequestContext.java new file mode 100644 index 0000000..bcfbdfa --- /dev/null +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/request/RequestContext.java @@ -0,0 +1,34 @@ +package org.minecraftoss.catacomb.account.request; + +import org.minecraftoss.catacomb.account.Account; +import org.minecraftoss.catacomb.account.AccountContext; +import org.minecraftoss.catacomb.account.AccountManager; + +import java.util.UUID; + +/** + * The context of an {@link Account} request. + * + *

When requesting an account, a {@link UUID} and a {@link RequestContext} + * is required. Depending on the context, the {@link AccountContext} associated with + * the returned account might vary. + * + * @see AccountManager#getAccount(RequestContext) + * @see Account#getAccountContext() + */ +public interface RequestContext { + + static RequestContext rawRequest(UUID accountId, String requestId) { + return new RequestContextImpl(accountId, requestId); + } + + /** + * The identifier used to identify the request. For example, this + * could be a plugin's name. + * + * @return the identifier of this context. + */ + String getRequesterIdentifier(); // TODO maybe use something like a "hierarchical key" (similar to permissions) or namespaced + + UUID getAccountIdentifier(); +} diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/request/RequestContextFactory.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/request/RequestContextFactory.java new file mode 100644 index 0000000..e53a193 --- /dev/null +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/request/RequestContextFactory.java @@ -0,0 +1,8 @@ +package org.minecraftoss.catacomb.account.request; + +import java.util.UUID; + +public interface RequestContextFactory { + + RequestContext fromAccountIdentifier(UUID accountIdentifier); +} diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/request/RequestContextImpl.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/request/RequestContextImpl.java new file mode 100644 index 0000000..344eaef --- /dev/null +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/request/RequestContextImpl.java @@ -0,0 +1,24 @@ +package org.minecraftoss.catacomb.account.request; + +import java.util.UUID; + +public class RequestContextImpl implements RequestContext { + + private final UUID accountIdentifier; + private final String requestIdentifier; + + protected RequestContextImpl(UUID accountIdentifier, String requestIdentifier) { + this.accountIdentifier = accountIdentifier; + this.requestIdentifier = requestIdentifier; + } + + @Override + public UUID getAccountIdentifier() { + return this.accountIdentifier; + } + + @Override + public String getRequesterIdentifier() { + return this.requestIdentifier; + } +} diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/currency/CurrencyManager.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/currency/CurrencyManager.java deleted file mode 100644 index 4b5cefe..0000000 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/currency/CurrencyManager.java +++ /dev/null @@ -1,6 +0,0 @@ -package org.minecraftoss.catacomb.currency; - -public interface CurrencyManager { - - Currency getDefaultCurrency(); -} diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/EconomyManager.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/EconomyManager.java new file mode 100644 index 0000000..5ecd864 --- /dev/null +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/EconomyManager.java @@ -0,0 +1,15 @@ +package org.minecraftoss.catacomb.transaction; + +import org.minecraftoss.catacomb.currency.Currency; +import org.minecraftoss.catacomb.transaction.result.TransactionResult; + +import java.math.BigDecimal; +import java.util.Set; +import java.util.UUID; + +public interface EconomyManager { + + TransactionResult handle(Transaction transaction, TransactionCondition condition); + + BigDecimal getBalance(UUID identifier, Currency currency, Set contexts); +} diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/Transaction.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/Transaction.java index 80924f7..5f86964 100644 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/Transaction.java +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/Transaction.java @@ -2,7 +2,6 @@ import org.minecraftoss.catacomb.currency.Currency; import org.minecraftoss.catacomb.account.Account; -import org.minecraftoss.catacomb.account.AccountContext; import java.math.BigDecimal; import java.util.HashSet; @@ -10,7 +9,7 @@ public interface Transaction { - static Transaction of(Account accountFrom, Account accountTo, Currency currency, BigDecimal amount, Set contexts) { + static Transaction of(Account accountFrom, Account accountTo, Currency currency, BigDecimal amount, Set contexts) { return new TransactionImpl(accountFrom, accountTo, currency, amount, new HashSet<>(contexts)); } @@ -22,5 +21,5 @@ static Transaction of(Account accountFrom, Account accountTo, Currency currency, BigDecimal getAmount(); - Set getContexts(); + Set getContexts(); } diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionContext.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionContext.java new file mode 100644 index 0000000..942a0af --- /dev/null +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionContext.java @@ -0,0 +1,5 @@ +package org.minecraftoss.catacomb.transaction; + +public interface TransactionContext { + // TODO +} diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionImpl.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionImpl.java index af30972..aa85036 100644 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionImpl.java +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionImpl.java @@ -2,20 +2,19 @@ import org.minecraftoss.catacomb.currency.Currency; import org.minecraftoss.catacomb.account.Account; -import org.minecraftoss.catacomb.account.AccountContext; import java.math.BigDecimal; import java.util.Collections; import java.util.Set; -public class TransactionImpl implements Transaction { +class TransactionImpl implements Transaction { private final Account accountFrom; private final Account accountTo; private final Currency currency; private final BigDecimal amount; - private final Set contexts; + private final Set contexts; - public TransactionImpl(Account accountFrom, Account accountTo, Currency currency, BigDecimal amount, Set contexts) { + TransactionImpl(Account accountFrom, Account accountTo, Currency currency, BigDecimal amount, Set contexts) { this.accountFrom = accountFrom; this.accountTo = accountTo; this.currency = currency; @@ -44,7 +43,7 @@ public BigDecimal getAmount() { } @Override - public Set getContexts() { + public Set getContexts() { return this.contexts; } } diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionManager.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionManager.java deleted file mode 100644 index 34c4b7e..0000000 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionManager.java +++ /dev/null @@ -1,6 +0,0 @@ -package org.minecraftoss.catacomb.transaction; - -public interface TransactionManager { - - TransactionResult handle(Transaction transaction, TransactionCondition condition); -} diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionResultState.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionResultState.java deleted file mode 100644 index 516f469..0000000 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionResultState.java +++ /dev/null @@ -1,9 +0,0 @@ -package org.minecraftoss.catacomb.transaction; - -public interface TransactionResultState & TransactionResultState> { - - enum BuiltinTransactionResultState implements TransactionResultState { - ACCEPTED, - DECLINED - } -} diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/result/BuiltinTransactionResultState.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/result/BuiltinTransactionResultState.java new file mode 100644 index 0000000..4cc61c5 --- /dev/null +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/result/BuiltinTransactionResultState.java @@ -0,0 +1,6 @@ +package org.minecraftoss.catacomb.transaction.result; + +public enum BuiltinTransactionResultState implements TransactionResultState { + ACCEPTED, + DECLINED +} diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionResult.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/result/TransactionResult.java similarity index 87% rename from catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionResult.java rename to catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/result/TransactionResult.java index c673b6e..f02356a 100644 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionResult.java +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/result/TransactionResult.java @@ -1,4 +1,6 @@ -package org.minecraftoss.catacomb.transaction; +package org.minecraftoss.catacomb.transaction.result; + +import org.minecraftoss.catacomb.transaction.Transaction; public class TransactionResult { private final TransactionResultState transactionResultState; diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/result/TransactionResultState.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/result/TransactionResultState.java new file mode 100644 index 0000000..3c2d8fa --- /dev/null +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/result/TransactionResultState.java @@ -0,0 +1,5 @@ +package org.minecraftoss.catacomb.transaction.result; + +public interface TransactionResultState & TransactionResultState> { + +} From 8aea8884cfa79dc274a6013986c0f9422949f6c8 Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Sun, 25 Oct 2020 14:04:33 +0100 Subject: [PATCH 07/12] Sponge boilerplate --- .../catacomb/sponge/SpongeRequestContext.java | 20 +++++++++++++++ .../sponge/SpongeRequestContextFactory.java | 20 +++++++++++++++ .../sponge/SpongeRequestContextImpl.java | 25 +++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 catacomb-sponge-api/src/main/java/org/minecraftoss/catacomb/sponge/SpongeRequestContext.java create mode 100644 catacomb-sponge-api/src/main/java/org/minecraftoss/catacomb/sponge/SpongeRequestContextFactory.java create mode 100644 catacomb-sponge-api/src/main/java/org/minecraftoss/catacomb/sponge/SpongeRequestContextImpl.java diff --git a/catacomb-sponge-api/src/main/java/org/minecraftoss/catacomb/sponge/SpongeRequestContext.java b/catacomb-sponge-api/src/main/java/org/minecraftoss/catacomb/sponge/SpongeRequestContext.java new file mode 100644 index 0000000..09a9441 --- /dev/null +++ b/catacomb-sponge-api/src/main/java/org/minecraftoss/catacomb/sponge/SpongeRequestContext.java @@ -0,0 +1,20 @@ +package org.minecraftoss.catacomb.sponge; + +import org.minecraftoss.catacomb.account.request.RequestContext; +import org.spongepowered.api.plugin.PluginContainer; + +import java.util.UUID; + +public interface SpongeRequestContext extends RequestContext { + + static SpongeRequestContext pluginRequest(UUID accountId, PluginContainer plugin) { + return new SpongeRequestContextImpl(accountId, plugin); + } + + PluginContainer getPlugin(); + + @Override + default String getRequesterIdentifier() { + return getPlugin().getId(); + } +} diff --git a/catacomb-sponge-api/src/main/java/org/minecraftoss/catacomb/sponge/SpongeRequestContextFactory.java b/catacomb-sponge-api/src/main/java/org/minecraftoss/catacomb/sponge/SpongeRequestContextFactory.java new file mode 100644 index 0000000..9246b14 --- /dev/null +++ b/catacomb-sponge-api/src/main/java/org/minecraftoss/catacomb/sponge/SpongeRequestContextFactory.java @@ -0,0 +1,20 @@ +package org.minecraftoss.catacomb.sponge; + +import org.minecraftoss.catacomb.account.request.RequestContext; +import org.minecraftoss.catacomb.account.request.RequestContextFactory; +import org.spongepowered.api.plugin.PluginContainer; + +import java.util.UUID; + +public class SpongeRequestContextFactory implements RequestContextFactory { + private final PluginContainer plugin; + + public SpongeRequestContextFactory(PluginContainer plugin) { + this.plugin = plugin; + } + + @Override + public RequestContext fromAccountIdentifier(UUID accountIdentifier) { + return SpongeRequestContext.pluginRequest(accountIdentifier, plugin); + } +} diff --git a/catacomb-sponge-api/src/main/java/org/minecraftoss/catacomb/sponge/SpongeRequestContextImpl.java b/catacomb-sponge-api/src/main/java/org/minecraftoss/catacomb/sponge/SpongeRequestContextImpl.java new file mode 100644 index 0000000..7b3bf51 --- /dev/null +++ b/catacomb-sponge-api/src/main/java/org/minecraftoss/catacomb/sponge/SpongeRequestContextImpl.java @@ -0,0 +1,25 @@ +package org.minecraftoss.catacomb.sponge; + +import org.spongepowered.api.plugin.PluginContainer; + +import java.util.UUID; + +class SpongeRequestContextImpl implements SpongeRequestContext { + private final UUID accountIdentifier; + private final PluginContainer plugin; + + SpongeRequestContextImpl(UUID accountIdentifier, PluginContainer plugin) { + this.accountIdentifier = accountIdentifier; + this.plugin = plugin; + } + + @Override + public PluginContainer getPlugin() { + return this.plugin; + } + + @Override + public UUID getAccountIdentifier() { + return this.accountIdentifier; + } +} From 28d0a09e84e5dc8fe3da7eacc107fd7b47632cca Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Sun, 25 Oct 2020 14:18:41 +0100 Subject: [PATCH 08/12] Make infinite account part of AccountContext --- .../java/org/minecraftoss/catacomb/account/Account.java | 8 ++------ .../org/minecraftoss/catacomb/account/AccountContext.java | 8 ++++++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/Account.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/Account.java index f57fbc4..a66afda 100644 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/Account.java +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/Account.java @@ -84,7 +84,7 @@ default TransactionResult transfer(Account to, BigDecimal amount) { // withdraw default TransactionResult withdraw(Currency currency, BigDecimal amount, TransactionCondition condition, Set contexts) { - return transfer(infiniteAccount(), currency, amount, condition, contexts); + return transfer(getAccountContext().getInfiniteAccount(), currency, amount, condition, contexts); } default TransactionResult withdraw(BigDecimal amount, TransactionCondition condition, Set contexts) { @@ -118,7 +118,7 @@ default TransactionResult withdraw(BigDecimal amount) { // deposit default TransactionResult deposit(Currency currency, BigDecimal amount, TransactionCondition condition, Set contexts) { - return infiniteAccount().transfer(this, currency, amount, condition, contexts); + return getAccountContext().getInfiniteAccount().transfer(this, currency, amount, condition, contexts); } default TransactionResult deposit(BigDecimal amount, TransactionCondition condition, Set contexts) { @@ -161,8 +161,4 @@ default TransactionResult deposit(BigDecimal amount) { * @return the context of this account. */ AccountContext getAccountContext(); - - static Account infiniteAccount() { - return InfiniteAccount.INSTANCE; - } } diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/AccountContext.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/AccountContext.java index 0391bd1..da91495 100644 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/AccountContext.java +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/AccountContext.java @@ -13,4 +13,12 @@ public interface AccountContext { Currency getDefaultCurrency(); Set getDefaultContexts(); + + // TODO better name? + /** + * The account that is used for withdraw/deposit transactions as target/source. + * + * @return an infinite account. + */ + Account getInfiniteAccount(); } From e8ea5551bc85ba538e8b568d83fea11a320382dc Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Sun, 25 Oct 2020 15:33:55 +0100 Subject: [PATCH 09/12] Basic TransactionContext implementation --- .../catacomb/CatacombService.java | 1 - .../{transaction => }/EconomyManager.java | 5 ++++- .../catacomb/SimpleCatacombService.java | 1 - .../transaction/TransactionContext.java | 9 +++++++- .../transaction/TransactionContextImpl.java | 21 +++++++++++++++++++ 5 files changed, 33 insertions(+), 4 deletions(-) rename catacomb-common-api/src/main/java/org/minecraftoss/catacomb/{transaction => }/EconomyManager.java (65%) create mode 100644 catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionContextImpl.java diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/CatacombService.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/CatacombService.java index f0f6dee..997a758 100644 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/CatacombService.java +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/CatacombService.java @@ -1,7 +1,6 @@ package org.minecraftoss.catacomb; import org.minecraftoss.catacomb.account.AccountManager; -import org.minecraftoss.catacomb.transaction.EconomyManager; import java.util.Set; diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/EconomyManager.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/EconomyManager.java similarity index 65% rename from catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/EconomyManager.java rename to catacomb-common-api/src/main/java/org/minecraftoss/catacomb/EconomyManager.java index 5ecd864..0292073 100644 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/EconomyManager.java +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/EconomyManager.java @@ -1,6 +1,9 @@ -package org.minecraftoss.catacomb.transaction; +package org.minecraftoss.catacomb; import org.minecraftoss.catacomb.currency.Currency; +import org.minecraftoss.catacomb.transaction.Transaction; +import org.minecraftoss.catacomb.transaction.TransactionCondition; +import org.minecraftoss.catacomb.transaction.TransactionContext; import org.minecraftoss.catacomb.transaction.result.TransactionResult; import java.math.BigDecimal; diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/SimpleCatacombService.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/SimpleCatacombService.java index f8a2de4..879229d 100644 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/SimpleCatacombService.java +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/SimpleCatacombService.java @@ -1,7 +1,6 @@ package org.minecraftoss.catacomb; import org.minecraftoss.catacomb.account.AccountManager; -import org.minecraftoss.catacomb.transaction.EconomyManager; import java.util.EnumSet; import java.util.Set; diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionContext.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionContext.java index 942a0af..5998460 100644 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionContext.java +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionContext.java @@ -1,5 +1,12 @@ package org.minecraftoss.catacomb.transaction; public interface TransactionContext { - // TODO + + static TransactionContext of(String key, String value) { + return new TransactionContextImpl(key, value); + } + + String getKey(); + + String getValue(); } diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionContextImpl.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionContextImpl.java new file mode 100644 index 0000000..c42942c --- /dev/null +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionContextImpl.java @@ -0,0 +1,21 @@ +package org.minecraftoss.catacomb.transaction; + +class TransactionContextImpl implements TransactionContext { + private final String key; + private final String value; + + TransactionContextImpl(String key, String value) { + this.key = key; + this.value = value; + } + + @Override + public String getKey() { + return this.key; + } + + @Override + public String getValue() { + return this.value; + } +} From ccbe28d1b7ecfd68e8c84aa481cff5836bcc8cac Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Mon, 26 Oct 2020 15:03:26 +0100 Subject: [PATCH 10/12] Add equals/hashCode to relevant classes --- .../bukkit/BukkitRequestContextImpl.java | 18 +++++++++++++ .../catacomb/account/InfiniteAccount.java | 2 +- .../account/request/RequestContextImpl.java | 18 +++++++++++++ .../transaction/TransactionContextImpl.java | 18 +++++++++++++ .../catacomb/transaction/TransactionImpl.java | 24 +++++++++++++++++ .../transaction/result/TransactionResult.java | 19 +++++++++++++ .../sponge/SpongeRequestContextImpl.java | 27 ++++++++++++++----- 7 files changed, 118 insertions(+), 8 deletions(-) diff --git a/catacomb-bukkit-api/src/main/java/org/minecraftoss/catacomb/bukkit/BukkitRequestContextImpl.java b/catacomb-bukkit-api/src/main/java/org/minecraftoss/catacomb/bukkit/BukkitRequestContextImpl.java index cdee2f6..c84714a 100644 --- a/catacomb-bukkit-api/src/main/java/org/minecraftoss/catacomb/bukkit/BukkitRequestContextImpl.java +++ b/catacomb-bukkit-api/src/main/java/org/minecraftoss/catacomb/bukkit/BukkitRequestContextImpl.java @@ -18,4 +18,22 @@ class BukkitRequestContextImpl extends RequestContextImpl implements BukkitReque public Plugin getRequestIdentifier() { return this.requestIdentifier; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof BukkitRequestContextImpl)) return false; + if (!super.equals(o)) return false; + + BukkitRequestContextImpl that = (BukkitRequestContextImpl) o; + + return requestIdentifier.equals(that.requestIdentifier); + } + + @Override + public int hashCode() { + int result = super.hashCode(); + result = 31 * result + requestIdentifier.hashCode(); + return result; + } } diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/InfiniteAccount.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/InfiniteAccount.java index 3dbdea2..8b17f65 100644 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/InfiniteAccount.java +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/InfiniteAccount.java @@ -11,7 +11,7 @@ import java.util.Set; import java.util.UUID; -enum InfiniteAccount implements Account { +public enum InfiniteAccount implements Account { INSTANCE; // TODO diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/request/RequestContextImpl.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/request/RequestContextImpl.java index 344eaef..3155193 100644 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/request/RequestContextImpl.java +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/account/request/RequestContextImpl.java @@ -21,4 +21,22 @@ public UUID getAccountIdentifier() { public String getRequesterIdentifier() { return this.requestIdentifier; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof RequestContextImpl)) return false; + + RequestContextImpl that = (RequestContextImpl) o; + + if (!accountIdentifier.equals(that.accountIdentifier)) return false; + return requestIdentifier.equals(that.requestIdentifier); + } + + @Override + public int hashCode() { + int result = accountIdentifier.hashCode(); + result = 31 * result + requestIdentifier.hashCode(); + return result; + } } diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionContextImpl.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionContextImpl.java index c42942c..64cdb1d 100644 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionContextImpl.java +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionContextImpl.java @@ -18,4 +18,22 @@ public String getKey() { public String getValue() { return this.value; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof TransactionContextImpl)) return false; + + TransactionContextImpl that = (TransactionContextImpl) o; + + if (!key.equals(that.key)) return false; + return value.equals(that.value); + } + + @Override + public int hashCode() { + int result = key.hashCode(); + result = 31 * result + value.hashCode(); + return result; + } } diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionImpl.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionImpl.java index aa85036..5fbd502 100644 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionImpl.java +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/TransactionImpl.java @@ -46,4 +46,28 @@ public BigDecimal getAmount() { public Set getContexts() { return this.contexts; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof TransactionImpl)) return false; + + TransactionImpl that = (TransactionImpl) o; + + if (!accountFrom.equals(that.accountFrom)) return false; + if (!accountTo.equals(that.accountTo)) return false; + if (!currency.equals(that.currency)) return false; + if (!amount.equals(that.amount)) return false; + return contexts.equals(that.contexts); + } + + @Override + public int hashCode() { + int result = accountFrom.hashCode(); + result = 31 * result + accountTo.hashCode(); + result = 31 * result + currency.hashCode(); + result = 31 * result + amount.hashCode(); + result = 31 * result + contexts.hashCode(); + return result; + } } diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/result/TransactionResult.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/result/TransactionResult.java index f02356a..26a20b0 100644 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/result/TransactionResult.java +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/result/TransactionResult.java @@ -24,4 +24,23 @@ public Transaction getTransaction() { } // TODO track changes? + + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof TransactionResult)) return false; + + TransactionResult that = (TransactionResult) o; + + if (!transactionResultState.equals(that.transactionResultState)) return false; + return transaction.equals(that.transaction); + } + + @Override + public int hashCode() { + int result = transactionResultState.hashCode(); + result = 31 * result + transaction.hashCode(); + return result; + } } diff --git a/catacomb-sponge-api/src/main/java/org/minecraftoss/catacomb/sponge/SpongeRequestContextImpl.java b/catacomb-sponge-api/src/main/java/org/minecraftoss/catacomb/sponge/SpongeRequestContextImpl.java index 7b3bf51..915399c 100644 --- a/catacomb-sponge-api/src/main/java/org/minecraftoss/catacomb/sponge/SpongeRequestContextImpl.java +++ b/catacomb-sponge-api/src/main/java/org/minecraftoss/catacomb/sponge/SpongeRequestContextImpl.java @@ -1,16 +1,16 @@ package org.minecraftoss.catacomb.sponge; +import org.minecraftoss.catacomb.account.request.RequestContextImpl; import org.spongepowered.api.plugin.PluginContainer; import java.util.UUID; -class SpongeRequestContextImpl implements SpongeRequestContext { - private final UUID accountIdentifier; +class SpongeRequestContextImpl extends RequestContextImpl implements SpongeRequestContext { private final PluginContainer plugin; - SpongeRequestContextImpl(UUID accountIdentifier, PluginContainer plugin) { - this.accountIdentifier = accountIdentifier; - this.plugin = plugin; + SpongeRequestContextImpl(UUID accountIdentifier, PluginContainer requestIdentifier) { + super(accountIdentifier, requestIdentifier.getId()); + this.plugin = requestIdentifier; } @Override @@ -19,7 +19,20 @@ public PluginContainer getPlugin() { } @Override - public UUID getAccountIdentifier() { - return this.accountIdentifier; + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof SpongeRequestContextImpl)) return false; + if (!super.equals(o)) return false; + + SpongeRequestContextImpl that = (SpongeRequestContextImpl) o; + + return plugin.equals(that.plugin); + } + + @Override + public int hashCode() { + int result = super.hashCode(); + result = 31 * result + plugin.hashCode(); + return result; } } From a362b9da2fdc8949b5ae0cf718d30605eb695e25 Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Mon, 26 Oct 2020 19:06:55 +0100 Subject: [PATCH 11/12] Allow better TransactionResultState extension --- .../transaction/result/BuiltinTransactionResultState.java | 8 +++++++- .../transaction/result/TransactionResultState.java | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/result/BuiltinTransactionResultState.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/result/BuiltinTransactionResultState.java index 4cc61c5..b329711 100644 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/result/BuiltinTransactionResultState.java +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/result/BuiltinTransactionResultState.java @@ -2,5 +2,11 @@ public enum BuiltinTransactionResultState implements TransactionResultState { ACCEPTED, - DECLINED + DECLINED, + FAILED; + + @Override + public BuiltinTransactionResultState getParent() { + return this; + } } diff --git a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/result/TransactionResultState.java b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/result/TransactionResultState.java index 3c2d8fa..d21cea1 100644 --- a/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/result/TransactionResultState.java +++ b/catacomb-common-api/src/main/java/org/minecraftoss/catacomb/transaction/result/TransactionResultState.java @@ -2,4 +2,5 @@ public interface TransactionResultState & TransactionResultState> { + BuiltinTransactionResultState getParent(); } From e63da1d97335607fa69d0a3027f8033336a03ac0 Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Tue, 27 Oct 2020 18:13:52 +0100 Subject: [PATCH 12/12] Minor fixes --- .../catacomb/bukkit/BukkitRequestContextFactory.java | 2 +- .../catacomb/sponge/SpongeRequestContextFactory.java | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/catacomb-bukkit-api/src/main/java/org/minecraftoss/catacomb/bukkit/BukkitRequestContextFactory.java b/catacomb-bukkit-api/src/main/java/org/minecraftoss/catacomb/bukkit/BukkitRequestContextFactory.java index 9d00dab..e7bcdd9 100644 --- a/catacomb-bukkit-api/src/main/java/org/minecraftoss/catacomb/bukkit/BukkitRequestContextFactory.java +++ b/catacomb-bukkit-api/src/main/java/org/minecraftoss/catacomb/bukkit/BukkitRequestContextFactory.java @@ -14,7 +14,7 @@ private BukkitRequestContextFactory(Plugin plugin) { this.plugin = plugin; } - static BukkitRequestContextFactory forPlugin(Plugin plugin) { + public static BukkitRequestContextFactory forPlugin(Plugin plugin) { return new BukkitRequestContextFactory(plugin); } diff --git a/catacomb-sponge-api/src/main/java/org/minecraftoss/catacomb/sponge/SpongeRequestContextFactory.java b/catacomb-sponge-api/src/main/java/org/minecraftoss/catacomb/sponge/SpongeRequestContextFactory.java index 9246b14..49831c1 100644 --- a/catacomb-sponge-api/src/main/java/org/minecraftoss/catacomb/sponge/SpongeRequestContextFactory.java +++ b/catacomb-sponge-api/src/main/java/org/minecraftoss/catacomb/sponge/SpongeRequestContextFactory.java @@ -9,10 +9,14 @@ public class SpongeRequestContextFactory implements RequestContextFactory { private final PluginContainer plugin; - public SpongeRequestContextFactory(PluginContainer plugin) { + SpongeRequestContextFactory(PluginContainer plugin) { this.plugin = plugin; } + static SpongeRequestContextFactory forPlugin(PluginContainer plugin) { + return new SpongeRequestContextFactory(plugin); + } + @Override public RequestContext fromAccountIdentifier(UUID accountIdentifier) { return SpongeRequestContext.pluginRequest(accountIdentifier, plugin);