From ba21acc9c8655d500e2562b6de6f6ebc0d4fb7a6 Mon Sep 17 00:00:00 2001 From: RednedEpic Date: Thu, 8 Aug 2019 20:42:55 -0500 Subject: [PATCH] Cleanup adding/removing players --- .../main/java/org/geysermc/api/Connector.java | 8 ++++ .../main/java/org/geysermc/api/Geyser.java | 23 ++++------- .../geysermc/connector/GeyserConnector.java | 41 +++++++++++-------- .../network/session/GeyserSession.java | 4 +- 4 files changed, 42 insertions(+), 34 deletions(-) diff --git a/api/src/main/java/org/geysermc/api/Connector.java b/api/src/main/java/org/geysermc/api/Connector.java index b305a197d..e2826f1d3 100644 --- a/api/src/main/java/org/geysermc/api/Connector.java +++ b/api/src/main/java/org/geysermc/api/Connector.java @@ -29,6 +29,7 @@ import org.geysermc.api.command.CommandMap; import org.geysermc.api.logger.Logger; import org.geysermc.api.plugin.PluginManager; +import java.util.Collection; import java.util.concurrent.ScheduledExecutorService; public interface Connector { @@ -61,6 +62,13 @@ public interface Connector { */ ScheduledExecutorService getGeneralThreadPool(); + /** + * Returns a collection of the connected players + * + * @return a collection of the connected players + */ + Collection getConnectedPlayers(); + /** * Shuts down the connector */ diff --git a/api/src/main/java/org/geysermc/api/Geyser.java b/api/src/main/java/org/geysermc/api/Geyser.java index 3edfdb737..4bb13a415 100644 --- a/api/src/main/java/org/geysermc/api/Geyser.java +++ b/api/src/main/java/org/geysermc/api/Geyser.java @@ -29,6 +29,7 @@ import org.geysermc.api.command.CommandMap; import org.geysermc.api.logger.Logger; import org.geysermc.api.plugin.PluginManager; +import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Objects; @@ -36,8 +37,6 @@ import java.util.UUID; import java.util.concurrent.ScheduledExecutorService; public class Geyser { - private static final Map players = new HashMap<>(); - private static Connector connector; @@ -94,23 +93,15 @@ public class Geyser { * @return the amount of online players */ public static int getPlayerCount() { - return players.size(); + return connector.getConnectedPlayers().size(); } /** - * Add a player - * @param p The player to add + * Returns a collection of the connected players + * + * @return a collection of the connected players */ - public static void addPlayer(Player p) { - players.put(Objects.requireNonNull(Objects.requireNonNull(Objects.requireNonNull(p, "player").getAuthenticationData(), "authData").getUUID(), "player uuid"), p); - players.put(Objects.requireNonNull(Objects.requireNonNull(Objects.requireNonNull(p, "player").getAuthenticationData(), "authData").getName(), "player name"), p); - } - - /** - * Disconnect a player - * @param p The player to remove/disconnect - */ - public static void removePlayer(Player p) { - players.remove(Objects.requireNonNull(Objects.requireNonNull(Objects.requireNonNull(p, "player").getAuthenticationData(), "authData").getName(), "player name")); + public static Collection getConnectedPlayers() { + return connector.getConnectedPlayers(); } } diff --git a/connector/src/main/java/org/geysermc/connector/GeyserConnector.java b/connector/src/main/java/org/geysermc/connector/GeyserConnector.java index 04f590647..45fbca0d3 100644 --- a/connector/src/main/java/org/geysermc/connector/GeyserConnector.java +++ b/connector/src/main/java/org/geysermc/connector/GeyserConnector.java @@ -29,10 +29,10 @@ import com.nukkitx.protocol.bedrock.BedrockPacketCodec; import com.nukkitx.protocol.bedrock.BedrockServer; import com.nukkitx.protocol.bedrock.v361.Bedrock_v361; import lombok.Getter; -import org.apache.commons.io.IOUtils; import org.fusesource.jansi.AnsiConsole; import org.geysermc.api.Connector; import org.geysermc.api.Geyser; +import org.geysermc.api.Player; import org.geysermc.api.command.CommandMap; import org.geysermc.api.logger.Logger; import org.geysermc.api.plugin.Plugin; @@ -43,6 +43,7 @@ import org.geysermc.connector.console.GeyserLogger; import org.geysermc.connector.metrics.Metrics; import org.geysermc.connector.network.ConnectorServerEventHandler; import org.geysermc.connector.network.remote.RemoteJavaServer; +import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.translators.TranslatorsInit; import org.geysermc.connector.plugin.GeyserPluginLoader; import org.geysermc.connector.plugin.GeyserPluginManager; @@ -53,12 +54,16 @@ import org.geysermc.connector.utils.Toolbox; import java.io.File; import java.io.IOException; import java.net.InetSocketAddress; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; import java.util.Random; import java.util.UUID; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; +@Getter public class GeyserConnector implements Connector { public static final BedrockPacketCodec BEDROCK_PACKET_CODEC = Bedrock_v361.V361_CODEC; @@ -66,34 +71,25 @@ public class GeyserConnector implements Connector { private static final String NAME = "Geyser"; private static final String VERSION = "1.0-SNAPSHOT"; + private final Map players = new HashMap<>(); + private static GeyserConnector instance; - @Getter private RemoteJavaServer remoteServer; - @Getter private Logger logger; - @Getter private CommandMap commandMap; - @Getter private GeyserConfiguration config; - - @Getter private GeyserPluginManager pluginManager; - @Getter private boolean shuttingDown = false; - @Getter private final ScheduledExecutorService generalThreadPool; - - @Getter private PingPassthroughThread passthroughThread; - @Getter - private Metrics METRICS; + private Metrics metrics; public static void main(String[] args) { instance = new GeyserConnector(); @@ -127,9 +123,9 @@ public class GeyserConnector implements Connector { shutdown(); } - METRICS = new Metrics("GeyserMC", instance.getConfig().getUUID(), true, java.util.logging.Logger.getLogger("")); + metrics = new Metrics("GeyserMC", instance.getConfig().getUUID(), true, java.util.logging.Logger.getLogger("")); - addMetrics(METRICS); + addMetrics(metrics); logger.setDebug(config.isDebugMode()); @@ -161,6 +157,10 @@ public class GeyserConnector implements Connector { }).join(); } + public Collection getConnectedPlayers() { + return players.values(); + } + public void shutdown() { logger.info("Shutting down connector."); for (Plugin plugin : pluginManager.getPlugins()) { @@ -174,9 +174,18 @@ public class GeyserConnector implements Connector { System.exit(0); } + public void addPlayer(Player player) { + players.put(player.getAuthenticationData().getName(), player); + players.put(player.getAuthenticationData().getUUID(), player); + } + + public void removePlayer(Player player) { + players.remove(player.getAuthenticationData().getName()); + players.remove(player.getAuthenticationData().getUUID()); + } + private static void addMetrics(Metrics m) { m.addCustomChart(new Metrics.SingleLineChart("servers", () -> 3 + new Random().nextInt(4))); - m.addCustomChart(new Metrics.SingleLineChart("players", () -> 5 + new Random().nextInt(7))); } } diff --git a/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java b/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java index 913b67ce5..231fe7ef5 100644 --- a/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java +++ b/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java @@ -127,7 +127,7 @@ public class GeyserSession implements PlayerSession, Player { public void authenticate(String username) { authenticate(username, ""); - Geyser.addPlayer(this); + connector.addPlayer(this); } public void authenticate(String username, String password) { @@ -206,7 +206,7 @@ public class GeyserSession implements PlayerSession, Player { @Override public void onDisconnect(String reason) { downstream.getSession().disconnect("Disconnected from server. Reason: " + reason); - Geyser.removePlayer(this); + connector.removePlayer(this); } public void setAuthenticationData(AuthData authData) {