Add more API for base protocols and skip manually added ones. (#4115)

This commit is contained in:
EnZaXD 2024-08-22 17:15:29 +02:00 committed by GitHub
parent b586b0d361
commit b2d523f5c0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 11 deletions

View File

@ -24,9 +24,10 @@ import java.util.Comparator;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* A {@link ProtocolVersion} with the version type {@link VersionType#SPECIAL} that compares equal to the given
* origin version. The origin version will also be used in {@link com.viaversion.viaversion.protocols.base.InitialBaseProtocol}
* to determine the correct base protocol.
* Intended API class for protocol versions with the version type {@link VersionType#SPECIAL}.
* <p>
* Compares equal to the given origin version and allows base protocol determination via {@link #getBaseProtocolVersion()}
* which can be null for special cases where there is no base protocol.
*/
public class RedirectProtocolVersion extends ProtocolVersion {
@ -56,4 +57,11 @@ public class RedirectProtocolVersion extends ProtocolVersion {
public ProtocolVersion getOrigin() {
return origin;
}
/**
* @return the protocol version used to determine the base protocol, null in case there is no base protocol.
*/
public @Nullable ProtocolVersion getBaseProtocolVersion() {
return origin;
}
}

View File

@ -96,17 +96,22 @@ public class InitialBaseProtocol extends AbstractProtocol<BaseClientboundPacket,
ProtocolManager protocolManager = Via.getManager().getProtocolManager();
List<ProtocolPathEntry> protocolPath = protocolManager.getProtocolPath(info.protocolVersion(), serverProtocol);
// Add Base Protocol
ProtocolPipeline pipeline = info.getPipeline();
// Special versions might compare equal to normal versions and would break this getter,
// platforms either need to use the RedirectProtocolVersion API or add the base protocols manually
// Save manually added protocols for later
List<Protocol> alreadyAdded = new ArrayList<>(pipeline.pipes());
// Special versions might compare equal to normal versions and would the normal lookup,
// platforms can use the RedirectProtocolVersion API or need to manually handle their base protocols.
ProtocolVersion baseProtocolVersion = null;
if (serverProtocol.getVersionType() != VersionType.SPECIAL) {
for (final Protocol protocol : protocolManager.getBaseProtocols(serverProtocol)) {
pipeline.add(protocol);
}
baseProtocolVersion = serverProtocol;
} else if (serverProtocol instanceof RedirectProtocolVersion version) {
for (final Protocol protocol : protocolManager.getBaseProtocols(version.getOrigin())) {
baseProtocolVersion = version.getBaseProtocolVersion();
}
if (baseProtocolVersion != null) {
// Add base protocols
for (final Protocol protocol : protocolManager.getBaseProtocols(baseProtocolVersion)) {
pipeline.add(protocol);
}
}
@ -131,7 +136,7 @@ public class InitialBaseProtocol extends AbstractProtocol<BaseClientboundPacket,
// Send client intention into the pipeline in case protocols down the line need to transform it
try {
final List<Protocol> protocols = new ArrayList<>(pipeline.pipes());
protocols.remove(this);
protocols.removeAll(alreadyAdded); // Skip all manually added protocols to prevent double handling
wrapper.apply(Direction.SERVERBOUND, State.HANDSHAKE, protocols);
} catch (CancelException e) {
wrapper.cancel();