Added API method to get real protocol version from shifted legacy version

This commit is contained in:
RaphiMC 2023-08-12 02:35:25 +02:00
parent 0181e3dc05
commit 0e02ecf818
No known key found for this signature in database
GPG Key ID: 0F6BB0657A03AC94
4 changed files with 18 additions and 7 deletions

View File

@ -70,13 +70,13 @@ public class LegacyProtocolVersion {
public static int protocolCompare(int a, int b) {
if (a > 0 || b > 0) {
if (a >= 0 || b >= 0) {
// If at least one is modern, then a straight compare works fine.
return a - b;
}
// Both are legacy
a = Math.abs(a);
b = Math.abs(b);
a = -a;
b = -b;
final int baseProtocolA = a >> 2;
final int baseProtocolB = b >> 2;
if (baseProtocolA != baseProtocolB) {
@ -88,6 +88,14 @@ public class LegacyProtocolVersion {
return discriminatorB - discriminatorA; // Higher discriminator means older version
}
public static int getRealProtocolVersion(final int protocolVersion) {
if (protocolVersion >= 0) {
return protocolVersion;
}
return -protocolVersion >> 2;
}
private static ProtocolVersion registerLegacy(final int version, final String name) {
return registerLegacy(version, name, null);

View File

@ -39,6 +39,7 @@ import com.viaversion.viaversion.libs.opennbt.tag.builtin.ShortTag;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag;
import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
import net.raphimc.vialegacy.ViaLegacy;
import net.raphimc.vialegacy.api.LegacyProtocolVersion;
import net.raphimc.vialegacy.api.data.BlockList1_6;
import net.raphimc.vialegacy.api.model.IdAndData;
import net.raphimc.vialegacy.api.model.Location;
@ -838,7 +839,7 @@ public class Protocol1_3_1_2to1_2_4_5 extends AbstractProtocol<ClientboundPacket
}
final PacketWrapper login = PacketWrapper.create(ServerboundPackets1_2_4.LOGIN, wrapper.user());
login.write(Type.INT, -(info.getServerProtocolVersion() >> 2)); // protocol id
login.write(Type.INT, LegacyProtocolVersion.getRealProtocolVersion(info.getServerProtocolVersion())); // protocol id
login.write(Types1_6_4.STRING, info.getUsername()); // username
login.write(Types1_6_4.STRING, ""); // level type
login.write(Type.INT, 0); // game mode

View File

@ -31,6 +31,7 @@ import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import com.viaversion.viaversion.api.type.Type;
import net.raphimc.vialegacy.ViaLegacy;
import net.raphimc.vialegacy.api.LegacyProtocolVersion;
import net.raphimc.vialegacy.api.data.ItemList1_6;
import net.raphimc.vialegacy.api.remapper.LegacyItemRewriter;
import net.raphimc.vialegacy.api.splitter.PreNettySplitter;
@ -69,7 +70,7 @@ public class Protocol1_4_2to1_3_1_2 extends AbstractProtocol<ClientboundPackets1
try {
final ProtocolInfo info = wrapper.user().getProtocolInfo();
final String[] pingParts = reason.split("§");
final String out = "§1\0" + (-info.getServerProtocolVersion() >> 2) + "\0" + ProtocolVersion.getProtocol(info.getServerProtocolVersion()).getName() + "\0" + pingParts[0] + "\0" + pingParts[1] + "\0" + pingParts[2];
final String out = "§1\0" + LegacyProtocolVersion.getRealProtocolVersion(info.getServerProtocolVersion()) + "\0" + ProtocolVersion.getProtocol(info.getServerProtocolVersion()).getName() + "\0" + pingParts[0] + "\0" + pingParts[1] + "\0" + pingParts[2];
wrapper.write(Types1_6_4.STRING, out);
} catch (Throwable e) {
ViaLegacy.getPlatform().getLogger().log(Level.WARNING, "Could not parse 1.3.1 ping: " + reason, e);

View File

@ -47,6 +47,7 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import net.raphimc.vialegacy.ViaLegacy;
import net.raphimc.vialegacy.api.LegacyProtocolVersion;
import net.raphimc.vialegacy.api.model.IdAndData;
import net.raphimc.vialegacy.api.remapper.LegacyItemRewriter;
import net.raphimc.vialegacy.api.splitter.PreNettySplitter;
@ -900,7 +901,7 @@ public class Protocol1_7_2_5to1_6_4 extends AbstractProtocol<ClientboundPackets1
wrapper.write(Type.UNSIGNED_BYTE, (short) 250); // packet id
wrapper.write(Types1_6_4.STRING, "MC|PingHost"); // channel
wrapper.write(Type.UNSIGNED_SHORT, 3 + 2 * ip.length() + 4); // length
wrapper.write(Type.UNSIGNED_BYTE, (short) (-wrapper.user().getProtocolInfo().getServerProtocolVersion() >> 2)); // protocol Id
wrapper.write(Type.UNSIGNED_BYTE, (short) LegacyProtocolVersion.getRealProtocolVersion(wrapper.user().getProtocolInfo().getServerProtocolVersion())); // protocol Id
wrapper.write(Types1_6_4.STRING, ip); // hostname
wrapper.write(Type.INT, port); // port
});
@ -927,7 +928,7 @@ public class Protocol1_7_2_5to1_6_4 extends AbstractProtocol<ClientboundPackets1
info.setUsername(name);
info.setUuid(ViaLegacy.getConfig().isLegacySkinLoading() ? Via.getManager().getProviders().get(GameProfileFetcher.class).getMojangUUID(name) : new GameProfile(name).uuid);
wrapper.write(Type.UNSIGNED_BYTE, (short) (-info.getServerProtocolVersion() >> 2)); // protocol id
wrapper.write(Type.UNSIGNED_BYTE, (short) LegacyProtocolVersion.getRealProtocolVersion(info.getServerProtocolVersion())); // protocol id
wrapper.write(Types1_6_4.STRING, name); // user name
wrapper.write(Types1_6_4.STRING, handshakeStorage.getHostname()); // hostname
wrapper.write(Type.INT, handshakeStorage.getPort()); // port