Search in sources :

Example 1 with Types1_16

use of com.viaversion.viaversion.api.type.types.version.Types1_16 in project ViaBackwards by ViaVersion.

the class EntityPackets1_16 method registerPackets.

@Override
protected void registerPackets() {
    protocol.registerClientbound(ClientboundPackets1_16.SPAWN_ENTITY, 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 - Pitch
            map(Type.BYTE);
            // 7 - Yaw
            map(Type.BYTE);
            // 8 - Data
            map(Type.INT);
            handler(wrapper -> {
                EntityType entityType = typeFromId(wrapper.get(Type.VAR_INT, 1));
                if (entityType == Entity1_16Types.LIGHTNING_BOLT) {
                    // Map to old weather entity packet
                    wrapper.cancel();
                    PacketWrapper spawnLightningPacket = wrapper.create(ClientboundPackets1_15.SPAWN_GLOBAL_ENTITY);
                    // Entity id
                    spawnLightningPacket.write(Type.VAR_INT, wrapper.get(Type.VAR_INT, 0));
                    // Lightning type
                    spawnLightningPacket.write(Type.BYTE, (byte) 1);
                    // X
                    spawnLightningPacket.write(Type.DOUBLE, wrapper.get(Type.DOUBLE, 0));
                    // Y
                    spawnLightningPacket.write(Type.DOUBLE, wrapper.get(Type.DOUBLE, 1));
                    // Z
                    spawnLightningPacket.write(Type.DOUBLE, wrapper.get(Type.DOUBLE, 2));
                    spawnLightningPacket.send(Protocol1_15_2To1_16.class);
                }
            });
            handler(getSpawnTrackerWithDataHandler(Entity1_16Types.FALLING_BLOCK));
        }
    });
    registerSpawnTracker(ClientboundPackets1_16.SPAWN_MOB);
    protocol.registerClientbound(ClientboundPackets1_16.RESPAWN, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Dimension Type
            map(dimensionTransformer);
            handler(wrapper -> {
                // Grab the tracker for world names
                WorldNameTracker worldNameTracker = wrapper.user().get(WorldNameTracker.class);
                // World Name
                String nextWorldName = wrapper.read(Type.STRING);
                // Seed
                wrapper.passthrough(Type.LONG);
                // Gamemode
                wrapper.passthrough(Type.UNSIGNED_BYTE);
                // Previous gamemode
                wrapper.read(Type.BYTE);
                // Grab client world
                ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                int dimension = wrapper.get(Type.INT, 0);
                // Send a dummy respawn with a different dimension if the world name was different and the same dimension was used
                if (clientWorld.getEnvironment() != null && dimension == clientWorld.getEnvironment().getId() && (wrapper.user().isClientSide() || Via.getPlatform().isProxy() || // Hotfix for https://github.com/ViaVersion/ViaBackwards/issues/381
                wrapper.user().getProtocolInfo().getProtocolVersion() <= ProtocolVersion.v1_12_2.getVersion() || !nextWorldName.equals(worldNameTracker.getWorldName()))) {
                    PacketWrapper packet = wrapper.create(ClientboundPackets1_15.RESPAWN);
                    packet.write(Type.INT, dimension == 0 ? -1 : 0);
                    packet.write(Type.LONG, 0L);
                    packet.write(Type.UNSIGNED_BYTE, (short) 0);
                    packet.write(Type.STRING, "default");
                    packet.send(Protocol1_15_2To1_16.class);
                }
                clientWorld.setEnvironment(dimension);
                // Level type
                wrapper.write(Type.STRING, "default");
                // Debug
                wrapper.read(Type.BOOLEAN);
                if (wrapper.read(Type.BOOLEAN)) {
                    wrapper.set(Type.STRING, 0, "flat");
                }
                // Keep all playerdata
                wrapper.read(Type.BOOLEAN);
                // Finally update the world name
                worldNameTracker.setWorldName(nextWorldName);
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16.JOIN_GAME, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Entity ID
            map(Type.INT);
            // Gamemode
            map(Type.UNSIGNED_BYTE);
            // Previous gamemode
            map(Type.BYTE, Type.NOTHING);
            // World list
            map(Type.STRING_ARRAY, Type.NOTHING);
            // whatever this is
            map(Type.NBT, Type.NOTHING);
            // Dimension Type
            map(dimensionTransformer);
            handler(wrapper -> {
                WorldNameTracker worldNameTracker = wrapper.user().get(WorldNameTracker.class);
                // Save the world name
                worldNameTracker.setWorldName(wrapper.read(Type.STRING));
            });
            // Seed
            map(Type.LONG);
            // Max players
            map(Type.UNSIGNED_BYTE);
            handler(wrapper -> {
                ClientWorld clientChunks = wrapper.user().get(ClientWorld.class);
                clientChunks.setEnvironment(wrapper.get(Type.INT, 1));
                tracker(wrapper.user()).addEntity(wrapper.get(Type.INT, 0), Entity1_16Types.PLAYER);
                // Level type
                wrapper.write(Type.STRING, "default");
                // View distance
                wrapper.passthrough(Type.VAR_INT);
                // Reduced debug info
                wrapper.passthrough(Type.BOOLEAN);
                // Show death screen
                wrapper.passthrough(Type.BOOLEAN);
                // Debug
                wrapper.read(Type.BOOLEAN);
                if (wrapper.read(Type.BOOLEAN)) {
                    wrapper.set(Type.STRING, 0, "flat");
                }
            });
        }
    });
    registerTracker(ClientboundPackets1_16.SPAWN_EXPERIENCE_ORB, Entity1_16Types.EXPERIENCE_ORB);
    // F Spawn Global Object, it is no longer with us :(
    registerTracker(ClientboundPackets1_16.SPAWN_PAINTING, Entity1_16Types.PAINTING);
    registerTracker(ClientboundPackets1_16.SPAWN_PLAYER, Entity1_16Types.PLAYER);
    registerRemoveEntities(ClientboundPackets1_16.DESTROY_ENTITIES);
    registerMetadataRewriter(ClientboundPackets1_16.ENTITY_METADATA, Types1_16.METADATA_LIST, Types1_14.METADATA_LIST);
    protocol.registerClientbound(ClientboundPackets1_16.ENTITY_PROPERTIES, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                wrapper.passthrough(Type.VAR_INT);
                int size = wrapper.passthrough(Type.INT);
                for (int i = 0; i < size; i++) {
                    String attributeIdentifier = wrapper.read(Type.STRING);
                    String oldKey = protocol.getMappingData().getAttributeMappings().get(attributeIdentifier);
                    wrapper.write(Type.STRING, oldKey != null ? oldKey : attributeIdentifier.replace("minecraft:", ""));
                    wrapper.passthrough(Type.DOUBLE);
                    int modifierSize = wrapper.passthrough(Type.VAR_INT);
                    for (int j = 0; j < modifierSize; j++) {
                        wrapper.passthrough(Type.UUID);
                        wrapper.passthrough(Type.DOUBLE);
                        wrapper.passthrough(Type.BYTE);
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16.PLAYER_INFO, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(packetWrapper -> {
                int action = packetWrapper.passthrough(Type.VAR_INT);
                int playerCount = packetWrapper.passthrough(Type.VAR_INT);
                for (int i = 0; i < playerCount; i++) {
                    packetWrapper.passthrough(Type.UUID);
                    if (action == 0) {
                        // Add
                        packetWrapper.passthrough(Type.STRING);
                        int properties = packetWrapper.passthrough(Type.VAR_INT);
                        for (int j = 0; j < properties; j++) {
                            packetWrapper.passthrough(Type.STRING);
                            packetWrapper.passthrough(Type.STRING);
                            if (packetWrapper.passthrough(Type.BOOLEAN)) {
                                packetWrapper.passthrough(Type.STRING);
                            }
                        }
                        packetWrapper.passthrough(Type.VAR_INT);
                        packetWrapper.passthrough(Type.VAR_INT);
                        if (packetWrapper.passthrough(Type.BOOLEAN)) {
                            // Display Name
                            protocol.getTranslatableRewriter().processText(packetWrapper.passthrough(Type.COMPONENT));
                        }
                    } else if (action == 1) {
                        // Update Game Mode
                        packetWrapper.passthrough(Type.VAR_INT);
                    } else if (action == 2) {
                        // Update Ping
                        packetWrapper.passthrough(Type.VAR_INT);
                    } else if (action == 3) {
                        // Update Display Name
                        if (packetWrapper.passthrough(Type.BOOLEAN)) {
                            // Display name
                            protocol.getTranslatableRewriter().processText(packetWrapper.passthrough(Type.COMPONENT));
                        }
                    }
                // 4 = Remove Player
                }
            });
        }
    });
}
Also used : Particle(com.viaversion.viaversion.api.type.types.Particle) Entity1_15Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_15Types) JsonElement(com.viaversion.viaversion.libs.gson.JsonElement) WorldNameTracker(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.data.WorldNameTracker) WolfDataMaskStorage(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.storage.WolfDataMaskStorage) MetaType(com.viaversion.viaversion.api.minecraft.metadata.MetaType) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) Via(com.viaversion.viaversion.api.Via) Item(com.viaversion.viaversion.api.minecraft.item.Item) ProtocolVersion(com.viaversion.viaversion.api.protocol.version.ProtocolVersion) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) Types1_14(com.viaversion.viaversion.api.type.types.version.Types1_14) Protocol1_15_2To1_16(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.Protocol1_15_2To1_16) Types1_16(com.viaversion.viaversion.api.type.types.version.Types1_16) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ClientboundPackets1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16) Type(com.viaversion.viaversion.api.type.Type) StoredEntityData(com.viaversion.viaversion.api.data.entity.StoredEntityData) Entity1_16Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_16Types) ValueTransformer(com.viaversion.viaversion.api.protocol.remapper.ValueTransformer) ClientboundPackets1_15(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15) EntityRewriter(com.viaversion.viabackwards.api.rewriters.EntityRewriter) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) WorldNameTracker(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.data.WorldNameTracker) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Protocol1_15_2To1_16(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.Protocol1_15_2To1_16) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld)

