Attempt to reduce memory usage and fix not being disconnected from Java

This commit is contained in:
RednedEpic 2019-10-16 13:55:05 -05:00 committed by RednedEpic
parent 20dda985bc
commit 7571df9903
7 changed files with 29 additions and 9 deletions

View file

@ -67,7 +67,7 @@ public interface Connector {
*
* @return a collection of the connected players
*/
Collection<Player> getConnectedPlayers();
Collection<? extends Player> getConnectedPlayers();
/**
* Shuts down the connector

View file

@ -97,7 +97,7 @@ public class Geyser {
*
* @return a collection of the connected players
*/
public static Collection<Player> getConnectedPlayers() {
public static Collection<? extends Player> getConnectedPlayers() {
return connector.getConnectedPlayers();
}
}

View file

@ -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<Object, Player> players = new HashMap<>();
private final Map<Object, GeyserSession> 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<Player> getConnectedPlayers() {
return new ArrayList<>(players.values());
@Override
public Collection<? extends Player> 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());

View file

@ -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);

View file

@ -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();
}

View file

@ -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);
}

View file

@ -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;
}
}