From 24f9651cc6e6279e3a2b709c0cf6766c2032fb4a Mon Sep 17 00:00:00 2001 From: RednedEpic Date: Mon, 6 Jul 2020 20:11:34 -0500 Subject: [PATCH] Convert map of players to list (may address #833) --- .../spigot/world/GeyserSpigotBlockPlaceListener.java | 2 +- .../platform/standalone/gui/GeyserStandaloneGUI.java | 7 +++---- .../java/org/geysermc/connector/GeyserConnector.java | 12 ++++++------ .../connector/command/defaults/ListCommand.java | 4 ++-- .../connector/command/defaults/OffhandCommand.java | 2 +- .../connector/command/defaults/ReloadCommand.java | 2 +- .../geysermc/connector/entity/FishingHookEntity.java | 2 +- .../network/ConnectorServerEventHandler.java | 9 --------- .../connector/network/session/GeyserSession.java | 9 ++++++++- 9 files changed, 23 insertions(+), 26 deletions(-) diff --git a/bootstrap/spigot/src/main/java/org/geysermc/platform/spigot/world/GeyserSpigotBlockPlaceListener.java b/bootstrap/spigot/src/main/java/org/geysermc/platform/spigot/world/GeyserSpigotBlockPlaceListener.java index f17a97e37..4fe93d459 100644 --- a/bootstrap/spigot/src/main/java/org/geysermc/platform/spigot/world/GeyserSpigotBlockPlaceListener.java +++ b/bootstrap/spigot/src/main/java/org/geysermc/platform/spigot/world/GeyserSpigotBlockPlaceListener.java @@ -47,7 +47,7 @@ public class GeyserSpigotBlockPlaceListener implements Listener { @EventHandler public void place(final BlockPlaceEvent event) { - for (GeyserSession session : connector.getPlayers().values()) { + for (GeyserSession session : connector.getPlayers()) { if (event.getPlayer() == Bukkit.getPlayer(session.getPlayerEntity().getUsername())) { LevelSoundEventPacket placeBlockSoundPacket = new LevelSoundEventPacket(); placeBlockSoundPacket.setSound(SoundEvent.PLACE); diff --git a/bootstrap/standalone/src/main/java/org/geysermc/platform/standalone/gui/GeyserStandaloneGUI.java b/bootstrap/standalone/src/main/java/org/geysermc/platform/standalone/gui/GeyserStandaloneGUI.java index 9be0e1c6a..0c0ec1127 100644 --- a/bootstrap/standalone/src/main/java/org/geysermc/platform/standalone/gui/GeyserStandaloneGUI.java +++ b/bootstrap/standalone/src/main/java/org/geysermc/platform/standalone/gui/GeyserStandaloneGUI.java @@ -42,7 +42,6 @@ import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.io.PrintStream; -import java.net.InetSocketAddress; import java.net.URL; import java.util.ArrayList; import java.util.List; @@ -292,10 +291,10 @@ public class GeyserStandaloneGUI { // Update player table playerTableModel.getDataVector().removeAllElements(); - for (Map.Entry player : GeyserConnector.getInstance().getPlayers().entrySet()) { + for (GeyserSession player : GeyserConnector.getInstance().getPlayers()) { Vector row = new Vector(); - row.add(player.getKey().getHostName()); - row.add(player.getValue().getPlayerEntity().getUsername()); + row.add(player.getSocketAddress().getHostName()); + row.add(player.getPlayerEntity().getUsername()); playerTableModel.addRow(row); } diff --git a/connector/src/main/java/org/geysermc/connector/GeyserConnector.java b/connector/src/main/java/org/geysermc/connector/GeyserConnector.java index 298f5bc82..675c2b9a2 100644 --- a/connector/src/main/java/org/geysermc/connector/GeyserConnector.java +++ b/connector/src/main/java/org/geysermc/connector/GeyserConnector.java @@ -59,8 +59,8 @@ import org.geysermc.connector.utils.LocaleUtils; import java.net.InetSocketAddress; import java.text.DecimalFormat; -import java.util.HashMap; -import java.util.Map; +import java.util.ArrayList; +import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -76,7 +76,7 @@ public class GeyserConnector { public static final String NAME = "Geyser"; public static final String VERSION = "DEV"; // A fallback for running in IDEs - private final Map players = new HashMap<>(); + private final List players = new ArrayList<>(); private static GeyserConnector instance; @@ -185,7 +185,7 @@ public class GeyserConnector { if (players.size() >= 1) { bootstrap.getGeyserLogger().info(LanguageUtils.getLocaleStringLog("geyser.core.shutdown.kick.log", players.size())); - for (GeyserSession playerSession : players.values()) { + for (GeyserSession playerSession : players) { playerSession.disconnect(LanguageUtils.getPlayerLocaleString("geyser.core.shutdown.kick.message", playerSession.getClientData().getLanguageCode())); } @@ -227,11 +227,11 @@ public class GeyserConnector { } public void addPlayer(GeyserSession player) { - players.put(player.getSocketAddress(), player); + players.add(player); } public void removePlayer(GeyserSession player) { - players.remove(player.getSocketAddress()); + players.remove(player); } public static GeyserConnector start(PlatformType platformType, GeyserBootstrap bootstrap) { diff --git a/connector/src/main/java/org/geysermc/connector/command/defaults/ListCommand.java b/connector/src/main/java/org/geysermc/connector/command/defaults/ListCommand.java index 0de73a5d1..3c78c5540 100644 --- a/connector/src/main/java/org/geysermc/connector/command/defaults/ListCommand.java +++ b/connector/src/main/java/org/geysermc/connector/command/defaults/ListCommand.java @@ -47,9 +47,9 @@ public class ListCommand extends GeyserCommand { public void execute(CommandSender sender, String[] args) { String message = ""; if (sender instanceof GeyserSession) { - message = LanguageUtils.getPlayerLocaleString("geyser.commands.list.message", ((GeyserSession) sender).getClientData().getLanguageCode(), connector.getPlayers().size(), connector.getPlayers().values().stream().map(GeyserSession::getName).collect(Collectors.joining(" "))); + message = LanguageUtils.getPlayerLocaleString("geyser.commands.list.message", ((GeyserSession) sender).getClientData().getLanguageCode(), connector.getPlayers().size(), connector.getPlayers().stream().map(GeyserSession::getName).collect(Collectors.joining(" "))); } else { - message = LanguageUtils.getLocaleStringLog("geyser.commands.list.message", connector.getPlayers().size(), connector.getPlayers().values().stream().map(GeyserSession::getName).collect(Collectors.joining(" "))); + message = LanguageUtils.getLocaleStringLog("geyser.commands.list.message", connector.getPlayers().size(), connector.getPlayers().stream().map(GeyserSession::getName).collect(Collectors.joining(" "))); } sender.sendMessage(message); diff --git a/connector/src/main/java/org/geysermc/connector/command/defaults/OffhandCommand.java b/connector/src/main/java/org/geysermc/connector/command/defaults/OffhandCommand.java index a49506b0f..b1b601328 100644 --- a/connector/src/main/java/org/geysermc/connector/command/defaults/OffhandCommand.java +++ b/connector/src/main/java/org/geysermc/connector/command/defaults/OffhandCommand.java @@ -59,7 +59,7 @@ public class OffhandCommand extends GeyserCommand { return; } // Needed for Bukkit - sender is not an instance of GeyserSession - for (GeyserSession session : connector.getPlayers().values()) { + for (GeyserSession session : connector.getPlayers()) { if (sender.getName().equals(session.getPlayerEntity().getUsername())) { ClientPlayerActionPacket releaseItemPacket = new ClientPlayerActionPacket(PlayerAction.SWAP_HANDS, new Position(0,0,0), BlockFace.DOWN); diff --git a/connector/src/main/java/org/geysermc/connector/command/defaults/ReloadCommand.java b/connector/src/main/java/org/geysermc/connector/command/defaults/ReloadCommand.java index d8bf8583b..9c24fa2be 100644 --- a/connector/src/main/java/org/geysermc/connector/command/defaults/ReloadCommand.java +++ b/connector/src/main/java/org/geysermc/connector/command/defaults/ReloadCommand.java @@ -56,7 +56,7 @@ public class ReloadCommand extends GeyserCommand { sender.sendMessage(message); - for (GeyserSession session : connector.getPlayers().values()) { + for (GeyserSession session : connector.getPlayers()) { session.disconnect(LanguageUtils.getPlayerLocaleString("geyser.commands.reload.kick", session.getClientData().getLanguageCode())); } connector.reload(); diff --git a/connector/src/main/java/org/geysermc/connector/entity/FishingHookEntity.java b/connector/src/main/java/org/geysermc/connector/entity/FishingHookEntity.java index cc7d749d9..4d0eb202f 100644 --- a/connector/src/main/java/org/geysermc/connector/entity/FishingHookEntity.java +++ b/connector/src/main/java/org/geysermc/connector/entity/FishingHookEntity.java @@ -37,7 +37,7 @@ public class FishingHookEntity extends Entity { public FishingHookEntity(long entityId, long geyserId, EntityType entityType, Vector3f position, Vector3f motion, Vector3f rotation, ProjectileData data) { super(entityId, geyserId, entityType, position, motion, rotation); - for (GeyserSession session : GeyserConnector.getInstance().getPlayers().values()) { + for (GeyserSession session : GeyserConnector.getInstance().getPlayers()) { Entity entity = session.getEntityCache().getEntityByJavaId(data.getOwnerId()); if (entity == null && session.getPlayerEntity().getEntityId() == data.getOwnerId()) { entity = session.getPlayerEntity(); 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 ad5cb42aa..6ca9063c2 100644 --- a/connector/src/main/java/org/geysermc/connector/network/ConnectorServerEventHandler.java +++ b/connector/src/main/java/org/geysermc/connector/network/ConnectorServerEventHandler.java @@ -108,15 +108,6 @@ public class ConnectorServerEventHandler implements BedrockServerEventHandler { public void onSessionCreation(BedrockServerSession bedrockServerSession) { bedrockServerSession.setLogging(true); bedrockServerSession.setPacketHandler(new UpstreamPacketHandler(connector, new GeyserSession(connector, bedrockServerSession))); - bedrockServerSession.addDisconnectHandler(disconnectReason -> { - connector.getLogger().info(LanguageUtils.getLocaleStringLog("geyser.network.disconnect", bedrockServerSession.getAddress().getAddress(), disconnectReason)); - - GeyserSession player = connector.getPlayers().get(bedrockServerSession.getAddress()); - if (player != null) { - player.disconnect(disconnectReason.name()); - connector.removePlayer(player); - } - }); 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 6b51c8921..0aa0ceaae 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 @@ -210,6 +210,13 @@ public class GeyserSession implements CommandSender { this.loggedIn = false; this.inventoryCache.getInventories().put(0, inventory); + + bedrockServerSession.addDisconnectHandler(disconnectReason -> { + connector.getLogger().info(LanguageUtils.getLocaleStringLog("geyser.network.disconnect", bedrockServerSession.getAddress().getAddress(), disconnectReason)); + + disconnect(disconnectReason.name()); + connector.removePlayer(this); + }); } public void connect(RemoteServer remoteServer) { @@ -428,7 +435,7 @@ public class GeyserSession implements CommandSender { downstream.getSession().disconnect(reason); } if (upstream != null && !upstream.isClosed()) { - connector.getPlayers().remove(this.upstream.getAddress()); + connector.getPlayers().remove(this); upstream.disconnect(reason); } }