WIP: listen to response

This commit is contained in:
Adrian Bergqvist 2022-09-15 08:23:24 +02:00
parent fcb2491c66
commit fe69711818
4 changed files with 25 additions and 11 deletions

View File

@ -1,5 +1,6 @@
package org.adde0109.ambassador.forge; package org.adde0109.ambassador.forge;
import com.velocitypowered.api.event.Continuation;
import com.velocitypowered.proxy.connection.MinecraftConnection; import com.velocitypowered.proxy.connection.MinecraftConnection;
import com.velocitypowered.proxy.connection.client.ClientConnectionPhase; import com.velocitypowered.proxy.connection.client.ClientConnectionPhase;
import com.velocitypowered.proxy.connection.client.ConnectedPlayer; import com.velocitypowered.proxy.connection.client.ConnectedPlayer;
@ -17,30 +18,37 @@ public class ForgeFML2ClientConnectionPhase implements VelocityForgeClientConnec
private final MinecraftConnection connection; private final MinecraftConnection connection;
private boolean isResettable; private boolean isResettable;
private ByteBuf modListData; private ByteBuf modListData;
private Continuation whenComplete;
ForgeFML2ClientConnectionPhase(MinecraftConnection connection) { ForgeFML2ClientConnectionPhase(MinecraftConnection connection) {
this.connection = connection; this.connection = connection;
} }
@Override @Override
public void handleLogin(@Nullable ForgeHandshakeUtils.CachedServerHandshake handshake) { public void handleLogin(ForgeHandshakeUtils.CachedServerHandshake handshake, Continuation continuation) {
VelocityForgeHandshakeSessionHandler sessionHandler = new VelocityForgeHandshakeSessionHandler(this);
if(handshake == null) { if(handshake == null) {
this.connection.write(new LoginPluginMessage(98,"fml:loginwrapper", Unpooled.wrappedBuffer(ForgeHandshakeUtils.generateResetPacket()))); this.connection.write(new LoginPluginMessage(98,"fml:loginwrapper", Unpooled.wrappedBuffer(ForgeHandshakeUtils.generateResetPacket())));
sessionHandler.listen(98);
} else { } else {
this.connection.delayedWrite(new LoginPluginMessage(1,"fml:loginwrapper", Unpooled.wrappedBuffer(handshake.modListPacket))); this.connection.delayedWrite(new LoginPluginMessage(1,"fml:loginwrapper", Unpooled.wrappedBuffer(handshake.modListPacket)));
sessionHandler.listen(1);
for (int i = 0;i<handshake.otherPackets.size();i++) { for (int i = 0;i<handshake.otherPackets.size();i++) {
this.connection.delayedWrite(new LoginPluginMessage(i+2,"fml:loginwrapper", Unpooled.wrappedBuffer(handshake.otherPackets.get(i)))); this.connection.delayedWrite(new LoginPluginMessage(i+2,"fml:loginwrapper", Unpooled.wrappedBuffer(handshake.otherPackets.get(i))));
sessionHandler.listen(i+2);
} }
//TODO:Register sent packets this.connection.setSessionHandler(sessionHandler);
this.connection.setSessionHandler(new VelocityForgeHandshakeSessionHandler(this));
this.connection.flush(); this.connection.flush();
} }
} }
@Override @Override
public void handle(LoginPluginResponse packet) { public void handle(LoginPluginResponse packet, boolean lastMessage) {
if (packet.getId() == 98) { if (packet.getId() == 98) {
isResettable = packet.isSuccess(); isResettable = packet.isSuccess();
} else { } else {
modListData = packet.content().retain(); modListData = packet.content().retain();
} }
if (lastMessage) {
whenComplete.resume();
}
} }
} }

View File

@ -42,7 +42,7 @@ public class ForgeHandshakeHandler {
if (ex != null) { if (ex != null) {
ambassador.logger.warn("Forge player, " + player.getUsername() + ", is entering vanilla-mode because of: " + ex.getMessage()); ambassador.logger.warn("Forge player, " + player.getUsername() + ", is entering vanilla-mode because of: " + ex.getMessage());
} }
phase.handleLogin(msg); phase.handleLogin(msg,continuation);
}, player.getConnection().eventLoop()); }, player.getConnection().eventLoop());
} }

View File

@ -1,5 +1,6 @@
package org.adde0109.ambassador.velocity; package org.adde0109.ambassador.velocity;
import com.velocitypowered.api.event.Continuation;
import com.velocitypowered.proxy.connection.MinecraftConnection; import com.velocitypowered.proxy.connection.MinecraftConnection;
import com.velocitypowered.proxy.connection.client.ClientConnectionPhase; import com.velocitypowered.proxy.connection.client.ClientConnectionPhase;
import com.velocitypowered.proxy.protocol.packet.LoginPluginResponse; import com.velocitypowered.proxy.protocol.packet.LoginPluginResponse;
@ -9,8 +10,8 @@ import javax.annotation.Nullable;
public interface VelocityForgeClientConnectionPhase extends ClientConnectionPhase { public interface VelocityForgeClientConnectionPhase extends ClientConnectionPhase {
default void handleLogin(@Nullable ForgeHandshakeUtils.CachedServerHandshake initialHandshake) { default void handleLogin(@Nullable ForgeHandshakeUtils.CachedServerHandshake initialHandshake, Continuation continuation) {
} }
default void handle(LoginPluginResponse packet) { default void handle(LoginPluginResponse packet, boolean lastMessage) {
} }
} }

View File

@ -1,11 +1,14 @@
package org.adde0109.ambassador.velocity; package org.adde0109.ambassador.velocity;
import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.connection.MinecraftSessionHandler;
import com.velocitypowered.proxy.connection.client.ClientConnectionPhase;
import com.velocitypowered.proxy.protocol.packet.LoginPluginResponse; import com.velocitypowered.proxy.protocol.packet.LoginPluginResponse;
import java.util.ArrayList;
import java.util.List;
public class VelocityForgeHandshakeSessionHandler implements MinecraftSessionHandler { public class VelocityForgeHandshakeSessionHandler implements MinecraftSessionHandler {
private final ArrayList<Integer> listenerList = new ArrayList();
private final VelocityForgeClientConnectionPhase phase; private final VelocityForgeClientConnectionPhase phase;
public VelocityForgeHandshakeSessionHandler(VelocityForgeClientConnectionPhase phase) { public VelocityForgeHandshakeSessionHandler(VelocityForgeClientConnectionPhase phase) {
this.phase = phase; this.phase = phase;
@ -13,10 +16,12 @@ public class VelocityForgeHandshakeSessionHandler implements MinecraftSessionHan
@Override @Override
public boolean handle(LoginPluginResponse packet) { public boolean handle(LoginPluginResponse packet) {
//TODO: Check if we sent it if (listenerList.removeIf(id -> id.equals(packet.getId()))) {
if (packet.getId() == 1 && packet.getId() == 98) { phase.handle(packet, listenerList.isEmpty());
phase.handle(packet);
} }
return true; return true;
} }
public void listen(int id) {
listenerList.add(id);
}
} }