Search in sources :

Example 1 with Chunk1_17Type

use of com.viaversion.viaversion.protocols.protocol1_17to1_16_4.types.Chunk1_17Type in project ViaBackwards by ViaVersion.

the class BlockItemPackets1_17 method registerPackets.

@Override
protected void registerPackets() {
    BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14);
    new RecipeRewriter1_16(protocol).registerDefaultHandler(ClientboundPackets1_17.DECLARE_RECIPES);
    registerSetCooldown(ClientboundPackets1_17.COOLDOWN);
    registerWindowItems(ClientboundPackets1_17.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
    registerSetSlot(ClientboundPackets1_17.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
    registerEntityEquipmentArray(ClientboundPackets1_17.ENTITY_EQUIPMENT, Type.FLAT_VAR_INT_ITEM);
    registerTradeList(ClientboundPackets1_17.TRADE_LIST, Type.FLAT_VAR_INT_ITEM);
    registerAdvancements(ClientboundPackets1_17.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
    blockRewriter.registerAcknowledgePlayerDigging(ClientboundPackets1_17.ACKNOWLEDGE_PLAYER_DIGGING);
    blockRewriter.registerBlockAction(ClientboundPackets1_17.BLOCK_ACTION);
    blockRewriter.registerEffect(ClientboundPackets1_17.EFFECT, 1010, 2001);
    registerCreativeInvAction(ServerboundPackets1_16_2.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
    protocol.registerServerbound(ServerboundPackets1_16_2.EDIT_BOOK, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)));
        }
    });
    // TODO This will cause desync issues for players under certain circumstances, but mostly works:tm:
    protocol.registerServerbound(ServerboundPackets1_16_2.CLICK_WINDOW, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Window Id
            map(Type.UNSIGNED_BYTE);
            // Slot
            map(Type.SHORT);
            // Button
            map(Type.BYTE);
            // Action id - removed
            map(Type.SHORT, Type.NOTHING);
            // Mode
            map(Type.VAR_INT);
            handler(wrapper -> {
                // The 1.17 client would check the entire inventory for changes before -> after a click and send the changed slots here
                // Empty array of slot+item
                wrapper.write(Type.VAR_INT, 0);
                // Expected is the carried item after clicking, old clients send the clicked one (*mostly* being the same)
                handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
            });
        }
    });
    protocol.registerServerbound(ServerboundPackets1_16_2.WINDOW_CONFIRMATION, null, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                wrapper.cancel();
                if (!ViaBackwards.getConfig().handlePingsAsInvAcknowledgements()) {
                    return;
                }
                // Handle ping packet replacement
                short inventoryId = wrapper.read(Type.UNSIGNED_BYTE);
                short confirmationId = wrapper.read(Type.SHORT);
                boolean accepted = wrapper.read(Type.BOOLEAN);
                if (inventoryId == 0 && accepted && wrapper.user().get(PingRequests.class).removeId(confirmationId)) {
                    PacketWrapper pongPacket = wrapper.create(ServerboundPackets1_17.PONG);
                    pongPacket.write(Type.INT, (int) confirmationId);
                    pongPacket.sendToServer(Protocol1_16_4To1_17.class);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_17.SPAWN_PARTICLE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Particle id
            map(Type.INT);
            // Long distance
            map(Type.BOOLEAN);
            // X
            map(Type.DOUBLE);
            // Y
            map(Type.DOUBLE);
            // Z
            map(Type.DOUBLE);
            // Offset X
            map(Type.FLOAT);
            // Offset Y
            map(Type.FLOAT);
            // Offset Z
            map(Type.FLOAT);
            // Particle data
            map(Type.FLOAT);
            // Particle count
            map(Type.INT);
            handler(wrapper -> {
                int id = wrapper.get(Type.INT, 0);
                if (id == 16) {
                    // R
                    wrapper.passthrough(Type.FLOAT);
                    // G
                    wrapper.passthrough(Type.FLOAT);
                    // B
                    wrapper.passthrough(Type.FLOAT);
                    // Scale
                    wrapper.passthrough(Type.FLOAT);
                    // Dust color transition -> Dust
                    // R
                    wrapper.read(Type.FLOAT);
                    // G
                    wrapper.read(Type.FLOAT);
                    // B
                    wrapper.read(Type.FLOAT);
                } else if (id == 37) {
                    // Vibration signal - no nice mapping possible without tracking entity positions and doing particle tasks
                    wrapper.set(Type.INT, 0, -1);
                    wrapper.cancel();
                }
            });
            handler(getSpawnParticleHandler(Type.FLAT_VAR_INT_ITEM));
        }
    });
    protocol.mergePacket(ClientboundPackets1_17.WORLD_BORDER_SIZE, ClientboundPackets1_16_2.WORLD_BORDER, 0);
    protocol.mergePacket(ClientboundPackets1_17.WORLD_BORDER_LERP_SIZE, ClientboundPackets1_16_2.WORLD_BORDER, 1);
    protocol.mergePacket(ClientboundPackets1_17.WORLD_BORDER_CENTER, ClientboundPackets1_16_2.WORLD_BORDER, 2);
    protocol.mergePacket(ClientboundPackets1_17.WORLD_BORDER_INIT, ClientboundPackets1_16_2.WORLD_BORDER, 3);
    protocol.mergePacket(ClientboundPackets1_17.WORLD_BORDER_WARNING_DELAY, ClientboundPackets1_16_2.WORLD_BORDER, 4);
    protocol.mergePacket(ClientboundPackets1_17.WORLD_BORDER_WARNING_DISTANCE, ClientboundPackets1_16_2.WORLD_BORDER, 5);
    // The Great Shrunkening
    // Chunk sections *will* be lost ¯\_(ツ)_/¯
    protocol.registerClientbound(ClientboundPackets1_17.UPDATE_LIGHT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // X
            map(Type.VAR_INT);
            // Z
            map(Type.VAR_INT);
            // Trust edges
            map(Type.BOOLEAN);
            handler(wrapper -> {
                EntityTracker tracker = wrapper.user().getEntityTracker(Protocol1_16_4To1_17.class);
                int startFromSection = Math.max(0, -(tracker.currentMinY() >> 4));
                long[] skyLightMask = wrapper.read(Type.LONG_ARRAY_PRIMITIVE);
                long[] blockLightMask = wrapper.read(Type.LONG_ARRAY_PRIMITIVE);
                int cutSkyLightMask = cutLightMask(skyLightMask, startFromSection);
                int cutBlockLightMask = cutLightMask(blockLightMask, startFromSection);
                wrapper.write(Type.VAR_INT, cutSkyLightMask);
                wrapper.write(Type.VAR_INT, cutBlockLightMask);
                long[] emptySkyLightMask = wrapper.read(Type.LONG_ARRAY_PRIMITIVE);
                long[] emptyBlockLightMask = wrapper.read(Type.LONG_ARRAY_PRIMITIVE);
                wrapper.write(Type.VAR_INT, cutLightMask(emptySkyLightMask, startFromSection));
                wrapper.write(Type.VAR_INT, cutLightMask(emptyBlockLightMask, startFromSection));
                writeLightArrays(wrapper, BitSet.valueOf(skyLightMask), cutSkyLightMask, startFromSection, tracker.currentWorldSectionHeight());
                writeLightArrays(wrapper, BitSet.valueOf(blockLightMask), cutBlockLightMask, startFromSection, tracker.currentWorldSectionHeight());
            });
        }

        private void writeLightArrays(PacketWrapper wrapper, BitSet bitMask, int cutBitMask, int startFromSection, int sectionHeight) throws Exception {
            // Length - throw it away
            wrapper.read(Type.VAR_INT);
            List<byte[]> light = new ArrayList<>();
            // Remove lower bounds
            for (int i = 0; i < startFromSection; i++) {
                if (bitMask.get(i)) {
                    wrapper.read(Type.BYTE_ARRAY_PRIMITIVE);
                }
            }
            // Add the important 18 sections
            for (int i = 0; i < 18; i++) {
                if (isSet(cutBitMask, i)) {
                    light.add(wrapper.read(Type.BYTE_ARRAY_PRIMITIVE));
                }
            }
            // Remove upper bounds
            for (int i = startFromSection + 18; i < sectionHeight + 2; i++) {
                if (bitMask.get(i)) {
                    wrapper.read(Type.BYTE_ARRAY_PRIMITIVE);
                }
            }
            // Aaand we're done
            for (byte[] bytes : light) {
                wrapper.write(Type.BYTE_ARRAY_PRIMITIVE, bytes);
            }
        }

        private boolean isSet(int mask, int i) {
            return (mask & (1 << i)) != 0;
        }
    });
    protocol.registerClientbound(ClientboundPackets1_17.MULTI_BLOCK_CHANGE, new PacketRemapper() {

        public void registerMap() {
            // Chunk pos
            map(Type.LONG);
            // Suppress light updates
            map(Type.BOOLEAN);
            handler((wrapper) -> {
                // Remove sections below y 0 and above 255
                long chunkPos = wrapper.get(Type.LONG, 0);
                int chunkY = (int) (chunkPos << 44 >> 44);
                if (chunkY < 0 || chunkY > 15) {
                    wrapper.cancel();
                    return;
                }
                BlockChangeRecord[] records = wrapper.passthrough(Type.VAR_LONG_BLOCK_CHANGE_RECORD_ARRAY);
                for (BlockChangeRecord record : records) {
                    record.setBlockId(protocol.getMappingData().getNewBlockStateId(record.getBlockId()));
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_17.BLOCK_CHANGE, new PacketRemapper() {

        public void registerMap() {
            map(Type.POSITION1_14);
            map(Type.VAR_INT);
            handler((wrapper) -> {
                int y = wrapper.get(Type.POSITION1_14, 0).getY();
                if (y < 0 || y > 255) {
                    wrapper.cancel();
                    return;
                }
                wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(wrapper.get(Type.VAR_INT, 0)));
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_17.CHUNK_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                EntityTracker tracker = wrapper.user().getEntityTracker(Protocol1_16_4To1_17.class);
                int currentWorldSectionHeight = tracker.currentWorldSectionHeight();
                Chunk chunk = wrapper.read(new Chunk1_17Type(currentWorldSectionHeight));
                wrapper.write(new Chunk1_16_2Type(), chunk);
                // Cut sections
                int startFromSection = Math.max(0, -(tracker.currentMinY() >> 4));
                chunk.setBiomeData(Arrays.copyOfRange(chunk.getBiomeData(), startFromSection * 64, (startFromSection * 64) + 1024));
                chunk.setBitmask(cutMask(chunk.getChunkMask(), startFromSection, false));
                chunk.setChunkMask(null);
                ChunkSection[] sections = Arrays.copyOfRange(chunk.getSections(), startFromSection, startFromSection + 16);
                chunk.setSections(sections);
                CompoundTag heightMaps = chunk.getHeightMap();
                for (Tag heightMapTag : heightMaps.values()) {
                    LongArrayTag heightMap = (LongArrayTag) heightMapTag;
                    int[] heightMapData = new int[256];
                    int bitsPerEntry = MathUtil.ceilLog2((currentWorldSectionHeight << 4) + 1);
                    // Shift back to 0 based and clamp to normal height with 9 bits
                    CompactArrayUtil.iterateCompactArrayWithPadding(bitsPerEntry, heightMapData.length, heightMap.getValue(), (i, v) -> heightMapData[i] = MathUtil.clamp(v + tracker.currentMinY(), 0, 255));
                    heightMap.setValue(CompactArrayUtil.createCompactArrayWithPadding(9, heightMapData.length, i -> heightMapData[i]));
                }
                for (int i = 0; i < 16; i++) {
                    ChunkSection section = sections[i];
                    if (section == null)
                        continue;
                    for (int j = 0; j < section.getPaletteSize(); j++) {
                        int old = section.getPaletteEntry(j);
                        section.setPaletteEntry(j, protocol.getMappingData().getNewBlockStateId(old));
                    }
                }
                chunk.getBlockEntities().removeIf(compound -> {
                    NumberTag tag = compound.get("y");
                    return tag != null && tag.asInt() < 0;
                });
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_17.BLOCK_ENTITY_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                int y = wrapper.passthrough(Type.POSITION1_14).getY();
                if (y < 0 || y > 255) {
                    wrapper.cancel();
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_17.BLOCK_BREAK_ANIMATION, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.VAR_INT);
            handler(wrapper -> {
                int y = wrapper.passthrough(Type.POSITION1_14).getY();
                if (y < 0 || y > 255) {
                    wrapper.cancel();
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_17.MAP_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Map ID
            map(Type.VAR_INT);
            // Scale
            map(Type.BYTE);
            // Tracking position
            handler(wrapper -> wrapper.write(Type.BOOLEAN, true));
            // Locked
            map(Type.BOOLEAN);
            handler(wrapper -> {
                boolean hasMarkers = wrapper.read(Type.BOOLEAN);
                if (!hasMarkers) {
                    // Array size
                    wrapper.write(Type.VAR_INT, 0);
                } else {
                    MapColorRewriter.getRewriteHandler(MapColorRewrites::getMappedColor).handle(wrapper);
                }
            });
        }
    });
}
Also used : ClientboundPackets1_17(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ClientboundPackets1_17) MathUtil(com.viaversion.viaversion.util.MathUtil) Arrays(java.util.Arrays) ServerboundPackets1_17(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ServerboundPackets1_17) PingRequests(com.viaversion.viabackwards.protocol.protocol1_16_4to1_17.storage.PingRequests) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) LongArrayTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.LongArrayTag) ArrayList(java.util.ArrayList) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Chunk1_17Type(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.types.Chunk1_17Type) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) MapColorRewrites(com.viaversion.viabackwards.protocol.protocol1_16_4to1_17.data.MapColorRewrites) MapColorRewriter(com.viaversion.viabackwards.api.rewriters.MapColorRewriter) Protocol1_16_4To1_17(com.viaversion.viabackwards.protocol.protocol1_16_4to1_17.Protocol1_16_4To1_17) Tag(com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) BlockChangeRecord(com.viaversion.viaversion.api.minecraft.BlockChangeRecord) CompactArrayUtil(com.viaversion.viaversion.util.CompactArrayUtil) ClientboundPackets1_16_2(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2) NumberTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag) List(java.util.List) ServerboundPackets1_16_2(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ServerboundPackets1_16_2) BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) ItemRewriter(com.viaversion.viabackwards.api.rewriters.ItemRewriter) Chunk1_16_2Type(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.types.Chunk1_16_2Type) ViaBackwards(com.viaversion.viabackwards.ViaBackwards) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) BitSet(java.util.BitSet) RecipeRewriter1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.RecipeRewriter1_16) Chunk1_17Type(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.types.Chunk1_17Type) RecipeRewriter1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.RecipeRewriter1_16) PingRequests(com.viaversion.viabackwards.protocol.protocol1_16_4to1_17.storage.PingRequests) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) BitSet(java.util.BitSet) MapColorRewrites(com.viaversion.viabackwards.protocol.protocol1_16_4to1_17.data.MapColorRewrites) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) BlockChangeRecord(com.viaversion.viaversion.api.minecraft.BlockChangeRecord) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) LongArrayTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.LongArrayTag) NumberTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag) ArrayList(java.util.ArrayList) List(java.util.List) Chunk1_16_2Type(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.types.Chunk1_16_2Type) LongArrayTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.LongArrayTag) Tag(com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag) NumberTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) Protocol1_16_4To1_17(com.viaversion.viabackwards.protocol.protocol1_16_4to1_17.Protocol1_16_4To1_17) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)

