From 0cd5f0894959c42e0c9d1276dbca8ccae0e06a1f Mon Sep 17 00:00:00 2001 From: xHyroM Date: Thu, 3 Aug 2023 22:29:54 +0200 Subject: [PATCH] feat: register api on platform, load managers, listeners in enable(), load at startup register api on platform - bukkit with ServicesManager --- .../lighteco/bukkit/BukkitLightEcoBootstrap.java | 4 +--- .../lighteco/bukkit/BukkitLightEcoPlugin.java | 7 +++++++ bukkit/src/main/resources/plugin.yml | 1 + .../dev/xhyrom/lighteco/common/cache/TypedMap.java | 4 ++++ .../common/managers/user/StandardUserManager.java | 2 +- .../xhyrom/lighteco/common/model/user/User.java | 4 ++++ .../common/plugin/AbstractLightEcoPlugin.java | 14 ++++++++------ 7 files changed, 26 insertions(+), 10 deletions(-) diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoBootstrap.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoBootstrap.java index 84412d7..5b08cc5 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoBootstrap.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoBootstrap.java @@ -27,9 +27,7 @@ public class BukkitLightEcoBootstrap implements LightEcoBootstrap, LoaderBootstr } @Override - public void onLoad() { - plugin.load(); - } + public void onLoad() {} @Override public void onEnable() { 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 cbd9768..beccf81 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoPlugin.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoPlugin.java @@ -1,5 +1,6 @@ package dev.xhyrom.lighteco.bukkit; +import dev.xhyrom.lighteco.api.LightEco; import dev.xhyrom.lighteco.api.managers.ContextManager; import dev.xhyrom.lighteco.api.platform.Platform; import dev.xhyrom.lighteco.bukkit.listeners.BukkitConnectionListener; @@ -9,6 +10,7 @@ import dev.xhyrom.lighteco.common.plugin.AbstractLightEcoPlugin; import dev.xhyrom.lighteco.common.managers.user.StandardUserManager; import lombok.Getter; import org.bukkit.entity.Player; +import org.bukkit.plugin.ServicePriority; import org.checkerframework.checker.nullness.qual.NonNull; public class BukkitLightEcoPlugin extends AbstractLightEcoPlugin { @@ -38,6 +40,11 @@ public class BukkitLightEcoPlugin extends AbstractLightEcoPlugin { this.contextManager = new BukkitContextManager(); } + @Override + protected void registerApiOnPlatform(LightEco api) { + this.getBootstrap().getServer().getServicesManager().register(LightEco.class, api, this.getBootstrap().getLoader(), ServicePriority.Normal); + } + @Override public Platform.@NonNull Type getPlatformType() { return Platform.Type.BUKKIT; diff --git a/bukkit/src/main/resources/plugin.yml b/bukkit/src/main/resources/plugin.yml index 580f79e..e27365b 100644 --- a/bukkit/src/main/resources/plugin.yml +++ b/bukkit/src/main/resources/plugin.yml @@ -4,6 +4,7 @@ version: ${version} main: dev.xhyrom.lighteco.bukkit.BukkitLightEcoLoader author: ${author} api-version: 1.20 +load: STARTUP softdepend: - Vault \ No newline at end of file diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/cache/TypedMap.java b/common/src/main/java/dev/xhyrom/lighteco/common/cache/TypedMap.java index d11d58f..50fe332 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/cache/TypedMap.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/cache/TypedMap.java @@ -17,4 +17,8 @@ public class TypedMap { public void put(T key, V value) { map.put(key, value); } + + public void clear() { + map.clear(); + } } 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 index ad00a6e..b394665 100644 --- 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 @@ -33,6 +33,6 @@ public class StandardUserManager extends AbstractManager implements @Override public void invalidateCaches() { - + values().forEach(User::invalidateCaches); } } \ No newline at end of file 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 63495b6..0e71cb6 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 @@ -33,4 +33,8 @@ public class User { public void setBalance(@NonNull Currency currency, @NonNull T balance) { balances.put(currency, balance); } + + public void invalidateCaches() { + balances.clear(); + } } 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 f9b68d0..ea60552 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,6 @@ package dev.xhyrom.lighteco.common.plugin; +import dev.xhyrom.lighteco.api.LightEco; import dev.xhyrom.lighteco.api.LightEcoProvider; import dev.xhyrom.lighteco.common.api.LightEcoApi; import dev.xhyrom.lighteco.common.storage.Storage; @@ -11,24 +12,25 @@ public abstract class AbstractLightEcoPlugin implements LightEcoPlugin { private Storage storage; private LightEcoApi api; - public final void load() { + public final void enable() { // setup storage StorageFactory factory = new StorageFactory(this); this.storage = factory.get(); + // register listeners + this.registerListeners(); + // setup managers this.setupManagers(); // register api this.api = new LightEcoApi(this); LightEcoProvider.set(this.api); - } - - public final void enable() { - // register listeners - this.registerListeners(); + this.registerApiOnPlatform(this.api); } protected abstract void registerListeners(); + protected abstract void setupManagers(); + protected abstract void registerApiOnPlatform(LightEco api); }