From 7571df9903b883e9e6630ac080a3247a3c74c53e Mon Sep 17 00:00:00 2001 From: RednedEpic Date: Wed, 16 Oct 2019 13:55:05 -0500 Subject: [PATCH] Attempt to reduce memory usage and fix not being disconnected from Java --- api/src/main/java/org/geysermc/api/Connector.java | 2 +- api/src/main/java/org/geysermc/api/Geyser.java | 2 +- .../java/org/geysermc/connector/GeyserConnector.java | 12 +++++++----- .../network/ConnectorServerEventHandler.java | 7 ++++++- .../connector/network/session/GeyserSession.java | 2 +- .../connector/network/session/UpstreamSession.java | 6 ++++++ .../connector/network/session/cache/EntityCache.java | 7 +++++++ 7 files changed, 29 insertions(+), 9 deletions(-) diff --git a/api/src/main/java/org/geysermc/api/Connector.java b/api/src/main/java/org/geysermc/api/Connector.java index e2826f1d..708372e3 100644 --- a/api/src/main/java/org/geysermc/api/Connector.java +++ b/api/src/main/java/org/geysermc/api/Connector.java @@ -67,7 +67,7 @@ public interface Connector { * * @return a collection of the connected players */ - Collection getConnectedPlayers(); + 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 37099339..2fc9c190 100644 --- a/api/src/main/java/org/geysermc/api/Geyser.java +++ b/api/src/main/java/org/geysermc/api/Geyser.java @@ -97,7 +97,7 @@ public class Geyser { * * @return a collection of the connected players */ - public static Collection getConnectedPlayers() { + 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 392afe52..430f0a9e 100644 --- a/connector/src/main/java/org/geysermc/connector/GeyserConnector.java +++ b/connector/src/main/java/org/geysermc/connector/GeyserConnector.java @@ -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; @@ -71,7 +72,7 @@ public class GeyserConnector implements Connector { public static final String NAME = "Geyser"; public static final String VERSION = "1.0-SNAPSHOT"; - private final Map players = new HashMap<>(); + private final Map players = new HashMap<>(); private static GeyserConnector instance; @@ -165,8 +166,9 @@ public class GeyserConnector implements Connector { logger.info(String.format("Done (%ss)! Run /help for help!", new DecimalFormat("#.###").format(completeTime))); } - public Collection getConnectedPlayers() { - return new ArrayList<>(players.values()); + @Override + public Collection getConnectedPlayers() { + return players.values(); } public void shutdown() { @@ -182,13 +184,13 @@ public class GeyserConnector implements Connector { System.exit(0); } - public void addPlayer(Player player) { + public void addPlayer(GeyserSession player) { players.put(player.getAuthenticationData().getName(), player); players.put(player.getAuthenticationData().getUUID(), player); players.put(player.getSocketAddress(), player); } - public void removePlayer(Player player) { + public void removePlayer(GeyserSession player) { players.remove(player.getAuthenticationData().getName()); players.remove(player.getAuthenticationData().getUUID()); players.remove(player.getSocketAddress()); diff --git a/connector/src/main/java/org/geysermc/connector/network/ConnectorServerEventHandler.java b/connector/src/main/java/org/geysermc/connector/network/ConnectorServerEventHandler.java index 2cc317b5..f9f12689 100644 --- a/connector/src/main/java/org/geysermc/connector/network/ConnectorServerEventHandler.java +++ b/connector/src/main/java/org/geysermc/connector/network/ConnectorServerEventHandler.java @@ -106,10 +106,15 @@ public class ConnectorServerEventHandler implements BedrockServerEventHandler { bedrockServerSession.addDisconnectHandler(disconnectReason -> { GeyserLogger.DEFAULT.info("Bedrock user with ip: " + bedrockServerSession.getAddress().getAddress() + " has disconnected for reason " + disconnectReason); - Player player = connector.getPlayers().get(bedrockServerSession.getAddress()); + GeyserSession player = connector.getPlayers().get(bedrockServerSession.getAddress()); if (player != null) { player.disconnect(disconnectReason.name()); connector.removePlayer(player); + + player.getEntityCache().clear(); + player.getInventoryCache().getInventories().clear(); + player.getWindowCache().getWindows().clear(); + player.getScoreboardCache().removeScoreboard(); } }); bedrockServerSession.setPacketCodec(GeyserConnector.BEDROCK_PACKET_CODEC); 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 b907187d..a4f57c08 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 @@ -133,7 +133,6 @@ public class GeyserSession implements Player { public void authenticate(String username) { authenticate(username, ""); - connector.addPlayer(this); } public void authenticate(String username, String password) { @@ -182,6 +181,7 @@ public class GeyserSession implements Player { }); downstream.getSession().connect(); + connector.addPlayer(this); } catch (RequestException ex) { ex.printStackTrace(); } diff --git a/connector/src/main/java/org/geysermc/connector/network/session/UpstreamSession.java b/connector/src/main/java/org/geysermc/connector/network/session/UpstreamSession.java index f8691bea..d14ff752 100644 --- a/connector/src/main/java/org/geysermc/connector/network/session/UpstreamSession.java +++ b/connector/src/main/java/org/geysermc/connector/network/session/UpstreamSession.java @@ -16,10 +16,16 @@ public class UpstreamSession { private boolean initialized = false; public void sendPacket(@NonNull BedrockPacket packet) { + if (isClosed()) + return; + session.sendPacket(packet); } public void sendPacketImmediately(@NonNull BedrockPacket packet) { + if (isClosed()) + return; + session.sendPacketImmediately(packet); } diff --git a/connector/src/main/java/org/geysermc/connector/network/session/cache/EntityCache.java b/connector/src/main/java/org/geysermc/connector/network/session/cache/EntityCache.java index 3bb702db..80b96500 100644 --- a/connector/src/main/java/org/geysermc/connector/network/session/cache/EntityCache.java +++ b/connector/src/main/java/org/geysermc/connector/network/session/cache/EntityCache.java @@ -117,4 +117,11 @@ public class EntityCache { public long removeBossBar(UUID uuid) { return bossbars.remove(uuid); } + + public void clear() { + entities = null; + entityIdTranslations = null; + playerEntities = null; + bossbars = null; + } }