Add ProtocolTranslator#getTargetVersion with channel argument

This commit is contained in:
FlorianMichael 2024-05-25 22:22:44 +02:00
parent 73d21aaf2c
commit 17ecf0c736
No known key found for this signature in database
GPG Key ID: C2FB87E71C425126
3 changed files with 17 additions and 7 deletions

View File

@ -74,11 +74,8 @@ public class ViaFabricPlusProtocol extends AbstractSimpleProtocol {
final ProtocolVersion version = wrapper.user().getProtocolInfo().serverProtocolVersion();
if (!payloadDiff.containsKey(channel) || version.olderThan(payloadDiff.get(channel).getLeft())) {
// Technically, it's wrong to just drop all payloads.
// However, ViaVersion doesn't translate them and the server can't detect if
// we handled the payload or not,
// so dropping them is easier
// than adding a bunch of useless translations for payloads
// Technically, it's wrong to just drop all payloads. However, ViaVersion doesn't translate them and the server can't detect if
// we handled the payload or not, so dropping them is easier than adding a bunch of useless translations for payloads
// which don't do anything on the client anyway.
wrapper.cancel();
return;
@ -101,7 +98,7 @@ public class ViaFabricPlusProtocol extends AbstractSimpleProtocol {
public void init(UserConnection connection) {
super.init(connection);
final ProtocolVersion serverVersion = connection.getChannel().attr(ProtocolTranslator.TARGET_VERSION_ATTRIBUTE_KEY).get();
final ProtocolVersion serverVersion = ProtocolTranslator.getTargetVersion(connection.getChannel());
// Add storages we need for different fixes here
if (serverVersion.equals(BedrockProtocolVersion.bedrockLatest)) {

View File

@ -158,6 +158,19 @@ public class ProtocolTranslator {
return targetVersion;
}
/**
* Gets the target version from the channel attribute, can be used in early stages of the connection
*
* @param channel the channel
* @return the target version
*/
public static ProtocolVersion getTargetVersion(final Channel channel) {
if (channel == null || !channel.hasAttr(TARGET_VERSION_ATTRIBUTE_KEY)) {
throw new IllegalStateException("Target version attribute not set");
}
return channel.attr(TARGET_VERSION_ATTRIBUTE_KEY).get();
}
/**
* Sets the target version
*

View File

@ -29,7 +29,7 @@ public class ViaFabricPlusBaseVersionProvider extends BaseVersionProvider {
@Override
public ProtocolVersion getClosestServerProtocol(UserConnection connection) throws Exception {
if (connection.isClientSide()) {
return connection.getChannel().attr(ProtocolTranslator.TARGET_VERSION_ATTRIBUTE_KEY).get();
return ProtocolTranslator.getTargetVersion(connection.getChannel());
} else {
return super.getClosestServerProtocol(connection);
}