Search in sources :

Example 6 with Block

use of com.viaversion.viabackwards.utils.Block in project ViaBackwards by ViaVersion.

the class LegacyBlockItemRewriter method handleChunk.

protected void handleChunk(Chunk chunk) {
    // Map Block Entities
    Map<Pos, CompoundTag> tags = new HashMap<>();
    for (CompoundTag tag : chunk.getBlockEntities()) {
        Tag xTag;
        Tag yTag;
        Tag zTag;
        if ((xTag = tag.get("x")) == null || (yTag = tag.get("y")) == null || (zTag = tag.get("z")) == null) {
            continue;
        }
        Pos pos = new Pos(((NumberTag) xTag).asInt() & 0xF, ((NumberTag) yTag).asInt(), ((NumberTag) zTag).asInt() & 0xF);
        tags.put(pos, tag);
        // 1.17
        if (pos.getY() < 0 || pos.getY() > 255)
            continue;
        ChunkSection section = chunk.getSections()[pos.getY() >> 4];
        if (section == null)
            continue;
        int block = section.getFlatBlock(pos.getX(), pos.getY() & 0xF, pos.getZ());
        int btype = block >> 4;
        MappedLegacyBlockItem settings = replacementData.get(btype);
        if (settings != null && settings.hasBlockEntityHandler()) {
            settings.getBlockEntityHandler().handleOrNewCompoundTag(block, tag);
        }
    }
    for (int i = 0; i < chunk.getSections().length; i++) {
        ChunkSection section = chunk.getSections()[i];
        if (section == null)
            continue;
        boolean hasBlockEntityHandler = false;
        // Map blocks
        for (int j = 0; j < section.getPaletteSize(); j++) {
            int block = section.getPaletteEntry(j);
            int btype = block >> 4;
            int meta = block & 0xF;
            Block b = handleBlock(btype, meta);
            if (b != null) {
                section.setPaletteEntry(j, (b.getId() << 4) | (b.getData() & 0xF));
            }
            // We already know that is has a handler
            if (hasBlockEntityHandler)
                continue;
            MappedLegacyBlockItem settings = replacementData.get(btype);
            if (settings != null && settings.hasBlockEntityHandler()) {
                hasBlockEntityHandler = true;
            }
        }
        if (!hasBlockEntityHandler)
            continue;
        // We need to handle a Block Entity :(
        for (int x = 0; x < 16; x++) {
            for (int y = 0; y < 16; y++) {
                for (int z = 0; z < 16; z++) {
                    int block = section.getFlatBlock(x, y, z);
                    int btype = block >> 4;
                    int meta = block & 15;
                    MappedLegacyBlockItem settings = replacementData.get(btype);
                    if (settings == null || !settings.hasBlockEntityHandler())
                        continue;
                    Pos pos = new Pos(x, (y + (i << 4)), z);
                    // Already handled above
                    if (tags.containsKey(pos))
                        continue;
                    CompoundTag tag = new CompoundTag();
                    tag.put("x", new IntTag(x + (chunk.getX() << 4)));
                    tag.put("y", new IntTag(y + (i << 4)));
                    tag.put("z", new IntTag(z + (chunk.getZ() << 4)));
                    settings.getBlockEntityHandler().handleOrNewCompoundTag(block, tag);
                    chunk.getBlockEntities().add(tag);
                }
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) Int2ObjectOpenHashMap(com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectOpenHashMap) MappedLegacyBlockItem(com.viaversion.viabackwards.api.data.MappedLegacyBlockItem) NumberTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag) Block(com.viaversion.viabackwards.utils.Block) Tag(com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag) StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) NumberTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) IntTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag) ByteTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ByteTag) 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)

Aggregations

Block (com.viaversion.viabackwards.utils.Block)6 EntityType (com.viaversion.viaversion.api.minecraft.entities.EntityType)3 Metadata (com.viaversion.viaversion.api.minecraft.metadata.Metadata)3 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)3 PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)3 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)3 MappedLegacyBlockItem (com.viaversion.viabackwards.api.data.MappedLegacyBlockItem)2 EntityData (com.viaversion.viabackwards.api.entities.storage.EntityData)2 WrappedMetadata (com.viaversion.viabackwards.api.entities.storage.WrappedMetadata)2 LegacyEntityRewriter (com.viaversion.viabackwards.api.rewriters.LegacyEntityRewriter)2 StoredEntityData (com.viaversion.viaversion.api.data.entity.StoredEntityData)2 Entity1_12Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_12Types)2 Type (com.viaversion.viaversion.api.type.Type)2 CompoundTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)2 Optional (java.util.Optional)2 PotionSplashHandler (com.viaversion.viabackwards.protocol.protocol1_10to1_11.PotionSplashHandler)1 Protocol1_10To1_11 (com.viaversion.viabackwards.protocol.protocol1_10to1_11.Protocol1_10To1_11)1 ChestedHorseStorage (com.viaversion.viabackwards.protocol.protocol1_10to1_11.storage.ChestedHorseStorage)1 Protocol1_11_1To1_12 (com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.Protocol1_11_1To1_12)1 ParrotStorage (com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.data.ParrotStorage)1