Search in sources :

Example 1 with MappedLegacyBlockItem

use of com.viaversion.viabackwards.api.data.MappedLegacyBlockItem in project ViaBackwards by ViaVersion.

the class LegacyBlockItemRewriter method handleBlock.

@Nullable
public Block handleBlock(int blockId, int data) {
    MappedLegacyBlockItem settings = replacementData.get(blockId);
    if (settings == null || !settings.isBlock())
        return null;
    Block block = settings.getBlock();
    // For some blocks, the data can still be useful (:
    if (block.getData() == -1) {
        return block.withData(data);
    }
    return block;
}
Also used : MappedLegacyBlockItem(com.viaversion.viabackwards.api.data.MappedLegacyBlockItem) Block(com.viaversion.viabackwards.utils.Block) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 2 with MappedLegacyBlockItem

use of com.viaversion.viabackwards.api.data.MappedLegacyBlockItem in project ViaBackwards by ViaVersion.

the class LegacyBlockItemRewriter method handleItemToClient.

@Override
@Nullable
public Item handleItemToClient(@Nullable Item item) {
    if (item == null)
        return null;
    MappedLegacyBlockItem data = replacementData.get(item.identifier());
    if (data == null) {
        // Just rewrite the id
        return super.handleItemToClient(item);
    }
    short originalData = item.data();
    item.setIdentifier(data.getId());
    // Keep original data if mapped data is set to -1
    if (data.getData() != -1) {
        item.setData(data.getData());
    }
    // Set display name
    if (data.getName() != null) {
        if (item.tag() == null) {
            item.setTag(new CompoundTag());
        }
        CompoundTag display = item.tag().get("display");
        if (display == null) {
            item.tag().put("display", display = new CompoundTag());
        }
        StringTag nameTag = display.get("Name");
        if (nameTag == null) {
            display.put("Name", nameTag = new StringTag(data.getName()));
            display.put(nbtTagName + "|customName", new ByteTag());
        }
        // Handle colors
        String value = nameTag.getValue();
        if (value.contains("%vb_color%")) {
            display.put("Name", new StringTag(value.replace("%vb_color%", BlockColors.get(originalData))));
        }
    }
    return item;
}
Also used : StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) MappedLegacyBlockItem(com.viaversion.viabackwards.api.data.MappedLegacyBlockItem) ByteTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ByteTag) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 3 with MappedLegacyBlockItem

use of com.viaversion.viabackwards.api.data.MappedLegacyBlockItem 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)

Example 4 with MappedLegacyBlockItem

use of com.viaversion.viabackwards.api.data.MappedLegacyBlockItem in project ViaBackwards by ViaVersion.

the class BlockItemPackets1_11 method registerRewrites.

@Override
protected void registerRewrites() {
    // Handle spawner block entity (map to itself with custom handler)
    MappedLegacyBlockItem data = replacementData.computeIfAbsent(52, s -> new MappedLegacyBlockItem(52, (short) -1, null, false));
    data.setBlockEntityHandler((b, tag) -> {
        EntityIdRewriter.toClientSpawner(tag, true);
        return tag;
    });
    enchantmentRewriter = new LegacyEnchantmentRewriter(nbtTagName);
    enchantmentRewriter.registerEnchantment(71, "§cCurse of Vanishing");
    enchantmentRewriter.registerEnchantment(10, "§cCurse of Binding");
    // Curses do not display their level
    enchantmentRewriter.setHideLevelForEnchants(71, 10);
}
Also used : LegacyEnchantmentRewriter(com.viaversion.viabackwards.api.rewriters.LegacyEnchantmentRewriter) MappedLegacyBlockItem(com.viaversion.viabackwards.api.data.MappedLegacyBlockItem)

Aggregations

MappedLegacyBlockItem (com.viaversion.viabackwards.api.data.MappedLegacyBlockItem)4 Block (com.viaversion.viabackwards.utils.Block)2 ByteTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.ByteTag)2 CompoundTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)2 StringTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag)2 Nullable (org.checkerframework.checker.nullness.qual.Nullable)2 LegacyEnchantmentRewriter (com.viaversion.viabackwards.api.rewriters.LegacyEnchantmentRewriter)1 ChunkSection (com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)1 Int2ObjectOpenHashMap (com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectOpenHashMap)1 IntTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag)1 NumberTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag)1 Tag (com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag)1 HashMap (java.util.HashMap)1