Example 2 with Chunk1_17Type

use of com.viaversion.viaversion.protocols.protocol1_17to1_16_4.types.Chunk1_17Type in project ViaBackwards by ViaVersion.

the class BlockItemPackets1_18 method registerPackets.

@Override
protected void registerPackets() {
    new RecipeRewriter1_16(protocol).registerDefaultHandler(ClientboundPackets1_18.DECLARE_RECIPES);
    registerSetCooldown(ClientboundPackets1_18.COOLDOWN);
    registerWindowItems1_17_1(ClientboundPackets1_18.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT, Type.FLAT_VAR_INT_ITEM);
    registerSetSlot1_17_1(ClientboundPackets1_18.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
    registerEntityEquipmentArray(ClientboundPackets1_18.ENTITY_EQUIPMENT, Type.FLAT_VAR_INT_ITEM);
    registerTradeList(ClientboundPackets1_18.TRADE_LIST, Type.FLAT_VAR_INT_ITEM);
    registerAdvancements(ClientboundPackets1_18.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
    registerClickWindow1_17_1(ServerboundPackets1_17.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
    protocol.registerClientbound(ClientboundPackets1_18.EFFECT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Effect id
            map(Type.INT);
            // Location
            map(Type.POSITION1_14);
            // Data
            map(Type.INT);
            handler(wrapper -> {
                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));
                }
            });
        }
    });
    registerCreativeInvAction(ServerboundPackets1_17.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
    protocol.registerClientbound(ClientboundPackets1_18.SPAWN_PARTICLE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Particle id
            map(Type.INT);
            // Override limiter
            map(Type.BOOLEAN);
            // X
            map(Type.DOUBLE);
            // Y
            map(Type.DOUBLE);
            // Z
            map(Type.DOUBLE);
            // Offset X
            map(Type.FLOAT);
            // Offset Y
            map(Type.FLOAT);
            // Offset Z
            map(Type.FLOAT);
            // Max speed
            map(Type.FLOAT);
            // Particle Count
            map(Type.INT);
            handler(wrapper -> {
                int id = wrapper.get(Type.INT, 0);
                if (id == 3) {
                    // Block marker
                    int blockState = wrapper.read(Type.VAR_INT);
                    if (blockState == 7786) {
                        // Light block
                        wrapper.set(Type.INT, 0, 3);
                    } else {
                        // Else assume barrier block
                        wrapper.set(Type.INT, 0, 2);
                    }
                    return;
                }
                ParticleMappings mappings = protocol.getMappingData().getParticleMappings();
                if (mappings.isBlockParticle(id)) {
                    int data = wrapper.passthrough(Type.VAR_INT);
                    wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(data));
                } else if (mappings.isItemParticle(id)) {
                    handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
                }
                int newId = protocol.getMappingData().getNewParticleId(id);
                if (newId != id) {
                    wrapper.set(Type.INT, 0, newId);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_18.BLOCK_ENTITY_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.POSITION1_14);
            handler(wrapper -> {
                final int id = wrapper.read(Type.VAR_INT);
                final CompoundTag tag = wrapper.read(Type.NBT);
                final int mappedId = BlockEntityIds.mappedId(id);
                if (mappedId == -1) {
                    wrapper.cancel();
                    return;
                }
                final String identifier = protocol.getMappingData().blockEntities().get(id);
                if (identifier == null) {
                    wrapper.cancel();
                    return;
                }
                // The 1.18 server doesn't include the id and positions (x, y, z) in the NBT anymore
                // If those were the only fields on the block entity (e.g.: skull, bed), we'll receive a null NBT
                // We initialize one and add the missing fields, so it can be handled correctly down the line
                final CompoundTag newTag = tag == null ? new CompoundTag() : tag;
                final Position pos = wrapper.get(Type.POSITION1_14, 0);
                // The protocol converters downstream rely on this field, let's add it back
                newTag.put("id", new StringTag("minecraft:" + identifier));
                // Weird glitches happen with the 1.17 client and below if these fields are missing
                // Some examples are block entity models becoming invisible (e.g.: signs, banners)
                newTag.put("x", new IntTag(pos.x()));
                newTag.put("y", new IntTag(pos.y()));
                newTag.put("z", new IntTag(pos.z()));
                handleSpawner(id, newTag);
                wrapper.write(Type.UNSIGNED_BYTE, (short) mappedId);
                wrapper.write(Type.NBT, newTag);
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_18.CHUNK_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                final EntityTracker tracker = protocol.getEntityRewriter().tracker(wrapper.user());
                final Chunk1_18Type chunkType = new Chunk1_18Type(tracker.currentWorldSectionHeight(), MathUtil.ceilLog2(protocol.getMappingData().getBlockStateMappings().size()), MathUtil.ceilLog2(tracker.biomesSent()));
                final Chunk oldChunk = wrapper.read(chunkType);
                final ChunkSection[] sections = oldChunk.getSections();
                final BitSet mask = new BitSet(oldChunk.getSections().length);
                final int[] biomeData = new int[sections.length * ChunkSection.BIOME_SIZE];
                int biomeIndex = 0;
                for (int j = 0; j < sections.length; j++) {
                    final ChunkSection section = sections[j];
                    // Write biome palette into biome array
                    final DataPalette biomePalette = section.palette(PaletteType.BIOMES);
                    for (int i = 0; i < ChunkSection.BIOME_SIZE; i++) {
                        biomeData[biomeIndex++] = biomePalette.idAt(i);
                    }
                    // Rewrite to empty section
                    if (section.getNonAirBlocksCount() == 0) {
                        sections[j] = null;
                    } else {
                        mask.set(j);
                    }
                }
                final List<CompoundTag> blockEntityTags = new ArrayList<>(oldChunk.blockEntities().size());
                for (final BlockEntity blockEntity : oldChunk.blockEntities()) {
                    final String id = protocol.getMappingData().blockEntities().get(blockEntity.typeId());
                    if (id == null) {
                        // Shrug
                        continue;
                    }
                    final CompoundTag tag;
                    if (blockEntity.tag() != null) {
                        tag = blockEntity.tag();
                        handleSpawner(blockEntity.typeId(), tag);
                    } else {
                        tag = new CompoundTag();
                    }
                    blockEntityTags.add(tag);
                    tag.put("x", new IntTag((oldChunk.getX() << 4) + blockEntity.sectionX()));
                    tag.put("y", new IntTag(blockEntity.y()));
                    tag.put("z", new IntTag((oldChunk.getZ() << 4) + blockEntity.sectionZ()));
                    tag.put("id", new StringTag("minecraft:" + id));
                }
                final Chunk chunk = new BaseChunk(oldChunk.getX(), oldChunk.getZ(), true, false, mask, oldChunk.getSections(), biomeData, oldChunk.getHeightMap(), blockEntityTags);
                wrapper.write(new Chunk1_17Type(tracker.currentWorldSectionHeight()), chunk);
                // Create and send light packet first
                final PacketWrapper lightPacket = wrapper.create(ClientboundPackets1_17_1.UPDATE_LIGHT);
                lightPacket.write(Type.VAR_INT, chunk.getX());
                lightPacket.write(Type.VAR_INT, chunk.getZ());
                // Trust edges
                lightPacket.write(Type.BOOLEAN, wrapper.read(Type.BOOLEAN));
                // Sky light mask
                lightPacket.write(Type.LONG_ARRAY_PRIMITIVE, wrapper.read(Type.LONG_ARRAY_PRIMITIVE));
                // Block light mask
                lightPacket.write(Type.LONG_ARRAY_PRIMITIVE, wrapper.read(Type.LONG_ARRAY_PRIMITIVE));
                // Empty sky light mask
                lightPacket.write(Type.LONG_ARRAY_PRIMITIVE, wrapper.read(Type.LONG_ARRAY_PRIMITIVE));
                // Empty block light mask
                lightPacket.write(Type.LONG_ARRAY_PRIMITIVE, wrapper.read(Type.LONG_ARRAY_PRIMITIVE));
                final int skyLightLength = wrapper.read(Type.VAR_INT);
                lightPacket.write(Type.VAR_INT, skyLightLength);
                for (int i = 0; i < skyLightLength; i++) {
                    lightPacket.write(Type.BYTE_ARRAY_PRIMITIVE, wrapper.read(Type.BYTE_ARRAY_PRIMITIVE));
                }
                final int blockLightLength = wrapper.read(Type.VAR_INT);
                lightPacket.write(Type.VAR_INT, blockLightLength);
                for (int i = 0; i < blockLightLength; i++) {
                    lightPacket.write(Type.BYTE_ARRAY_PRIMITIVE, wrapper.read(Type.BYTE_ARRAY_PRIMITIVE));
                }
                lightPacket.send(Protocol1_17_1To1_18.class);
            });
        }
    });
    protocol.cancelClientbound(ClientboundPackets1_18.SET_SIMULATION_DISTANCE);
}
Also used : MathUtil(com.viaversion.viaversion.util.MathUtil) ServerboundPackets1_17(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ServerboundPackets1_17) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) ArrayList(java.util.ArrayList) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Chunk1_17Type(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.types.Chunk1_17Type) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) Protocol1_17_1To1_18(com.viaversion.viabackwards.protocol.protocol1_17_1to1_18.Protocol1_17_1To1_18) ClientboundPackets1_17_1(com.viaversion.viaversion.protocols.protocol1_17_1to1_17.ClientboundPackets1_17_1) PaletteType(com.viaversion.viaversion.api.minecraft.chunks.PaletteType) IntTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag) Chunk1_18Type(com.viaversion.viaversion.protocols.protocol1_18to1_17_1.types.Chunk1_18Type) BlockEntity(com.viaversion.viaversion.api.minecraft.blockentity.BlockEntity) Position(com.viaversion.viaversion.api.minecraft.Position) ClientboundPackets1_18(com.viaversion.viaversion.protocols.protocol1_18to1_17_1.ClientboundPackets1_18) BlockEntityIds(com.viaversion.viabackwards.protocol.protocol1_17_1to1_18.data.BlockEntityIds) BaseChunk(com.viaversion.viaversion.api.minecraft.chunks.BaseChunk) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) DataPalette(com.viaversion.viaversion.api.minecraft.chunks.DataPalette) List(java.util.List) ItemRewriter(com.viaversion.viabackwards.api.rewriters.ItemRewriter) ParticleMappings(com.viaversion.viaversion.api.data.ParticleMappings) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) BitSet(java.util.BitSet) RecipeRewriter1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.RecipeRewriter1_16) StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) BaseChunk(com.viaversion.viaversion.api.minecraft.chunks.BaseChunk) Chunk1_17Type(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.types.Chunk1_17Type) RecipeRewriter1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.RecipeRewriter1_16) Position(com.viaversion.viaversion.api.minecraft.Position) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) BitSet(java.util.BitSet) Protocol1_17_1To1_18(com.viaversion.viabackwards.protocol.protocol1_17_1to1_18.Protocol1_17_1To1_18) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) BaseChunk(com.viaversion.viaversion.api.minecraft.chunks.BaseChunk) DataPalette(com.viaversion.viaversion.api.minecraft.chunks.DataPalette) Chunk1_18Type(com.viaversion.viaversion.protocols.protocol1_18to1_17_1.types.Chunk1_18Type) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) ArrayList(java.util.ArrayList) List(java.util.List) ParticleMappings(com.viaversion.viaversion.api.data.ParticleMappings) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) IntTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag) BlockEntity(com.viaversion.viaversion.api.minecraft.blockentity.BlockEntity)

Example 3 with Chunk1_17Type

use of com.viaversion.viaversion.protocols.protocol1_17to1_16_4.types.Chunk1_17Type in project ViaVersion by ViaVersion.

the class WorldPackets method register.

public static void register(Protocol1_17To1_16_4 protocol) {
    BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14);
    blockRewriter.registerBlockAction(ClientboundPackets1_16_2.BLOCK_ACTION);
    blockRewriter.registerBlockChange(ClientboundPackets1_16_2.BLOCK_CHANGE);
    blockRewriter.registerVarLongMultiBlockChange(ClientboundPackets1_16_2.MULTI_BLOCK_CHANGE);
    blockRewriter.registerAcknowledgePlayerDigging(ClientboundPackets1_16_2.ACKNOWLEDGE_PLAYER_DIGGING);
    protocol.registerClientbound(ClientboundPackets1_16_2.WORLD_BORDER, null, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                // Border packet actions have been split into individual packets (the content hasn't changed)
                int type = wrapper.read(Type.VAR_INT);
                ClientboundPacketType packetType;
                switch(type) {
                    case 0:
                        packetType = ClientboundPackets1_17.WORLD_BORDER_SIZE;
                        break;
                    case 1:
                        packetType = ClientboundPackets1_17.WORLD_BORDER_LERP_SIZE;
                        break;
                    case 2:
                        packetType = ClientboundPackets1_17.WORLD_BORDER_CENTER;
                        break;
                    case 3:
                        packetType = ClientboundPackets1_17.WORLD_BORDER_INIT;
                        break;
                    case 4:
                        packetType = ClientboundPackets1_17.WORLD_BORDER_WARNING_DELAY;
                        break;
                    case 5:
                        packetType = ClientboundPackets1_17.WORLD_BORDER_WARNING_DISTANCE;
                        break;
                    default:
                        throw new IllegalArgumentException("Invalid world border type received: " + type);
                }
                wrapper.setId(packetType.getId());
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16_2.UPDATE_LIGHT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // x
            map(Type.VAR_INT);
            // y
            map(Type.VAR_INT);
            // trust edges
            map(Type.BOOLEAN);
            handler(wrapper -> {
                int skyLightMask = wrapper.read(Type.VAR_INT);
                int blockLightMask = wrapper.read(Type.VAR_INT);
                // Now all written as a representation of BitSets
                // Sky light mask
                wrapper.write(Type.LONG_ARRAY_PRIMITIVE, toBitSetLongArray(skyLightMask));
                // Block light mask
                wrapper.write(Type.LONG_ARRAY_PRIMITIVE, toBitSetLongArray(blockLightMask));
                // Empty sky light mask
                wrapper.write(Type.LONG_ARRAY_PRIMITIVE, toBitSetLongArray(wrapper.read(Type.VAR_INT)));
                // Empty block light mask
                wrapper.write(Type.LONG_ARRAY_PRIMITIVE, toBitSetLongArray(wrapper.read(Type.VAR_INT)));
                writeLightArrays(wrapper, skyLightMask);
                writeLightArrays(wrapper, blockLightMask);
            });
        }

        private void writeLightArrays(PacketWrapper wrapper, int bitMask) throws Exception {
            List<byte[]> light = new ArrayList<>();
            for (int i = 0; i < 18; i++) {
                if (isSet(bitMask, i)) {
                    light.add(wrapper.read(Type.BYTE_ARRAY_PRIMITIVE));
                }
            }
            // Now needs the length of the bytearray-array
            wrapper.write(Type.VAR_INT, light.size());
            for (byte[] bytes : light) {
                wrapper.write(Type.BYTE_ARRAY_PRIMITIVE, bytes);
            }
        }

        private long[] toBitSetLongArray(int bitmask) {
            return new long[] { bitmask };
        }

        private boolean isSet(int mask, int i) {
            return (mask & (1 << i)) != 0;
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16_2.CHUNK_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                Chunk chunk = wrapper.read(new Chunk1_16_2Type());
                if (!chunk.isFullChunk()) {
                    // All chunks are full chunk packets now (1.16 already stopped sending non-full chunks)
                    // Construct multi block change packets instead
                    // Height map updates are lost (unless we want to fully cache and resend entire chunks)
                    // Block entities are always empty for non-full chunks in Vanilla
                    writeMultiBlockChangePacket(wrapper, chunk);
                    wrapper.cancel();
                    return;
                }
                // Normal full chunk writing
                wrapper.write(new Chunk1_17Type(chunk.getSections().length), chunk);
                // 1.17 uses a bitset for the mask
                chunk.setChunkMask(BitSet.valueOf(new long[] { chunk.getBitmask() }));
                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);
                        section.setPaletteEntry(i, protocol.getMappingData().getNewBlockStateId(old));
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16_2.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);
            // World List
            map(Type.STRING_ARRAY);
            // Registry
            map(Type.NBT);
            // Current dimension
            map(Type.NBT);
            handler(wrapper -> {
                // Add new dimension fields
                CompoundTag dimensionRegistry = wrapper.get(Type.NBT, 0).get("minecraft:dimension_type");
                ListTag dimensions = dimensionRegistry.get("value");
                for (Tag dimension : dimensions) {
                    CompoundTag dimensionCompound = ((CompoundTag) dimension).get("element");
                    addNewDimensionData(dimensionCompound);
                }
                CompoundTag currentDimensionTag = wrapper.get(Type.NBT, 1);
                addNewDimensionData(currentDimensionTag);
                UserConnection user = wrapper.user();
                user.getEntityTracker(Protocol1_17To1_16_4.class).addEntity(wrapper.get(Type.INT, 0), Entity1_17Types.PLAYER);
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16_2.RESPAWN, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                CompoundTag dimensionData = wrapper.passthrough(Type.NBT);
                addNewDimensionData(dimensionData);
            });
        }
    });
    blockRewriter.registerEffect(ClientboundPackets1_16_2.EFFECT, 1010, 2001);
}
Also used : ClientboundPackets1_17(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ClientboundPackets1_17) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) BlockChangeRecord1_16_2(com.viaversion.viaversion.api.minecraft.BlockChangeRecord1_16_2) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) IntTag(com.github.steveice10.opennbt.tag.builtin.IntTag) BlockChangeRecord(com.viaversion.viaversion.api.minecraft.BlockChangeRecord) ClientboundPackets1_16_2(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2) ArrayList(java.util.ArrayList) ClientboundPacketType(com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Protocol1_17To1_16_4(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.Protocol1_17To1_16_4) Type(com.viaversion.viaversion.api.type.Type) Chunk1_17Type(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.types.Chunk1_17Type) List(java.util.List) Tag(com.github.steveice10.opennbt.tag.builtin.Tag) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) Entity1_17Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types) BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) Chunk1_16_2Type(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.types.Chunk1_16_2Type) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) BitSet(java.util.BitSet) Chunk1_17Type(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.types.Chunk1_17Type) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ClientboundPacketType(com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) ArrayList(java.util.ArrayList) List(java.util.List) Chunk1_16_2Type(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.types.Chunk1_16_2Type) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) IntTag(com.github.steveice10.opennbt.tag.builtin.IntTag) Tag(com.github.steveice10.opennbt.tag.builtin.Tag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag)

Example 4 with Chunk1_17Type

use of com.viaversion.viaversion.protocols.protocol1_17to1_16_4.types.Chunk1_17Type in project ViaVersion by ViaVersion.

the class WorldPackets method register.

public static void register(final Protocol1_18To1_17_1 protocol) {
    protocol.registerClientbound(ClientboundPackets1_17_1.BLOCK_ENTITY_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.POSITION1_14);
            handler(wrapper -> {
                final short id = wrapper.read(Type.UNSIGNED_BYTE);
                final int newId = BlockEntityIds.newId(id);
                wrapper.write(Type.VAR_INT, newId);
                handleSpawners(newId, wrapper.passthrough(Type.NBT));
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_17_1.UPDATE_LIGHT, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                final int chunkX = wrapper.passthrough(Type.VAR_INT);
                final int chunkZ = wrapper.passthrough(Type.VAR_INT);
                if (wrapper.user().get(ChunkLightStorage.class).isLoaded(chunkX, chunkZ)) {
                    if (!Via.getConfig().cache1_17Light()) {
                        // Light packets updating already sent chunks are the same as before
                        return;
                    }
                // Pass through and cache light data
                } else {
                    // Cancel and cache the light data
                    wrapper.cancel();
                }
                final boolean trustEdges = wrapper.passthrough(Type.BOOLEAN);
                final long[] skyLightMask = wrapper.passthrough(Type.LONG_ARRAY_PRIMITIVE);
                final long[] blockLightMask = wrapper.passthrough(Type.LONG_ARRAY_PRIMITIVE);
                final long[] emptySkyLightMask = wrapper.passthrough(Type.LONG_ARRAY_PRIMITIVE);
                final long[] emptyBlockLightMask = wrapper.passthrough(Type.LONG_ARRAY_PRIMITIVE);
                final int skyLightLenght = wrapper.passthrough(Type.VAR_INT);
                final byte[][] skyLight = new byte[skyLightLenght][];
                for (int i = 0; i < skyLightLenght; i++) {
                    skyLight[i] = wrapper.passthrough(Type.BYTE_ARRAY_PRIMITIVE);
                }
                final int blockLightLength = wrapper.passthrough(Type.VAR_INT);
                final byte[][] blockLight = new byte[blockLightLength][];
                for (int i = 0; i < blockLightLength; i++) {
                    blockLight[i] = wrapper.passthrough(Type.BYTE_ARRAY_PRIMITIVE);
                }
                final ChunkLightStorage lightStorage = wrapper.user().get(ChunkLightStorage.class);
                lightStorage.storeLight(chunkX, chunkZ, new ChunkLightStorage.ChunkLight(trustEdges, skyLightMask, blockLightMask, emptySkyLightMask, emptyBlockLightMask, skyLight, blockLight));
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_17_1.CHUNK_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                final EntityTracker tracker = protocol.getEntityRewriter().tracker(wrapper.user());
                final Chunk oldChunk = wrapper.read(new Chunk1_17Type(tracker.currentWorldSectionHeight()));
                final List<BlockEntity> blockEntities = new ArrayList<>(oldChunk.getBlockEntities().size());
                for (final CompoundTag tag : oldChunk.getBlockEntities()) {
                    final NumberTag xTag = tag.get("x");
                    final NumberTag yTag = tag.get("y");
                    final NumberTag zTag = tag.get("z");
                    final StringTag idTag = tag.get("id");
                    if (xTag == null || yTag == null || zTag == null || idTag == null) {
                        continue;
                    }
                    final String id = idTag.getValue();
                    final int typeId = protocol.getMappingData().blockEntityIds().getInt(id.replace("minecraft:", ""));
                    if (typeId == -1) {
                        Via.getPlatform().getLogger().warning("Unknown block entity: " + id);
                    }
                    handleSpawners(typeId, tag);
                    final byte packedXZ = (byte) ((xTag.asInt() & 15) << 4 | (zTag.asInt() & 15));
                    blockEntities.add(new BlockEntityImpl(packedXZ, yTag.asShort(), typeId, tag));
                }
                final int[] biomeData = oldChunk.getBiomeData();
                final ChunkSection[] sections = oldChunk.getSections();
                for (int i = 0; i < sections.length; i++) {
                    ChunkSection section = sections[i];
                    if (section == null) {
                        // There's no section mask anymore
                        section = new ChunkSectionImpl();
                        sections[i] = section;
                        section.setNonAirBlocksCount(0);
                        final DataPaletteImpl blockPalette = new DataPaletteImpl(ChunkSection.SIZE);
                        blockPalette.addId(0);
                        section.addPalette(PaletteType.BLOCKS, blockPalette);
                    }
                    // Fill biome palette
                    final DataPaletteImpl biomePalette = new DataPaletteImpl(ChunkSection.BIOME_SIZE);
                    section.addPalette(PaletteType.BIOMES, biomePalette);
                    final int offset = i * ChunkSection.BIOME_SIZE;
                    for (int biomeIndex = 0, biomeArrayIndex = offset; biomeIndex < ChunkSection.BIOME_SIZE; biomeIndex++, biomeArrayIndex++) {
                        // Also catch invalid biomes with id -1
                        final int biome = biomeData[biomeArrayIndex];
                        biomePalette.setIdAt(biomeIndex, biome != -1 ? biome : 0);
                    }
                }
                final Chunk chunk = new Chunk1_18(oldChunk.getX(), oldChunk.getZ(), sections, oldChunk.getHeightMap(), blockEntities);
                wrapper.write(new Chunk1_18Type(tracker.currentWorldSectionHeight(), MathUtil.ceilLog2(protocol.getMappingData().getBlockStateMappings().mappedSize()), MathUtil.ceilLog2(tracker.biomesSent())), chunk);
                final ChunkLightStorage lightStorage = wrapper.user().get(ChunkLightStorage.class);
                final boolean alreadyLoaded = !lightStorage.addLoadedChunk(chunk.getX(), chunk.getZ());
                // Append light data to chunk packet
                final ChunkLightStorage.ChunkLight light = Via.getConfig().cache1_17Light() ? lightStorage.getLight(chunk.getX(), chunk.getZ()) : lightStorage.removeLight(chunk.getX(), chunk.getZ());
                if (light == null) {
                    Via.getPlatform().getLogger().warning("No light data found for chunk at " + chunk.getX() + ", " + chunk.getZ() + ". Chunk was already loaded: " + alreadyLoaded);
                    final BitSet emptyLightMask = new BitSet();
                    emptyLightMask.set(0, tracker.currentWorldSectionHeight() + 2);
                    wrapper.write(Type.BOOLEAN, false);
                    wrapper.write(Type.LONG_ARRAY_PRIMITIVE, new long[0]);
                    wrapper.write(Type.LONG_ARRAY_PRIMITIVE, new long[0]);
                    wrapper.write(Type.LONG_ARRAY_PRIMITIVE, emptyLightMask.toLongArray());
                    wrapper.write(Type.LONG_ARRAY_PRIMITIVE, emptyLightMask.toLongArray());
                    wrapper.write(Type.VAR_INT, 0);
                    wrapper.write(Type.VAR_INT, 0);
                } else {
                    wrapper.write(Type.BOOLEAN, light.trustEdges());
                    wrapper.write(Type.LONG_ARRAY_PRIMITIVE, light.skyLightMask());
                    wrapper.write(Type.LONG_ARRAY_PRIMITIVE, light.blockLightMask());
                    wrapper.write(Type.LONG_ARRAY_PRIMITIVE, light.emptySkyLightMask());
                    wrapper.write(Type.LONG_ARRAY_PRIMITIVE, light.emptyBlockLightMask());
                    wrapper.write(Type.VAR_INT, light.skyLight().length);
                    for (final byte[] skyLight : light.skyLight()) {
                        wrapper.write(Type.BYTE_ARRAY_PRIMITIVE, skyLight);
                    }
                    wrapper.write(Type.VAR_INT, light.blockLight().length);
                    for (final byte[] blockLight : light.blockLight()) {
                        wrapper.write(Type.BYTE_ARRAY_PRIMITIVE, blockLight);
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_17_1.UNLOAD_CHUNK, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                final int chunkX = wrapper.passthrough(Type.INT);
                final int chunkZ = wrapper.passthrough(Type.INT);
                wrapper.user().get(ChunkLightStorage.class).clear(chunkX, chunkZ);
            });
        }
    });
}
Also used : BlockEntityImpl(com.viaversion.viaversion.api.minecraft.blockentity.BlockEntityImpl) MathUtil(com.viaversion.viaversion.util.MathUtil) ChunkLightStorage(com.viaversion.viaversion.protocols.protocol1_18to1_17_1.storage.ChunkLightStorage) NumberTag(com.github.steveice10.opennbt.tag.builtin.NumberTag) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) Via(com.viaversion.viaversion.api.Via) Protocol1_18To1_17_1(com.viaversion.viaversion.protocols.protocol1_18to1_17_1.Protocol1_18To1_17_1) com.viaversion.viaversion.api.minecraft.chunks(com.viaversion.viaversion.api.minecraft.chunks) ArrayList(java.util.ArrayList) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Chunk1_17Type(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.types.Chunk1_17Type) List(java.util.List) ClientboundPackets1_17_1(com.viaversion.viaversion.protocols.protocol1_17_1to1_17.ClientboundPackets1_17_1) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) BitSet(java.util.BitSet) BlockEntityIds(com.viaversion.viaversion.protocols.protocol1_18to1_17_1.BlockEntityIds) Chunk1_18Type(com.viaversion.viaversion.protocols.protocol1_18to1_17_1.types.Chunk1_18Type) BlockEntity(com.viaversion.viaversion.api.minecraft.blockentity.BlockEntity) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) Chunk1_17Type(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.types.Chunk1_17Type) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) BitSet(java.util.BitSet) BlockEntityImpl(com.viaversion.viaversion.api.minecraft.blockentity.BlockEntityImpl) Chunk1_18Type(com.viaversion.viaversion.protocols.protocol1_18to1_17_1.types.Chunk1_18Type) ChunkLightStorage(com.viaversion.viaversion.protocols.protocol1_18to1_17_1.storage.ChunkLightStorage) NumberTag(com.github.steveice10.opennbt.tag.builtin.NumberTag) ArrayList(java.util.ArrayList) List(java.util.List) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag)

Aggregations

PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)4 Type (com.viaversion.viaversion.api.type.Type)4 Chunk1_17Type (com.viaversion.viaversion.protocols.protocol1_17to1_16_4.types.Chunk1_17Type)4 ArrayList (java.util.ArrayList)4 BitSet (java.util.BitSet)4 List (java.util.List)4 EntityTracker (com.viaversion.viaversion.api.data.entity.EntityTracker)3 Chunk (com.viaversion.viaversion.api.minecraft.chunks.Chunk)3 ChunkSection (com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)3 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)3 MathUtil (com.viaversion.viaversion.util.MathUtil)3 CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)2 ItemRewriter (com.viaversion.viabackwards.api.rewriters.ItemRewriter)2 BlockChangeRecord (com.viaversion.viaversion.api.minecraft.BlockChangeRecord)2 BlockEntity (com.viaversion.viaversion.api.minecraft.blockentity.BlockEntity)2 CompoundTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)2 ClientboundPackets1_16_2 (com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2)2 Chunk1_16_2Type (com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.types.Chunk1_16_2Type)2 RecipeRewriter1_16 (com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.RecipeRewriter1_16)2 ClientboundPackets1_17_1 (com.viaversion.viaversion.protocols.protocol1_17_1to1_17.ClientboundPackets1_17_1)2