Search in sources :

Example 6 with NumberTag

use of com.github.steveice10.opennbt.tag.builtin.NumberTag in project ViaVersion by ViaVersion.

the class BannerHandler method transform.

@Override
public int transform(UserConnection user, CompoundTag tag) {
    BlockStorage storage = user.get(BlockStorage.class);
    Position position = new Position((int) getLong(tag.get("x")), (short) getLong(tag.get("y")), (int) getLong(tag.get("z")));
    if (!storage.contains(position)) {
        Via.getPlatform().getLogger().warning("Received an banner color update packet, but there is no banner! O_o " + tag);
        return -1;
    }
    int blockId = storage.get(position).getOriginal();
    Tag base = tag.get("Base");
    int color = 0;
    if (base != null) {
        color = ((NumberTag) tag.get("Base")).asInt();
    }
    // Standing banner
    if (blockId >= BANNER_START && blockId <= BANNER_STOP) {
        blockId += ((15 - color) * 16);
    // Wall banner
    } else if (blockId >= WALL_BANNER_START && blockId <= WALL_BANNER_STOP) {
        blockId += ((15 - color) * 4);
    } else {
        Via.getPlatform().getLogger().warning("Why does this block have the banner block entity? :(" + tag);
    }
    if (tag.get("Patterns") instanceof ListTag) {
        for (Tag pattern : (ListTag) tag.get("Patterns")) {
            if (pattern instanceof CompoundTag) {
                Tag c = ((CompoundTag) pattern).get("Color");
                if (c instanceof IntTag) {
                    // Invert color id
                    ((IntTag) c).setValue(15 - (int) c.getValue());
                }
            }
        }
    }
    Tag name = tag.get("CustomName");
    if (name instanceof StringTag) {
        ((StringTag) name).setValue(ChatRewriter.legacyTextToJsonString(((StringTag) name).getValue()));
    }
    return blockId;
}
Also used : StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) BlockStorage(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockStorage) Position(com.viaversion.viaversion.api.minecraft.Position) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) Tag(com.github.steveice10.opennbt.tag.builtin.Tag) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) NumberTag(com.github.steveice10.opennbt.tag.builtin.NumberTag) IntTag(com.github.steveice10.opennbt.tag.builtin.IntTag) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) IntTag(com.github.steveice10.opennbt.tag.builtin.IntTag)

Example 7 with NumberTag

use of com.github.steveice10.opennbt.tag.builtin.NumberTag in project ViaVersion by ViaVersion.

the class SkullHandler method transform.

@Override
public int transform(UserConnection user, CompoundTag tag) {
    BlockStorage storage = user.get(BlockStorage.class);
    Position position = new Position((int) getLong(tag.get("x")), (short) getLong(tag.get("y")), (int) getLong(tag.get("z")));
    if (!storage.contains(position)) {
        Via.getPlatform().getLogger().warning("Received an head update packet, but there is no head! O_o " + tag);
        return -1;
    }
    int id = storage.get(position).getOriginal();
    if (id >= SKULL_WALL_START && id <= SKULL_END) {
        Tag skullType = tag.get("SkullType");
        if (skullType != null) {
            id += ((NumberTag) tag.get("SkullType")).asInt() * 20;
        }
        if (tag.contains("Rot")) {
            id += ((NumberTag) tag.get("Rot")).asInt();
        }
    } else {
        Via.getPlatform().getLogger().warning("Why does this block have the skull block entity? " + tag);
        return -1;
    }
    return id;
}
Also used : BlockStorage(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockStorage) Position(com.viaversion.viaversion.api.minecraft.Position) NumberTag(com.github.steveice10.opennbt.tag.builtin.NumberTag) Tag(com.github.steveice10.opennbt.tag.builtin.Tag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) NumberTag(com.github.steveice10.opennbt.tag.builtin.NumberTag)

Example 8 with NumberTag

use of com.github.steveice10.opennbt.tag.builtin.NumberTag 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)

Example 9 with NumberTag

use of com.github.steveice10.opennbt.tag.builtin.NumberTag in project ViaVersion by ViaVersion.

the class BlockEntity method handle.

public static void handle(List<CompoundTag> tags, UserConnection connection) {
    for (CompoundTag tag : tags) {
        try {
            if (!tag.contains("id"))
                throw new Exception("NBT tag not handled because the id key is missing");
            String id = (String) tag.get("id").getValue();
            if (!types.containsKey(id))
                throw new Exception("Not handled id: " + id);
            int newId = types.get(id);
            if (newId == -1)
                continue;
            int x = ((NumberTag) tag.get("x")).asInt();
            int y = ((NumberTag) tag.get("y")).asInt();
            int z = ((NumberTag) tag.get("z")).asInt();
            Position pos = new Position(x, (short) y, z);
            updateBlockEntity(pos, (short) newId, tag, connection);
        } catch (Exception e) {
            if (Via.getManager().isDebug()) {
                Via.getPlatform().getLogger().warning("Block Entity: " + e.getMessage() + ": " + tag);
            }
        }
    }
}
Also used : Position(com.viaversion.viaversion.api.minecraft.Position) NumberTag(com.github.steveice10.opennbt.tag.builtin.NumberTag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag)

Example 10 with NumberTag

use of com.github.steveice10.opennbt.tag.builtin.NumberTag in project ViaVersion by ViaVersion.

the class TagStringReader method intArray.

private int[] intArray() throws StringTagParseException {
    if (this.buffer.takeIf(Tokens.ARRAY_END)) {
        return EMPTY_INT_ARRAY;
    }
    final IntStream.Builder builder = IntStream.builder();
    while (this.buffer.hasMore()) {
        final Tag value = this.tag();
        if (!(value instanceof IntTag)) {
            throw this.buffer.makeError("All elements of an int array must be ints!");
        }
        builder.add(((NumberTag) value).asInt());
        if (this.separatorOrCompleteWith(Tokens.ARRAY_END)) {
            return builder.build().toArray();
        }
    }
    throw this.buffer.makeError("Reached end of document without array close");
}
Also used : ByteTag(com.github.steveice10.opennbt.tag.builtin.ByteTag) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) IntArrayTag(com.github.steveice10.opennbt.tag.builtin.IntArrayTag) NumberTag(com.github.steveice10.opennbt.tag.builtin.NumberTag) IntTag(com.github.steveice10.opennbt.tag.builtin.IntTag) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) Tag(com.github.steveice10.opennbt.tag.builtin.Tag) LongTag(com.github.steveice10.opennbt.tag.builtin.LongTag) ShortTag(com.github.steveice10.opennbt.tag.builtin.ShortTag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) LongArrayTag(com.github.steveice10.opennbt.tag.builtin.LongArrayTag) ByteArrayTag(com.github.steveice10.opennbt.tag.builtin.ByteArrayTag) DoubleTag(com.github.steveice10.opennbt.tag.builtin.DoubleTag) FloatTag(com.github.steveice10.opennbt.tag.builtin.FloatTag) IntStream(java.util.stream.IntStream) IntTag(com.github.steveice10.opennbt.tag.builtin.IntTag)

Aggregations

CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)10 NumberTag (com.github.steveice10.opennbt.tag.builtin.NumberTag)10 Tag (com.github.steveice10.opennbt.tag.builtin.Tag)8 StringTag (com.github.steveice10.opennbt.tag.builtin.StringTag)7 ListTag (com.github.steveice10.opennbt.tag.builtin.ListTag)5 Position (com.viaversion.viaversion.api.minecraft.Position)5 IntTag (com.github.steveice10.opennbt.tag.builtin.IntTag)4 BlockStorage (com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockStorage)4 ShortTag (com.github.steveice10.opennbt.tag.builtin.ShortTag)3 IntArrayTag (com.github.steveice10.opennbt.tag.builtin.IntArrayTag)2 LongTag (com.github.steveice10.opennbt.tag.builtin.LongTag)2 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)2 ByteArrayTag (com.github.steveice10.opennbt.tag.builtin.ByteArrayTag)1 ByteTag (com.github.steveice10.opennbt.tag.builtin.ByteTag)1 DoubleTag (com.github.steveice10.opennbt.tag.builtin.DoubleTag)1 FloatTag (com.github.steveice10.opennbt.tag.builtin.FloatTag)1 LongArrayTag (com.github.steveice10.opennbt.tag.builtin.LongArrayTag)1 Via (com.viaversion.viaversion.api.Via)1 UserConnection (com.viaversion.viaversion.api.connection.UserConnection)1 EntityTracker (com.viaversion.viaversion.api.data.entity.EntityTracker)1