Search in sources :

Example 1 with Tag

use of cn.nukkit.nbt.tag.Tag in project Nukkit by Nukkit.

the class BlockBrewingStand method place.

@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    if (!block.down().isTransparent()) {
        getLevel().setBlock(block, this, true, true);
        CompoundTag nbt = new CompoundTag().putList(new ListTag<>("Items")).putString("id", BlockEntity.BREWING_STAND).putInt("x", (int) this.x).putInt("y", (int) this.y).putInt("z", (int) this.z);
        if (item.hasCustomName()) {
            nbt.putString("CustomName", item.getCustomName());
        }
        if (item.hasCustomBlockData()) {
            Map<String, Tag> customData = item.getCustomBlockData().getTags();
            for (Map.Entry<String, Tag> tag : customData.entrySet()) {
                nbt.put(tag.getKey(), tag.getValue());
            }
        }
        new BlockEntityBrewingStand(getLevel().getChunk((int) this.x >> 4, (int) this.z >> 4), nbt);
        return true;
    }
    return false;
}
Also used : StringTag(cn.nukkit.nbt.tag.StringTag) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) Tag(cn.nukkit.nbt.tag.Tag) ListTag(cn.nukkit.nbt.tag.ListTag) Map(java.util.Map) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) BlockEntityBrewingStand(cn.nukkit.blockentity.BlockEntityBrewingStand)

Example 2 with Tag

use of cn.nukkit.nbt.tag.Tag in project Nukkit by Nukkit.

the class BlockEnchantingTable method place.

@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    this.getLevel().setBlock(block, this, true, true);
    CompoundTag nbt = new CompoundTag().putString("id", BlockEntity.ENCHANT_TABLE).putInt("x", (int) this.x).putInt("y", (int) this.y).putInt("z", (int) this.z);
    if (item.hasCustomName()) {
        nbt.putString("CustomName", item.getCustomName());
    }
    if (item.hasCustomBlockData()) {
        Map<String, Tag> customData = item.getCustomBlockData().getTags();
        for (Map.Entry<String, Tag> tag : customData.entrySet()) {
            nbt.put(tag.getKey(), tag.getValue());
        }
    }
    BlockEntity.createBlockEntity(BlockEntity.ENCHANT_TABLE, getLevel().getChunk((int) this.x >> 4, (int) this.z >> 4), nbt);
    return true;
}
Also used : CompoundTag(cn.nukkit.nbt.tag.CompoundTag) StringTag(cn.nukkit.nbt.tag.StringTag) Tag(cn.nukkit.nbt.tag.Tag) ListTag(cn.nukkit.nbt.tag.ListTag) Map(java.util.Map) CompoundTag(cn.nukkit.nbt.tag.CompoundTag)

Example 3 with Tag

use of cn.nukkit.nbt.tag.Tag in project Nukkit by Nukkit.

the class BlockFlowerPot method place.

@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    if (face != BlockFace.UP)
        return false;
    CompoundTag nbt = new CompoundTag().putString("id", BlockEntity.FLOWER_POT).putInt("x", (int) this.x).putInt("y", (int) this.y).putInt("z", (int) this.z).putShort("item", 0).putInt("data", 0);
    if (item.hasCustomBlockData()) {
        for (Tag aTag : item.getCustomBlockData().getAllTags()) {
            nbt.put(aTag.getName(), aTag);
        }
    }
    new BlockEntityFlowerPot(getLevel().getChunk((int) block.x >> 4, (int) block.z >> 4), nbt);
    this.getLevel().setBlock(block, this, true, true);
    return true;
}
Also used : CompoundTag(cn.nukkit.nbt.tag.CompoundTag) Tag(cn.nukkit.nbt.tag.Tag) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) BlockEntityFlowerPot(cn.nukkit.blockentity.BlockEntityFlowerPot)

Example 4 with Tag

use of cn.nukkit.nbt.tag.Tag in project Nukkit by Nukkit.

the class BlockFurnaceBurning method place.

@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    int[] faces = { 2, 5, 3, 4 };
    this.setDamage(faces[player != null ? player.getDirection().getHorizontalIndex() : 0]);
    this.getLevel().setBlock(block, this, true, true);
    CompoundTag nbt = new CompoundTag().putList(new ListTag<>("Items")).putString("id", BlockEntity.FURNACE).putInt("x", (int) this.x).putInt("y", (int) this.y).putInt("z", (int) this.z);
    if (item.hasCustomName()) {
        nbt.putString("CustomName", item.getCustomName());
    }
    if (item.hasCustomBlockData()) {
        Map<String, Tag> customData = item.getCustomBlockData().getTags();
        for (Map.Entry<String, Tag> tag : customData.entrySet()) {
            nbt.put(tag.getKey(), tag.getValue());
        }
    }
    new BlockEntityFurnace(this.getLevel().getChunk((int) (this.x) >> 4, (int) (this.z) >> 4), nbt);
    return true;
}
Also used : BlockEntityFurnace(cn.nukkit.blockentity.BlockEntityFurnace) StringTag(cn.nukkit.nbt.tag.StringTag) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) Tag(cn.nukkit.nbt.tag.Tag) ListTag(cn.nukkit.nbt.tag.ListTag) Map(java.util.Map) CompoundTag(cn.nukkit.nbt.tag.CompoundTag)

Example 5 with Tag

use of cn.nukkit.nbt.tag.Tag in project Nukkit by Nukkit.

the class Chunk method fromBinary.

public static Chunk fromBinary(byte[] data, LevelProvider provider) {
    try {
        int chunkX = Binary.readLInt(new byte[] { data[0], data[1], data[2], data[3] });
        int chunkZ = Binary.readLInt(new byte[] { data[4], data[5], data[6], data[7] });
        byte[] chunkData = Binary.subBytes(data, 8, data.length - 1);
        int flags = data[data.length - 1];
        List<CompoundTag> entities = new ArrayList<>();
        List<CompoundTag> tiles = new ArrayList<>();
        Map<Integer, Integer> extraDataMap = new HashMap<>();
        if (provider instanceof LevelDB) {
            byte[] entityData = ((LevelDB) provider).getDatabase().get(EntitiesKey.create(chunkX, chunkZ).toArray());
            if (entityData != null && entityData.length > 0) {
                try (NBTInputStream nbtInputStream = new NBTInputStream(new ByteArrayInputStream(entityData), ByteOrder.LITTLE_ENDIAN)) {
                    while (nbtInputStream.available() > 0) {
                        Tag tag = Tag.readNamedTag(nbtInputStream);
                        if (!(tag instanceof CompoundTag)) {
                            throw new IOException("Root tag must be a named compound tag");
                        }
                        entities.add((CompoundTag) tag);
                    }
                }
            }
            byte[] tileData = ((LevelDB) provider).getDatabase().get(TilesKey.create(chunkX, chunkZ).toArray());
            if (tileData != null && tileData.length > 0) {
                try (NBTInputStream nbtInputStream = new NBTInputStream(new ByteArrayInputStream(tileData), ByteOrder.LITTLE_ENDIAN)) {
                    while (nbtInputStream.available() > 0) {
                        Tag tag = Tag.readNamedTag(nbtInputStream);
                        if (!(tag instanceof CompoundTag)) {
                            throw new IOException("Root tag must be a named compound tag");
                        }
                        tiles.add((CompoundTag) tag);
                    }
                }
            }
            byte[] extraData = ((LevelDB) provider).getDatabase().get(ExtraDataKey.create(chunkX, chunkZ).toArray());
            if (extraData != null && extraData.length > 0) {
                BinaryStream stream = new BinaryStream(tileData);
                int count = stream.getInt();
                for (int i = 0; i < count; ++i) {
                    int key = stream.getInt();
                    int value = stream.getShort();
                    extraDataMap.put(key, value);
                }
            }
            /*if (!entities.isEmpty() || !blockEntities.isEmpty()) {
                    CompoundTag ct = new CompoundTag();
                    ListTag<CompoundTag> entityList = new ListTag<>("entities");
                    ListTag<CompoundTag> tileList = new ListTag<>("blockEntities");

                    entityList.list = entities;
                    tileList.list = blockEntities;
                    ct.putList(entityList);
                    ct.putList(tileList);
                    NBTIO.write(ct, new File(Nukkit.DATA_PATH + chunkX + "_" + chunkZ + ".dat"));
                }*/
            Chunk chunk = new Chunk(provider, chunkX, chunkZ, chunkData, entities, tiles, extraDataMap);
            if ((flags & 0x01) > 0) {
                chunk.setGenerated();
            }
            if ((flags & 0x02) > 0) {
                chunk.setPopulated();
            }
            if ((flags & 0x04) > 0) {
                chunk.setLightPopulated();
            }
            return chunk;
        }
    } catch (Exception e) {
        Server.getInstance().getLogger().logException(e);
    }
    return null;
}
Also used : BinaryStream(cn.nukkit.utils.BinaryStream) IOException(java.io.IOException) BaseFullChunk(cn.nukkit.level.format.generic.BaseFullChunk) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) NBTInputStream(cn.nukkit.nbt.stream.NBTInputStream) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) Tag(cn.nukkit.nbt.tag.Tag) CompoundTag(cn.nukkit.nbt.tag.CompoundTag)

Aggregations

CompoundTag (cn.nukkit.nbt.tag.CompoundTag)13 Tag (cn.nukkit.nbt.tag.Tag)13 Map (java.util.Map)7 ListTag (cn.nukkit.nbt.tag.ListTag)6 StringTag (cn.nukkit.nbt.tag.StringTag)6 BlockEntity (cn.nukkit.blockentity.BlockEntity)2 BlockEntityChest (cn.nukkit.blockentity.BlockEntityChest)2 BlockEntityBrewingStand (cn.nukkit.blockentity.BlockEntityBrewingStand)1 BlockEntityCauldron (cn.nukkit.blockentity.BlockEntityCauldron)1 BlockEntityEnderChest (cn.nukkit.blockentity.BlockEntityEnderChest)1 BlockEntityFlowerPot (cn.nukkit.blockentity.BlockEntityFlowerPot)1 BlockEntityFurnace (cn.nukkit.blockentity.BlockEntityFurnace)1 BlockEntityItemFrame (cn.nukkit.blockentity.BlockEntityItemFrame)1 BlockEntitySign (cn.nukkit.blockentity.BlockEntitySign)1 BlockEntitySkull (cn.nukkit.blockentity.BlockEntitySkull)1 BaseFullChunk (cn.nukkit.level.format.generic.BaseFullChunk)1 BlockFace (cn.nukkit.math.BlockFace)1 NBTInputStream (cn.nukkit.nbt.stream.NBTInputStream)1 BinaryStream (cn.nukkit.utils.BinaryStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1