Search in sources :

Example 21 with NbtMap

use of com.nukkitx.nbt.NbtMap in project JukeboxMC by LucGamesYT.

the class BlockPalette method init.

public static void init() {
    InputStream resourceAsStream = Bootstrap.class.getClassLoader().getResourceAsStream("block_palette.nbt");
    if (resourceAsStream != null) {
        try (NBTInputStream nbtReader = new NBTInputStream(new DataInputStream(new GZIPInputStream(resourceAsStream)))) {
            NbtMap nbtMap = (NbtMap) nbtReader.readTag();
            for (NbtMap blockMap : nbtMap.getList("blocks", NbtType.COMPOUND)) {
                int runtimeId = RUNTIME_COUNTER.getAndIncrement();
                BLOCK_PALETTE.put(runtimeId, blockMap);
                BLOCK_DATA.put(new BlockData(blockMap.getString("name"), blockMap.getCompound("states")), runtimeId);
                DEFAULTS.putIfAbsent(blockMap.getString("name"), runtimeId);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) DataInputStream(java.io.DataInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) NBTInputStream(com.nukkitx.nbt.NBTInputStream) InputStream(java.io.InputStream) NBTInputStream(com.nukkitx.nbt.NBTInputStream) NbtMap(com.nukkitx.nbt.NbtMap) Bootstrap(org.jukeboxmc.Bootstrap) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Example 22 with NbtMap

use of com.nukkitx.nbt.NbtMap in project JukeboxMC by LucGamesYT.

the class ItemCrossbow method getChargedItem.

public Item getChargedItem() {
    if (this.nbt != null && this.nbt.containsKey("chargedItem", NbtType.COMPOUND)) {
        NbtMap chargedItem = this.nbt.getCompound("chargedItem");
        String name = chargedItem.getString("Name", "minecraft:arrow");
        Item item = ItemType.get(name);
        item.setAmount(chargedItem.getByte("Count", (byte) 1));
        item.setMeta(chargedItem.getByte("Damage", (byte) 0));
        item.setNBT(chargedItem);
        return item;
    }
    return null;
}
Also used : NbtMap(com.nukkitx.nbt.NbtMap)

Example 23 with NbtMap

use of com.nukkitx.nbt.NbtMap in project JukeboxMC by LucGamesYT.

the class CreativeItems method init.

public static void init() {
    Gson GSON = new Gson();
    try (InputStream inputStream = Objects.requireNonNull(Bootstrap.class.getClassLoader().getResourceAsStream("creative_items.json"))) {
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        Map<String, List<Map<String, Object>>> itemEntries = GSON.fromJson(inputStreamReader, Map.class);
        int netIdCounter = 0;
        for (Map<String, Object> itemEntry : itemEntries.get("items")) {
            Item item;
            String identifier = (String) itemEntry.get("id");
            if (itemEntry.containsKey("blockRuntimeId")) {
                item = get(identifier, (int) (double) itemEntry.get("blockRuntimeId"));
            } else {
                item = get(identifier);
            }
            if (itemEntry.containsKey("damage")) {
                item.setMeta((short) (double) itemEntry.get("damage"));
            }
            String nbtTag = (String) itemEntry.get("nbt_b64");
            if (nbtTag != null)
                try (NBTInputStream nbtReader = NbtUtils.createReaderLE(new ByteArrayInputStream(Base64.getDecoder().decode(nbtTag.getBytes())))) {
                    item.setNBT((NbtMap) nbtReader.readTag());
                }
            CREATIVE_ITEMS.add(item.toNetwork(netIdCounter++));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) NBTInputStream(com.nukkitx.nbt.NBTInputStream) InputStream(java.io.InputStream) NbtMap(com.nukkitx.nbt.NbtMap) Gson(com.google.gson.Gson) IOException(java.io.IOException) Item(org.jukeboxmc.item.Item) ByteArrayInputStream(java.io.ByteArrayInputStream) NBTInputStream(com.nukkitx.nbt.NBTInputStream)

Example 24 with NbtMap

use of com.nukkitx.nbt.NbtMap in project JukeboxMC by LucGamesYT.

the class LevelDB method loadBlockEntities.

public static void loadBlockEntities(Chunk chunk, byte[] blockEntityData) {
    ByteBuf byteBuf = Unpooled.wrappedBuffer(blockEntityData);
    try {
        NBTInputStream reader = NbtUtils.createReaderLE(new ByteBufInputStream(byteBuf));
        while (byteBuf.readableBytes() > 0) {
            try {
                NbtMap nbtMap = (NbtMap) reader.readTag();
                int x = nbtMap.getInt("x", 0);
                int y = nbtMap.getInt("y", 0);
                int z = nbtMap.getInt("z", 0);
                Block block = chunk.getBlock(x, y, z, 0);
                if (block != null && block.hasBlockEntity()) {
                    BlockEntity blockEntity = BlockEntityType.getBlockEntityById(nbtMap.getString("id"), block);
                    if (blockEntity != null) {
                        blockEntity.fromCompound(nbtMap);
                        chunk.setBlockEntity(x, y, z, blockEntity);
                    }
                }
            } catch (IOException e) {
                break;
            }
        }
    } finally {
        byteBuf.release();
    }
}
Also used : NBTInputStream(com.nukkitx.nbt.NBTInputStream) NbtMap(com.nukkitx.nbt.NbtMap) Block(org.jukeboxmc.block.Block) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) BlockEntity(org.jukeboxmc.blockentity.BlockEntity)

Example 25 with NbtMap

use of com.nukkitx.nbt.NbtMap in project JukeboxMC by LucGamesYT.

the class LevelDB method loadSection.

public static void loadSection(SubChunk chunk, byte[] chunkData) {
    ByteBuf buffer = Unpooled.wrappedBuffer(chunkData);
    try {
        byte subChunkVersion = buffer.readByte();
        chunk.setSubChunkVersion(subChunkVersion);
        int storages = 1;
        switch(subChunkVersion) {
            case 9:
            case 8:
                storages = buffer.readByte();
                chunk.setLayer(storages);
                if (subChunkVersion == 9) {
                    byte subY = buffer.readByte();
                }
            case 1:
                for (int layer = 0; layer < storages; layer++) {
                    try {
                        buffer.markReaderIndex();
                        chunk.blocks[layer].readFromStoragePersistent(buffer, compound -> {
                            String identifier = compound.getString("name");
                            NbtMap states = compound.getCompound("states");
                            return BlockPalette.getRuntimeId(identifier, Objects.requireNonNullElse(states, NbtMap.EMPTY));
                        });
                    } catch (IllegalArgumentException e) {
                        buffer.resetReaderIndex();
                        chunk.blocks[layer].readFromStorageRuntime(buffer, runtimeId -> runtimeId, null);
                    }
                }
                break;
        }
    } finally {
        buffer.release();
    }
}
Also used : NbtMap(com.nukkitx.nbt.NbtMap) NbtUtils(com.nukkitx.nbt.NbtUtils) BlockEntityType(org.jukeboxmc.blockentity.BlockEntityType) BlockPalette(org.jukeboxmc.block.BlockPalette) SubChunk(org.jukeboxmc.world.chunk.SubChunk) IOException(java.io.IOException) ObjectPalette(org.jukeboxmc.world.palette.object.ObjectPalette) NBTInputStream(com.nukkitx.nbt.NBTInputStream) Unpooled(io.netty.buffer.Unpooled) Biome(org.jukeboxmc.world.Biome) Objects(java.util.Objects) Chunk(org.jukeboxmc.world.chunk.Chunk) ByteBuf(io.netty.buffer.ByteBuf) BlockEntity(org.jukeboxmc.blockentity.BlockEntity) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) Block(org.jukeboxmc.block.Block) NbtMap(com.nukkitx.nbt.NbtMap) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

NbtMap (com.nukkitx.nbt.NbtMap)50 Item (org.jukeboxmc.item.Item)16 NbtMapBuilder (com.nukkitx.nbt.NbtMapBuilder)14 IOException (java.io.IOException)12 ArrayList (java.util.ArrayList)11 NBTInputStream (com.nukkitx.nbt.NBTInputStream)9 ItemData (com.nukkitx.protocol.bedrock.data.inventory.ItemData)5 ByteBuf (io.netty.buffer.ByteBuf)5 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Map (java.util.Map)4 BlockEntityShulkerBox (org.jukeboxmc.blockentity.BlockEntityShulkerBox)4 CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)3 Vector3i (com.nukkitx.math.vector.Vector3i)3 InputStream (java.io.InputStream)3 ItemAir (org.jukeboxmc.item.ItemAir)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ComponentItemData (com.nukkitx.protocol.bedrock.data.inventory.ComponentItemData)2 BlockEntityDataPacket (com.nukkitx.protocol.bedrock.packet.BlockEntityDataPacket)2 UpdateBlockPacket (com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket)2