Example 2 with Types1_16

use of com.viaversion.viaversion.api.type.types.version.Types1_16 in project ViaBackwards by ViaVersion.

the class EntityPackets1_17 method registerPackets.

@Override
protected void registerPackets() {
    registerTrackerWithData(ClientboundPackets1_17.SPAWN_ENTITY, Entity1_17Types.FALLING_BLOCK);
    registerSpawnTracker(ClientboundPackets1_17.SPAWN_MOB);
    registerTracker(ClientboundPackets1_17.SPAWN_EXPERIENCE_ORB, Entity1_17Types.EXPERIENCE_ORB);
    registerTracker(ClientboundPackets1_17.SPAWN_PAINTING, Entity1_17Types.PAINTING);
    registerTracker(ClientboundPackets1_17.SPAWN_PLAYER, Entity1_17Types.PLAYER);
    registerMetadataRewriter(ClientboundPackets1_17.ENTITY_METADATA, Types1_17.METADATA_LIST, Types1_16.METADATA_LIST);
    protocol.registerClientbound(ClientboundPackets1_17.REMOVE_ENTITY, ClientboundPackets1_16_2.DESTROY_ENTITIES, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                int entityId = wrapper.read(Type.VAR_INT);
                tracker(wrapper.user()).removeEntity(entityId);
                // Write into single value array
                int[] array = { entityId };
                wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, array);
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_17.JOIN_GAME, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Entity ID
            map(Type.INT);
            // Hardcore
            map(Type.BOOLEAN);
            // Gamemode
            map(Type.UNSIGNED_BYTE);
            // Previous Gamemode
            map(Type.BYTE);
            // Worlds
            map(Type.STRING_ARRAY);
            // Dimension registry
            map(Type.NBT);
            // Current dimension data
            map(Type.NBT);
            // World
            map(Type.STRING);
            handler(wrapper -> {
                byte previousGamemode = wrapper.get(Type.BYTE, 0);
                if (previousGamemode == -1) {
                    // "Unset" gamemode removed
                    wrapper.set(Type.BYTE, 0, (byte) 0);
                }
            });
            handler(getTrackerHandler(Entity1_17Types.PLAYER, Type.INT));
            handler(worldDataTrackerHandler(1));
            handler(wrapper -> {
                CompoundTag registry = wrapper.get(Type.NBT, 0);
                CompoundTag biomeRegistry = registry.get("minecraft:worldgen/biome");
                ListTag biomes = biomeRegistry.get("value");
                for (Tag biome : biomes) {
                    CompoundTag biomeCompound = ((CompoundTag) biome).get("element");
                    StringTag category = biomeCompound.get("category");
                    if (category.getValue().equalsIgnoreCase("underground")) {
                        category.setValue("none");
                    }
                }
                CompoundTag dimensionRegistry = registry.get("minecraft:dimension_type");
                ListTag dimensions = dimensionRegistry.get("value");
                for (Tag dimension : dimensions) {
                    CompoundTag dimensionCompound = ((CompoundTag) dimension).get("element");
                    reduceExtendedHeight(dimensionCompound, false);
                }
                reduceExtendedHeight(wrapper.get(Type.NBT, 1), true);
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_17.RESPAWN, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Dimension data
            map(Type.NBT);
            // World
            map(Type.STRING);
            handler(worldDataTrackerHandler(0));
            handler(wrapper -> reduceExtendedHeight(wrapper.get(Type.NBT, 0), true));
        }
    });
    protocol.registerClientbound(ClientboundPackets1_17.PLAYER_POSITION, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.DOUBLE);
            map(Type.DOUBLE);
            map(Type.DOUBLE);
            map(Type.FLOAT);
            map(Type.FLOAT);
            map(Type.BYTE);
            map(Type.VAR_INT);
            handler(wrapper -> {
                // Dismount vehicle ¯\_(ツ)_/¯
                wrapper.read(Type.BOOLEAN);
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_17.ENTITY_PROPERTIES, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Entity id
            map(Type.VAR_INT);
            handler(wrapper -> {
                // Collection length
                wrapper.write(Type.INT, wrapper.read(Type.VAR_INT));
            });
        }
    });
    // TODO translatables
    protocol.mergePacket(ClientboundPackets1_17.COMBAT_ENTER, ClientboundPackets1_16_2.COMBAT_EVENT, 0);
    protocol.mergePacket(ClientboundPackets1_17.COMBAT_END, ClientboundPackets1_16_2.COMBAT_EVENT, 1);
    protocol.mergePacket(ClientboundPackets1_17.COMBAT_KILL, ClientboundPackets1_16_2.COMBAT_EVENT, 2);
}
Also used : ClientboundPackets1_17(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ClientboundPackets1_17) Particle(com.viaversion.viaversion.api.type.types.Particle) Protocol1_16_4To1_17(com.viaversion.viabackwards.protocol.protocol1_16_4to1_17.Protocol1_16_4To1_17) MetaType(com.viaversion.viaversion.api.minecraft.metadata.MetaType) Tag(com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) ListTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag) Types1_17(com.viaversion.viaversion.api.type.types.version.Types1_17) Entity1_16_2Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_16_2Types) Types1_16(com.viaversion.viaversion.api.type.types.version.Types1_16) ClientboundPackets1_16_2(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Entity1_17Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types) ViaBackwards(com.viaversion.viabackwards.ViaBackwards) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) IntTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag) EntityRewriter(com.viaversion.viabackwards.api.rewriters.EntityRewriter) StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Tag(com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag) StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) ListTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) IntTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag) ListTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)

