Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Draft of API #3

Draft
wants to merge 12 commits into
base: dev
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}

public static BukkitRequestContextFactory forPlugin(Plugin plugin) {
return new BukkitRequestContextFactory(plugin);
}

@Override
public RequestContext fromAccountIdentifier(UUID accountIdentifier) {
return BukkitRequestContext.pluginRequest(accountIdentifier, this.plugin);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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;
}

@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;
}
}
2 changes: 1 addition & 1 deletion catacomb-bukkit/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.minecraftoss.catacomb;

@SuppressWarnings("unused")
public enum Capability {

/**
* Accounts that are provided by a third-party plugin.
*
* <p>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.
*
* <p>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.
*
* <p>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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.minecraftoss.catacomb;

import org.minecraftoss.catacomb.account.AccountManager;

import java.util.Set;

public interface CatacombService {

default boolean queryCapabilities(Capability capability) {
return false;
}

default boolean queryCapabilities(Set<Capability> capabilities) {
for (Capability capability : capabilities) {
if (!queryCapabilities(capability)) {
return false;
}
}
return true;
}

EconomyManager getEconomyManager();

AccountManager getAccountManager();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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;
import java.util.Set;
import java.util.UUID;

public interface EconomyManager {

TransactionResult handle(Transaction transaction, TransactionCondition condition);

BigDecimal getBalance(UUID identifier, Currency currency, Set<TransactionContext> contexts);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.minecraftoss.catacomb;

import org.minecraftoss.catacomb.account.AccountManager;

import java.util.EnumSet;
import java.util.Set;

public class SimpleCatacombService implements CatacombService {
private final EconomyManager economyManager;
private final AccountManager accountManager;
private final Set<Capability> capabilities;

public SimpleCatacombService(EconomyManager economyManager,
AccountManager accountManager, Set<Capability> 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<Capability> capabilities) {
return this.capabilities.containsAll(capabilities);
}

@Override
public EconomyManager getEconomyManager() {
return this.economyManager;
}

@Override
public AccountManager getAccountManager() {
return this.accountManager;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
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.TransactionContext;
import org.minecraftoss.catacomb.transaction.TransactionCondition;
import org.minecraftoss.catacomb.transaction.result.TransactionResult;

import java.math.BigDecimal;
import java.util.Set;
import java.util.UUID;

public interface Account {

UUID getIdentifier();

default BigDecimal getBalance(Currency currency, Set<TransactionContext> 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);

default BigDecimal getInitialBalance() {
return getInitialBalance(getAccountContext().getDefaultCurrency());
}

// account reset

TransactionResult resetBalance(Currency currency, Set<TransactionContext> contexts);

default TransactionResult resetBalance(Currency currency) {
return resetBalance(currency, getAccountContext().getDefaultContexts());
}

default TransactionResult resetBalance() {
return resetBalance(getAccountContext().getDefaultCurrency());
}

// transfer

default TransactionResult transfer(Account to, Currency currency, BigDecimal amount, TransactionCondition condition, Set<TransactionContext> 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<TransactionContext> contexts) {
return transfer(to, getAccountContext().getDefaultCurrency(), amount, condition, contexts);
}

default TransactionResult transfer(Account to, Currency currency, BigDecimal amount, Set<TransactionContext> contexts) {
return transfer(to, currency, amount, getAccountContext().getDefaultTransactionCondition(), contexts);
}

default TransactionResult transfer(Account to, BigDecimal amount, Set<TransactionContext> 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, getAccountContext().getDefaultContexts());
}

default TransactionResult transfer(Account to, BigDecimal amount, TransactionCondition condition) {
return transfer(to, getAccountContext().getDefaultCurrency(), amount, condition);
}

default TransactionResult transfer(Account to, Currency currency, BigDecimal amount) {
return transfer(to, currency, amount, getAccountContext().getDefaultTransactionCondition());
}

default TransactionResult transfer(Account to, BigDecimal amount) {
return transfer(to, getAccountContext().getDefaultCurrency(), amount);
}

// withdraw

default TransactionResult withdraw(Currency currency, BigDecimal amount, TransactionCondition condition, Set<TransactionContext> contexts) {
return transfer(getAccountContext().getInfiniteAccount(), currency, amount, condition, contexts);
}

default TransactionResult withdraw(BigDecimal amount, TransactionCondition condition, Set<TransactionContext> contexts) {
return withdraw(getAccountContext().getDefaultCurrency(), amount, condition, contexts);
}

default TransactionResult withdraw(Currency currency, BigDecimal amount, Set<TransactionContext> contexts) {
return withdraw(currency, amount, getAccountContext().getDefaultTransactionCondition(), contexts);
}

default TransactionResult withdraw(BigDecimal amount, Set<TransactionContext> contexts) {
return withdraw(getAccountContext().getDefaultCurrency(), amount, contexts);
}

default TransactionResult withdraw(Currency currency, BigDecimal amount, TransactionCondition condition) {
return withdraw(currency, amount, condition, getAccountContext().getDefaultContexts());
}

default TransactionResult withdraw(BigDecimal amount, TransactionCondition condition) {
return withdraw(getAccountContext().getDefaultCurrency(), amount, condition);
}

default TransactionResult withdraw(Currency currency, BigDecimal amount) {
return withdraw(currency, amount, getAccountContext().getDefaultTransactionCondition());
}

default TransactionResult withdraw(BigDecimal amount) {
return withdraw(getAccountContext().getDefaultCurrency(), amount);
}

// deposit

default TransactionResult deposit(Currency currency, BigDecimal amount, TransactionCondition condition, Set<TransactionContext> contexts) {
return getAccountContext().getInfiniteAccount().transfer(this, currency, amount, condition, contexts);
}

default TransactionResult deposit(BigDecimal amount, TransactionCondition condition, Set<TransactionContext> contexts) {
return deposit(getAccountContext().getDefaultCurrency(), amount, condition, contexts);
}

default TransactionResult deposit(Currency currency, BigDecimal amount, Set<TransactionContext> contexts) {
return deposit(currency, amount, getAccountContext().getDefaultTransactionCondition(), contexts);
}

default TransactionResult deposit(BigDecimal amount, Set<TransactionContext> contexts) {
return deposit(getAccountContext().getDefaultCurrency(), amount, contexts);
}

default TransactionResult deposit(Currency currency, BigDecimal amount, TransactionCondition condition) {
return deposit(currency, amount, condition, getAccountContext().getDefaultContexts());
}

default TransactionResult deposit(BigDecimal amount, TransactionCondition condition) {
return deposit(getAccountContext().getDefaultCurrency(), amount, condition);
}

default TransactionResult deposit(Currency currency, BigDecimal amount) {
return deposit(currency, amount, getAccountContext().getDefaultTransactionCondition());
}

default TransactionResult deposit(BigDecimal 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).
*
* <p>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();
}
Loading