Add HandshakeEvent

This commit is contained in:
thiccaxe 2021-04-04 16:26:58 -07:00
parent 4f8e94dcf2
commit 7c0fcadd4f
3 changed files with 57 additions and 3 deletions

View File

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

View File

@ -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:

View File

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