Example 3 with Types1_16

use of com.viaversion.viaversion.api.type.types.version.Types1_16 in project ViaBackwards by ViaVersion.

the class EntityPackets1_16_2 method registerPackets.

@Override
protected void registerPackets() {
    registerTrackerWithData(ClientboundPackets1_16_2.SPAWN_ENTITY, Entity1_16_2Types.FALLING_BLOCK);
    registerSpawnTracker(ClientboundPackets1_16_2.SPAWN_MOB);
    registerTracker(ClientboundPackets1_16_2.SPAWN_EXPERIENCE_ORB, Entity1_16_2Types.EXPERIENCE_ORB);
    registerTracker(ClientboundPackets1_16_2.SPAWN_PAINTING, Entity1_16_2Types.PAINTING);
    registerTracker(ClientboundPackets1_16_2.SPAWN_PLAYER, Entity1_16_2Types.PLAYER);
    registerRemoveEntities(ClientboundPackets1_16_2.DESTROY_ENTITIES);
    registerMetadataRewriter(ClientboundPackets1_16_2.ENTITY_METADATA, Types1_16.METADATA_LIST);
    protocol.registerClientbound(ClientboundPackets1_16_2.JOIN_GAME, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Entity ID
            map(Type.INT);
            handler(wrapper -> {
                boolean hardcore = wrapper.read(Type.BOOLEAN);
                short gamemode = wrapper.read(Type.UNSIGNED_BYTE);
                if (hardcore) {
                    gamemode |= 0x08;
                }
                wrapper.write(Type.UNSIGNED_BYTE, gamemode);
            });
            // Previous Gamemode
            map(Type.BYTE);
            // World List
            map(Type.STRING_ARRAY);
            handler(wrapper -> {
                // Just screw the registry and write the defaults for 1.16 and 1.16.1 clients
                wrapper.read(Type.NBT);
                wrapper.write(Type.NBT, EntityPackets.DIMENSIONS_TAG);
                CompoundTag dimensionData = wrapper.read(Type.NBT);
                wrapper.write(Type.STRING, getDimensionFromData(dimensionData));
            });
            // Dimension
            map(Type.STRING);
            // Seed
            map(Type.LONG);
            handler(wrapper -> {
                int maxPlayers = wrapper.read(Type.VAR_INT);
                wrapper.write(Type.UNSIGNED_BYTE, (short) Math.max(maxPlayers, 255));
            });
            // ...
            handler(getTrackerHandler(Entity1_16_2Types.PLAYER, Type.INT));
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16_2.RESPAWN, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                CompoundTag dimensionData = wrapper.read(Type.NBT);
                wrapper.write(Type.STRING, getDimensionFromData(dimensionData));
            });
        }
    });
}
Also used : Set(java.util.Set) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) Sets(com.google.common.collect.Sets) Entity1_16_2Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_16_2Types) Types1_16(com.viaversion.viaversion.api.type.types.version.Types1_16) ClientboundPackets1_16_2(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Entity1_16Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_16Types) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) Protocol1_16_1To1_16_2(com.viaversion.viabackwards.protocol.protocol1_16_1to1_16_2.Protocol1_16_1To1_16_2) EntityRewriter(com.viaversion.viabackwards.api.rewriters.EntityRewriter) EntityPackets(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.packets.EntityPackets) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)

Example 4 with Types1_16

use of com.viaversion.viaversion.api.type.types.version.Types1_16 in project ViaVersion by ViaVersion.

the class EntityPackets method register.

public static void register(Protocol1_16_2To1_16_1 protocol) {
    MetadataRewriter1_16_2To1_16_1 metadataRewriter = protocol.get(MetadataRewriter1_16_2To1_16_1.class);
    metadataRewriter.registerTrackerWithData(ClientboundPackets1_16.SPAWN_ENTITY, Entity1_16_2Types.FALLING_BLOCK);
    metadataRewriter.registerTracker(ClientboundPackets1_16.SPAWN_MOB);
    metadataRewriter.registerTracker(ClientboundPackets1_16.SPAWN_PLAYER, Entity1_16_2Types.PLAYER);
    metadataRewriter.registerMetadataRewriter(ClientboundPackets1_16.ENTITY_METADATA, Types1_16.METADATA_LIST);
    metadataRewriter.registerRemoveEntities(ClientboundPackets1_16.DESTROY_ENTITIES);
    protocol.registerClientbound(ClientboundPackets1_16.JOIN_GAME, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Entity ID
            map(Type.INT);
            handler(wrapper -> {
                short gamemode = wrapper.read(Type.UNSIGNED_BYTE);
                // Hardcore
                wrapper.write(Type.BOOLEAN, (gamemode & 0x08) != 0);
                gamemode &= ~0x08;
                wrapper.write(Type.UNSIGNED_BYTE, gamemode);
            });
            // Previous Gamemode
            map(Type.BYTE);
            // World List
            map(Type.STRING_ARRAY);
            handler(wrapper -> {
                // Throw away the old dimension registry, extra conversion would be too hard of a hit
                wrapper.read(Type.NBT);
                wrapper.write(Type.NBT, protocol.getMappingData().getDimensionRegistry());
                // Instead of the dimension's resource key, it now just wants the data directly
                String dimensionType = wrapper.read(Type.STRING);
                wrapper.write(Type.NBT, getDimensionData(dimensionType));
            });
            // Dimension
            map(Type.STRING);
            // Seed
            map(Type.LONG);
            // Max players
            map(Type.UNSIGNED_BYTE, Type.VAR_INT);
            // ...
            handler(wrapper -> {
                wrapper.user().getEntityTracker(Protocol1_16_2To1_16_1.class).addEntity(wrapper.get(Type.INT, 0), Entity1_16_2Types.PLAYER);
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16.RESPAWN, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                String dimensionType = wrapper.read(Type.STRING);
                wrapper.write(Type.NBT, getDimensionData(dimensionType));
            });
        }
    });
}
Also used : PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ClientboundPackets1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16) Type(com.viaversion.viaversion.api.type.Type) Protocol1_16_2To1_16_1(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.Protocol1_16_2To1_16_1) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) Via(com.viaversion.viaversion.api.Via) MetadataRewriter1_16_2To1_16_1(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.metadata.MetadataRewriter1_16_2To1_16_1) Entity1_16_2Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_16_2Types) Types1_16(com.viaversion.viaversion.api.type.types.version.Types1_16) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) MetadataRewriter1_16_2To1_16_1(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.metadata.MetadataRewriter1_16_2To1_16_1)

Example 5 with Types1_16

use of com.viaversion.viaversion.api.type.types.version.Types1_16 in project ViaVersion by ViaVersion.

the class EntityPackets method register.

public static void register(Protocol1_16To1_15_2 protocol) {
    MetadataRewriter1_16To1_15_2 metadataRewriter = protocol.get(MetadataRewriter1_16To1_15_2.class);
    // Spawn lightning -> Spawn entity
    protocol.registerClientbound(ClientboundPackets1_15.SPAWN_GLOBAL_ENTITY, ClientboundPackets1_16.SPAWN_ENTITY, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                int entityId = wrapper.passthrough(Type.VAR_INT);
                byte type = wrapper.read(Type.BYTE);
                if (type != 1) {
                    // Cancel if not lightning/invalid id
                    wrapper.cancel();
                    return;
                }
                wrapper.user().getEntityTracker(Protocol1_16To1_15_2.class).addEntity(entityId, Entity1_16Types.LIGHTNING_BOLT);
                // uuid
                wrapper.write(Type.UUID, UUID.randomUUID());
                // entity type
                wrapper.write(Type.VAR_INT, Entity1_16Types.LIGHTNING_BOLT.getId());
                // x
                wrapper.passthrough(Type.DOUBLE);
                // y
                wrapper.passthrough(Type.DOUBLE);
                // z
                wrapper.passthrough(Type.DOUBLE);
                // yaw
                wrapper.write(Type.BYTE, (byte) 0);
                // pitch
                wrapper.write(Type.BYTE, (byte) 0);
                // data
                wrapper.write(Type.INT, 0);
                // velocity
                wrapper.write(Type.SHORT, (short) 0);
                // velocity
                wrapper.write(Type.SHORT, (short) 0);
                // velocity
                wrapper.write(Type.SHORT, (short) 0);
            });
        }
    });
    metadataRewriter.registerTrackerWithData(ClientboundPackets1_15.SPAWN_ENTITY, Entity1_16Types.FALLING_BLOCK);
    metadataRewriter.registerTracker(ClientboundPackets1_15.SPAWN_MOB);
    metadataRewriter.registerTracker(ClientboundPackets1_15.SPAWN_PLAYER, Entity1_16Types.PLAYER);
    metadataRewriter.registerMetadataRewriter(ClientboundPackets1_15.ENTITY_METADATA, Types1_14.METADATA_LIST, Types1_16.METADATA_LIST);
    metadataRewriter.registerRemoveEntities(ClientboundPackets1_15.DESTROY_ENTITIES);
    protocol.registerClientbound(ClientboundPackets1_15.RESPAWN, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(DIMENSION_HANDLER);
            // Seed
            map(Type.LONG);
            // Gamemode
            map(Type.UNSIGNED_BYTE);
            handler(wrapper -> {
                // Previous gamemode, set to none
                wrapper.write(Type.BYTE, (byte) -1);
                String levelType = wrapper.read(Type.STRING);
                // debug
                wrapper.write(Type.BOOLEAN, false);
                wrapper.write(Type.BOOLEAN, levelType.equals("flat"));
                // keep all playerdata
                wrapper.write(Type.BOOLEAN, true);
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_15.JOIN_GAME, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Entity ID
            map(Type.INT);
            // Gamemode
            map(Type.UNSIGNED_BYTE);
            handler(wrapper -> {
                // Previous gamemode, set to none
                wrapper.write(Type.BYTE, (byte) -1);
                // World list - only used for command completion
                wrapper.write(Type.STRING_ARRAY, Arrays.copyOf(WORLD_NAMES, WORLD_NAMES.length));
                // Dimension registry
                wrapper.write(Type.NBT, DIMENSIONS_TAG.clone());
            });
            // Dimension
            handler(DIMENSION_HANDLER);
            // Seed
            map(Type.LONG);
            // Max players
            map(Type.UNSIGNED_BYTE);
            handler(wrapper -> {
                wrapper.user().getEntityTracker(Protocol1_16To1_15_2.class).addEntity(wrapper.get(Type.INT, 0), Entity1_16Types.PLAYER);
                // level type
                final String type = wrapper.read(Type.STRING);
                // View distance
                wrapper.passthrough(Type.VAR_INT);
                // Reduced debug info
                wrapper.passthrough(Type.BOOLEAN);
                // Show death screen
                wrapper.passthrough(Type.BOOLEAN);
                // Debug
                wrapper.write(Type.BOOLEAN, false);
                wrapper.write(Type.BOOLEAN, type.equals("flat"));
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_15.ENTITY_PROPERTIES, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                wrapper.passthrough(Type.VAR_INT);
                int size = wrapper.passthrough(Type.INT);
                int actualSize = size;
                for (int i = 0; i < size; i++) {
                    // Attributes have been renamed and are now namespaced identifiers
                    String key = wrapper.read(Type.STRING);
                    String attributeIdentifier = protocol.getMappingData().getAttributeMappings().get(key);
                    if (attributeIdentifier == null) {
                        attributeIdentifier = "minecraft:" + key;
                        if (!com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.MappingData.isValid1_13Channel(attributeIdentifier)) {
                            if (!Via.getConfig().isSuppressConversionWarnings()) {
                                Via.getPlatform().getLogger().warning("Invalid attribute: " + key);
                            }
                            actualSize--;
                            wrapper.read(Type.DOUBLE);
                            int modifierSize = wrapper.read(Type.VAR_INT);
                            for (int j = 0; j < modifierSize; j++) {
                                wrapper.read(Type.UUID);
                                wrapper.read(Type.DOUBLE);
                                wrapper.read(Type.BYTE);
                            }
                            continue;
                        }
                    }
                    wrapper.write(Type.STRING, attributeIdentifier);
                    wrapper.passthrough(Type.DOUBLE);
                    int modifierSize = wrapper.passthrough(Type.VAR_INT);
                    for (int j = 0; j < modifierSize; j++) {
                        wrapper.passthrough(Type.UUID);
                        wrapper.passthrough(Type.DOUBLE);
                        wrapper.passthrough(Type.BYTE);
                    }
                }
                if (size != actualSize) {
                    wrapper.set(Type.INT, 0, actualSize);
                }
            });
        }
    });
    protocol.registerServerbound(ServerboundPackets1_16.ANIMATION, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                InventoryTracker1_16 inventoryTracker = wrapper.user().get(InventoryTracker1_16.class);
                // Don't send an arm swing if the player has an inventory opened.
                if (inventoryTracker.getInventory() != -1) {
                    wrapper.cancel();
                }
            });
        }
    });
}
Also used : PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) ByteTag(com.github.steveice10.opennbt.tag.builtin.ByteTag) Arrays(java.util.Arrays) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) UUID(java.util.UUID) Via(com.viaversion.viaversion.api.Via) IntTag(com.github.steveice10.opennbt.tag.builtin.IntTag) Types1_14(com.viaversion.viaversion.api.type.types.version.Types1_14) Protocol1_16To1_15_2(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.Protocol1_16To1_15_2) Types1_16(com.viaversion.viaversion.api.type.types.version.Types1_16) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ClientboundPackets1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16) WorldIdentifiers(com.viaversion.viaversion.api.minecraft.WorldIdentifiers) Type(com.viaversion.viaversion.api.type.Type) ServerboundPackets1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16) InventoryTracker1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.storage.InventoryTracker1_16) LongTag(com.github.steveice10.opennbt.tag.builtin.LongTag) Entity1_16Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_16Types) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) ClientboundPackets1_15(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15) MetadataRewriter1_16To1_15_2(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.metadata.MetadataRewriter1_16To1_15_2) FloatTag(com.github.steveice10.opennbt.tag.builtin.FloatTag) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) InventoryTracker1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.storage.InventoryTracker1_16) MetadataRewriter1_16To1_15_2(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.metadata.MetadataRewriter1_16To1_15_2)

Aggregations

PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)6 Type (com.viaversion.viaversion.api.type.Type)6 Types1_16 (com.viaversion.viaversion.api.type.types.version.Types1_16)6 Entity1_16_2Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_16_2Types)4 EntityType (com.viaversion.viaversion.api.minecraft.entities.EntityType)4 EntityRewriter (com.viaversion.viabackwards.api.rewriters.EntityRewriter)3 Via (com.viaversion.viaversion.api.Via)3 Entity1_16Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_16Types)3 ClientboundPackets1_16_2 (com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2)3 ClientboundPackets1_16 (com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16)3 CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)2 Entity1_17Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types)2 MetaType (com.viaversion.viaversion.api.minecraft.metadata.MetaType)2 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)2 Particle (com.viaversion.viaversion.api.type.types.Particle)2 Types1_14 (com.viaversion.viaversion.api.type.types.version.Types1_14)2 Types1_17 (com.viaversion.viaversion.api.type.types.version.Types1_17)2 CompoundTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)2 StringTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag)2 ClientboundPackets1_15 (com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15)2