Search in sources :

Example 11 with Protocol

use of com.viaversion.viaversion.api.protocol.Protocol in project ViaVersion by ViaVersion.

the class ProtocolManagerImpl method getProtocolPath.

/**
 * Calculates a path to get from an input protocol to the server's protocol.
 *
 * @param current       current items in the path
 * @param clientVersion current input version
 * @param serverVersion desired output version
 * @return path that has been generated, null if failed
 */
@Nullable
private Int2ObjectSortedMap<Protocol> getProtocolPath(Int2ObjectSortedMap<Protocol> current, int clientVersion, int serverVersion) {
    // Fail-safe, protocol too complicated.
    if (current.size() > maxProtocolPathSize)
        return null;
    // First, check if there is any protocols for this
    Int2ObjectMap<Protocol> toServerProtocolMap = registryMap.get(clientVersion);
    if (toServerProtocolMap == null) {
        // Not supported
        return null;
    }
    // Next, check if there is a direct, single Protocol path
    Protocol protocol = toServerProtocolMap.get(serverVersion);
    if (protocol != null) {
        current.put(serverVersion, protocol);
        // Easy solution
        return current;
    }
    // There might be a more advanced solution... So we'll see if any of the others can get us there
    Int2ObjectSortedMap<Protocol> shortest = null;
    for (Int2ObjectMap.Entry<Protocol> entry : toServerProtocolMap.int2ObjectEntrySet()) {
        // Ensure we don't go back to already contained versions
        int translatedToVersion = entry.getIntKey();
        if (current.containsKey(translatedToVersion))
            continue;
        // Check if the new version is farther away than the current client version
        if (onlyCheckLoweringPathEntries && Math.abs(serverVersion - translatedToVersion) > Math.abs(serverVersion - clientVersion)) {
            continue;
        }
        // Create a copy
        Int2ObjectSortedMap<Protocol> newCurrent = new Int2ObjectLinkedOpenHashMap<>(current);
        newCurrent.put(translatedToVersion, entry.getValue());
        // Calculate the rest of the protocol starting from translatedToVersion and take the shortest
        newCurrent = getProtocolPath(newCurrent, translatedToVersion, serverVersion);
        if (newCurrent != null && (shortest == null || newCurrent.size() < shortest.size())) {
            shortest = newCurrent;
        }
    }
    // null if none found
    return shortest;
}
Also used : Int2ObjectMap(it.unimi.dsi.fastutil.ints.Int2ObjectMap) Int2ObjectLinkedOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectLinkedOpenHashMap) Protocol(com.viaversion.viaversion.api.protocol.Protocol) BaseProtocol(com.viaversion.viaversion.protocols.base.BaseProtocol) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 12 with Protocol

use of com.viaversion.viaversion.api.protocol.Protocol in project ViaVersion by ViaVersion.

the class ProtocolManagerImpl method getProtocolPath.

@Override
@Nullable
public List<ProtocolPathEntry> getProtocolPath(int clientVersion, int serverVersion) {
    // Nothing to do!
    if (clientVersion == serverVersion)
        return null;
    // Check cache
    ProtocolPathKey protocolKey = new ProtocolPathKeyImpl(clientVersion, serverVersion);
    List<ProtocolPathEntry> protocolList = pathCache.get(protocolKey);
    if (protocolList != null) {
        return protocolList;
    }
    // Calculate path
    Int2ObjectSortedMap<Protocol> outputPath = getProtocolPath(new Int2ObjectLinkedOpenHashMap<>(), clientVersion, serverVersion);
    if (outputPath == null) {
        return null;
    }
    List<ProtocolPathEntry> path = new ArrayList<>(outputPath.size());
    for (Int2ObjectMap.Entry<Protocol> entry : outputPath.int2ObjectEntrySet()) {
        path.add(new ProtocolPathEntryImpl(entry.getIntKey(), entry.getValue()));
    }
    pathCache.put(protocolKey, path);
    return path;
}
Also used : ProtocolPathEntry(com.viaversion.viaversion.api.protocol.ProtocolPathEntry) ProtocolPathKey(com.viaversion.viaversion.api.protocol.ProtocolPathKey) ArrayList(java.util.ArrayList) Int2ObjectMap(it.unimi.dsi.fastutil.ints.Int2ObjectMap) Protocol(com.viaversion.viaversion.api.protocol.Protocol) BaseProtocol(com.viaversion.viaversion.protocols.base.BaseProtocol) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 13 with Protocol

