Update to latest PacketLib commit.

This commit is contained in:
Steveice10 2021-04-20 18:28:14 -07:00
parent 9161ba7942
commit 893cc683cb
4 changed files with 37 additions and 56 deletions

View file

@ -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.LongTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag; import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.opennbt.tag.builtin.Tag; 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.ProxyInfo;
import com.github.steveice10.packetlib.Server; import com.github.steveice10.packetlib.Server;
import com.github.steveice10.packetlib.Session; 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.DisconnectedEvent;
import com.github.steveice10.packetlib.event.session.PacketReceivedEvent; import com.github.steveice10.packetlib.event.session.PacketReceivedEvent;
import com.github.steveice10.packetlib.event.session.SessionAdapter; 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.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration; import net.kyori.adventure.text.format.TextDecoration;
@ -61,24 +61,19 @@ public class MinecraftProtocolTest {
SessionService sessionService = new SessionService(); SessionService sessionService = new SessionService();
sessionService.setProxy(AUTH_PROXY); 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.SESSION_SERVICE_KEY, sessionService);
server.setGlobalFlag(MinecraftConstants.VERIFY_USERS_KEY, VERIFY_USERS); server.setGlobalFlag(MinecraftConstants.VERIFY_USERS_KEY, VERIFY_USERS);
server.setGlobalFlag(MinecraftConstants.SERVER_INFO_BUILDER_KEY, new ServerInfoBuilder() { server.setGlobalFlag(MinecraftConstants.SERVER_INFO_BUILDER_KEY, (ServerInfoBuilder) session ->
@Override new ServerStatusInfo(
public ServerStatusInfo buildInfo(Session session) {
return new ServerStatusInfo(
new VersionInfo(MinecraftConstants.GAME_VERSION, MinecraftConstants.PROTOCOL_VERSION), new VersionInfo(MinecraftConstants.GAME_VERSION, MinecraftConstants.PROTOCOL_VERSION),
new PlayerInfo(100, 0, new GameProfile[0]), new PlayerInfo(100, 0, new GameProfile[0]),
Component.text("Hello world!"), Component.text("Hello world!"),
null null
); )
} );
});
server.setGlobalFlag(MinecraftConstants.SERVER_LOGIN_HANDLER_KEY, new ServerLoginHandler() { server.setGlobalFlag(MinecraftConstants.SERVER_LOGIN_HANDLER_KEY, (ServerLoginHandler) session ->
@Override
public void loggedIn(Session session) {
session.send(new ServerJoinGamePacket( session.send(new ServerJoinGamePacket(
0, 0,
false, false,
@ -96,9 +91,8 @@ public class MinecraftProtocolTest {
false, false,
false, false,
false false
)); ))
} );
});
server.setGlobalFlag(MinecraftConstants.SERVER_COMPRESSION_THRESHOLD, 100); server.setGlobalFlag(MinecraftConstants.SERVER_COMPRESSION_THRESHOLD, 100);
server.addListener(new ServerAdapter() { server.addListener(new ServerAdapter() {
@ -153,28 +147,23 @@ public class MinecraftProtocolTest {
sessionService.setProxy(AUTH_PROXY); sessionService.setProxy(AUTH_PROXY);
MinecraftProtocol protocol = new MinecraftProtocol(); MinecraftProtocol protocol = new MinecraftProtocol();
Client client = new Client(HOST, PORT, protocol, new TcpSessionFactory(PROXY)); Session client = new TcpClientSession(HOST, PORT, protocol, PROXY);
client.getSession().setFlag(MinecraftConstants.SESSION_SERVICE_KEY, sessionService); client.setFlag(MinecraftConstants.SESSION_SERVICE_KEY, sessionService);
client.getSession().setFlag(MinecraftConstants.SERVER_INFO_HANDLER_KEY, new ServerInfoHandler() { client.setFlag(MinecraftConstants.SERVER_INFO_HANDLER_KEY, (ServerInfoHandler) (session, info) -> {
@Override System.out.println("Version: " + info.getVersionInfo().getVersionName()
public void handle(Session session, ServerStatusInfo info) { + ", " + info.getVersionInfo().getProtocolVersion());
System.out.println("Version: " + info.getVersionInfo().getVersionName() + ", " + info.getVersionInfo().getProtocolVersion()); System.out.println("Player Count: " + info.getPlayerInfo().getOnlinePlayers()
System.out.println("Player Count: " + info.getPlayerInfo().getOnlinePlayers() + " / " + info.getPlayerInfo().getMaxPlayers()); + " / " + info.getPlayerInfo().getMaxPlayers());
System.out.println("Players: " + Arrays.toString(info.getPlayerInfo().getPlayers())); System.out.println("Players: " + Arrays.toString(info.getPlayerInfo().getPlayers()));
System.out.println("Description: " + info.getDescription()); System.out.println("Description: " + info.getDescription());
System.out.println("Icon: " + info.getIconPng()); System.out.println("Icon: " + info.getIconPng());
}
}); });
client.getSession().setFlag(MinecraftConstants.SERVER_PING_TIME_HANDLER_KEY, new ServerPingTimeHandler() { client.setFlag(MinecraftConstants.SERVER_PING_TIME_HANDLER_KEY, (ServerPingTimeHandler) (session, pingTime) ->
@Override System.out.println("Server ping took " + pingTime + "ms"));
public void handle(Session session, long pingTime) {
System.out.println("Server ping took " + pingTime + "ms");
}
});
client.getSession().connect(); client.connect();
while(client.getSession().isConnected()) { while(client.isConnected()) {
try { try {
Thread.sleep(5); Thread.sleep(5);
} catch(InterruptedException e) { } catch(InterruptedException e) {
@ -206,9 +195,9 @@ public class MinecraftProtocolTest {
SessionService sessionService = new SessionService(); SessionService sessionService = new SessionService();
sessionService.setProxy(AUTH_PROXY); sessionService.setProxy(AUTH_PROXY);
Client client = new Client(HOST, PORT, protocol, new TcpSessionFactory(PROXY)); Session client = new TcpClientSession(HOST, PORT, protocol, PROXY);
client.getSession().setFlag(MinecraftConstants.SESSION_SERVICE_KEY, sessionService); client.setFlag(MinecraftConstants.SESSION_SERVICE_KEY, sessionService);
client.getSession().addListener(new SessionAdapter() { client.addListener(new SessionAdapter() {
@Override @Override
public void packetReceived(PacketReceivedEvent event) { public void packetReceived(PacketReceivedEvent event) {
if(event.getPacket() instanceof ServerJoinGamePacket) { if(event.getPacket() instanceof ServerJoinGamePacket) {
@ -229,7 +218,7 @@ public class MinecraftProtocolTest {
} }
}); });
client.getSession().connect(); client.connect();
} }
private static CompoundTag getDimensionTag() { private static CompoundTag getDimensionTag() {

View file

@ -62,7 +62,7 @@
<dependency> <dependency>
<groupId>com.github.steveice10</groupId> <groupId>com.github.steveice10</groupId>
<artifactId>packetlib</artifactId> <artifactId>packetlib</artifactId>
<version>1.8</version> <version>ac5600734e</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>

View file

@ -157,7 +157,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.client.StatusQueryPacket;
import com.github.steveice10.mc.protocol.packet.status.server.StatusPongPacket; import com.github.steveice10.mc.protocol.packet.status.server.StatusPongPacket;
import com.github.steveice10.mc.protocol.packet.status.server.StatusResponsePacket; 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.Server;
import com.github.steveice10.packetlib.Session; import com.github.steveice10.packetlib.Session;
import com.github.steveice10.packetlib.crypt.AESEncryption; import com.github.steveice10.packetlib.crypt.AESEncryption;
@ -165,9 +164,7 @@ import com.github.steveice10.packetlib.crypt.PacketEncryption;
import com.github.steveice10.packetlib.packet.DefaultPacketHeader; import com.github.steveice10.packetlib.packet.DefaultPacketHeader;
import com.github.steveice10.packetlib.packet.PacketHeader; import com.github.steveice10.packetlib.packet.PacketHeader;
import com.github.steveice10.packetlib.packet.PacketProtocol; import com.github.steveice10.packetlib.packet.PacketProtocol;
import lombok.AccessLevel;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull; import lombok.NonNull;
import lombok.Setter; import lombok.Setter;
@ -327,7 +324,7 @@ public class MinecraftProtocol extends PacketProtocol {
} }
@Override @Override
public void newClientSession(Client client, Session session) { public void newClientSession(Session session) {
session.setFlag(MinecraftConstants.PROFILE_KEY, this.profile); session.setFlag(MinecraftConstants.PROFILE_KEY, this.profile);
session.setFlag(MinecraftConstants.ACCESS_TOKEN_KEY, this.accessToken); session.setFlag(MinecraftConstants.ACCESS_TOKEN_KEY, this.accessToken);

View file

@ -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.IntTag;
import com.github.steveice10.opennbt.tag.builtin.ListTag; import com.github.steveice10.opennbt.tag.builtin.ListTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag; 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.Server;
import com.github.steveice10.packetlib.Session; import com.github.steveice10.packetlib.Session;
import com.github.steveice10.packetlib.event.session.DisconnectedEvent; import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
import com.github.steveice10.packetlib.event.session.PacketReceivedEvent; import com.github.steveice10.packetlib.event.session.PacketReceivedEvent;
import com.github.steveice10.packetlib.event.session.SessionAdapter; import com.github.steveice10.packetlib.event.session.SessionAdapter;
import com.github.steveice10.packetlib.packet.Packet; 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 net.kyori.adventure.text.Component;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; 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_INFO_HANDLER_KEY;
import static com.github.steveice10.mc.protocol.MinecraftConstants.SERVER_LOGIN_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.MinecraftConstants.VERIFY_USERS_KEY;
import static com.github.steveice10.mc.protocol.data.SubProtocol.STATUS;
import static java.util.concurrent.TimeUnit.SECONDS; import static java.util.concurrent.TimeUnit.SECONDS;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
@ -56,7 +55,7 @@ public class MinecraftProtocolTest {
@BeforeClass @BeforeClass
public static void setupServer() { 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(VERIFY_USERS_KEY, false);
server.setGlobalFlag(SERVER_COMPRESSION_THRESHOLD, 100); server.setGlobalFlag(SERVER_COMPRESSION_THRESHOLD, 100);
server.setGlobalFlag(SERVER_INFO_BUILDER_KEY, (ServerInfoBuilder) session -> SERVER_INFO); server.setGlobalFlag(SERVER_INFO_BUILDER_KEY, (ServerInfoBuilder) session -> SERVER_INFO);
@ -75,10 +74,8 @@ public class MinecraftProtocolTest {
@Test @Test
public void testStatus() throws InterruptedException { 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 { try {
Session session = client.getSession();
ServerInfoHandlerTest handler = new ServerInfoHandlerTest(); ServerInfoHandlerTest handler = new ServerInfoHandlerTest();
session.setFlag(SERVER_INFO_HANDLER_KEY, handler); session.setFlag(SERVER_INFO_HANDLER_KEY, handler);
session.addListener(new DisconnectListener()); session.addListener(new DisconnectListener());
@ -88,16 +85,14 @@ public class MinecraftProtocolTest {
assertNotNull("Failed to get server info.", handler.info); assertNotNull("Failed to get server info.", handler.info);
assertEquals("Received incorrect server info.", SERVER_INFO, handler.info); assertEquals("Received incorrect server info.", SERVER_INFO, handler.info);
} finally { } finally {
client.getSession().disconnect("Status test complete."); session.disconnect("Status test complete.");
} }
} }
@Test @Test
public void testLogin() throws InterruptedException { 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 { try {
Session session = client.getSession();
LoginListenerTest listener = new LoginListenerTest(); LoginListenerTest listener = new LoginListenerTest();
session.addListener(listener); session.addListener(listener);
session.addListener(new DisconnectListener()); session.addListener(new DisconnectListener());
@ -107,7 +102,7 @@ public class MinecraftProtocolTest {
assertNotNull("Failed to log in.", listener.packet); assertNotNull("Failed to log in.", listener.packet);
assertEquals("Received incorrect join packet.", JOIN_GAME_PACKET, listener.packet); assertEquals("Received incorrect join packet.", JOIN_GAME_PACKET, listener.packet);
} finally { } finally {
client.getSession().disconnect("Login test complete."); session.disconnect("Login test complete.");
} }
} }