Packet Context Class

This commit is contained in:
Adrian Bergqvist 2023-11-23 18:07:00 +01:00
parent 0e5f3feb49
commit 669a856ec4
No known key found for this signature in database
GPG Key ID: FAE7D8EDE225E686
9 changed files with 77 additions and 50 deletions

View File

@ -68,6 +68,8 @@ public enum VelocityForgeBackendConnectionPhase implements BackendConnectionPhas
//Reset client if not ready to receive new handshake
VelocityForgeClientConnectionPhase clientPhase = (VelocityForgeClientConnectionPhase) player.getPhase();
if (clientPhase != VelocityForgeClientConnectionPhase.NOT_STARTED) {
//Initial Forge
//Forge -> Forge
clientPhase.resetConnectionPhase(player);
if (clientPhase == VelocityForgeClientConnectionPhase.COMPLETE)
{

View File

@ -16,6 +16,7 @@ import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import net.kyori.adventure.text.Component;
import org.adde0109.ambassador.Ambassador;
import org.adde0109.ambassador.forge.packet.Context;
import org.adde0109.ambassador.forge.packet.IForgeLoginWrapperPacket;
import org.adde0109.ambassador.forge.packet.ModListReplyPacket;
import org.adde0109.ambassador.forge.pipeline.ForgeLoginWrapperDecoder;
@ -96,7 +97,7 @@ public enum VelocityForgeClientConnectionPhase implements ClientConnectionPhase
@Override
public boolean handle(ConnectedPlayer player, IForgeLoginWrapperPacket msg, VelocityServerConnection server) {
if (msg.getId() == 98) {
if (msg.getContext().getResponseID() == 98) {
player.getConnection().getChannel().pipeline().remove(ForgeConstants.RESET_LISTENER);
player.setPhase(NOT_STARTED);
@ -104,7 +105,7 @@ public enum VelocityForgeClientConnectionPhase implements ClientConnectionPhase
if (!(server.getConnection().getType() instanceof ForgeFMLConnectionType)) {
// -> vanilla
complete(player, msg.getSuccess());
complete(player, ((Context.ClientContext) msg.getContext()).success());
player.getConnectionInFlight().getConnection().getChannel().config().setAutoRead(true);
}

View File

@ -0,0 +1,35 @@
package org.adde0109.ambassador.forge.packet;
public class Context {
private final int responseID;
private Context(int responseID) {
this.responseID = responseID;
}
static Context createContext(int responseID) {
return new Context(responseID);
}
static ClientContext createContext(int responseID, boolean clientSuccess) {
return new ClientContext(responseID,clientSuccess);
}
public int getResponseID() {
return responseID;
}
public static class ClientContext extends Context {
private final boolean clientSuccess;
ClientContext(int responseID, boolean clientSuccess) {
super(responseID);
this.clientSuccess = clientSuccess;
}
public boolean success() {
return clientSuccess;
}
}
}

View File

@ -4,28 +4,31 @@ import com.velocitypowered.proxy.protocol.packet.LoginPluginResponse;
import com.velocitypowered.proxy.protocol.util.DeferredByteBufHolder;
import io.netty.buffer.ByteBuf;
public class GenericForgeLoginWrapperPacket extends DeferredByteBufHolder implements IForgeLoginWrapperPacket {
private final int id;
private final boolean success;
public class GenericForgeLoginWrapperPacket<T extends Context> extends DeferredByteBufHolder implements IForgeLoginWrapperPacket<T> {
public GenericForgeLoginWrapperPacket(ByteBuf input, int id, boolean success) {
private final T context;
private GenericForgeLoginWrapperPacket(ByteBuf input, T context) {
super(input);
this.id = id;
this.success = success;
this.context = context;
}
static public GenericForgeLoginWrapperPacket<Context> create(ByteBuf input, int id) {
return new GenericForgeLoginWrapperPacket<>(input, Context.createContext(id));
}
static public GenericForgeLoginWrapperPacket<Context> create(ByteBuf input, int id, boolean success) {
return new GenericForgeLoginWrapperPacket<>(input, Context.createContext(id, success));
}
@Override
public LoginPluginResponse encode() {
return new LoginPluginResponse(id, true, content());
public ByteBuf encode() {
return content();
}
@Override
public int getId() {
return id;
public T getContext() {
return context;
}
@Override
public boolean getSuccess() {
return success;
}
}

View File

@ -1,10 +1,8 @@
package org.adde0109.ambassador.forge.packet;
import com.velocitypowered.proxy.protocol.packet.LoginPluginResponse;
import io.netty.buffer.ByteBuf;
public interface IForgeLoginWrapperPacket {
public LoginPluginResponse encode();
public int getId();
public boolean getSuccess();
public interface IForgeLoginWrapperPacket<T extends Context> {
ByteBuf encode();
T getContext();
}

View File

@ -12,27 +12,22 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ModListReplyPacket implements IForgeLoginWrapperPacket {
public class ModListReplyPacket implements IForgeLoginWrapperPacket<Context.ClientContext> {
private List<String> mods;
private Map<ChannelIdentifier, String> channels;
private Map<String, String> registries;
private final int id;
private final boolean success;
private final Context.ClientContext context;
private ModListReplyPacket(List<String> mods, Map<ChannelIdentifier,
String> channels, Map<String, String> registries, int id, boolean success) {
this.mods = mods;
this.channels = channels;
this.registries = registries;
this.id = id;
this.success = success;
this.context = Context.createContext(id, success);
}
public static ModListReplyPacket read(LoginPluginResponse msg) {
ByteBuf input = msg.content();
public static ModListReplyPacket read(ByteBuf input, int msgID) {
List<String> mods = new ArrayList<>();
int len = ProtocolUtils.readVarInt(input);
@ -50,11 +45,11 @@ public class ModListReplyPacket implements IForgeLoginWrapperPacket {
for (int x = 0; x < len; x++)
registries.put(ProtocolUtils.readString(input, 32767), ProtocolUtils.readString(input, 0x100));
return new ModListReplyPacket(mods, channels, registries, msg.getId(), true);
return new ModListReplyPacket(mods, channels, registries, msgID, true);
}
@Override
public LoginPluginResponse encode() {
public ByteBuf encode() {
ByteBuf buf = Unpooled.buffer();
ProtocolUtils.writeVarInt(buf, 2);
@ -79,17 +74,12 @@ public class ModListReplyPacket implements IForgeLoginWrapperPacket {
ProtocolUtils.writeVarInt(output, buf.readableBytes());
output.writeBytes(buf);
return new LoginPluginResponse(id, true, output);
return output;
}
@Override
public int getId() {
return id;
}
@Override
public boolean getSuccess() {
return success;
public Context.ClientContext getContext() {
return context;
}
public List<String> getMods() {

View File

@ -26,16 +26,16 @@ public class ForgeLoginWrapperDecoder extends MessageToMessageDecoder<LoginPlugi
String channel = ProtocolUtils.readString(buf);
if (!channel.equals("fml:handshake")) {
buf.readerIndex(originalReaderIndex);
out.add(new GenericForgeLoginWrapperPacket(buf.retain(), msg.getId(), true));
out.add(GenericForgeLoginWrapperPacket.create(buf.retain(), msg.getId(), true));
return;
}
int length = ProtocolUtils.readVarInt(buf);
int packetID = ProtocolUtils.readVarInt(buf);
if (packetID == 2) {
out.add(ModListReplyPacket.read(msg));
out.add(ModListReplyPacket.read(msg.content(), msg.getId()));
} else {
buf.readerIndex(originalReaderIndex);
out.add(new GenericForgeLoginWrapperPacket(buf.retain(), msg.getId(), true));
out.add(GenericForgeLoginWrapperPacket.create(buf.retain(), msg.getId(), true));
}
}

View File

@ -26,7 +26,7 @@ public class FML2CRPMResetCompleteDecoder extends ChannelInboundHandlerAdapter {
boolean success = buf.readBoolean();
if (id == 98) {
try {
IForgeLoginWrapperPacket packet = new GenericForgeLoginWrapperPacket(Unpooled.EMPTY_BUFFER, id, success);
IForgeLoginWrapperPacket packet = GenericForgeLoginWrapperPacket.create(Unpooled.EMPTY_BUFFER, id, success);
ctx.fireChannelRead(packet);
} finally {
buf.release();

View File

@ -26,19 +26,17 @@ public class VelocityHandshakeSessionHandler extends HandshakeSessionHandler {
handshake.handle(original);
if (connection.getType() == ConnectionTypes.VANILLA) {
final String[] markerSplit = handshake.getServerAddress().split("\0");
if (connection.getState() == StateRegistry.LOGIN && markerSplit.length > 1) {
if (connection.getState() == StateRegistry.LOGIN && markerSplit.length > 1 && markerSplit[1].startsWith("FML")) {
switch (markerSplit[1]) {
case "FML2":
connection.setType(ForgeConstants.ForgeFML2);
connection.getChannel().pipeline().addAfter(Connections.MINECRAFT_ENCODER,ForgeConstants.SERVER_SUCCESS_LISTENER, new OutboundSuccessHolder());
connection.getChannel().pipeline().addAfter(Connections.MINECRAFT_ENCODER,ForgeConstants.PLUGIN_PACKET_QUEUE, new ClientPacketQueue(StateRegistry.LOGIN));
break;
case "FML3":
connection.setType(ForgeConstants.ForgeFML3);
connection.getChannel().pipeline().addAfter(Connections.MINECRAFT_ENCODER,ForgeConstants.SERVER_SUCCESS_LISTENER, new OutboundSuccessHolder());
connection.getChannel().pipeline().addAfter(Connections.MINECRAFT_ENCODER,ForgeConstants.PLUGIN_PACKET_QUEUE, new ClientPacketQueue(StateRegistry.LOGIN));
break;
}
connection.getChannel().pipeline().addAfter(Connections.MINECRAFT_ENCODER,ForgeConstants.SERVER_SUCCESS_LISTENER, new OutboundSuccessHolder());
connection.getChannel().pipeline().addAfter(Connections.MINECRAFT_ENCODER,ForgeConstants.PLUGIN_PACKET_QUEUE, new ClientPacketQueue(StateRegistry.LOGIN));
}
}
return true;