From 7c0fcadd4fd2ac3fd633df1c489f7e99f87c0b23 Mon Sep 17 00:00:00 2001 From: thiccaxe <51303986+thiccaxe@users.noreply.github.com> Date: Sun, 4 Apr 2021 16:26:58 -0700 Subject: [PATCH] Add HandshakeEvent --- .../server/event/server/HandshakeEvent.java | 51 +++++++++++++++++++ .../client/handshake/HandshakePacket.java | 6 ++- src/test/java/demo/Main.java | 3 +- 3 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 src/main/java/net/minestom/server/event/server/HandshakeEvent.java diff --git a/src/main/java/net/minestom/server/event/server/HandshakeEvent.java b/src/main/java/net/minestom/server/event/server/HandshakeEvent.java new file mode 100644 index 000000000..4bbed4391 --- /dev/null +++ b/src/main/java/net/minestom/server/event/server/HandshakeEvent.java @@ -0,0 +1,51 @@ +package net.minestom.server.event.server; + +import net.minestom.server.event.Event; +import net.minestom.server.network.player.PlayerConnection; +import net.minestom.server.ping.ResponseData; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + + +/** + * Called by a HandshakePacket. + */ +public class HandshakeEvent extends Event { + + private final String serverAddress; + private final int serverPort; + private final int protocolVersion; + private final PlayerConnection connection; + + public HandshakeEvent(@Nullable String serverAddress, int serverPort, int protocolVersion, @NotNull PlayerConnection connection) { + this.serverAddress = serverAddress; + this.serverPort = serverPort; + this.protocolVersion = protocolVersion; + this.connection = connection; + } + + /** + * @return the server address a client used to connect to the server + */ + public @Nullable String getServerAddress() { + return serverAddress; + } + /** + * @return the server port a client used to connect to the server + */ + public int getServerPort() { + return serverPort; + } + /** + * @return the protocol version a client used when connecting to the server + */ + public int getProtocolVersion() { + return protocolVersion; + } + /** + * @return the player connection + */ + public @NotNull PlayerConnection getConnection() { + return connection; + } +} diff --git a/src/main/java/net/minestom/server/network/packet/client/handshake/HandshakePacket.java b/src/main/java/net/minestom/server/network/packet/client/handshake/HandshakePacket.java index efe60b9b8..d0c14820d 100644 --- a/src/main/java/net/minestom/server/network/packet/client/handshake/HandshakePacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/handshake/HandshakePacket.java @@ -4,6 +4,7 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import net.minestom.server.MinecraftServer; import net.minestom.server.entity.PlayerSkin; +import net.minestom.server.event.server.HandshakeEvent; import net.minestom.server.extras.bungee.BungeeCordProxy; import net.minestom.server.network.ConnectionState; import net.minestom.server.network.packet.client.ClientPreplayPacket; @@ -81,20 +82,21 @@ public class HandshakePacket implements ClientPreplayPacket { nettyPlayerConnection.UNSAFE_setBungeeUuid(playerUuid); nettyPlayerConnection.UNSAFE_setBungeeSkin(playerSkin); - } else { nettyPlayerConnection.sendPacket(new LoginDisconnectPacket(INVALID_BUNGEE_FORWARDING)); nettyPlayerConnection.disconnect(); return; } } else { - // Happen when a client ping the server, ignore + // Happen when a client ping the server, trigger HandshakeEvent + MinecraftServer.getGlobalEventHandler().callEvent(HandshakeEvent.class, new HandshakeEvent(null, serverPort, protocolVersion, connection)); return; } } switch (nextState) { case 1: + MinecraftServer.getGlobalEventHandler().callEvent(HandshakeEvent.class, new HandshakeEvent(serverAddress, serverPort, protocolVersion, connection)); connection.setConnectionState(ConnectionState.STATUS); break; case 2: diff --git a/src/test/java/demo/Main.java b/src/test/java/demo/Main.java index 600bd8b4e..b3ed5b852 100644 --- a/src/test/java/demo/Main.java +++ b/src/test/java/demo/Main.java @@ -8,6 +8,7 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import net.minestom.server.MinecraftServer; import net.minestom.server.command.CommandManager; +import net.minestom.server.event.server.HandshakeEvent; import net.minestom.server.event.server.StatusRequestEvent; import net.minestom.server.extras.optifine.OptifineSupport; import net.minestom.server.instance.block.BlockManager; @@ -70,7 +71,7 @@ public class Main { responseData.addPlayer("Use " + (char)0x00a7 + "7section characters", UUID.randomUUID()); responseData.addPlayer((char)0x00a7 + "7" + (char)0x00a7 + "ofor formatting" + (char)0x00a7 + "r: (" + (char)0x00a7 + "6char" + (char)0x00a7 + "r)" + (char)0x00a7 + "90x00a7", UUID.randomUUID()); - + }); PlayerInit.init();