Skip to content

Commit

Permalink
Use client version to roughly determine known packs
Browse files Browse the repository at this point in the history
  • Loading branch information
valaphee committed Dec 30, 2024
1 parent 3801214 commit 3e9af9d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
import com.velocitypowered.proxy.protocol.util.PluginMessageUtil;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
Expand Down Expand Up @@ -301,7 +303,8 @@ public boolean handle(TransferPacket packet) {
public boolean handle(KnownPacksPacket packet) {
// Server expects us to reply to this packet
if (serverConn.getPlayer().getConnection().getState() != StateRegistry.CONFIG) {
serverConn.ensureConnected().write(new KnownPacksPacket());
List<KnownPacksPacket.KnownPack> clientPacks = List.of(new KnownPacksPacket.KnownPack("minecraft", "core", serverConn.getPlayer().getProtocolVersion().getVersionIntroducedIn()));
serverConn.ensureConnected().write(new KnownPacksPacket(Arrays.stream(packet.getPacks()).distinct().filter(clientPacks::contains).toArray(KnownPacksPacket.KnownPack[]::new)));
return true;
}
return false; // forward
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import com.velocitypowered.proxy.util.except.QuietDecoderException;
import io.netty.buffer.ByteBuf;

import java.util.Arrays;
import java.util.Objects;

public class KnownPacksPacket implements MinecraftPacket {

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

public KnownPacksPacket(KnownPack[] packs) {
this.packs = packs;
}

@Override
public void decode(ByteBuf buf, ProtocolUtils.Direction direction,
ProtocolVersion protocolVersion) {
Expand Down Expand Up @@ -68,6 +75,17 @@ public boolean handle(MinecraftSessionHandler handler) {
return handler.handle(this);
}

public KnownPack[] getPacks() {
return packs;
}

@Override
public String toString() {
return "KnownPacksPacket{" +
"packs=" + Arrays.toString(packs) +
'}';
}

public record KnownPack(String namespace, String id, String version) {
private static KnownPack read(ByteBuf buf) {
return new KnownPack(ProtocolUtils.readString(buf), ProtocolUtils.readString(buf), ProtocolUtils.readString(buf));
Expand All @@ -78,5 +96,26 @@ private void write(ByteBuf buf) {
ProtocolUtils.writeString(buf, id);
ProtocolUtils.writeString(buf, version);
}

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
KnownPack knownPack = (KnownPack) o;
return Objects.equals(id, knownPack.id) && Objects.equals(version, knownPack.version) && Objects.equals(namespace, knownPack.namespace);
}

@Override
public int hashCode() {
return Objects.hash(namespace, id, version);
}

@Override
public String toString() {
return "KnownPack{" +
"namespace='" + namespace + '\'' +
", id='" + id + '\'' +
", version='" + version + '\'' +
'}';
}
}
}

0 comments on commit 3e9af9d

Please sign in to comment.