This commit is contained in:
creeper123123321 2020-12-05 16:06:58 -03:00
parent 1946e4ed83
commit 51ae50e268
2 changed files with 17 additions and 9 deletions

View File

@ -30,7 +30,6 @@ import com.github.creeper123123321.viafabric.service.ProtocolAutoDetector;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.util.ReferenceCountUtil;
import us.myles.ViaVersion.api.Pair;
import java.net.InetSocketAddress;
@ -59,8 +58,6 @@ public class ProtocolDetectionHandler extends ChannelDuplexHandler {
}, 10, TimeUnit.SECONDS);
ProtocolAutoDetector.SERVER_VER.get(((InetSocketAddress) ctx.channel().remoteAddress()))
.whenComplete((obj, ex) -> {
hold = false;
drainQueue(ctx);
ctx.pipeline().remove(this);
timeoutRun.cancel(false);
});
@ -91,6 +88,12 @@ public class ProtocolDetectionHandler extends ChannelDuplexHandler {
}
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
drainQueue(ctx);
super.channelInactive(ctx);
}
private void drainQueue(ChannelHandlerContext ctx) {
queuedMessages.forEach(it -> ctx.write(it.getKey(), it.getValue()));
queuedMessages.clear();
@ -100,8 +103,7 @@ public class ProtocolDetectionHandler extends ChannelDuplexHandler {
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
queuedMessages.forEach(ReferenceCountUtil::release);
queuedMessages.clear();
drainQueue(ctx);
super.handlerRemoved(ctx);
}
}

View File

@ -42,6 +42,7 @@ import net.minecraft.network.packet.c2s.handshake.HandshakeC2SPacket;
import net.minecraft.network.packet.c2s.query.QueryRequestC2SPacket;
import net.minecraft.network.packet.s2c.query.QueryPongS2CPacket;
import net.minecraft.network.packet.s2c.query.QueryResponseS2CPacket;
import net.minecraft.server.ServerMetadata;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import us.myles.ViaVersion.api.protocol.ProtocolVersion;
@ -92,10 +93,15 @@ public class ProtocolAutoDetector {
clientConnection.setPacketListener(new ClientQueryPacketListener() {
@Override
public void onResponse(QueryResponseS2CPacket packet) {
ProtocolVersion ver = ProtocolVersion.getProtocol(packet.getServerMetadata().getVersion()
.getProtocolVersion());
future.complete(ver);
ViaFabric.JLOGGER.info("Auto-detected " + ver + " for " + address);
ServerMetadata meta = packet.getServerMetadata();
ServerMetadata.Version version;
if (meta != null && (version = meta.getVersion()) != null) {
ProtocolVersion ver = ProtocolVersion.getProtocol(version.getProtocolVersion());
future.complete(ver);
ViaFabric.JLOGGER.info("Auto-detected " + ver + " for " + address);
} else {
future.completeExceptionally(new IllegalArgumentException("Null version in query response"));
}
clientConnection.disconnect(new LiteralText(""));
}