mirror of
https://github.com/GeyserMC/MCProtocolLib.git
synced 2024-11-14 19:34:58 -05:00
Update for PacketLib changes.
This commit is contained in:
parent
41051d5109
commit
3c6cbb2cff
4 changed files with 45 additions and 44 deletions
|
@ -24,6 +24,7 @@ import com.github.steveice10.mc.protocol.packet.ingame.client.ClientChatPacket;
|
|||
import com.github.steveice10.mc.protocol.packet.ingame.server.ServerChatPacket;
|
||||
import com.github.steveice10.mc.protocol.packet.ingame.server.ServerJoinGamePacket;
|
||||
import com.github.steveice10.packetlib.Client;
|
||||
import com.github.steveice10.packetlib.ProxyInfo;
|
||||
import com.github.steveice10.packetlib.Server;
|
||||
import com.github.steveice10.packetlib.Session;
|
||||
import com.github.steveice10.packetlib.event.server.ServerAdapter;
|
||||
|
@ -43,14 +44,14 @@ public class MinecraftProtocolTest {
|
|||
private static final boolean VERIFY_USERS = false;
|
||||
private static final String HOST = "127.0.0.1";
|
||||
private static final int PORT = 25565;
|
||||
private static final Proxy PROXY = Proxy.NO_PROXY;
|
||||
private static final ProxyInfo PROXY = null;
|
||||
private static final Proxy AUTH_PROXY = Proxy.NO_PROXY;
|
||||
private static final String USERNAME = "Username";
|
||||
private static final String PASSWORD = "Password";
|
||||
|
||||
public static void main(String[] args) {
|
||||
if(SPAWN_SERVER) {
|
||||
Server server = new Server(HOST, PORT, MinecraftProtocol.class, new TcpSessionFactory(PROXY));
|
||||
Server server = new Server(HOST, PORT, MinecraftProtocol.class, new TcpSessionFactory());
|
||||
server.setGlobalFlag(MinecraftConstants.AUTH_PROXY_KEY, AUTH_PROXY);
|
||||
server.setGlobalFlag(MinecraftConstants.VERIFY_USERS_KEY, VERIFY_USERS);
|
||||
server.setGlobalFlag(MinecraftConstants.SERVER_INFO_BUILDER_KEY, new ServerInfoBuilder() {
|
||||
|
|
2
pom.xml
2
pom.xml
|
@ -62,7 +62,7 @@
|
|||
<dependency>
|
||||
<groupId>com.github.steveice10</groupId>
|
||||
<artifactId>packetlib</artifactId>
|
||||
<version>1.4</version>
|
||||
<version>1.5-SNAPSHOT</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
|
|
@ -27,6 +27,7 @@ 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.event.session.ConnectedEvent;
|
||||
import com.github.steveice10.packetlib.event.session.PacketReceivedEvent;
|
||||
import com.github.steveice10.packetlib.event.session.PacketSentEvent;
|
||||
import com.github.steveice10.packetlib.event.session.SessionAdapter;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.NonNull;
|
||||
|
@ -55,12 +56,7 @@ public class ClientListener extends SessionAdapter {
|
|||
throw new IllegalStateException("Failed to generate shared key.", e);
|
||||
}
|
||||
|
||||
Proxy proxy = event.getSession().<Proxy>getFlag(MinecraftConstants.AUTH_PROXY_KEY);
|
||||
if(proxy == null) {
|
||||
proxy = Proxy.NO_PROXY;
|
||||
}
|
||||
|
||||
SessionService sessionService = new SessionService(proxy);
|
||||
SessionService sessionService = new SessionService(event.getSession().getFlag(MinecraftConstants.AUTH_PROXY_KEY, Proxy.NO_PROXY));
|
||||
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);
|
||||
|
@ -118,20 +114,28 @@ public class ClientListener extends SessionAdapter {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void packetSent(PacketSentEvent event) {
|
||||
if(event.getPacket() instanceof HandshakePacket) {
|
||||
// Once the HandshakePacket has been sent, switch to the next protocol mode.
|
||||
MinecraftProtocol protocol = (MinecraftProtocol) event.getSession().getPacketProtocol();
|
||||
protocol.setSubProtocol(this.targetSubProtocol, true, event.getSession());
|
||||
|
||||
if(this.targetSubProtocol == SubProtocol.LOGIN) {
|
||||
GameProfile profile = event.getSession().getFlag(MinecraftConstants.PROFILE_KEY);
|
||||
event.getSession().send(new LoginStartPacket(profile != null ? profile.getName() : ""));
|
||||
} else {
|
||||
event.getSession().send(new StatusQueryPacket());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connected(ConnectedEvent event) {
|
||||
MinecraftProtocol protocol = (MinecraftProtocol) event.getSession().getPacketProtocol();
|
||||
if(this.targetSubProtocol == SubProtocol.LOGIN) {
|
||||
event.getSession().send(new HandshakePacket(MinecraftConstants.PROTOCOL_VERSION, event.getSession().getHost(), event.getSession().getPort(), HandshakeIntent.LOGIN));
|
||||
|
||||
GameProfile profile = event.getSession().getFlag(MinecraftConstants.PROFILE_KEY);
|
||||
protocol.setSubProtocol(SubProtocol.LOGIN, true, event.getSession());
|
||||
event.getSession().send(new LoginStartPacket(profile != null ? profile.getName() : ""));
|
||||
} else if(this.targetSubProtocol == SubProtocol.STATUS) {
|
||||
event.getSession().send(new HandshakePacket(MinecraftConstants.PROTOCOL_VERSION, event.getSession().getHost(), event.getSession().getPort(), HandshakeIntent.STATUS));
|
||||
|
||||
protocol.setSubProtocol(SubProtocol.STATUS, true, event.getSession());
|
||||
event.getSession().send(new StatusQueryPacket());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,6 +27,7 @@ import com.github.steveice10.packetlib.Session;
|
|||
import com.github.steveice10.packetlib.event.session.ConnectedEvent;
|
||||
import com.github.steveice10.packetlib.event.session.DisconnectingEvent;
|
||||
import com.github.steveice10.packetlib.event.session.PacketReceivedEvent;
|
||||
import com.github.steveice10.packetlib.event.session.PacketSentEvent;
|
||||
import com.github.steveice10.packetlib.event.session.SessionAdapter;
|
||||
|
||||
import javax.crypto.SecretKey;
|
||||
|
@ -100,8 +101,7 @@ public class ServerListener extends SessionAdapter {
|
|||
if(event.getPacket() instanceof LoginStartPacket) {
|
||||
this.username = event.<LoginStartPacket>getPacket().getUsername();
|
||||
|
||||
boolean verify = event.getSession().hasFlag(MinecraftConstants.VERIFY_USERS_KEY) ? event.getSession().<Boolean>getFlag(MinecraftConstants.VERIFY_USERS_KEY) : true;
|
||||
if(verify) {
|
||||
if(event.getSession().getFlag(MinecraftConstants.VERIFY_USERS_KEY, true)) {
|
||||
event.getSession().send(new EncryptionRequestPacket(SERVER_ID, KEY_PAIR.getPublic(), this.verifyToken));
|
||||
} else {
|
||||
new Thread(new UserAuthTask(event.getSession(), null)).start();
|
||||
|
@ -150,6 +150,23 @@ public class ServerListener extends SessionAdapter {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void packetSent(PacketSentEvent event) {
|
||||
Session session = event.getSession();
|
||||
if(event.getPacket() instanceof LoginSetCompressionPacket) {
|
||||
session.setCompressionThreshold(event.<LoginSetCompressionPacket>getPacket().getThreshold());
|
||||
session.send(new LoginSuccessPacket(session.getFlag(MinecraftConstants.PROFILE_KEY)));
|
||||
} else if(event.getPacket() instanceof LoginSuccessPacket) {
|
||||
((MinecraftProtocol) session.getPacketProtocol()).setSubProtocol(SubProtocol.GAME, false, session);
|
||||
ServerLoginHandler handler = session.getFlag(MinecraftConstants.SERVER_LOGIN_HANDLER_KEY);
|
||||
if(handler != null) {
|
||||
handler.loggedIn(session);
|
||||
}
|
||||
|
||||
new Thread(new KeepAliveTask(session)).start();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnecting(DisconnectingEvent event) {
|
||||
MinecraftProtocol protocol = (MinecraftProtocol) event.getSession().getPacketProtocol();
|
||||
|
@ -171,16 +188,9 @@ public class ServerListener extends SessionAdapter {
|
|||
|
||||
@Override
|
||||
public void run() {
|
||||
boolean verify = this.session.hasFlag(MinecraftConstants.VERIFY_USERS_KEY) ? this.session.<Boolean>getFlag(MinecraftConstants.VERIFY_USERS_KEY) : true;
|
||||
|
||||
GameProfile profile = null;
|
||||
if(verify && this.key != null) {
|
||||
Proxy proxy = this.session.<Proxy>getFlag(MinecraftConstants.AUTH_PROXY_KEY);
|
||||
if(proxy == null) {
|
||||
proxy = Proxy.NO_PROXY;
|
||||
}
|
||||
|
||||
SessionService sessionService = new SessionService(proxy);
|
||||
if(this.key != null) {
|
||||
SessionService sessionService = new SessionService(this.session.getFlag(MinecraftConstants.AUTH_PROXY_KEY, Proxy.NO_PROXY));
|
||||
try {
|
||||
profile = sessionService.getProfileByServer(username, sessionService.getServerId(SERVER_ID, KEY_PAIR.getPublic(), this.key));
|
||||
} catch(RequestException e) {
|
||||
|
@ -195,24 +205,10 @@ public class ServerListener extends SessionAdapter {
|
|||
profile = new GameProfile(UUID.nameUUIDFromBytes(("OfflinePlayer:" + username).getBytes()), username);
|
||||
}
|
||||
|
||||
int threshold;
|
||||
if(this.session.hasFlag(MinecraftConstants.SERVER_COMPRESSION_THRESHOLD)) {
|
||||
threshold = this.session.getFlag(MinecraftConstants.SERVER_COMPRESSION_THRESHOLD);
|
||||
} else {
|
||||
threshold = DEFAULT_COMPRESSION_THRESHOLD;
|
||||
}
|
||||
|
||||
this.session.send(new LoginSetCompressionPacket(threshold));
|
||||
this.session.setCompressionThreshold(threshold);
|
||||
this.session.send(new LoginSuccessPacket(profile));
|
||||
this.session.setFlag(MinecraftConstants.PROFILE_KEY, profile);
|
||||
((MinecraftProtocol) this.session.getPacketProtocol()).setSubProtocol(SubProtocol.GAME, false, this.session);
|
||||
ServerLoginHandler handler = this.session.getFlag(MinecraftConstants.SERVER_LOGIN_HANDLER_KEY);
|
||||
if(handler != null) {
|
||||
handler.loggedIn(this.session);
|
||||
}
|
||||
|
||||
new Thread(new KeepAliveTask(this.session)).start();
|
||||
int threshold = session.getFlag(MinecraftConstants.SERVER_COMPRESSION_THRESHOLD, DEFAULT_COMPRESSION_THRESHOLD);
|
||||
this.session.send(new LoginSetCompressionPacket(threshold));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue