Skip to content

Commit 4b02251

Browse files
committed
Use client version to roughly determine known packs
1 parent 4ad8a9c commit 4b02251

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

proxy/src/main/java/com/velocitypowered/proxy/connection/backend/ConfigSessionHandler.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
import io.netty.buffer.Unpooled;
6363
import java.io.IOException;
6464
import java.net.InetSocketAddress;
65+
import java.util.Arrays;
66+
import java.util.List;
6567
import java.util.concurrent.CompletableFuture;
6668
import io.netty.buffer.ByteBuf;
6769
import io.netty.buffer.Unpooled;
@@ -327,7 +329,8 @@ public boolean handle(TransferPacket packet) {
327329
public boolean handle(KnownPacksPacket packet) {
328330
// Server expects us to reply to this packet
329331
if (serverConn.getPlayer().getConnection().getState() != StateRegistry.CONFIG) {
330-
serverConn.ensureConnected().write(new KnownPacksPacket());
332+
List<KnownPacksPacket.KnownPack> clientPacks = List.of(new KnownPacksPacket.KnownPack("minecraft", "core", serverConn.getPlayer().getProtocolVersion().getVersionIntroducedIn()));
333+
serverConn.ensureConnected().write(new KnownPacksPacket(Arrays.stream(packet.getPacks()).distinct().filter(clientPacks::contains).toArray(KnownPacksPacket.KnownPack[]::new)));
331334
return true;
332335
}
333336
return false; // forward

proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/config/KnownPacksPacket.java

+39
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import com.velocitypowered.proxy.util.except.QuietDecoderException;
2525
import io.netty.buffer.ByteBuf;
2626

27+
import java.util.Arrays;
28+
import java.util.Objects;
29+
2730
public class KnownPacksPacket implements MinecraftPacket {
2831

2932
private static final int MAX_LENGTH_PACKS = Integer.getInteger("velocity.max-known-packs", 64);
@@ -36,6 +39,10 @@ public KnownPacksPacket() {
3639
packs = new KnownPack[0];
3740
}
3841

42+
public KnownPacksPacket(KnownPack[] packs) {
43+
this.packs = packs;
44+
}
45+
3946
@Override
4047
public void decode(ByteBuf buf, ProtocolUtils.Direction direction,
4148
ProtocolVersion protocolVersion) {
@@ -68,6 +75,17 @@ public boolean handle(MinecraftSessionHandler handler) {
6875
return handler.handle(this);
6976
}
7077

78+
public KnownPack[] getPacks() {
79+
return packs;
80+
}
81+
82+
@Override
83+
public String toString() {
84+
return "KnownPacksPacket{" +
85+
"packs=" + Arrays.toString(packs) +
86+
'}';
87+
}
88+
7189
public record KnownPack(String namespace, String id, String version) {
7290
private static KnownPack read(ByteBuf buf) {
7391
return new KnownPack(ProtocolUtils.readString(buf), ProtocolUtils.readString(buf), ProtocolUtils.readString(buf));
@@ -78,5 +96,26 @@ private void write(ByteBuf buf) {
7896
ProtocolUtils.writeString(buf, id);
7997
ProtocolUtils.writeString(buf, version);
8098
}
99+
100+
@Override
101+
public boolean equals(Object o) {
102+
if (o == null || getClass() != o.getClass()) return false;
103+
KnownPack knownPack = (KnownPack) o;
104+
return Objects.equals(id, knownPack.id) && Objects.equals(version, knownPack.version) && Objects.equals(namespace, knownPack.namespace);
105+
}
106+
107+
@Override
108+
public int hashCode() {
109+
return Objects.hash(namespace, id, version);
110+
}
111+
112+
@Override
113+
public String toString() {
114+
return "KnownPack{" +
115+
"namespace='" + namespace + '\'' +
116+
", id='" + id + '\'' +
117+
", version='" + version + '\'' +
118+
'}';
119+
}
81120
}
82121
}

0 commit comments

Comments
 (0)