From 54ffb4edddd8af64d415ca95c9487eb1a476cef9 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Tue, 24 May 2022 02:27:45 +0200 Subject: [PATCH] Using a default implementation of IPlayerManager and IOffPlayer with minimal functionnalities, for standalone apps (discord bots, ...) --- .../lib/core/players/IPlayerManager.java | 25 ++++++++++-- .../lib/core/players/StandaloneOffPlayer.java | 38 +++++++++++++++++++ .../core/players/StandalonePlayerManager.java | 32 ++++++++++++++++ 3 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 Core/src/main/java/fr/pandacube/lib/core/players/StandaloneOffPlayer.java create mode 100644 Core/src/main/java/fr/pandacube/lib/core/players/StandalonePlayerManager.java diff --git a/Core/src/main/java/fr/pandacube/lib/core/players/IPlayerManager.java b/Core/src/main/java/fr/pandacube/lib/core/players/IPlayerManager.java index 2fdc074..296629d 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/players/IPlayerManager.java +++ b/Core/src/main/java/fr/pandacube/lib/core/players/IPlayerManager.java @@ -22,7 +22,26 @@ import net.md_5.bungee.api.chat.BaseComponent; public abstract class IPlayerManager { private static IPlayerManager instance; - public static synchronized IPlayerManager getInstance() { return instance; } + + public static synchronized IPlayerManager getInstance() { + if (instance == null) { + try { + new StandalonePlayerManager(); // will set the instance value itself (see IPlayerManager constructor) + } catch (DBInitTableException e) { + throw new RuntimeException(e); + } + } + return instance; + } + + private static synchronized void setInstance(IPlayerManager newInstance) { + if (instance != null && !(instance instanceof StandalonePlayerManager)) { + throw new IllegalStateException("only one instance of playerManager is possible"); + } + instance = newInstance; + } + + private Map onlinePlayers = Collections.synchronizedMap(new HashMap<>()); @@ -33,9 +52,7 @@ public abstract class IPlayerManager { + + public StandalonePlayerManager() throws DBInitTableException { + super(); + } + + @Override + protected StandaloneOffPlayer newOffPlayerInstance(UUID p) { + return new StandaloneOffPlayer(p); + } + + @Override + protected void sendMessageToConsole(Component message) { + Log.info(Chat.chatComponent(message).getLegacyText()); + } + +}