Using a default implementation of IPlayerManager and IOffPlayer with minimal functionnalities, for standalone apps (discord bots, ...)

This commit is contained in:
Marc Baloup 2022-05-24 02:27:45 +02:00
parent e8c93a0676
commit 54ffb4eddd
Signed by: marcbal
GPG Key ID: BBC0FE3ABC30B893
3 changed files with 91 additions and 4 deletions

View File

@ -22,7 +22,26 @@ import net.md_5.bungee.api.chat.BaseComponent;
public abstract class IPlayerManager<OP extends IOnlinePlayer, OF extends IOffPlayer> { public abstract class IPlayerManager<OP extends IOnlinePlayer, OF extends IOffPlayer> {
private static IPlayerManager<?, ?> instance; 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<UUID, OP> onlinePlayers = Collections.synchronizedMap(new HashMap<>()); private Map<UUID, OP> onlinePlayers = Collections.synchronizedMap(new HashMap<>());
@ -33,9 +52,7 @@ public abstract class IPlayerManager<OP extends IOnlinePlayer, OF extends IOffPl
public IPlayerManager() throws DBInitTableException { public IPlayerManager() throws DBInitTableException {
synchronized (IPlayerManager.class) { setInstance(this);
instance = this;
}
DB.initTable(SQLPlayer.class); DB.initTable(SQLPlayer.class);
DB.initTable(SQLPlayerConfig.class); DB.initTable(SQLPlayerConfig.class);

View File

@ -0,0 +1,38 @@
package fr.pandacube.lib.core.players;
import java.util.UUID;
/* package */ class StandaloneOffPlayer implements IOffPlayer {
private final UUID uniqueId;
public StandaloneOffPlayer(UUID id) {
if (id == null) throw new IllegalArgumentException("id cannot be null");
uniqueId = id;
}
@Override
public UUID getUniqueId() {
return uniqueId;
}
@Override
public boolean isOnline() {
return false;
}
@Override
public IOnlinePlayer getOnlineInstance() {
return null;
}
private String displayName = null;
@Override
public String getDisplayName() {
if (displayName == null)
displayName = getDisplayNameFromPermissionSystem();
return displayName;
}
}

View File

@ -0,0 +1,32 @@
package fr.pandacube.lib.core.players;
import java.util.UUID;
import fr.pandacube.lib.core.chat.Chat;
import fr.pandacube.lib.core.db.DBInitTableException;
import fr.pandacube.lib.core.util.Log;
import net.kyori.adventure.text.Component;
/**
* A standalone player manager, using an implementation of {@link IPlayerManager}
* that does not manage online players. This is used to ease access to players data
* on standalone applications (web api, discord bot, ...)
*
*/
/* package */ class StandalonePlayerManager extends IPlayerManager<IOnlinePlayer, StandaloneOffPlayer> {
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());
}
}