3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-20 06:50:08 +01:00

Cleanup config packet registering in AbstractProtocol

Dieser Commit ist enthalten in:
Nassim Jahnke 2024-01-26 14:35:19 +01:00
Ursprung 37561705ed
Commit ce8fab7c44
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: EF6771C01F6EF02F
5 geänderte Dateien mit 56 neuen und 24 gelöschten Zeilen

Datei anzeigen

@ -125,43 +125,51 @@ public abstract class AbstractProtocol<CU extends ClientboundPacketType, CM exte
// even if there will be multiple of these handlers
final SU configurationAcknowledgedPacket = configurationAcknowledgedPacket();
if (configurationAcknowledgedPacket != null) {
registerServerbound(configurationAcknowledgedPacket, setClientStateHandler(State.CONFIGURATION));
appendServerbound(configurationAcknowledgedPacket, setClientStateHandler(State.CONFIGURATION));
}
final CU startConfigurationPacket = startConfigurationPacket();
if (startConfigurationPacket != null) {
registerClientbound(startConfigurationPacket, setServerStateHandler(State.CONFIGURATION));
appendClientbound(startConfigurationPacket, setServerStateHandler(State.CONFIGURATION));
}
final ServerboundPacketType finishConfigurationPacket = serverboundFinishConfigurationPacket();
if (finishConfigurationPacket != null) {
final int id = finishConfigurationPacket.getId();
final PacketMapping mapping = serverboundMappings.mappedPacket(State.CONFIGURATION, id); // Use existing handler if present
registerServerbound(State.CONFIGURATION, id, id, wrapper -> {
if (mapping != null) {
mapping.applyType(wrapper);
if (mapping.handler() != null) {
mapping.handler().handle(wrapper);
}
}
setClientStateHandler(State.PLAY).handle(wrapper);
}, true);
appendServerbound(finishConfigurationPacket, setClientStateHandler(State.PLAY));
}
final ClientboundPacketType clientboundFinishConfigurationPacket = clientboundFinishConfigurationPacket();
if (clientboundFinishConfigurationPacket != null) {
final int id = clientboundFinishConfigurationPacket.getId();
final PacketMapping mapping = clientboundMappings.mappedPacket(State.CONFIGURATION, id); // Use existing handler if present
registerClientbound(State.CONFIGURATION, id, id, wrapper -> {
appendClientbound(clientboundFinishConfigurationPacket, setServerStateHandler(State.PLAY));
}
}
private void appendClientbound(final ClientboundPacketType type, final PacketHandler handler) {
final PacketMapping mapping = clientboundMappings.mappedPacket(type.state(), type.getId()); // Use existing handler if present
final PacketHandler newHandler;
final int mappedPacketId;
if (mapping != null) {
mapping.applyType(wrapper);
if (mapping.handler() != null) {
mapping.handler().handle(wrapper);
newHandler = mapping.handler().append(handler);
mappedPacketId = mapping.mappedPacketId() != null ? mapping.mappedPacketId() : type.getId();
} else {
newHandler = handler;
mappedPacketId = type.getId();
}
registerClientbound(type.state(), type.getId(), mappedPacketId, newHandler, true);
}
setServerStateHandler(State.PLAY).handle(wrapper);
}, true);
private void appendServerbound(final ServerboundPacketType type, final PacketHandler handler) {
final PacketMapping mapping = serverboundMappings.mappedPacket(type.state(), type.getId()); // Use existing handler if present
final PacketHandler newHandler;
final int mappedPacketId;
if (mapping != null) {
newHandler = mapping.handler().append(handler);
mappedPacketId = mapping.mappedPacketId() != null ? mapping.mappedPacketId() : type.getId();
} else {
newHandler = handler;
mappedPacketId = type.getId();
}
registerServerbound(type.state(), type.getId(), mappedPacketId, newHandler, true);
}
private <U extends PacketType, M extends PacketType> void registerPacketIdChanges(

Datei anzeigen

@ -41,6 +41,11 @@ final class PacketIdMapping implements PacketMapping {
wrapper.setId(mappedPacketId);
}
@Override
public Integer mappedPacketId() {
return mappedPacketId;
}
@Override
public @Nullable PacketHandler handler() {
return handler;

Datei anzeigen

@ -39,6 +39,13 @@ public interface PacketMapping {
*/
void applyType(PacketWrapper wrapper);
/**
* Returns the mapped packet id if present.
*
* @return mapped packet type, or null if no action has to be taken
*/
@Nullable Integer mappedPacketId();
/**
* Returns a packet transformer to transform a packet from one protocol version to another.
*

Datei anzeigen

@ -43,6 +43,11 @@ final class PacketTypeMapping implements PacketMapping {
}
}
@Override
public @Nullable Integer mappedPacketId() {
return mappedPacketType != null ? mappedPacketType.getId() : null;
}
@Override
public @Nullable PacketHandler handler() {
return handler;

Datei anzeigen

@ -34,4 +34,11 @@ public interface PacketHandler {
* @throws Exception if an error occurs during the packet handling
*/
void handle(PacketWrapper wrapper) throws Exception;
default PacketHandler append(final PacketHandler handler) {
return wrapper -> {
this.handle(wrapper);
handler.handle(wrapper);
};
}
}