use of com.viaversion.viaversion.api.protocol.Protocol in project ViaVersion by ViaVersion.

the class EntityPackets method register.

public static void register(Protocol protocol) {
    final PacketHandler metaTypeHandler = wrapper -> {
        for (Metadata metadata : wrapper.get(Types1_13_2.METADATA_LIST, 0)) {
            metadata.setMetaType(Types1_13_2.META_TYPES.byId(metadata.metaType().typeId()));
        }
    };
    protocol.registerClientbound(ClientboundPackets1_13.SPAWN_MOB, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Entity UUID
            map(Type.UUID);
            // 2 - Entity Type
            map(Type.VAR_INT);
            // 3 - X
            map(Type.DOUBLE);
            // 4 - Y
            map(Type.DOUBLE);
            // 5 - Z
            map(Type.DOUBLE);
            // 6 - Yaw
            map(Type.BYTE);
            // 7 - Pitch
            map(Type.BYTE);
            // 8 - Head Pitch
            map(Type.BYTE);
            // 9 - Velocity X
            map(Type.SHORT);
            // 10 - Velocity Y
            map(Type.SHORT);
            // 11 - Velocity Z
            map(Type.SHORT);
            // 12 - Metadata
            map(Types1_13.METADATA_LIST, Types1_13_2.METADATA_LIST);
            handler(metaTypeHandler);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_13.SPAWN_PLAYER, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Player UUID
            map(Type.UUID);
            // 2 - X
            map(Type.DOUBLE);
            // 3 - Y
            map(Type.DOUBLE);
            // 4 - Z
            map(Type.DOUBLE);
            // 5 - Yaw
            map(Type.BYTE);
            // 6 - Pitch
            map(Type.BYTE);
            // 7 - Metadata
            map(Types1_13.METADATA_LIST, Types1_13_2.METADATA_LIST);
            handler(metaTypeHandler);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_13.ENTITY_METADATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Metadata list
            map(Types1_13.METADATA_LIST, Types1_13_2.METADATA_LIST);
            handler(metaTypeHandler);
        }
    });
}
Also used : PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ClientboundPackets1_13(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13) Type(com.viaversion.viaversion.api.type.Type) Types1_13_2(com.viaversion.viaversion.api.type.types.version.Types1_13_2) Protocol(com.viaversion.viaversion.api.protocol.Protocol) Types1_13(com.viaversion.viaversion.api.type.types.version.Types1_13) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata)

Aggregations

Protocol (com.viaversion.viaversion.api.protocol.Protocol)13 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)5 Type (com.viaversion.viaversion.api.type.Type)5 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)4 Chunk (com.viaversion.viaversion.api.minecraft.chunks.Chunk)3 ChunkSection (com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)3 AbstractSimpleProtocol (com.viaversion.viaversion.api.protocol.AbstractSimpleProtocol)3 ProtocolPathEntry (com.viaversion.viaversion.api.protocol.ProtocolPathEntry)3 PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)3 ArrayList (java.util.ArrayList)3 Nullable (org.checkerframework.checker.nullness.qual.Nullable)3 Via (com.viaversion.viaversion.api.Via)2 BaseProtocol (com.viaversion.viaversion.protocols.base.BaseProtocol)2 ClientboundPackets1_13 (com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13)2 ClientWorld (com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld)2 Int2ObjectMap (it.unimi.dsi.fastutil.ints.Int2ObjectMap)2 CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)1 StringTag (com.github.steveice10.opennbt.tag.builtin.StringTag)1 ProtocolInfo (com.viaversion.viaversion.api.connection.ProtocolInfo)1 StorableObject (com.viaversion.viaversion.api.connection.StorableObject)1