mirror of
https://github.com/GeyserMC/MCProtocolLib.git
synced 2024-11-14 19:34:58 -05:00
Merge branch 'master' of https://github.com/Steveice10/MCProtocolLib into feature/1.17
This commit is contained in:
commit
13bf22b60c
7 changed files with 69 additions and 162 deletions
|
@ -1,4 +1,4 @@
|
|||
Copyright (C) 2013-2019 Steveice10
|
||||
Copyright (C) 2013-2021 Steveice10
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
|
|
|
@ -26,7 +26,6 @@ import com.github.steveice10.opennbt.tag.builtin.ListTag;
|
|||
import com.github.steveice10.opennbt.tag.builtin.LongTag;
|
||||
import com.github.steveice10.opennbt.tag.builtin.StringTag;
|
||||
import com.github.steveice10.opennbt.tag.builtin.Tag;
|
||||
import com.github.steveice10.packetlib.Client;
|
||||
import com.github.steveice10.packetlib.ProxyInfo;
|
||||
import com.github.steveice10.packetlib.Server;
|
||||
import com.github.steveice10.packetlib.Session;
|
||||
|
@ -37,7 +36,8 @@ import com.github.steveice10.packetlib.event.server.SessionRemovedEvent;
|
|||
import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
|
||||
import com.github.steveice10.packetlib.event.session.PacketReceivedEvent;
|
||||
import com.github.steveice10.packetlib.event.session.SessionAdapter;
|
||||
import com.github.steveice10.packetlib.tcp.TcpSessionFactory;
|
||||
import com.github.steveice10.packetlib.tcp.TcpClientSession;
|
||||
import com.github.steveice10.packetlib.tcp.TcpServer;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.format.TextDecoration;
|
||||
|
@ -61,24 +61,19 @@ public class MinecraftProtocolTest {
|
|||
SessionService sessionService = new SessionService();
|
||||
sessionService.setProxy(AUTH_PROXY);
|
||||
|
||||
Server server = new Server(HOST, PORT, MinecraftProtocol.class, new TcpSessionFactory());
|
||||
Server server = new TcpServer(HOST, PORT, MinecraftProtocol.class);
|
||||
server.setGlobalFlag(MinecraftConstants.SESSION_SERVICE_KEY, sessionService);
|
||||
server.setGlobalFlag(MinecraftConstants.VERIFY_USERS_KEY, VERIFY_USERS);
|
||||
server.setGlobalFlag(MinecraftConstants.SERVER_INFO_BUILDER_KEY, new ServerInfoBuilder() {
|
||||
@Override
|
||||
public ServerStatusInfo buildInfo(Session session) {
|
||||
return new ServerStatusInfo(
|
||||
server.setGlobalFlag(MinecraftConstants.SERVER_INFO_BUILDER_KEY, (ServerInfoBuilder) session ->
|
||||
new ServerStatusInfo(
|
||||
new VersionInfo(MinecraftConstants.GAME_VERSION, MinecraftConstants.PROTOCOL_VERSION),
|
||||
new PlayerInfo(100, 0, new GameProfile[0]),
|
||||
Component.text("Hello world!"),
|
||||
null
|
||||
);
|
||||
}
|
||||
});
|
||||
)
|
||||
);
|
||||
|
||||
server.setGlobalFlag(MinecraftConstants.SERVER_LOGIN_HANDLER_KEY, new ServerLoginHandler() {
|
||||
@Override
|
||||
public void loggedIn(Session session) {
|
||||
server.setGlobalFlag(MinecraftConstants.SERVER_LOGIN_HANDLER_KEY, (ServerLoginHandler) session ->
|
||||
session.send(new ServerJoinGamePacket(
|
||||
0,
|
||||
false,
|
||||
|
@ -96,9 +91,8 @@ public class MinecraftProtocolTest {
|
|||
false,
|
||||
false,
|
||||
false
|
||||
));
|
||||
}
|
||||
});
|
||||
))
|
||||
);
|
||||
|
||||
server.setGlobalFlag(MinecraftConstants.SERVER_COMPRESSION_THRESHOLD, 100);
|
||||
server.addListener(new ServerAdapter() {
|
||||
|
@ -152,29 +146,24 @@ public class MinecraftProtocolTest {
|
|||
SessionService sessionService = new SessionService();
|
||||
sessionService.setProxy(AUTH_PROXY);
|
||||
|
||||
MinecraftProtocol protocol = new MinecraftProtocol(SubProtocol.STATUS);
|
||||
Client client = new Client(HOST, PORT, protocol, new TcpSessionFactory(PROXY));
|
||||
client.getSession().setFlag(MinecraftConstants.SESSION_SERVICE_KEY, sessionService);
|
||||
client.getSession().setFlag(MinecraftConstants.SERVER_INFO_HANDLER_KEY, new ServerInfoHandler() {
|
||||
@Override
|
||||
public void handle(Session session, ServerStatusInfo info) {
|
||||
System.out.println("Version: " + info.getVersionInfo().getVersionName() + ", " + info.getVersionInfo().getProtocolVersion());
|
||||
System.out.println("Player Count: " + info.getPlayerInfo().getOnlinePlayers() + " / " + info.getPlayerInfo().getMaxPlayers());
|
||||
System.out.println("Players: " + Arrays.toString(info.getPlayerInfo().getPlayers()));
|
||||
System.out.println("Description: " + info.getDescription());
|
||||
System.out.println("Icon: " + info.getIconPng());
|
||||
}
|
||||
MinecraftProtocol protocol = new MinecraftProtocol();
|
||||
Session client = new TcpClientSession(HOST, PORT, protocol, PROXY);
|
||||
client.setFlag(MinecraftConstants.SESSION_SERVICE_KEY, sessionService);
|
||||
client.setFlag(MinecraftConstants.SERVER_INFO_HANDLER_KEY, (ServerInfoHandler) (session, info) -> {
|
||||
System.out.println("Version: " + info.getVersionInfo().getVersionName()
|
||||
+ ", " + info.getVersionInfo().getProtocolVersion());
|
||||
System.out.println("Player Count: " + info.getPlayerInfo().getOnlinePlayers()
|
||||
+ " / " + info.getPlayerInfo().getMaxPlayers());
|
||||
System.out.println("Players: " + Arrays.toString(info.getPlayerInfo().getPlayers()));
|
||||
System.out.println("Description: " + info.getDescription());
|
||||
System.out.println("Icon: " + info.getIconPng());
|
||||
});
|
||||
|
||||
client.getSession().setFlag(MinecraftConstants.SERVER_PING_TIME_HANDLER_KEY, new ServerPingTimeHandler() {
|
||||
@Override
|
||||
public void handle(Session session, long pingTime) {
|
||||
System.out.println("Server ping took " + pingTime + "ms");
|
||||
}
|
||||
});
|
||||
client.setFlag(MinecraftConstants.SERVER_PING_TIME_HANDLER_KEY, (ServerPingTimeHandler) (session, pingTime) ->
|
||||
System.out.println("Server ping took " + pingTime + "ms"));
|
||||
|
||||
client.getSession().connect();
|
||||
while(client.getSession().isConnected()) {
|
||||
client.connect();
|
||||
while(client.isConnected()) {
|
||||
try {
|
||||
Thread.sleep(5);
|
||||
} catch(InterruptedException e) {
|
||||
|
@ -193,9 +182,7 @@ public class MinecraftProtocolTest {
|
|||
authService.setProxy(AUTH_PROXY);
|
||||
authService.login();
|
||||
|
||||
// Can also use "new MinecraftProtocol(USERNAME, PASSWORD)"
|
||||
// if you don't need a proxy or any other customizations.
|
||||
protocol = new MinecraftProtocol(authService);
|
||||
protocol = new MinecraftProtocol(authService.getSelectedProfile(), authService.getAccessToken());
|
||||
System.out.println("Successfully authenticated user.");
|
||||
} catch(RequestException e) {
|
||||
e.printStackTrace();
|
||||
|
@ -208,9 +195,9 @@ public class MinecraftProtocolTest {
|
|||
SessionService sessionService = new SessionService();
|
||||
sessionService.setProxy(AUTH_PROXY);
|
||||
|
||||
Client client = new Client(HOST, PORT, protocol, new TcpSessionFactory(PROXY));
|
||||
client.getSession().setFlag(MinecraftConstants.SESSION_SERVICE_KEY, sessionService);
|
||||
client.getSession().addListener(new SessionAdapter() {
|
||||
Session client = new TcpClientSession(HOST, PORT, protocol, PROXY);
|
||||
client.setFlag(MinecraftConstants.SESSION_SERVICE_KEY, sessionService);
|
||||
client.addListener(new SessionAdapter() {
|
||||
@Override
|
||||
public void packetReceived(PacketReceivedEvent event) {
|
||||
if(event.getPacket() instanceof ServerJoinGamePacket) {
|
||||
|
@ -231,7 +218,7 @@ public class MinecraftProtocolTest {
|
|||
}
|
||||
});
|
||||
|
||||
client.getSession().connect();
|
||||
client.connect();
|
||||
}
|
||||
|
||||
private static CompoundTag getDimensionTag() {
|
||||
|
|
6
pom.xml
6
pom.xml
|
@ -56,13 +56,13 @@
|
|||
<dependency>
|
||||
<groupId>com.github.steveice10</groupId>
|
||||
<artifactId>opennbt</artifactId>
|
||||
<version>1.3</version>
|
||||
<version>1.4</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.steveice10</groupId>
|
||||
<artifactId>packetlib</artifactId>
|
||||
<version>1.8</version>
|
||||
<version>2.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
@ -74,7 +74,7 @@
|
|||
<dependency>
|
||||
<groupId>net.kyori</groupId>
|
||||
<artifactId>adventure-text-serializer-gson</artifactId>
|
||||
<version>4.2.0</version>
|
||||
<version>4.7.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
|
|
|
@ -48,6 +48,13 @@ public class ClientListener extends SessionAdapter {
|
|||
MinecraftProtocol protocol = (MinecraftProtocol) event.getSession().getPacketProtocol();
|
||||
if(protocol.getSubProtocol() == SubProtocol.LOGIN) {
|
||||
if(event.getPacket() instanceof EncryptionRequestPacket) {
|
||||
GameProfile profile = event.getSession().getFlag(MinecraftConstants.PROFILE_KEY);
|
||||
String accessToken = event.getSession().getFlag(MinecraftConstants.ACCESS_TOKEN_KEY);
|
||||
|
||||
if(profile == null || accessToken == null) {
|
||||
throw new IllegalStateException("Cannot reply to EncryptionRequestPacket without profile and access token.");
|
||||
}
|
||||
|
||||
EncryptionRequestPacket packet = event.getPacket();
|
||||
SecretKey key;
|
||||
try {
|
||||
|
@ -59,9 +66,7 @@ public class ClientListener extends SessionAdapter {
|
|||
}
|
||||
|
||||
SessionService sessionService = event.getSession().getFlag(MinecraftConstants.SESSION_SERVICE_KEY, new SessionService());
|
||||
GameProfile profile = event.getSession().getFlag(MinecraftConstants.PROFILE_KEY);
|
||||
String serverId = sessionService.getServerId(packet.getServerId(), packet.getPublicKey(), key);
|
||||
String accessToken = event.getSession().getFlag(MinecraftConstants.ACCESS_TOKEN_KEY);
|
||||
try {
|
||||
sessionService.joinServer(profile, accessToken, serverId);
|
||||
} catch(ServiceUnavailableException e) {
|
||||
|
@ -78,12 +83,9 @@ public class ClientListener extends SessionAdapter {
|
|||
event.getSession().send(new EncryptionResponsePacket(packet.getPublicKey(), key, packet.getVerifyToken()));
|
||||
protocol.enableEncryption(key);
|
||||
} else if(event.getPacket() instanceof LoginSuccessPacket) {
|
||||
LoginSuccessPacket packet = event.getPacket();
|
||||
event.getSession().setFlag(MinecraftConstants.PROFILE_KEY, packet.getProfile());
|
||||
protocol.setSubProtocol(SubProtocol.GAME, true, event.getSession());
|
||||
} else if(event.getPacket() instanceof LoginDisconnectPacket) {
|
||||
LoginDisconnectPacket packet = event.getPacket();
|
||||
event.getSession().disconnect(packet.getReason().toString());
|
||||
event.getSession().disconnect(event.<LoginDisconnectPacket>getPacket().getReason().toString());
|
||||
} else if(event.getPacket() instanceof LoginSetCompressionPacket) {
|
||||
event.getSession().setCompressionThreshold(event.<LoginSetCompressionPacket>getPacket().getThreshold());
|
||||
}
|
||||
|
@ -125,7 +127,7 @@ public class ClientListener extends SessionAdapter {
|
|||
|
||||
if(this.targetSubProtocol == SubProtocol.LOGIN) {
|
||||
GameProfile profile = event.getSession().getFlag(MinecraftConstants.PROFILE_KEY);
|
||||
event.getSession().send(new LoginStartPacket(profile != null ? profile.getName() : ""));
|
||||
event.getSession().send(new LoginStartPacket(profile.getName()));
|
||||
} else {
|
||||
event.getSession().send(new StatusQueryPacket());
|
||||
}
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
package com.github.steveice10.mc.protocol;
|
||||
|
||||
import com.github.steveice10.mc.auth.data.GameProfile;
|
||||
import com.github.steveice10.mc.auth.exception.request.RequestException;
|
||||
import com.github.steveice10.mc.auth.service.AuthenticationService;
|
||||
import com.github.steveice10.mc.protocol.data.SubProtocol;
|
||||
import com.github.steveice10.mc.protocol.packet.handshake.client.HandshakePacket;
|
||||
import com.github.steveice10.mc.protocol.packet.ingame.client.*;
|
||||
|
@ -97,7 +95,6 @@ import com.github.steveice10.mc.protocol.packet.status.client.StatusPingPacket;
|
|||
import com.github.steveice10.mc.protocol.packet.status.client.StatusQueryPacket;
|
||||
import com.github.steveice10.mc.protocol.packet.status.server.StatusPongPacket;
|
||||
import com.github.steveice10.mc.protocol.packet.status.server.StatusResponsePacket;
|
||||
import com.github.steveice10.packetlib.Client;
|
||||
import com.github.steveice10.packetlib.Server;
|
||||
import com.github.steveice10.packetlib.Session;
|
||||
import com.github.steveice10.packetlib.crypt.AESEncryption;
|
||||
|
@ -106,9 +103,8 @@ import com.github.steveice10.packetlib.packet.DefaultPacketHeader;
|
|||
import com.github.steveice10.packetlib.packet.Packet;
|
||||
import com.github.steveice10.packetlib.packet.PacketHeader;
|
||||
import com.github.steveice10.packetlib.packet.PacketProtocol;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.NonNull;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.security.GeneralSecurityException;
|
||||
|
@ -119,11 +115,10 @@ import java.util.function.BiConsumer;
|
|||
/**
|
||||
* Implements the Minecraft protocol.
|
||||
*/
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class MinecraftProtocol extends PacketProtocol {
|
||||
private SubProtocol subProtocol = SubProtocol.HANDSHAKE;
|
||||
private final PacketHeader packetHeader = new DefaultPacketHeader();
|
||||
private AESEncryption encryption = null;
|
||||
private AESEncryption encryption;
|
||||
|
||||
private SubProtocol targetSubProtocol;
|
||||
|
||||
|
@ -131,13 +126,13 @@ public class MinecraftProtocol extends PacketProtocol {
|
|||
* The player's identity.
|
||||
*/
|
||||
@Getter
|
||||
private GameProfile profile = null;
|
||||
private GameProfile profile;
|
||||
|
||||
/**
|
||||
* Authentication access token.
|
||||
*/
|
||||
@Getter
|
||||
private String accessToken = "";
|
||||
private String accessToken;
|
||||
|
||||
/**
|
||||
* Whether to add the default client and server listeners for performing initial login.
|
||||
|
@ -147,101 +142,31 @@ public class MinecraftProtocol extends PacketProtocol {
|
|||
private boolean useDefaultListeners = true;
|
||||
|
||||
/**
|
||||
* Constructs a new MinecraftProtocol instance, starting with a specific {@link SubProtocol}.
|
||||
* @param subProtocol {@link SubProtocol} to start with.
|
||||
* Constructs a new MinecraftProtocol instance for making status queries.
|
||||
*/
|
||||
public MinecraftProtocol(SubProtocol subProtocol) {
|
||||
if(subProtocol != SubProtocol.LOGIN && subProtocol != SubProtocol.STATUS) {
|
||||
throw new IllegalArgumentException("Only login and status modes are permitted.");
|
||||
}
|
||||
|
||||
this.targetSubProtocol = subProtocol;
|
||||
if(subProtocol == SubProtocol.LOGIN) {
|
||||
this.profile = new GameProfile((UUID) null, "Player");
|
||||
}
|
||||
public MinecraftProtocol() {
|
||||
this.targetSubProtocol = SubProtocol.STATUS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new MinecraftProtocol instance, adopting the given username.
|
||||
* @param username Username to adopt.
|
||||
* Constructs a new MinecraftProtocol instance for logging in using offline mode.
|
||||
* @param username Username to use.
|
||||
*/
|
||||
public MinecraftProtocol(String username) {
|
||||
this(SubProtocol.LOGIN);
|
||||
this.profile = new GameProfile((UUID) null, username);
|
||||
public MinecraftProtocol(@NonNull String username) {
|
||||
this(new GameProfile((UUID) null, username), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new MinecraftProtocol instance, logging in with the given password credentials.
|
||||
* @param username Username to log in as. Must be the same as when the access token was generated.
|
||||
* @param password Password to log in with.
|
||||
* @throws RequestException If the log in request fails.
|
||||
*/
|
||||
@Deprecated
|
||||
public MinecraftProtocol(String username, String password) throws RequestException {
|
||||
this(createAuthServiceForPasswordLogin(username, password));
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new MinecraftProtocol instance, logging in with the given token credentials.
|
||||
* @param username Username to log in as. Must be the same as when the access token was generated.
|
||||
* @param clientToken Client token to log in as. Must be the same as when the access token was generated.
|
||||
* @param accessToken Access token to log in with.
|
||||
* @throws RequestException If the log in request fails.
|
||||
*/
|
||||
@Deprecated
|
||||
public MinecraftProtocol(String username, String clientToken, String accessToken) throws RequestException {
|
||||
this(createAuthServiceForTokenLogin(username, clientToken, accessToken));
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new MinecraftProtocol instance, copying authentication information
|
||||
* from a logged-in {@link AuthenticationService}.
|
||||
* @param authService {@link AuthenticationService} to copy from.
|
||||
*/
|
||||
public MinecraftProtocol(AuthenticationService authService) {
|
||||
this(authService.getSelectedProfile(), authService.getAccessToken());
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new MinecraftProtocol from authentication information.
|
||||
* Constructs a new MinecraftProtocol instance for logging in.
|
||||
* @param profile GameProfile to use.
|
||||
* @param clientToken Client token to use.
|
||||
* @param accessToken Access token to use.
|
||||
* @param accessToken Access token to use, or null if using offline mode.
|
||||
*/
|
||||
@Deprecated
|
||||
public MinecraftProtocol(GameProfile profile, String clientToken, String accessToken) {
|
||||
this(SubProtocol.LOGIN);
|
||||
public MinecraftProtocol(@NonNull GameProfile profile, String accessToken) {
|
||||
this.targetSubProtocol = SubProtocol.LOGIN;
|
||||
this.profile = profile;
|
||||
this.accessToken = accessToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new MinecraftProtocol from authentication information.
|
||||
* @param profile GameProfile to use.
|
||||
* @param accessToken Access token to use.
|
||||
*/
|
||||
public MinecraftProtocol(GameProfile profile, String accessToken) {
|
||||
this(SubProtocol.LOGIN);
|
||||
this.profile = profile;
|
||||
this.accessToken = accessToken;
|
||||
}
|
||||
|
||||
private static AuthenticationService createAuthServiceForPasswordLogin(String username, String password) throws RequestException {
|
||||
AuthenticationService auth = new AuthenticationService(UUID.randomUUID().toString());
|
||||
auth.setUsername(username);
|
||||
auth.setPassword(password);
|
||||
auth.login();
|
||||
return auth;
|
||||
}
|
||||
|
||||
private static AuthenticationService createAuthServiceForTokenLogin(String username, String clientToken, String accessToken) throws RequestException {
|
||||
AuthenticationService auth = new AuthenticationService(clientToken);
|
||||
auth.setUsername(username);
|
||||
auth.setAccessToken(accessToken);
|
||||
auth.login();
|
||||
return auth;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSRVRecordPrefix() {
|
||||
return "_minecraft";
|
||||
|
@ -258,11 +183,9 @@ public class MinecraftProtocol extends PacketProtocol {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void newClientSession(Client client, Session session) {
|
||||
if(this.profile != null) {
|
||||
session.setFlag(MinecraftConstants.PROFILE_KEY, this.profile);
|
||||
session.setFlag(MinecraftConstants.ACCESS_TOKEN_KEY, this.accessToken);
|
||||
}
|
||||
public void newClientSession(Session session) {
|
||||
session.setFlag(MinecraftConstants.PROFILE_KEY, this.profile);
|
||||
session.setFlag(MinecraftConstants.ACCESS_TOKEN_KEY, this.accessToken);
|
||||
|
||||
this.setSubProtocol(SubProtocol.HANDSHAKE, true, session);
|
||||
|
||||
|
|
|
@ -53,7 +53,7 @@ public class StatusResponsePacket implements Packet {
|
|||
|
||||
PlayerInfo players = new PlayerInfo(plrs.get("max").getAsInt(), plrs.get("online").getAsInt(), profiles);
|
||||
JsonElement desc = obj.get("description");
|
||||
Component description = DefaultComponentSerializer.get().deserialize(desc.toString());
|
||||
Component description = DefaultComponentSerializer.get().serializer().fromJson(desc, Component.class);
|
||||
byte[] icon = null;
|
||||
if(obj.has("favicon")) {
|
||||
icon = this.stringToIcon(obj.get("favicon").getAsString());
|
||||
|
|
|
@ -14,14 +14,14 @@ import com.github.steveice10.opennbt.tag.builtin.FloatTag;
|
|||
import com.github.steveice10.opennbt.tag.builtin.IntTag;
|
||||
import com.github.steveice10.opennbt.tag.builtin.ListTag;
|
||||
import com.github.steveice10.opennbt.tag.builtin.StringTag;
|
||||
import com.github.steveice10.packetlib.Client;
|
||||
import com.github.steveice10.packetlib.Server;
|
||||
import com.github.steveice10.packetlib.Session;
|
||||
import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
|
||||
import com.github.steveice10.packetlib.event.session.PacketReceivedEvent;
|
||||
import com.github.steveice10.packetlib.event.session.SessionAdapter;
|
||||
import com.github.steveice10.packetlib.packet.Packet;
|
||||
import com.github.steveice10.packetlib.tcp.TcpSessionFactory;
|
||||
import com.github.steveice10.packetlib.tcp.TcpClientSession;
|
||||
import com.github.steveice10.packetlib.tcp.TcpServer;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
|
@ -34,7 +34,6 @@ import static com.github.steveice10.mc.protocol.MinecraftConstants.SERVER_INFO_B
|
|||
import static com.github.steveice10.mc.protocol.MinecraftConstants.SERVER_INFO_HANDLER_KEY;
|
||||
import static com.github.steveice10.mc.protocol.MinecraftConstants.SERVER_LOGIN_HANDLER_KEY;
|
||||
import static com.github.steveice10.mc.protocol.MinecraftConstants.VERIFY_USERS_KEY;
|
||||
import static com.github.steveice10.mc.protocol.data.SubProtocol.STATUS;
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
@ -56,7 +55,7 @@ public class MinecraftProtocolTest {
|
|||
|
||||
@BeforeClass
|
||||
public static void setupServer() {
|
||||
server = new Server(HOST, PORT, MinecraftProtocol.class, new TcpSessionFactory());
|
||||
server = new TcpServer(HOST, PORT, MinecraftProtocol.class);
|
||||
server.setGlobalFlag(VERIFY_USERS_KEY, false);
|
||||
server.setGlobalFlag(SERVER_COMPRESSION_THRESHOLD, 100);
|
||||
server.setGlobalFlag(SERVER_INFO_BUILDER_KEY, (ServerInfoBuilder) session -> SERVER_INFO);
|
||||
|
@ -75,10 +74,8 @@ public class MinecraftProtocolTest {
|
|||
|
||||
@Test
|
||||
public void testStatus() throws InterruptedException {
|
||||
Client client = new Client(HOST, PORT, new MinecraftProtocol(STATUS), new TcpSessionFactory());
|
||||
Session session = new TcpClientSession(HOST, PORT, new MinecraftProtocol());
|
||||
try {
|
||||
Session session = client.getSession();
|
||||
|
||||
ServerInfoHandlerTest handler = new ServerInfoHandlerTest();
|
||||
session.setFlag(SERVER_INFO_HANDLER_KEY, handler);
|
||||
session.addListener(new DisconnectListener());
|
||||
|
@ -88,16 +85,14 @@ public class MinecraftProtocolTest {
|
|||
assertNotNull("Failed to get server info.", handler.info);
|
||||
assertEquals("Received incorrect server info.", SERVER_INFO, handler.info);
|
||||
} finally {
|
||||
client.getSession().disconnect("Status test complete.");
|
||||
session.disconnect("Status test complete.");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLogin() throws InterruptedException {
|
||||
Client client = new Client(HOST, PORT, new MinecraftProtocol("Username"), new TcpSessionFactory());
|
||||
Session session = new TcpClientSession(HOST, PORT, new MinecraftProtocol("Username"));
|
||||
try {
|
||||
Session session = client.getSession();
|
||||
|
||||
LoginListenerTest listener = new LoginListenerTest();
|
||||
session.addListener(listener);
|
||||
session.addListener(new DisconnectListener());
|
||||
|
@ -107,7 +102,7 @@ public class MinecraftProtocolTest {
|
|||
assertNotNull("Failed to log in.", listener.packet);
|
||||
assertEquals("Received incorrect join packet.", JOIN_GAME_PACKET, listener.packet);
|
||||
} finally {
|
||||
client.getSession().disconnect("Login test complete.");
|
||||
session.disconnect("Login test complete.");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue