Search in sources :

Example 11 with ClientboundPackets1_14

use of com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14 in project ViaBackwards by ViaVersion.

the class Protocol1_14_4To1_15 method registerPackets.

@Override
protected void registerPackets() {
    executeAsyncAfterLoaded(Protocol1_15To1_14_4.class, MAPPINGS::load);
    translatableRewriter.registerBossBar(ClientboundPackets1_15.BOSSBAR);
    translatableRewriter.registerChatMessage(ClientboundPackets1_15.CHAT_MESSAGE);
    translatableRewriter.registerCombatEvent(ClientboundPackets1_15.COMBAT_EVENT);
    translatableRewriter.registerDisconnect(ClientboundPackets1_15.DISCONNECT);
    translatableRewriter.registerOpenWindow(ClientboundPackets1_15.OPEN_WINDOW);
    translatableRewriter.registerTabList(ClientboundPackets1_15.TAB_LIST);
    translatableRewriter.registerTitle(ClientboundPackets1_15.TITLE);
    translatableRewriter.registerPing();
    (blockItemPackets = new BlockItemPackets1_15(this)).register();
    entityRewriter.register();
    SoundRewriter soundRewriter = new SoundRewriter(this);
    soundRewriter.registerSound(ClientboundPackets1_15.SOUND);
    soundRewriter.registerSound(ClientboundPackets1_15.ENTITY_SOUND);
    soundRewriter.registerNamedSound(ClientboundPackets1_15.NAMED_SOUND);
    soundRewriter.registerStopSound(ClientboundPackets1_15.STOP_SOUND);
    // Explosion - manually send an explosion sound
    registerClientbound(ClientboundPackets1_15.EXPLOSION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // x
            map(Type.FLOAT);
            // y
            map(Type.FLOAT);
            // z
            map(Type.FLOAT);
            handler(wrapper -> {
                PacketWrapper soundPacket = wrapper.create(ClientboundPackets1_14.SOUND);
                // entity.generic.explode
                soundPacket.write(Type.VAR_INT, 243);
                // blocks category
                soundPacket.write(Type.VAR_INT, 4);
                // x
                soundPacket.write(Type.INT, toEffectCoordinate(wrapper.get(Type.FLOAT, 0)));
                // y
                soundPacket.write(Type.INT, toEffectCoordinate(wrapper.get(Type.FLOAT, 1)));
                // z
                soundPacket.write(Type.INT, toEffectCoordinate(wrapper.get(Type.FLOAT, 2)));
                // volume
                soundPacket.write(Type.FLOAT, 4F);
                // pitch - usually semi randomized by the server, but we don't really have to care about that
                soundPacket.write(Type.FLOAT, 1F);
                soundPacket.send(Protocol1_14_4To1_15.class);
            });
        }

        private int toEffectCoordinate(float coordinate) {
            return (int) (coordinate * 8);
        }
    });
    new TagRewriter(this).register(ClientboundPackets1_15.TAGS, RegistryType.ENTITY);
    new StatisticsRewriter(this).register(ClientboundPackets1_15.STATISTICS);
}
Also used : BlockItemPackets1_15(com.viaversion.viabackwards.protocol.protocol1_14_4to1_15.packets.BlockItemPackets1_15) TranslatableRewriter(com.viaversion.viabackwards.api.rewriters.TranslatableRewriter) Entity1_15Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_15Types) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) ServerboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14) BackwardsProtocol(com.viaversion.viabackwards.api.BackwardsProtocol) RegistryType(com.viaversion.viaversion.api.minecraft.RegistryType) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) ImmediateRespawn(com.viaversion.viabackwards.protocol.protocol1_14_4to1_15.data.ImmediateRespawn) TagRewriter(com.viaversion.viaversion.rewriter.TagRewriter) StatisticsRewriter(com.viaversion.viaversion.rewriter.StatisticsRewriter) BlockItemPackets1_15(com.viaversion.viabackwards.protocol.protocol1_14_4to1_15.packets.BlockItemPackets1_15) BackwardsMappings(com.viaversion.viabackwards.api.data.BackwardsMappings) Protocol1_15To1_14_4(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.Protocol1_15To1_14_4) EntityTrackerBase(com.viaversion.viaversion.data.entity.EntityTrackerBase) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) ClientboundPackets1_15(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15) ClientboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14) EntityRewriter(com.viaversion.viaversion.api.rewriter.EntityRewriter) EntityPackets1_15(com.viaversion.viabackwards.protocol.protocol1_14_4to1_15.packets.EntityPackets1_15) SoundRewriter(com.viaversion.viabackwards.api.rewriters.SoundRewriter) StatisticsRewriter(com.viaversion.viaversion.rewriter.StatisticsRewriter) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) TagRewriter(com.viaversion.viaversion.rewriter.TagRewriter) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) SoundRewriter(com.viaversion.viabackwards.api.rewriters.SoundRewriter)

Example 12 with ClientboundPackets1_14

use of com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14 in project ViaVersion by ViaVersion.

the class EntityPackets method register.

public static void register(Protocol1_14To1_13_2 protocol) {
    MetadataRewriter1_14To1_13_2 metadataRewriter = protocol.get(MetadataRewriter1_14To1_13_2.class);
    protocol.registerClientbound(ClientboundPackets1_13.SPAWN_ENTITY, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity id
            map(Type.VAR_INT);
            // 1 - UUID
            map(Type.UUID);
            // 2 - Type
            map(Type.BYTE, 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);
            // 9 - Velocity X
            map(Type.SHORT);
            // 10 - Velocity Y
            map(Type.SHORT);
            // 11 - Velocity Z
            map(Type.SHORT);
            // Track Entity
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityId = wrapper.get(Type.VAR_INT, 0);
                    int typeId = wrapper.get(Type.VAR_INT, 1);
                    Entity1_13Types.EntityType type1_13 = Entity1_13Types.getTypeFromId(typeId, true);
                    typeId = metadataRewriter.newEntityId(type1_13.getId());
                    EntityType type1_14 = Entity1_14Types.getTypeFromId(typeId);
                    if (type1_14 != null) {
                        int data = wrapper.get(Type.INT, 0);
                        if (type1_14.is(Entity1_14Types.FALLING_BLOCK)) {
                            wrapper.set(Type.INT, 0, protocol.getMappingData().getNewBlockStateId(data));
                        } else if (type1_14.is(Entity1_14Types.MINECART)) {
                            // default is 0 = rideable minecart
                            switch(data) {
                                case 1:
                                    typeId = Entity1_14Types.CHEST_MINECART.getId();
                                    break;
                                case 2:
                                    typeId = Entity1_14Types.FURNACE_MINECART.getId();
                                    break;
                                case 3:
                                    typeId = Entity1_14Types.TNT_MINECART.getId();
                                    break;
                                case 4:
                                    typeId = Entity1_14Types.SPAWNER_MINECART.getId();
                                    break;
                                case 5:
                                    typeId = Entity1_14Types.HOPPER_MINECART.getId();
                                    break;
                                case 6:
                                    typeId = Entity1_14Types.COMMAND_BLOCK_MINECART.getId();
                                    break;
                            }
                        } else if ((type1_14.is(Entity1_14Types.ITEM) && data > 0) || type1_14.isOrHasParent(Entity1_14Types.ABSTRACT_ARROW)) {
                            if (type1_14.isOrHasParent(Entity1_14Types.ABSTRACT_ARROW)) {
                                wrapper.set(Type.INT, 0, data - 1);
                            }
                            // send velocity in separate packet, 1.14 is now ignoring the velocity
                            PacketWrapper velocity = wrapper.create(0x45);
                            velocity.write(Type.VAR_INT, entityId);
                            velocity.write(Type.SHORT, wrapper.get(Type.SHORT, 0));
                            velocity.write(Type.SHORT, wrapper.get(Type.SHORT, 1));
                            velocity.write(Type.SHORT, wrapper.get(Type.SHORT, 2));
                            velocity.scheduleSend(Protocol1_14To1_13_2.class);
                        }
                        // Register Type ID
                        wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class).addEntity(entityId, type1_14);
                    }
                    wrapper.set(Type.VAR_INT, 1, 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_2.METADATA_LIST, Types1_14.METADATA_LIST);
            handler(metadataRewriter.trackerAndRewriterHandler(Types1_14.METADATA_LIST));
        }
    });
    protocol.registerClientbound(ClientboundPackets1_13.SPAWN_PAINTING, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.VAR_INT);
            map(Type.UUID);
            map(Type.VAR_INT);
            map(Type.POSITION, Type.POSITION1_14);
            map(Type.BYTE);
        }
    });
    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_2.METADATA_LIST, Types1_14.METADATA_LIST);
            handler(metadataRewriter.trackerAndRewriterHandler(Types1_14.METADATA_LIST, Entity1_14Types.PLAYER));
        }
    });
    protocol.registerClientbound(ClientboundPackets1_13.ENTITY_ANIMATION, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.VAR_INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    short animation = wrapper.passthrough(Type.UNSIGNED_BYTE);
                    if (animation == 2) {
                        // Leave bed
                        EntityTracker1_14 tracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class);
                        int entityId = wrapper.get(Type.VAR_INT, 0);
                        tracker.setSleeping(entityId, false);
                        PacketWrapper metadataPacket = wrapper.create(ClientboundPackets1_14.ENTITY_METADATA);
                        metadataPacket.write(Type.VAR_INT, entityId);
                        List<Metadata> metadataList = new LinkedList<>();
                        if (tracker.clientEntityId() != entityId) {
                            metadataList.add(new Metadata(6, Types1_14.META_TYPES.poseType, MetadataRewriter1_14To1_13_2.recalculatePlayerPose(entityId, tracker)));
                        }
                        metadataList.add(new Metadata(12, Types1_14.META_TYPES.optionalPositionType, null));
                        metadataPacket.write(Types1_14.METADATA_LIST, metadataList);
                        metadataPacket.scheduleSend(Protocol1_14To1_13_2.class);
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_13.USE_BED, ClientboundPackets1_14.ENTITY_METADATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.VAR_INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    EntityTracker1_14 tracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class);
                    int entityId = wrapper.get(Type.VAR_INT, 0);
                    tracker.setSleeping(entityId, true);
                    Position position = wrapper.read(Type.POSITION);
                    List<Metadata> metadataList = new LinkedList<>();
                    metadataList.add(new Metadata(12, Types1_14.META_TYPES.optionalPositionType, position));
                    if (tracker.clientEntityId() != entityId) {
                        metadataList.add(new Metadata(6, Types1_14.META_TYPES.poseType, MetadataRewriter1_14To1_13_2.recalculatePlayerPose(entityId, tracker)));
                    }
                    wrapper.write(Types1_14.METADATA_LIST, metadataList);
                }
            });
        }
    });
    metadataRewriter.registerRemoveEntities(ClientboundPackets1_13.DESTROY_ENTITIES);
    metadataRewriter.registerMetadataRewriter(ClientboundPackets1_13.ENTITY_METADATA, Types1_13_2.METADATA_LIST, Types1_14.METADATA_LIST);
}
Also used : EntityTracker1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14) Position(com.viaversion.viaversion.api.minecraft.Position) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) MetadataRewriter1_14To1_13_2(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.metadata.MetadataRewriter1_14To1_13_2) LinkedList(java.util.LinkedList) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Protocol1_14To1_13_2(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2) Entity1_13Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types)

Example 13 with ClientboundPackets1_14

use of com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14 in project ViaVersion by ViaVersion.

the class WorldPackets method register.

public static void register(Protocol1_14To1_13_2 protocol) {
    BlockRewriter blockRewriter = new BlockRewriter(protocol, null);
    protocol.registerClientbound(ClientboundPackets1_13.BLOCK_BREAK_ANIMATION, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.VAR_INT);
            map(Type.POSITION, Type.POSITION1_14);
            map(Type.BYTE);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_13.BLOCK_ENTITY_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.POSITION, Type.POSITION1_14);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_13.BLOCK_ACTION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Location
            map(Type.POSITION, Type.POSITION1_14);
            // Action id
            map(Type.UNSIGNED_BYTE);
            // Action param
            map(Type.UNSIGNED_BYTE);
            // Block id - /!\ NOT BLOCK STATE
            map(Type.VAR_INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockId(wrapper.get(Type.VAR_INT, 0)));
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_13.BLOCK_CHANGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.POSITION, Type.POSITION1_14);
            map(Type.VAR_INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int id = wrapper.get(Type.VAR_INT, 0);
                    wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(id));
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_13.SERVER_DIFFICULTY, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.UNSIGNED_BYTE);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // Added in 19w11a. Maybe https://bugs.mojang.com/browse/MC-44471 ?
                    wrapper.write(Type.BOOLEAN, false);
                }
            });
        }
    });
    blockRewriter.registerMultiBlockChange(ClientboundPackets1_13.MULTI_BLOCK_CHANGE);
    protocol.registerClientbound(ClientboundPackets1_13.EXPLOSION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // X
            map(Type.FLOAT);
            // Y
            map(Type.FLOAT);
            // Z
            map(Type.FLOAT);
            // Radius
            map(Type.FLOAT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    for (int i = 0; i < 3; i++) {
                        float coord = wrapper.get(Type.FLOAT, i);
                        if (coord < 0f) {
                            coord = (int) coord;
                            wrapper.set(Type.FLOAT, i, coord);
                        }
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_13.CHUNK_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                    Chunk chunk = wrapper.read(new Chunk1_13Type(clientWorld));
                    wrapper.write(new Chunk1_14Type(), chunk);
                    int[] motionBlocking = new int[16 * 16];
                    int[] worldSurface = new int[16 * 16];
                    for (int s = 0; s < chunk.getSections().length; s++) {
                        ChunkSection section = chunk.getSections()[s];
                        if (section == null)
                            continue;
                        boolean hasBlock = false;
                        for (int i = 0; i < section.getPaletteSize(); i++) {
                            int old = section.getPaletteEntry(i);
                            int newId = protocol.getMappingData().getNewBlockStateId(old);
                            if (!hasBlock && newId != air && newId != voidAir && newId != caveAir) {
                                // air, void_air, cave_air
                                hasBlock = true;
                            }
                            section.setPaletteEntry(i, newId);
                        }
                        if (!hasBlock) {
                            section.setNonAirBlocksCount(0);
                            continue;
                        }
                        int nonAirBlockCount = 0;
                        for (int x = 0; x < 16; x++) {
                            for (int y = 0; y < 16; y++) {
                                for (int z = 0; z < 16; z++) {
                                    int id = section.getFlatBlock(x, y, z);
                                    if (id != air && id != voidAir && id != caveAir) {
                                        nonAirBlockCount++;
                                        // +1 (top of the block)
                                        worldSurface[x + z * 16] = y + s * 16 + 1;
                                    }
                                    if (protocol.getMappingData().getMotionBlocking().contains(id)) {
                                        // +1 (top of the block)
                                        motionBlocking[x + z * 16] = y + s * 16 + 1;
                                    }
                                    // Manually update light for non full blocks (block light must not be sent)
                                    if (Via.getConfig().isNonFullBlockLightFix() && protocol.getMappingData().getNonFullBlocks().contains(id)) {
                                        setNonFullLight(chunk, section, s, x, y, z);
                                    }
                                }
                            }
                        }
                        section.setNonAirBlocksCount(nonAirBlockCount);
                    }
                    CompoundTag heightMap = new CompoundTag();
                    heightMap.put("MOTION_BLOCKING", new LongArrayTag(encodeHeightMap(motionBlocking)));
                    heightMap.put("WORLD_SURFACE", new LongArrayTag(encodeHeightMap(worldSurface)));
                    chunk.setHeightMap(heightMap);
                    PacketWrapper lightPacket = wrapper.create(ClientboundPackets1_14.UPDATE_LIGHT);
                    lightPacket.write(Type.VAR_INT, chunk.getX());
                    lightPacket.write(Type.VAR_INT, chunk.getZ());
                    // all 18 bits set if ground up
                    int skyLightMask = chunk.isFullChunk() ? 0x3ffff : 0;
                    int blockLightMask = 0;
                    for (int i = 0; i < chunk.getSections().length; i++) {
                        ChunkSection sec = chunk.getSections()[i];
                        if (sec == null)
                            continue;
                        if (!chunk.isFullChunk() && sec.getLight().hasSkyLight()) {
                            skyLightMask |= (1 << (i + 1));
                        }
                        blockLightMask |= (1 << (i + 1));
                    }
                    lightPacket.write(Type.VAR_INT, skyLightMask);
                    lightPacket.write(Type.VAR_INT, blockLightMask);
                    // empty sky light mask
                    lightPacket.write(Type.VAR_INT, 0);
                    // empty block light mask
                    lightPacket.write(Type.VAR_INT, 0);
                    // only do this on the initial chunk send (not when chunk.isGroundUp() is false)
                    if (chunk.isFullChunk())
                        // chunk below 0
                        lightPacket.write(Type.BYTE_ARRAY_PRIMITIVE, FULL_LIGHT);
                    for (ChunkSection section : chunk.getSections()) {
                        if (section == null || !section.getLight().hasSkyLight()) {
                            if (chunk.isFullChunk()) {
                                lightPacket.write(Type.BYTE_ARRAY_PRIMITIVE, FULL_LIGHT);
                            }
                            continue;
                        }
                        lightPacket.write(Type.BYTE_ARRAY_PRIMITIVE, section.getLight().getSkyLight());
                    }
                    if (chunk.isFullChunk())
                        // chunk above 255
                        lightPacket.write(Type.BYTE_ARRAY_PRIMITIVE, FULL_LIGHT);
                    for (ChunkSection section : chunk.getSections()) {
                        if (section == null)
                            continue;
                        lightPacket.write(Type.BYTE_ARRAY_PRIMITIVE, section.getLight().getBlockLight());
                    }
                    EntityTracker1_14 entityTracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class);
                    int diffX = Math.abs(entityTracker.getChunkCenterX() - chunk.getX());
                    int diffZ = Math.abs(entityTracker.getChunkCenterZ() - chunk.getZ());
                    if (entityTracker.isForceSendCenterChunk() || diffX >= SERVERSIDE_VIEW_DISTANCE || diffZ >= SERVERSIDE_VIEW_DISTANCE) {
                        // Set center chunk
                        PacketWrapper fakePosLook = wrapper.create(ClientboundPackets1_14.UPDATE_VIEW_POSITION);
                        fakePosLook.write(Type.VAR_INT, chunk.getX());
                        fakePosLook.write(Type.VAR_INT, chunk.getZ());
                        fakePosLook.send(Protocol1_14To1_13_2.class);
                        entityTracker.setChunkCenterX(chunk.getX());
                        entityTracker.setChunkCenterZ(chunk.getZ());
                    }
                    lightPacket.send(Protocol1_14To1_13_2.class);
                    // Remove light references from chunk sections
                    for (ChunkSection section : chunk.getSections()) {
                        if (section != null) {
                            section.setLight(null);
                        }
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_13.EFFECT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Effect Id
            map(Type.INT);
            // Location
            map(Type.POSITION, Type.POSITION1_14);
            // Data
            map(Type.INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int id = wrapper.get(Type.INT, 0);
                    int data = wrapper.get(Type.INT, 1);
                    if (id == 1010) {
                        // Play record
                        wrapper.set(Type.INT, 1, protocol.getMappingData().getNewItemId(data));
                    } else if (id == 2001) {
                        // Block break + block break sound
                        wrapper.set(Type.INT, 1, protocol.getMappingData().getNewBlockStateId(data));
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_13.JOIN_GAME, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.INT);
            // 1 - Gamemode
            map(Type.UNSIGNED_BYTE);
            // 2 - Dimension
            map(Type.INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // Store the player
                    ClientWorld clientChunks = wrapper.user().get(ClientWorld.class);
                    int dimensionId = wrapper.get(Type.INT, 1);
                    clientChunks.setEnvironment(dimensionId);
                    int entityId = wrapper.get(Type.INT, 0);
                    Entity1_14Types entType = Entity1_14Types.PLAYER;
                    // Register Type ID
                    EntityTracker1_14 tracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class);
                    tracker.addEntity(entityId, entType);
                    tracker.setClientEntityId(entityId);
                }
            });
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // 19w11a removed difficulty from join game
                    short difficulty = wrapper.read(Type.UNSIGNED_BYTE);
                    PacketWrapper difficultyPacket = wrapper.create(ClientboundPackets1_14.SERVER_DIFFICULTY);
                    difficultyPacket.write(Type.UNSIGNED_BYTE, difficulty);
                    // Unknown value added in 19w11a
                    difficultyPacket.write(Type.BOOLEAN, false);
                    difficultyPacket.scheduleSend(protocol.getClass());
                    // Max Players
                    wrapper.passthrough(Type.UNSIGNED_BYTE);
                    // Level Type
                    wrapper.passthrough(Type.STRING);
                    // Serverside view distance, added in 19w13a
                    wrapper.write(Type.VAR_INT, SERVERSIDE_VIEW_DISTANCE);
                }
            });
            handler(wrapper -> {
                // Manually send the packet
                wrapper.send(Protocol1_14To1_13_2.class);
                wrapper.cancel();
                // View distance has to be sent after the join packet
                sendViewDistancePacket(wrapper.user());
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_13.MAP_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.VAR_INT);
            map(Type.BYTE);
            map(Type.BOOLEAN);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // new value, probably if the map is locked (added in 19w02a), old maps are not locked
                    wrapper.write(Type.BOOLEAN, false);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_13.RESPAWN, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Dimension ID
            map(Type.INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                    int dimensionId = wrapper.get(Type.INT, 0);
                    clientWorld.setEnvironment(dimensionId);
                    EntityTracker1_14 entityTracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class);
                    // The client may reset the center chunk if dimension is changed
                    entityTracker.setForceSendCenterChunk(true);
                }
            });
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // 19w11a removed difficulty from respawn
                    short difficulty = wrapper.read(Type.UNSIGNED_BYTE);
                    PacketWrapper difficultyPacket = wrapper.create(ClientboundPackets1_14.SERVER_DIFFICULTY);
                    difficultyPacket.write(Type.UNSIGNED_BYTE, difficulty);
                    // Unknown value added in 19w11a
                    difficultyPacket.write(Type.BOOLEAN, false);
                    difficultyPacket.scheduleSend(protocol.getClass());
                }
            });
            handler(wrapper -> {
                // Manually send the packet and update the viewdistance after
                wrapper.send(Protocol1_14To1_13_2.class);
                wrapper.cancel();
                sendViewDistancePacket(wrapper.user());
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_13.SPAWN_POSITION, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.POSITION, Type.POSITION1_14);
        }
    });
}
Also used : PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) Arrays(java.util.Arrays) EntityTracker1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Chunk1_14Type(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.types.Chunk1_14Type) Via(com.viaversion.viaversion.api.Via) CompactArrayUtil(com.viaversion.viaversion.util.CompactArrayUtil) NibbleArray(com.viaversion.viaversion.api.minecraft.chunks.NibbleArray) 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) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) Protocol1_14To1_13_2(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) LongArrayTag(com.github.steveice10.opennbt.tag.builtin.LongArrayTag) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) BlockFace(com.viaversion.viaversion.api.minecraft.BlockFace) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) ClientboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14) Chunk1_13Type(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.types.Chunk1_13Type) Entity1_14Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_14Types) EntityTracker1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Chunk1_14Type(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.types.Chunk1_14Type) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Chunk1_13Type(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.types.Chunk1_13Type) LongArrayTag(com.github.steveice10.opennbt.tag.builtin.LongArrayTag) Entity1_14Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_14Types) Protocol1_14To1_13_2(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag)

Example 14 with ClientboundPackets1_14

use of com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14 in project ViaVersion by ViaVersion.

the class PlayerPackets method register.

public static void register(Protocol protocol) {
    protocol.registerClientbound(ClientboundPackets1_14.RESPAWN, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.INT);
            // Level Seed
            handler(wrapper -> wrapper.write(Type.LONG, 0L));
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.JOIN_GAME, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.INT);
            // 1 - Gamemode
            map(Type.UNSIGNED_BYTE);
            // 2 - Dimension
            map(Type.INT);
            handler(wrapper -> {
                // Register Type ID
                EntityTracker tracker = wrapper.user().getEntityTracker(Protocol1_15To1_14_4.class);
                int entityId = wrapper.get(Type.INT, 0);
                tracker.addEntity(entityId, Entity1_15Types.PLAYER);
            });
            // Level Seed
            handler(wrapper -> wrapper.write(Type.LONG, 0L));
            // 3 - Max Players
            map(Type.UNSIGNED_BYTE);
            // 4 - Level Type
            map(Type.STRING);
            // 5 - View Distance
            map(Type.VAR_INT);
            // 6 - Reduce Debug Info
            map(Type.BOOLEAN);
            // Show Death Screen
            handler(wrapper -> wrapper.write(Type.BOOLEAN, !Via.getConfig().is1_15InstantRespawn()));
        }
    });
}
Also used : PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Entity1_15Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_15Types) Protocol1_15To1_14_4(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.Protocol1_15To1_14_4) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) Protocol(com.viaversion.viaversion.api.protocol.Protocol) ClientboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14) Via(com.viaversion.viaversion.api.Via) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Protocol1_15To1_14_4(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.Protocol1_15To1_14_4)

Example 15 with ClientboundPackets1_14

use of com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14 in project ViaVersion by ViaVersion.

the class WorldPackets method register.

public static void register(Protocol1_15To1_14_4 protocol) {
    BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14);
    blockRewriter.registerBlockAction(ClientboundPackets1_14.BLOCK_ACTION);
    blockRewriter.registerBlockChange(ClientboundPackets1_14.BLOCK_CHANGE);
    blockRewriter.registerMultiBlockChange(ClientboundPackets1_14.MULTI_BLOCK_CHANGE);
    blockRewriter.registerAcknowledgePlayerDigging(ClientboundPackets1_14.ACKNOWLEDGE_PLAYER_DIGGING);
    protocol.registerClientbound(ClientboundPackets1_14.CHUNK_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    Chunk chunk = wrapper.read(new Chunk1_14Type());
                    wrapper.write(new Chunk1_15Type(), chunk);
                    if (chunk.isFullChunk()) {
                        int[] biomeData = chunk.getBiomeData();
                        int[] newBiomeData = new int[1024];
                        if (biomeData != null) {
                            // Now in 4x4x4 areas - take the biome of each "middle"
                            for (int i = 0; i < 4; ++i) {
                                for (int j = 0; j < 4; ++j) {
                                    int x = (j << 2) + 2;
                                    int z = (i << 2) + 2;
                                    int oldIndex = (z << 4 | x);
                                    newBiomeData[i << 2 | j] = biomeData[oldIndex];
                                }
                            }
                            // ... and copy it to the new y layers
                            for (int i = 1; i < 64; ++i) {
                                System.arraycopy(newBiomeData, 0, newBiomeData, i * 16, 16);
                            }
                        }
                        chunk.setBiomeData(newBiomeData);
                    }
                    for (int s = 0; s < chunk.getSections().length; s++) {
                        ChunkSection section = chunk.getSections()[s];
                        if (section == null)
                            continue;
                        for (int i = 0; i < section.getPaletteSize(); i++) {
                            int old = section.getPaletteEntry(i);
                            int newId = protocol.getMappingData().getNewBlockStateId(old);
                            section.setPaletteEntry(i, newId);
                        }
                    }
                }
            });
        }
    });
    blockRewriter.registerEffect(ClientboundPackets1_14.EFFECT, 1010, 2001);
    protocol.registerClientbound(ClientboundPackets1_14.SPAWN_PARTICLE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Particle ID
            map(Type.INT);
            // 1 - Long Distance
            map(Type.BOOLEAN);
            // 2 - X
            map(Type.FLOAT, Type.DOUBLE);
            // 3 - Y
            map(Type.FLOAT, Type.DOUBLE);
            // 4 - Z
            map(Type.FLOAT, Type.DOUBLE);
            // 5 - Offset X
            map(Type.FLOAT);
            // 6 - Offset Y
            map(Type.FLOAT);
            // 7 - Offset Z
            map(Type.FLOAT);
            // 8 - Particle Data
            map(Type.FLOAT);
            // 9 - Particle Count
            map(Type.INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int id = wrapper.get(Type.INT, 0);
                    if (id == 3 || id == 23) {
                        int data = wrapper.passthrough(Type.VAR_INT);
                        wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(data));
                    } else if (id == 32) {
                        protocol.getItemRewriter().handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
                    }
                }
            });
        }
    });
}
Also used : BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Chunk1_15Type(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.types.Chunk1_15Type) Chunk1_14Type(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.types.Chunk1_14Type) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)

Aggregations

PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)13 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)12 Type (com.viaversion.viaversion.api.type.Type)11 ClientboundPackets1_14 (com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14)11 PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)8 Metadata (com.viaversion.viaversion.api.minecraft.metadata.Metadata)5 Protocol1_13_2To1_14 (com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14)4 Entity1_15Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_15Types)4 ClientboundPackets1_13 (com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13)4 Protocol1_14To1_13_2 (com.viaversion.viaversion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2)4 ServerboundPackets1_14 (com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14)4 ViaBackwards (com.viaversion.viabackwards.ViaBackwards)3 BackwardsProtocol (com.viaversion.viabackwards.api.BackwardsProtocol)3 Via (com.viaversion.viaversion.api.Via)3 UserConnection (com.viaversion.viaversion.api.connection.UserConnection)3 Position (com.viaversion.viaversion.api.minecraft.Position)3 Chunk (com.viaversion.viaversion.api.minecraft.chunks.Chunk)3 ChunkSection (com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)3 Entity1_14Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_14Types)3 EntityType (com.viaversion.viaversion.api.minecraft.entities.EntityType)3