From 9a0b64b371747b172c1f9a7acb4becba84854849 Mon Sep 17 00:00:00 2001 From: xHyroM Date: Wed, 2 Aug 2023 21:01:30 +0200 Subject: [PATCH] feat: user management, currency management --- .../dev/xhyrom/lighteco/api/LightEco.java | 3 ++ .../exceptions/NotImplementedException.java | 7 +++ .../api/managers/CurrencyManager.java | 13 +++++ .../lighteco/api/model/currency/Currency.java | 32 +++++++++---- .../xhyrom/lighteco/api/model/user/User.java | 9 ++++ .../lighteco/api/platform/Platform.java | 8 ++++ .../lighteco/bukkit/BukkitLightEcoLoader.java | 5 +- .../lighteco/bukkit/BukkitLightEcoPlugin.java | 10 +++- .../lighteco/bukkit/test/TestCurrency.java | 24 ++++++++++ .../lighteco/bukkit/test/TestPlugin.java | 25 ++++++++++ bukkit/src/main/resources/plugin.yml | 2 +- .../lighteco/common/api/LightEcoApi.java | 16 ++++++- .../common/api/impl/ApiAbstractManager.java | 13 +++++ .../lighteco/common/api/impl/ApiCurrency.java | 35 ++++++++++++++ .../common/api/impl/ApiCurrencyManager.java | 37 +++++++++++++++ .../lighteco/common/api/impl/ApiUser.java | 30 ++++++++++++ .../common/api/impl/ApiUserManager.java | 40 ++++++++++++++++ .../common/managers/AbstractManager.java | 44 +++++++++++++++++ .../lighteco/common/managers/Manager.java | 21 +++++++++ .../common/managers/StandardUserManager.java | 41 ---------------- .../managers/currency/CurrencyManager.java | 13 +++++ .../currency/StandardCurrencyManager.java | 47 +++++++++++++++++++ .../managers/user/StandardUserManager.java | 39 +++++++++++++++ .../common/managers/user/UserManager.java | 13 +++++ .../common/model/currency/Currency.java | 44 +++++++++++++++++ .../lighteco/common/model/user/User.java | 18 +++++-- .../common/plugin/AbstractLightEcoPlugin.java | 8 ++++ .../common/plugin/LightEcoPlugin.java | 4 +- .../memory/MemoryStorageProvider.java | 6 +-- 29 files changed, 544 insertions(+), 63 deletions(-) create mode 100644 api/src/main/java/dev/xhyrom/lighteco/api/exceptions/NotImplementedException.java create mode 100644 api/src/main/java/dev/xhyrom/lighteco/api/managers/CurrencyManager.java create mode 100644 bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/test/TestCurrency.java create mode 100644 bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/test/TestPlugin.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiAbstractManager.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiCurrency.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiCurrencyManager.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiUser.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiUserManager.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/managers/AbstractManager.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/managers/Manager.java delete mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/managers/StandardUserManager.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/managers/currency/CurrencyManager.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/managers/currency/StandardCurrencyManager.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/managers/user/StandardUserManager.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/managers/user/UserManager.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/model/currency/Currency.java diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/LightEco.java b/api/src/main/java/dev/xhyrom/lighteco/api/LightEco.java index b0b4172..4c32528 100644 --- a/api/src/main/java/dev/xhyrom/lighteco/api/LightEco.java +++ b/api/src/main/java/dev/xhyrom/lighteco/api/LightEco.java @@ -1,5 +1,6 @@ package dev.xhyrom.lighteco.api; +import dev.xhyrom.lighteco.api.managers.CurrencyManager; import dev.xhyrom.lighteco.api.managers.UserManager; import dev.xhyrom.lighteco.api.platform.Platform; import dev.xhyrom.lighteco.api.platform.PlayerAdapter; @@ -16,5 +17,7 @@ public interface LightEco { @NonNull UserManager getUserManager(); + @NonNull CurrencyManager getCurrencyManager(); + @NonNull PlayerAdapter getPlayerAdapter(@NonNull Class playerClass); } diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/exceptions/NotImplementedException.java b/api/src/main/java/dev/xhyrom/lighteco/api/exceptions/NotImplementedException.java new file mode 100644 index 0000000..202f8b8 --- /dev/null +++ b/api/src/main/java/dev/xhyrom/lighteco/api/exceptions/NotImplementedException.java @@ -0,0 +1,7 @@ +package dev.xhyrom.lighteco.api.exceptions; + +public class NotImplementedException extends RuntimeException { + public NotImplementedException() { + super("This feature is not implemented yet!"); + } +} diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/managers/CurrencyManager.java b/api/src/main/java/dev/xhyrom/lighteco/api/managers/CurrencyManager.java new file mode 100644 index 0000000..99aab20 --- /dev/null +++ b/api/src/main/java/dev/xhyrom/lighteco/api/managers/CurrencyManager.java @@ -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> getRegisteredCurrencies(); + + Currency getCurrency(@NonNull String identifier); + void registerCurrency(@NonNull Currency currency); +} diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/model/currency/Currency.java b/api/src/main/java/dev/xhyrom/lighteco/api/model/currency/Currency.java index e7ebb77..0ce23e3 100644 --- a/api/src/main/java/dev/xhyrom/lighteco/api/model/currency/Currency.java +++ b/api/src/main/java/dev/xhyrom/lighteco/api/model/currency/Currency.java @@ -1,17 +1,28 @@ package dev.xhyrom.lighteco.api.model.currency; -public interface Currency { - String getIdentifier(); +import dev.xhyrom.lighteco.api.exceptions.NotImplementedException; +import dev.xhyrom.lighteco.api.model.user.User; +import lombok.Getter; +import java.util.List; + +public abstract class Currency { + @Getter + private final Class valueType; + public Currency(Class 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 * @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 @@ -20,18 +31,23 @@ public interface Currency { * @param amount The amount to calculate the tax for * @return The tax */ - default double calculateTax(T amount) { + public double calculateTax(T amount) { return 0; }; + // Implemented in common module + public List getTopUsers(int length) { + throw new NotImplementedException(); + } + /** * Represents the type of currency */ - enum Type { + public enum Type { /** * A currency that is only available on a single server */ - EXCLUSIVE, + LOCAL, /** * A currency that is available on all servers (proxy) */ diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/model/user/User.java b/api/src/main/java/dev/xhyrom/lighteco/api/model/user/User.java index 97ae61c..1a06df0 100644 --- a/api/src/main/java/dev/xhyrom/lighteco/api/model/user/User.java +++ b/api/src/main/java/dev/xhyrom/lighteco/api/model/user/User.java @@ -1,5 +1,6 @@ package dev.xhyrom.lighteco.api.model.user; +import dev.xhyrom.lighteco.api.model.currency.Currency; import org.checkerframework.checker.nullness.qual.NonNull; import java.util.UUID; @@ -18,4 +19,12 @@ public interface User { * @return the username */ @NonNull String getUsername(); + + /** + * Get the balance of this user for the specified currency. + * + * @param currency the currency + * @return the balance + */ + T getBalance(@NonNull Currency currency); } diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/platform/Platform.java b/api/src/main/java/dev/xhyrom/lighteco/api/platform/Platform.java index 6babce5..a2aeee2 100644 --- a/api/src/main/java/dev/xhyrom/lighteco/api/platform/Platform.java +++ b/api/src/main/java/dev/xhyrom/lighteco/api/platform/Platform.java @@ -19,5 +19,13 @@ public interface Platform { public String getName() { return displayName; } + + public boolean isLocal() { + return this == BUKKIT; + } + + public boolean isProxy() { + return this == VELOCITY || this == BUNGEECORD; + } } } diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoLoader.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoLoader.java index 44e70ab..2a409ed 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoLoader.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoLoader.java @@ -1,12 +1,15 @@ package dev.xhyrom.lighteco.bukkit; +import dev.xhyrom.lighteco.bukkit.test.TestPlugin; import org.bukkit.plugin.java.JavaPlugin; public class BukkitLightEcoLoader extends JavaPlugin { - private BukkitLightEcoPlugin plugin = new BukkitLightEcoPlugin(); + private final BukkitLightEcoPlugin plugin = new BukkitLightEcoPlugin(); @Override public void onEnable() { plugin.enable(); + + new TestPlugin(this); } } diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoPlugin.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoPlugin.java index 1e92cd6..a418463 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoPlugin.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoPlugin.java @@ -1,23 +1,29 @@ package dev.xhyrom.lighteco.bukkit; 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.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.managers.StandardUserManager; +import dev.xhyrom.lighteco.common.managers.user.StandardUserManager; import lombok.Getter; import org.bukkit.entity.Player; import org.checkerframework.checker.nullness.qual.NonNull; public class BukkitLightEcoPlugin extends AbstractLightEcoPlugin { @Getter - private UserManager userManager; + private StandardUserManager userManager; + @Getter + private StandardCurrencyManager currencyManager; @Getter private ContextManager contextManager; @Override public void setupManagers() { this.userManager = new StandardUserManager(this); + this.currencyManager = new StandardCurrencyManager(this); this.contextManager = new BukkitContextManager(); } diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/test/TestCurrency.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/test/TestCurrency.java new file mode 100644 index 0000000..306a32e --- /dev/null +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/test/TestCurrency.java @@ -0,0 +1,24 @@ +package dev.xhyrom.lighteco.bukkit.test; + +import dev.xhyrom.lighteco.api.model.currency.Currency; + +public class TestCurrency extends Currency { + public TestCurrency() { + super(Integer.class); + } + + @Override + public String getIdentifier() { + return "test"; + } + + @Override + public Type getType() { + return Type.LOCAL; + } + + @Override + public boolean isPayable() { + return true; + } +} diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/test/TestPlugin.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/test/TestPlugin.java new file mode 100644 index 0000000..e407293 --- /dev/null +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/test/TestPlugin.java @@ -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() + ")"); + }); + }); + } +} diff --git a/bukkit/src/main/resources/plugin.yml b/bukkit/src/main/resources/plugin.yml index 5cdd92e..580f79e 100644 --- a/bukkit/src/main/resources/plugin.yml +++ b/bukkit/src/main/resources/plugin.yml @@ -1,7 +1,7 @@ name: ${name} description: ${description} version: ${version} -main: dev.xhyrom.lighteco.bukkit.BukkitLightEcoPlugin +main: dev.xhyrom.lighteco.bukkit.BukkitLightEcoLoader author: ${author} api-version: 1.20 diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/api/LightEcoApi.java b/common/src/main/java/dev/xhyrom/lighteco/common/api/LightEcoApi.java index 6567e22..86d1cc5 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/api/LightEcoApi.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/api/LightEcoApi.java @@ -1,9 +1,12 @@ package dev.xhyrom.lighteco.common.api; 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.platform.Platform; 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.api.impl.ApiPlatform; import dev.xhyrom.lighteco.common.api.impl.ApiPlayerAdapter; @@ -13,13 +16,17 @@ public class LightEcoApi implements LightEco { private final LightEcoPlugin plugin; private final Platform platform; + private final UserManager userManager; + private final CurrencyManager currencyManager; private final PlayerAdapter playerAdapter; public LightEcoApi(LightEcoPlugin plugin) { this.plugin = 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 @@ -29,7 +36,12 @@ public class LightEcoApi implements LightEco { @Override public @NonNull UserManager getUserManager() { - return this.plugin.getUserManager(); + return this.userManager; + } + + @Override + public @NonNull CurrencyManager getCurrencyManager() { + return this.currencyManager; } @Override diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiAbstractManager.java b/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiAbstractManager.java new file mode 100644 index 0000000..026b2a7 --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiAbstractManager.java @@ -0,0 +1,13 @@ +package dev.xhyrom.lighteco.common.api.impl; + +import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; + +public abstract class ApiAbstractManager { + protected final LightEcoPlugin plugin; + protected final H handler; + + protected ApiAbstractManager(LightEcoPlugin plugin, H handler) { + this.plugin = plugin; + this.handler = handler; + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiCurrency.java b/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiCurrency.java new file mode 100644 index 0000000..f9c3bee --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiCurrency.java @@ -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 getTopUsers(int length) { + return this.handler.getTopUsers(length) + .stream() + .map(ApiUserManager::wrap) + .toList(); + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiCurrencyManager.java b/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiCurrencyManager.java new file mode 100644 index 0000000..9c13844 --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiCurrencyManager.java @@ -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 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> 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); + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiUser.java b/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiUser.java new file mode 100644 index 0000000..46e6636 --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiUser.java @@ -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 getBalance(@NonNull Currency currency) { + return this.handler.getBalance(currency); + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiUserManager.java b/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiUserManager.java new file mode 100644 index 0000000..837a409 --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiUserManager.java @@ -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 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 loadUser(@NonNull UUID uniqueId) { + return this.plugin.getStorage().loadUser(uniqueId); + } + + @Override + public @NonNull CompletableFuture 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); + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/managers/AbstractManager.java b/common/src/main/java/dev/xhyrom/lighteco/common/managers/AbstractManager.java new file mode 100644 index 0000000..2e7ea13 --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/managers/AbstractManager.java @@ -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 implements Manager { + public final Map map = new HashMap<>(); + + @Override + public Collection keys() { + return this.map.keySet(); + } + + @Override + public Collection 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 identifiers) { + identifiers.forEach(this::unload); + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/managers/Manager.java b/common/src/main/java/dev/xhyrom/lighteco/common/managers/Manager.java new file mode 100644 index 0000000..c352d65 --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/managers/Manager.java @@ -0,0 +1,21 @@ +package dev.xhyrom.lighteco.common.managers; + +import java.util.Collection; +import java.util.concurrent.CompletableFuture; + +public interface Manager { + T apply(I identifier); + + Collection keys(); + Collection values(); + + T getOrMake(I identifier); + + T getIfLoaded(I identifier); + + boolean isLoaded(I identifier); + + void unload(I identifier); + + void unload(Collection identifiers); +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/managers/StandardUserManager.java b/common/src/main/java/dev/xhyrom/lighteco/common/managers/StandardUserManager.java deleted file mode 100644 index a583aa5..0000000 --- a/common/src/main/java/dev/xhyrom/lighteco/common/managers/StandardUserManager.java +++ /dev/null @@ -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 loadUser(@NonNull UUID uniqueId) { - return null; - } - - @Override - public @NonNull CompletableFuture saveUser(User user) { - return null; - } - - @Override - public @Nullable User getUser(@NonNull UUID uniqueId) { - return null; - } - - @Override - public boolean isLoaded(@NonNull UUID uniqueId) { - return false; - } -} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/managers/currency/CurrencyManager.java b/common/src/main/java/dev/xhyrom/lighteco/common/managers/currency/CurrencyManager.java new file mode 100644 index 0000000..e962e7c --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/managers/currency/CurrencyManager.java @@ -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> { + @NonNull Collection> getRegisteredCurrencies(); + + void registerCurrency(@NonNull Currency currency); +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/managers/currency/StandardCurrencyManager.java b/common/src/main/java/dev/xhyrom/lighteco/common/managers/currency/StandardCurrencyManager.java new file mode 100644 index 0000000..5b8d49d --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/managers/currency/StandardCurrencyManager.java @@ -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> 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> 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); + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/managers/user/StandardUserManager.java b/common/src/main/java/dev/xhyrom/lighteco/common/managers/user/StandardUserManager.java new file mode 100644 index 0000000..e7e8600 --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/managers/user/StandardUserManager.java @@ -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 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 load() { + Set 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() { + + } +} \ No newline at end of file diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/managers/user/UserManager.java b/common/src/main/java/dev/xhyrom/lighteco/common/managers/user/UserManager.java new file mode 100644 index 0000000..f51c39d --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/managers/user/UserManager.java @@ -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 { + CompletableFuture load(); + + void invalidateCaches(); +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/model/currency/Currency.java b/common/src/main/java/dev/xhyrom/lighteco/common/model/currency/Currency.java new file mode 100644 index 0000000..31626d0 --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/model/currency/Currency.java @@ -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 { + private final LightEcoPlugin plugin; + + @Getter + private final dev.xhyrom.lighteco.api.model.currency.Currency proxy; + + public Currency(LightEcoPlugin plugin, dev.xhyrom.lighteco.api.model.currency.Currency 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 getValueType() { + return proxy.getValueType(); + } + + public boolean isPayable() { + return proxy.isPayable(); + } + + public List getTopUsers(int length) { + List users = new ArrayList<>(); + users.add(new User(plugin, new UUID(0, 0))); + + return users; + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/model/user/User.java b/common/src/main/java/dev/xhyrom/lighteco/common/model/user/User.java index ab609dc..28c22ab 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/model/user/User.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/model/user/User.java @@ -1,20 +1,30 @@ 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 org.checkerframework.checker.nullness.qual.NonNull; 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 private final UUID uniqueId; - public User(UUID uniqueId) { + public User(LightEcoPlugin plugin, UUID uniqueId) { + this.plugin = plugin; this.uniqueId = uniqueId; } - @Override - public @NonNull String getUsername() { + // TODO: finish + public T getBalance(@NonNull Currency currency) { + this.plugin.getCurrencyManager().getIfLoaded(currency.getIdentifier()); return null; } } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/AbstractLightEcoPlugin.java b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/AbstractLightEcoPlugin.java index 2c6d8e5..d821c69 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/AbstractLightEcoPlugin.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/AbstractLightEcoPlugin.java @@ -1,5 +1,7 @@ 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.StorageFactory; import lombok.Getter; @@ -7,11 +9,17 @@ import lombok.Getter; public abstract class AbstractLightEcoPlugin implements LightEcoPlugin { @Getter private Storage storage; + private LightEcoApi api; public final void enable() { StorageFactory factory = new StorageFactory(this); this.storage = factory.get(); + + this.setupManagers(); + + this.api = new LightEcoApi(this); + LightEcoProvider.set(this.api); } protected abstract void setupManagers(); diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/LightEcoPlugin.java b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/LightEcoPlugin.java index c10740e..a7568ca 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/LightEcoPlugin.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/LightEcoPlugin.java @@ -1,8 +1,9 @@ package dev.xhyrom.lighteco.common.plugin; 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.common.managers.currency.CurrencyManager; +import dev.xhyrom.lighteco.common.managers.user.UserManager; import dev.xhyrom.lighteco.common.storage.Storage; import org.checkerframework.checker.nullness.qual.NonNull; @@ -10,6 +11,7 @@ public interface LightEcoPlugin { Platform.@NonNull Type getPlatformType(); @NonNull UserManager getUserManager(); + @NonNull CurrencyManager getCurrencyManager(); @NonNull ContextManager getContextManager(); @NonNull Storage getStorage(); diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/memory/MemoryStorageProvider.java b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/memory/MemoryStorageProvider.java index f55e396..f03c898 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/memory/MemoryStorageProvider.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/memory/MemoryStorageProvider.java @@ -8,7 +8,7 @@ import java.util.HashMap; import java.util.UUID; public class MemoryStorageProvider implements StorageProvider { - private HashMap users = new HashMap<>(); + private final HashMap userDatabase = new HashMap<>(); @Override public @NonNull String[] getIdentifiers() { @@ -17,11 +17,11 @@ public class MemoryStorageProvider implements StorageProvider { @Override public @NonNull User loadUser(@NonNull UUID uniqueId) { - return users.get(uniqueId); + return userDatabase.get(uniqueId); } @Override public void saveUser(@NonNull User user) { - users.put(user.getUniqueId(), user); + userDatabase.put(user.getUniqueId(), user); } }