This commit is contained in:
creeper123123321 2020-12-05 16:06:58 -03:00
parent 00aed06518
commit bac7e86217
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.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise; import io.netty.channel.ChannelPromise;
import io.netty.util.ReferenceCountUtil;
import us.myles.ViaVersion.api.Pair; import us.myles.ViaVersion.api.Pair;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
@ -59,8 +58,6 @@ public class ProtocolDetectionHandler extends ChannelDuplexHandler {
}, 10, TimeUnit.SECONDS); }, 10, TimeUnit.SECONDS);
ProtocolAutoDetector.SERVER_VER.get(((InetSocketAddress) ctx.channel().remoteAddress())) ProtocolAutoDetector.SERVER_VER.get(((InetSocketAddress) ctx.channel().remoteAddress()))
.whenComplete((obj, ex) -> { .whenComplete((obj, ex) -> {
hold = false;
drainQueue(ctx);
ctx.pipeline().remove(this); ctx.pipeline().remove(this);
timeoutRun.cancel(false); 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) { private void drainQueue(ChannelHandlerContext ctx) {
queuedMessages.forEach(it -> ctx.write(it.getKey(), it.getValue())); queuedMessages.forEach(it -> ctx.write(it.getKey(), it.getValue()));
queuedMessages.clear(); queuedMessages.clear();
@ -100,8 +103,7 @@ public class ProtocolDetectionHandler extends ChannelDuplexHandler {
@Override @Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
queuedMessages.forEach(ReferenceCountUtil::release); drainQueue(ctx);
queuedMessages.clear();
super.handlerRemoved(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.c2s.query.QueryRequestC2SPacket;
import net.minecraft.network.packet.s2c.query.QueryPongS2CPacket; import net.minecraft.network.packet.s2c.query.QueryPongS2CPacket;
import net.minecraft.network.packet.s2c.query.QueryResponseS2CPacket; import net.minecraft.network.packet.s2c.query.QueryResponseS2CPacket;
import net.minecraft.server.ServerMetadata;
import net.minecraft.text.LiteralText; import net.minecraft.text.LiteralText;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import us.myles.ViaVersion.api.protocol.ProtocolVersion; import us.myles.ViaVersion.api.protocol.ProtocolVersion;
@ -92,10 +93,15 @@ public class ProtocolAutoDetector {
clientConnection.setPacketListener(new ClientQueryPacketListener() { clientConnection.setPacketListener(new ClientQueryPacketListener() {
@Override @Override
public void onResponse(QueryResponseS2CPacket packet) { public void onResponse(QueryResponseS2CPacket packet) {
ProtocolVersion ver = ProtocolVersion.getProtocol(packet.getServerMetadata().getVersion() ServerMetadata meta = packet.getServerMetadata();
.getProtocolVersion()); ServerMetadata.Version version;
future.complete(ver); if (meta != null && (version = meta.getVersion()) != null) {
ViaFabric.JLOGGER.info("Auto-detected " + ver + " for " + address); 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(LiteralText.EMPTY); clientConnection.disconnect(LiteralText.EMPTY);
} }