Search in sources :

Example 6 with IntTag

use of com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag 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 7 with IntTag

use of com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag in project ViaBackwards by ViaVersion.

the class LegacyEnchantmentRewriter method rewriteEnchantmentsToClient.

public void rewriteEnchantmentsToClient(CompoundTag tag, boolean storedEnchant) {
    String key = storedEnchant ? "StoredEnchantments" : "ench";
    ListTag enchantments = tag.get(key);
    ListTag remappedEnchantments = new ListTag(CompoundTag.class);
    List<Tag> lore = new ArrayList<>();
    for (Tag enchantmentEntry : enchantments.clone()) {
        Tag idTag = ((CompoundTag) enchantmentEntry).get("id");
        if (idTag == null)
            continue;
        short newId = ((NumberTag) idTag).asShort();
        String enchantmentName = enchantmentMappings.get(newId);
        if (enchantmentName != null) {
            enchantments.remove(enchantmentEntry);
            short level = ((NumberTag) ((CompoundTag) enchantmentEntry).get("lvl")).asShort();
            if (hideLevelForEnchants != null && hideLevelForEnchants.contains(newId)) {
                lore.add(new StringTag(enchantmentName));
            } else {
                lore.add(new StringTag(enchantmentName + " " + EnchantmentRewriter.getRomanNumber(level)));
            }
            remappedEnchantments.add(enchantmentEntry);
        }
    }
    if (!lore.isEmpty()) {
        if (!storedEnchant && enchantments.size() == 0) {
            CompoundTag dummyEnchantment = new CompoundTag();
            dummyEnchantment.put("id", new ShortTag((short) 0));
            dummyEnchantment.put("lvl", new ShortTag((short) 0));
            enchantments.add(dummyEnchantment);
            tag.put(nbtTagName + "|dummyEnchant", new ByteTag());
            IntTag hideFlags = tag.get("HideFlags");
            if (hideFlags == null) {
                hideFlags = new IntTag();
            } else {
                tag.put(nbtTagName + "|oldHideFlags", new IntTag(hideFlags.asByte()));
            }
            int flags = hideFlags.asByte() | 1;
            hideFlags.setValue(flags);
            tag.put("HideFlags", hideFlags);
        }
        tag.put(nbtTagName + "|" + key, remappedEnchantments);
        CompoundTag display = tag.get("display");
        if (display == null) {
            tag.put("display", display = new CompoundTag());
        }
        ListTag loreTag = display.get("Lore");
        if (loreTag == null) {
            display.put("Lore", loreTag = new ListTag(StringTag.class));
        }
        lore.addAll(loreTag.getValue());
        loreTag.setValue(lore);
    }
}
Also used : StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) ArrayList(java.util.ArrayList) ListTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag) ShortTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ShortTag) ByteTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ByteTag) NumberTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag) Tag(com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag) StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) ListTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag) NumberTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag) ShortTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ShortTag) 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) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) IntTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag)

Example 8 with IntTag

use of com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag in project ViaBackwards by ViaVersion.

the class LegacyEnchantmentRewriter method rewriteEnchantmentsToServer.

public void rewriteEnchantmentsToServer(CompoundTag tag, boolean storedEnchant) {
    String key = storedEnchant ? "StoredEnchantments" : "ench";
    ListTag remappedEnchantments = tag.remove(nbtTagName + "|" + key);
    ListTag enchantments = tag.get(key);
    if (enchantments == null) {
        enchantments = new ListTag(CompoundTag.class);
    }
    if (!storedEnchant && tag.remove(nbtTagName + "|dummyEnchant") != null) {
        for (Tag enchantment : enchantments.clone()) {
            short id = ((NumberTag) ((CompoundTag) enchantment).get("id")).asShort();
            short level = ((NumberTag) ((CompoundTag) enchantment).get("lvl")).asShort();
            if (id == 0 && level == 0) {
                enchantments.remove(enchantment);
            }
        }
        IntTag hideFlags = tag.remove(nbtTagName + "|oldHideFlags");
        if (hideFlags != null) {
            tag.put("HideFlags", new IntTag(hideFlags.asByte()));
        } else {
            tag.remove("HideFlags");
        }
    }
    CompoundTag display = tag.get("display");
    // A few null checks just to be safe, though they shouldn't actually be
    ListTag lore = display != null ? display.get("Lore") : null;
    for (Tag enchantment : remappedEnchantments.clone()) {
        enchantments.add(enchantment);
        if (lore != null && lore.size() != 0) {
            lore.remove(lore.get(0));
        }
    }
    if (lore != null && lore.size() == 0) {
        display.remove("Lore");
        if (display.isEmpty()) {
            tag.remove("display");
        }
    }
    tag.put(key, enchantments);
}
Also used : NumberTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag) Tag(com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag) StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) ListTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag) NumberTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag) ShortTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ShortTag) 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) ListTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) IntTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag)

Example 9 with IntTag

use of com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag in project ViaBackwards by ViaVersion.

the class BannerHandler method transform.

@Override
public CompoundTag transform(UserConnection user, int blockId, CompoundTag tag) {
    // Normal banners
    if (blockId >= BANNER_START && blockId <= BANNER_STOP) {
        int color = (blockId - BANNER_START) >> 4;
        tag.put("Base", new IntTag((15 - color)));
    } else // Wall banners
    if (blockId >= WALL_BANNER_START && blockId <= WALL_BANNER_STOP) {
        int color = (blockId - WALL_BANNER_START) >> 2;
        tag.put("Base", new IntTag((15 - color)));
    } else {
        ViaBackwards.getPlatform().getLogger().warning("Why does this block have the banner block entity? :(" + tag);
    }
    // Invert colors
    Tag patternsTag = tag.get("Patterns");
    if (patternsTag instanceof ListTag) {
        for (Tag pattern : (ListTag) patternsTag) {
            if (!(pattern instanceof CompoundTag))
                continue;
            IntTag c = ((CompoundTag) pattern).get("Color");
            // Invert color id
            c.setValue(15 - c.asInt());
        }
    }
    return tag;
}
Also used : Tag(com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) IntTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag) ListTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag) ListTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag) IntTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)

Example 10 with IntTag

use of com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag in project ViaBackwards by ViaVersion.

the class PistonHandler method transform.

@Override
public CompoundTag transform(UserConnection user, int blockId, CompoundTag tag) {
    CompoundTag blockState = tag.get("blockState");
    if (blockState == null)
        return tag;
    String dataFromTag = getDataFromTag(blockState);
    if (dataFromTag == null)
        return tag;
    Integer id = pistonIds.get(dataFromTag);
    if (id == null) {
        // TODO see why this could be null and if this is bad
        return tag;
    }
    tag.put("blockId", new IntTag(id >> 4));
    tag.put("blockData", new IntTag(id & 15));
    return tag;
}
Also used : CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) IntTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag)

Aggregations

IntTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag)16 CompoundTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)13 StringTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag)12 Tag (com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag)10 ByteTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.ByteTag)9 ListTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag)9 NumberTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag)8 ShortTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.ShortTag)7 ArrayList (java.util.ArrayList)3 ChunkSection (com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)2 MappedItem (com.viaversion.viabackwards.api.data.MappedItem)1 MappedLegacyBlockItem (com.viaversion.viabackwards.api.data.MappedLegacyBlockItem)1 ItemRewriter (com.viaversion.viabackwards.api.rewriters.ItemRewriter)1 Protocol1_17_1To1_18 (com.viaversion.viabackwards.protocol.protocol1_17_1to1_18.Protocol1_17_1To1_18)1 BlockEntityIds (com.viaversion.viabackwards.protocol.protocol1_17_1to1_18.data.BlockEntityIds)1 Block (com.viaversion.viabackwards.utils.Block)1 ParticleMappings (com.viaversion.viaversion.api.data.ParticleMappings)1 EntityTracker (com.viaversion.viaversion.api.data.entity.EntityTracker)1 Position (com.viaversion.viaversion.api.minecraft.Position)1 BlockEntity (com.viaversion.viaversion.api.minecraft.blockentity.BlockEntity)1