mirror of
https://github.com/xHyroM/lighteco.git
synced 2024-11-24 15:51:06 +01:00
feat: user management, currency management
This commit is contained in:
parent
7757558442
commit
9a0b64b371
29 changed files with 544 additions and 63 deletions
|
@ -1,5 +1,6 @@
|
||||||
package dev.xhyrom.lighteco.api;
|
package dev.xhyrom.lighteco.api;
|
||||||
|
|
||||||
|
import dev.xhyrom.lighteco.api.managers.CurrencyManager;
|
||||||
import dev.xhyrom.lighteco.api.managers.UserManager;
|
import dev.xhyrom.lighteco.api.managers.UserManager;
|
||||||
import dev.xhyrom.lighteco.api.platform.Platform;
|
import dev.xhyrom.lighteco.api.platform.Platform;
|
||||||
import dev.xhyrom.lighteco.api.platform.PlayerAdapter;
|
import dev.xhyrom.lighteco.api.platform.PlayerAdapter;
|
||||||
|
@ -16,5 +17,7 @@ public interface LightEco {
|
||||||
|
|
||||||
@NonNull UserManager getUserManager();
|
@NonNull UserManager getUserManager();
|
||||||
|
|
||||||
|
@NonNull CurrencyManager getCurrencyManager();
|
||||||
|
|
||||||
<T> @NonNull PlayerAdapter<T> getPlayerAdapter(@NonNull Class<T> playerClass);
|
<T> @NonNull PlayerAdapter<T> getPlayerAdapter(@NonNull Class<T> playerClass);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
package dev.xhyrom.lighteco.api.exceptions;
|
||||||
|
|
||||||
|
public class NotImplementedException extends RuntimeException {
|
||||||
|
public NotImplementedException() {
|
||||||
|
super("This feature is not implemented yet!");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package dev.xhyrom.lighteco.api.managers;
|
||||||
|
|
||||||
|
import dev.xhyrom.lighteco.api.model.currency.Currency;
|
||||||
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
public interface CurrencyManager {
|
||||||
|
@NonNull Collection<Currency<?>> getRegisteredCurrencies();
|
||||||
|
|
||||||
|
Currency<?> getCurrency(@NonNull String identifier);
|
||||||
|
void registerCurrency(@NonNull Currency<?> currency);
|
||||||
|
}
|
|
@ -1,17 +1,28 @@
|
||||||
package dev.xhyrom.lighteco.api.model.currency;
|
package dev.xhyrom.lighteco.api.model.currency;
|
||||||
|
|
||||||
public interface Currency<T> {
|
import dev.xhyrom.lighteco.api.exceptions.NotImplementedException;
|
||||||
String getIdentifier();
|
import dev.xhyrom.lighteco.api.model.user.User;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public abstract class Currency<T> {
|
||||||
|
@Getter
|
||||||
|
private final Class<T> valueType;
|
||||||
|
public Currency(Class<T> valueType) {
|
||||||
|
this.valueType = valueType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract String getIdentifier();
|
||||||
/**
|
/**
|
||||||
* Get the type of the currency, either {@link Type#EXCLUSIVE} or {@link Type#GLOBAL}
|
* Get the type of the currency, either {@link Type#LOCAL} or {@link Type#GLOBAL}
|
||||||
*
|
*
|
||||||
* @see Type
|
* @see Type
|
||||||
* @return The type of the currency
|
* @return The type of the currency
|
||||||
*/
|
*/
|
||||||
Type getType();
|
public abstract Type getType();
|
||||||
|
|
||||||
boolean isPayable();
|
public abstract boolean isPayable();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculate the tax for the given amount
|
* Calculate the tax for the given amount
|
||||||
|
@ -20,18 +31,23 @@ public interface Currency<T> {
|
||||||
* @param amount The amount to calculate the tax for
|
* @param amount The amount to calculate the tax for
|
||||||
* @return The tax
|
* @return The tax
|
||||||
*/
|
*/
|
||||||
default double calculateTax(T amount) {
|
public double calculateTax(T amount) {
|
||||||
return 0;
|
return 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Implemented in common module
|
||||||
|
public List<User> getTopUsers(int length) {
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents the type of currency
|
* Represents the type of currency
|
||||||
*/
|
*/
|
||||||
enum Type {
|
public enum Type {
|
||||||
/**
|
/**
|
||||||
* A currency that is only available on a single server
|
* A currency that is only available on a single server
|
||||||
*/
|
*/
|
||||||
EXCLUSIVE,
|
LOCAL,
|
||||||
/**
|
/**
|
||||||
* A currency that is available on all servers (proxy)
|
* A currency that is available on all servers (proxy)
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package dev.xhyrom.lighteco.api.model.user;
|
package dev.xhyrom.lighteco.api.model.user;
|
||||||
|
|
||||||
|
import dev.xhyrom.lighteco.api.model.currency.Currency;
|
||||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
@ -18,4 +19,12 @@ public interface User {
|
||||||
* @return the username
|
* @return the username
|
||||||
*/
|
*/
|
||||||
@NonNull String getUsername();
|
@NonNull String getUsername();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the balance of this user for the specified currency.
|
||||||
|
*
|
||||||
|
* @param currency the currency
|
||||||
|
* @return the balance
|
||||||
|
*/
|
||||||
|
<T> T getBalance(@NonNull Currency<T> currency);
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,5 +19,13 @@ public interface Platform {
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return displayName;
|
return displayName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isLocal() {
|
||||||
|
return this == BUKKIT;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isProxy() {
|
||||||
|
return this == VELOCITY || this == BUNGEECORD;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,15 @@
|
||||||
package dev.xhyrom.lighteco.bukkit;
|
package dev.xhyrom.lighteco.bukkit;
|
||||||
|
|
||||||
|
import dev.xhyrom.lighteco.bukkit.test.TestPlugin;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
public class BukkitLightEcoLoader extends JavaPlugin {
|
public class BukkitLightEcoLoader extends JavaPlugin {
|
||||||
private BukkitLightEcoPlugin plugin = new BukkitLightEcoPlugin();
|
private final BukkitLightEcoPlugin plugin = new BukkitLightEcoPlugin();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
plugin.enable();
|
plugin.enable();
|
||||||
|
|
||||||
|
new TestPlugin(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,23 +1,29 @@
|
||||||
package dev.xhyrom.lighteco.bukkit;
|
package dev.xhyrom.lighteco.bukkit;
|
||||||
|
|
||||||
import dev.xhyrom.lighteco.api.managers.ContextManager;
|
import dev.xhyrom.lighteco.api.managers.ContextManager;
|
||||||
|
import dev.xhyrom.lighteco.api.managers.CurrencyManager;
|
||||||
import dev.xhyrom.lighteco.api.managers.UserManager;
|
import dev.xhyrom.lighteco.api.managers.UserManager;
|
||||||
import dev.xhyrom.lighteco.api.platform.Platform;
|
import dev.xhyrom.lighteco.api.platform.Platform;
|
||||||
|
import dev.xhyrom.lighteco.common.api.impl.ApiCurrencyManager;
|
||||||
|
import dev.xhyrom.lighteco.common.managers.currency.StandardCurrencyManager;
|
||||||
import dev.xhyrom.lighteco.common.plugin.AbstractLightEcoPlugin;
|
import dev.xhyrom.lighteco.common.plugin.AbstractLightEcoPlugin;
|
||||||
import dev.xhyrom.lighteco.common.managers.StandardUserManager;
|
import dev.xhyrom.lighteco.common.managers.user.StandardUserManager;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
|
||||||
public class BukkitLightEcoPlugin extends AbstractLightEcoPlugin {
|
public class BukkitLightEcoPlugin extends AbstractLightEcoPlugin {
|
||||||
@Getter
|
@Getter
|
||||||
private UserManager userManager;
|
private StandardUserManager userManager;
|
||||||
|
@Getter
|
||||||
|
private StandardCurrencyManager currencyManager;
|
||||||
@Getter
|
@Getter
|
||||||
private ContextManager<Player> contextManager;
|
private ContextManager<Player> contextManager;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setupManagers() {
|
public void setupManagers() {
|
||||||
this.userManager = new StandardUserManager(this);
|
this.userManager = new StandardUserManager(this);
|
||||||
|
this.currencyManager = new StandardCurrencyManager(this);
|
||||||
this.contextManager = new BukkitContextManager();
|
this.contextManager = new BukkitContextManager();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
package dev.xhyrom.lighteco.bukkit.test;
|
||||||
|
|
||||||
|
import dev.xhyrom.lighteco.api.model.currency.Currency;
|
||||||
|
|
||||||
|
public class TestCurrency extends Currency<Integer> {
|
||||||
|
public TestCurrency() {
|
||||||
|
super(Integer.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getIdentifier() {
|
||||||
|
return "test";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Type getType() {
|
||||||
|
return Type.LOCAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isPayable() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
package dev.xhyrom.lighteco.bukkit.test;
|
||||||
|
|
||||||
|
import dev.xhyrom.lighteco.api.LightEcoProvider;
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
|
public class TestPlugin {
|
||||||
|
private final JavaPlugin plugin;
|
||||||
|
|
||||||
|
public TestPlugin(JavaPlugin plugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
|
||||||
|
plugin.getLogger().info("TestPlugin loaded!");
|
||||||
|
|
||||||
|
LightEcoProvider.get().getCurrencyManager().registerCurrency(new TestCurrency());
|
||||||
|
|
||||||
|
plugin.getLogger().info("TestCurrency registered!");
|
||||||
|
|
||||||
|
LightEcoProvider.get().getCurrencyManager().getRegisteredCurrencies().forEach(currency -> {
|
||||||
|
plugin.getLogger().info("Currency: " + currency.getIdentifier() + " (" + currency.getType() + ", " + currency.getValueType() + ", " + currency.isPayable() + ")");
|
||||||
|
currency.getTopUsers(5).forEach(user -> {
|
||||||
|
plugin.getLogger().info("User: " + user.getUniqueId() + " (" + user.getUsername() + ")");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
name: ${name}
|
name: ${name}
|
||||||
description: ${description}
|
description: ${description}
|
||||||
version: ${version}
|
version: ${version}
|
||||||
main: dev.xhyrom.lighteco.bukkit.BukkitLightEcoPlugin
|
main: dev.xhyrom.lighteco.bukkit.BukkitLightEcoLoader
|
||||||
author: ${author}
|
author: ${author}
|
||||||
api-version: 1.20
|
api-version: 1.20
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
package dev.xhyrom.lighteco.common.api;
|
package dev.xhyrom.lighteco.common.api;
|
||||||
|
|
||||||
import dev.xhyrom.lighteco.api.LightEco;
|
import dev.xhyrom.lighteco.api.LightEco;
|
||||||
|
import dev.xhyrom.lighteco.api.managers.CurrencyManager;
|
||||||
import dev.xhyrom.lighteco.api.managers.UserManager;
|
import dev.xhyrom.lighteco.api.managers.UserManager;
|
||||||
import dev.xhyrom.lighteco.api.platform.Platform;
|
import dev.xhyrom.lighteco.api.platform.Platform;
|
||||||
import dev.xhyrom.lighteco.api.platform.PlayerAdapter;
|
import dev.xhyrom.lighteco.api.platform.PlayerAdapter;
|
||||||
|
import dev.xhyrom.lighteco.common.api.impl.ApiCurrencyManager;
|
||||||
|
import dev.xhyrom.lighteco.common.api.impl.ApiUserManager;
|
||||||
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
|
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
|
||||||
import dev.xhyrom.lighteco.common.api.impl.ApiPlatform;
|
import dev.xhyrom.lighteco.common.api.impl.ApiPlatform;
|
||||||
import dev.xhyrom.lighteco.common.api.impl.ApiPlayerAdapter;
|
import dev.xhyrom.lighteco.common.api.impl.ApiPlayerAdapter;
|
||||||
|
@ -13,13 +16,17 @@ public class LightEcoApi implements LightEco {
|
||||||
private final LightEcoPlugin plugin;
|
private final LightEcoPlugin plugin;
|
||||||
|
|
||||||
private final Platform platform;
|
private final Platform platform;
|
||||||
|
private final UserManager userManager;
|
||||||
|
private final CurrencyManager currencyManager;
|
||||||
private final PlayerAdapter<?> playerAdapter;
|
private final PlayerAdapter<?> playerAdapter;
|
||||||
|
|
||||||
public LightEcoApi(LightEcoPlugin plugin) {
|
public LightEcoApi(LightEcoPlugin plugin) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
|
|
||||||
this.platform = new ApiPlatform(plugin);
|
this.platform = new ApiPlatform(plugin);
|
||||||
this.playerAdapter = new ApiPlayerAdapter<>(plugin.getUserManager(), plugin.getContextManager());
|
this.userManager = new ApiUserManager(plugin, plugin.getUserManager());
|
||||||
|
this.currencyManager = new ApiCurrencyManager(plugin, plugin.getCurrencyManager());
|
||||||
|
this.playerAdapter = new ApiPlayerAdapter<>(userManager, plugin.getContextManager());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -29,7 +36,12 @@ public class LightEcoApi implements LightEco {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NonNull UserManager getUserManager() {
|
public @NonNull UserManager getUserManager() {
|
||||||
return this.plugin.getUserManager();
|
return this.userManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NonNull CurrencyManager getCurrencyManager() {
|
||||||
|
return this.currencyManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
package dev.xhyrom.lighteco.common.api.impl;
|
||||||
|
|
||||||
|
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
|
||||||
|
|
||||||
|
public abstract class ApiAbstractManager<H> {
|
||||||
|
protected final LightEcoPlugin plugin;
|
||||||
|
protected final H handler;
|
||||||
|
|
||||||
|
protected ApiAbstractManager(LightEcoPlugin plugin, H handler) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
this.handler = handler;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,35 @@
|
||||||
|
package dev.xhyrom.lighteco.common.api.impl;
|
||||||
|
|
||||||
|
import dev.xhyrom.lighteco.api.model.currency.Currency;
|
||||||
|
import dev.xhyrom.lighteco.api.model.user.User;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ApiCurrency extends Currency {
|
||||||
|
private final dev.xhyrom.lighteco.common.model.currency.Currency<?> handler;
|
||||||
|
|
||||||
|
public ApiCurrency(dev.xhyrom.lighteco.common.model.currency.Currency<?> handler) {
|
||||||
|
super(handler.getValueType());
|
||||||
|
|
||||||
|
this.handler = handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getIdentifier() {
|
||||||
|
return this.handler.getIdentifier();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Currency.Type getType() {
|
||||||
|
return this.handler.getType();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isPayable() {
|
||||||
|
return this.handler.isPayable();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<User> getTopUsers(int length) {
|
||||||
|
return this.handler.getTopUsers(length)
|
||||||
|
.stream()
|
||||||
|
.map(ApiUserManager::wrap)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
package dev.xhyrom.lighteco.common.api.impl;
|
||||||
|
|
||||||
|
import dev.xhyrom.lighteco.api.managers.CurrencyManager;
|
||||||
|
import dev.xhyrom.lighteco.api.model.currency.Currency;
|
||||||
|
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
|
||||||
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class ApiCurrencyManager extends ApiAbstractManager<dev.xhyrom.lighteco.common.managers.currency.CurrencyManager> implements CurrencyManager {
|
||||||
|
public ApiCurrencyManager(LightEcoPlugin plugin, dev.xhyrom.lighteco.common.managers.currency.CurrencyManager handler) {
|
||||||
|
super(plugin, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Currency<?> wrap(dev.xhyrom.lighteco.common.model.currency.Currency<?> handler) {
|
||||||
|
return new ApiCurrency(handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NonNull Collection<Currency<?>> getRegisteredCurrencies() {
|
||||||
|
return this.handler.values()
|
||||||
|
.stream().map(this::wrap)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Currency<?> getCurrency(@NonNull String identifier) {
|
||||||
|
return wrap(this.handler.getIfLoaded(identifier));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerCurrency(@NonNull Currency<?> currency) {
|
||||||
|
dev.xhyrom.lighteco.common.model.currency.Currency<?> internal = new dev.xhyrom.lighteco.common.model.currency.Currency<>(plugin, currency);
|
||||||
|
this.handler.registerCurrency(internal);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
package dev.xhyrom.lighteco.common.api.impl;
|
||||||
|
|
||||||
|
import dev.xhyrom.lighteco.api.model.currency.Currency;
|
||||||
|
import dev.xhyrom.lighteco.api.model.user.User;
|
||||||
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class ApiUser implements User {
|
||||||
|
private final dev.xhyrom.lighteco.common.model.user.User handler;
|
||||||
|
|
||||||
|
public ApiUser(dev.xhyrom.lighteco.common.model.user.User handler) {
|
||||||
|
this.handler = handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NonNull UUID getUniqueId() {
|
||||||
|
return this.handler.getUniqueId();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NonNull String getUsername() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> T getBalance(@NonNull Currency<T> currency) {
|
||||||
|
return this.handler.getBalance(currency);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,40 @@
|
||||||
|
package dev.xhyrom.lighteco.common.api.impl;
|
||||||
|
|
||||||
|
import dev.xhyrom.lighteco.api.managers.UserManager;
|
||||||
|
import dev.xhyrom.lighteco.api.model.user.User;
|
||||||
|
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
|
||||||
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
public class ApiUserManager extends ApiAbstractManager<dev.xhyrom.lighteco.common.managers.user.UserManager> implements UserManager {
|
||||||
|
public ApiUserManager(LightEcoPlugin plugin, dev.xhyrom.lighteco.common.managers.user.UserManager handler) {
|
||||||
|
super(plugin, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static User wrap(dev.xhyrom.lighteco.common.model.user.User handler) {
|
||||||
|
return handler.getProxy();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NonNull CompletableFuture<User> loadUser(@NonNull UUID uniqueId) {
|
||||||
|
return this.plugin.getStorage().loadUser(uniqueId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NonNull CompletableFuture<Void> saveUser(@NonNull User user) {
|
||||||
|
return this.plugin.getStorage().saveUser(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @Nullable User getUser(@NonNull UUID uniqueId) {
|
||||||
|
return wrap(this.handler.getIfLoaded(uniqueId));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isLoaded(@NonNull UUID uniqueId) {
|
||||||
|
return this.handler.isLoaded(uniqueId);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
package dev.xhyrom.lighteco.common.managers;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public abstract class AbstractManager<I, T> implements Manager<I, T> {
|
||||||
|
public final Map<I, T> map = new HashMap<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<I> keys() {
|
||||||
|
return this.map.keySet();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<T> values() {
|
||||||
|
return this.map.values();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T getOrMake(I identifier) {
|
||||||
|
return this.map.getOrDefault(identifier, this.apply(identifier));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T getIfLoaded(I identifier) {
|
||||||
|
return this.map.get(identifier);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isLoaded(I identifier) {
|
||||||
|
return this.map.containsKey(identifier);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void unload(I identifier) {
|
||||||
|
this.map.remove(identifier);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void unload(Collection<I> identifiers) {
|
||||||
|
identifiers.forEach(this::unload);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
package dev.xhyrom.lighteco.common.managers;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
public interface Manager<I, T> {
|
||||||
|
T apply(I identifier);
|
||||||
|
|
||||||
|
Collection<I> keys();
|
||||||
|
Collection<T> values();
|
||||||
|
|
||||||
|
T getOrMake(I identifier);
|
||||||
|
|
||||||
|
T getIfLoaded(I identifier);
|
||||||
|
|
||||||
|
boolean isLoaded(I identifier);
|
||||||
|
|
||||||
|
void unload(I identifier);
|
||||||
|
|
||||||
|
void unload(Collection<I> identifiers);
|
||||||
|
}
|
|
@ -1,41 +0,0 @@
|
||||||
package dev.xhyrom.lighteco.common.managers;
|
|
||||||
|
|
||||||
import dev.xhyrom.lighteco.api.model.user.User;
|
|
||||||
import dev.xhyrom.lighteco.api.managers.UserManager;
|
|
||||||
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
|
|
||||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
|
||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
|
||||||
|
|
||||||
import java.util.UUID;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
|
|
||||||
public class StandardUserManager implements UserManager {
|
|
||||||
private final LightEcoPlugin plugin;
|
|
||||||
|
|
||||||
public StandardUserManager(LightEcoPlugin plugin) {
|
|
||||||
this.plugin = plugin;
|
|
||||||
}
|
|
||||||
|
|
||||||
public User apply(UUID uniqueId) {
|
|
||||||
return new dev.xhyrom.lighteco.common.model.user.User(uniqueId);
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public @NonNull CompletableFuture<User> loadUser(@NonNull UUID uniqueId) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NonNull CompletableFuture<Void> saveUser(User user) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable User getUser(@NonNull UUID uniqueId) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isLoaded(@NonNull UUID uniqueId) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
package dev.xhyrom.lighteco.common.managers.currency;
|
||||||
|
|
||||||
|
import dev.xhyrom.lighteco.common.managers.Manager;
|
||||||
|
import dev.xhyrom.lighteco.common.model.currency.Currency;
|
||||||
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
public interface CurrencyManager extends Manager<String, Currency<?>> {
|
||||||
|
@NonNull Collection<Currency<?>> getRegisteredCurrencies();
|
||||||
|
|
||||||
|
void registerCurrency(@NonNull Currency<?> currency);
|
||||||
|
}
|
|
@ -0,0 +1,47 @@
|
||||||
|
package dev.xhyrom.lighteco.common.managers.currency;
|
||||||
|
|
||||||
|
import dev.xhyrom.lighteco.common.managers.AbstractManager;
|
||||||
|
import dev.xhyrom.lighteco.common.model.currency.Currency;
|
||||||
|
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
|
||||||
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import static dev.xhyrom.lighteco.api.model.currency.Currency.Type;
|
||||||
|
|
||||||
|
public class StandardCurrencyManager extends AbstractManager<String, Currency<?>> implements CurrencyManager {
|
||||||
|
private final LightEcoPlugin plugin;
|
||||||
|
|
||||||
|
public StandardCurrencyManager(LightEcoPlugin plugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Currency<?> apply(String identifier) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Currency<?> getOrMake(String identifier) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NonNull Collection<Currency<?>> getRegisteredCurrencies() {
|
||||||
|
return this.values();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerCurrency(@NonNull Currency<?> currency) {
|
||||||
|
if (currency.getType() == Type.GLOBAL && this.plugin.getPlatformType().isLocal())
|
||||||
|
throw new IllegalArgumentException("Cannot register global currency on local platform");
|
||||||
|
|
||||||
|
if (currency.getType() == Type.LOCAL && this.plugin.getPlatformType().isProxy())
|
||||||
|
throw new IllegalArgumentException("Cannot register local currency on proxy platform");
|
||||||
|
|
||||||
|
if (this.isLoaded(currency.getIdentifier()))
|
||||||
|
throw new IllegalArgumentException("Currency with identifier " + currency.getIdentifier() + " already registered");
|
||||||
|
|
||||||
|
this.map.put(currency.getIdentifier(), currency);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,39 @@
|
||||||
|
package dev.xhyrom.lighteco.common.managers.user;
|
||||||
|
|
||||||
|
import dev.xhyrom.lighteco.common.managers.AbstractManager;
|
||||||
|
import dev.xhyrom.lighteco.common.model.user.User;
|
||||||
|
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
public class StandardUserManager extends AbstractManager<UUID, User> implements UserManager {
|
||||||
|
private final LightEcoPlugin plugin;
|
||||||
|
|
||||||
|
public StandardUserManager(LightEcoPlugin plugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public User apply(UUID uniqueId) {
|
||||||
|
return new User(this.plugin, uniqueId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CompletableFuture<Void> load() {
|
||||||
|
Set<UUID> uniqueIds = new HashSet<>(keys());
|
||||||
|
// TODO: Add all players lol
|
||||||
|
//uniqueIds.addAll(this.plugin.getBootstrap().getOnlinePlayers());
|
||||||
|
|
||||||
|
return uniqueIds.stream()
|
||||||
|
.map(id -> this.plugin.getStorage().loadUser(id))
|
||||||
|
.collect(CompletableFuture::allOf, (future, userFuture) -> future.join(), (future, userFuture) -> future.join());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void invalidateCaches() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package dev.xhyrom.lighteco.common.managers.user;
|
||||||
|
|
||||||
|
import dev.xhyrom.lighteco.common.managers.Manager;
|
||||||
|
import dev.xhyrom.lighteco.common.model.user.User;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
public interface UserManager extends Manager<UUID, User> {
|
||||||
|
CompletableFuture<Void> load();
|
||||||
|
|
||||||
|
void invalidateCaches();
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
package dev.xhyrom.lighteco.common.model.currency;
|
||||||
|
|
||||||
|
import dev.xhyrom.lighteco.common.model.user.User;
|
||||||
|
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class Currency<T> {
|
||||||
|
private final LightEcoPlugin plugin;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private final dev.xhyrom.lighteco.api.model.currency.Currency<T> proxy;
|
||||||
|
|
||||||
|
public Currency(LightEcoPlugin plugin, dev.xhyrom.lighteco.api.model.currency.Currency<T> proxy) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
this.proxy = proxy;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getIdentifier() {
|
||||||
|
return proxy.getIdentifier();
|
||||||
|
}
|
||||||
|
|
||||||
|
public dev.xhyrom.lighteco.api.model.currency.Currency.Type getType() {
|
||||||
|
return proxy.getType();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Class<T> getValueType() {
|
||||||
|
return proxy.getValueType();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isPayable() {
|
||||||
|
return proxy.isPayable();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<User> getTopUsers(int length) {
|
||||||
|
List<User> users = new ArrayList<>();
|
||||||
|
users.add(new User(plugin, new UUID(0, 0)));
|
||||||
|
|
||||||
|
return users;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,20 +1,30 @@
|
||||||
package dev.xhyrom.lighteco.common.model.user;
|
package dev.xhyrom.lighteco.common.model.user;
|
||||||
|
|
||||||
|
import dev.xhyrom.lighteco.api.model.currency.Currency;
|
||||||
|
import dev.xhyrom.lighteco.common.api.impl.ApiUser;
|
||||||
|
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public class User implements dev.xhyrom.lighteco.api.model.user.User {
|
public class User {
|
||||||
|
@Getter
|
||||||
|
private final LightEcoPlugin plugin;
|
||||||
|
@Getter
|
||||||
|
private final ApiUser proxy = new ApiUser(this);
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private final UUID uniqueId;
|
private final UUID uniqueId;
|
||||||
|
|
||||||
public User(UUID uniqueId) {
|
public User(LightEcoPlugin plugin, UUID uniqueId) {
|
||||||
|
this.plugin = plugin;
|
||||||
this.uniqueId = uniqueId;
|
this.uniqueId = uniqueId;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
// TODO: finish
|
||||||
public @NonNull String getUsername() {
|
public <T> T getBalance(@NonNull Currency<T> currency) {
|
||||||
|
this.plugin.getCurrencyManager().getIfLoaded(currency.getIdentifier());
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package dev.xhyrom.lighteco.common.plugin;
|
package dev.xhyrom.lighteco.common.plugin;
|
||||||
|
|
||||||
|
import dev.xhyrom.lighteco.api.LightEcoProvider;
|
||||||
|
import dev.xhyrom.lighteco.common.api.LightEcoApi;
|
||||||
import dev.xhyrom.lighteco.common.storage.Storage;
|
import dev.xhyrom.lighteco.common.storage.Storage;
|
||||||
import dev.xhyrom.lighteco.common.storage.StorageFactory;
|
import dev.xhyrom.lighteco.common.storage.StorageFactory;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
@ -7,11 +9,17 @@ import lombok.Getter;
|
||||||
public abstract class AbstractLightEcoPlugin implements LightEcoPlugin {
|
public abstract class AbstractLightEcoPlugin implements LightEcoPlugin {
|
||||||
@Getter
|
@Getter
|
||||||
private Storage storage;
|
private Storage storage;
|
||||||
|
private LightEcoApi api;
|
||||||
|
|
||||||
public final void enable() {
|
public final void enable() {
|
||||||
StorageFactory factory = new StorageFactory(this);
|
StorageFactory factory = new StorageFactory(this);
|
||||||
|
|
||||||
this.storage = factory.get();
|
this.storage = factory.get();
|
||||||
|
|
||||||
|
this.setupManagers();
|
||||||
|
|
||||||
|
this.api = new LightEcoApi(this);
|
||||||
|
LightEcoProvider.set(this.api);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract void setupManagers();
|
protected abstract void setupManagers();
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
package dev.xhyrom.lighteco.common.plugin;
|
package dev.xhyrom.lighteco.common.plugin;
|
||||||
|
|
||||||
import dev.xhyrom.lighteco.api.managers.ContextManager;
|
import dev.xhyrom.lighteco.api.managers.ContextManager;
|
||||||
import dev.xhyrom.lighteco.api.managers.UserManager;
|
|
||||||
import dev.xhyrom.lighteco.api.platform.Platform;
|
import dev.xhyrom.lighteco.api.platform.Platform;
|
||||||
|
import dev.xhyrom.lighteco.common.managers.currency.CurrencyManager;
|
||||||
|
import dev.xhyrom.lighteco.common.managers.user.UserManager;
|
||||||
import dev.xhyrom.lighteco.common.storage.Storage;
|
import dev.xhyrom.lighteco.common.storage.Storage;
|
||||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
|
||||||
|
@ -10,6 +11,7 @@ public interface LightEcoPlugin {
|
||||||
Platform.@NonNull Type getPlatformType();
|
Platform.@NonNull Type getPlatformType();
|
||||||
|
|
||||||
@NonNull UserManager getUserManager();
|
@NonNull UserManager getUserManager();
|
||||||
|
@NonNull CurrencyManager getCurrencyManager();
|
||||||
@NonNull ContextManager<?> getContextManager();
|
@NonNull ContextManager<?> getContextManager();
|
||||||
|
|
||||||
@NonNull Storage getStorage();
|
@NonNull Storage getStorage();
|
||||||
|
|
|
@ -8,7 +8,7 @@ import java.util.HashMap;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public class MemoryStorageProvider implements StorageProvider {
|
public class MemoryStorageProvider implements StorageProvider {
|
||||||
private HashMap<UUID, User> users = new HashMap<>();
|
private final HashMap<UUID, User> userDatabase = new HashMap<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NonNull String[] getIdentifiers() {
|
public @NonNull String[] getIdentifiers() {
|
||||||
|
@ -17,11 +17,11 @@ public class MemoryStorageProvider implements StorageProvider {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NonNull User loadUser(@NonNull UUID uniqueId) {
|
public @NonNull User loadUser(@NonNull UUID uniqueId) {
|
||||||
return users.get(uniqueId);
|
return userDatabase.get(uniqueId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void saveUser(@NonNull User user) {
|
public void saveUser(@NonNull User user) {
|
||||||
users.put(user.getUniqueId(), user);
|
userDatabase.put(user.getUniqueId(), user);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue