Search in sources :

Example 31 with CompoundTag

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

the class ItemBookWritten method writeBook.

public Item writeBook(String author, String title, ListTag<CompoundTag> pages) {
    // Minecraft does not support more than 50 pages
    if (pages.size() > 50 || pages.size() <= 0)
        return this;
    // Book content can only be updated once
    if (this.isWritten)
        return this;
    CompoundTag tag;
    if (!this.hasCompoundTag()) {
        tag = new CompoundTag();
    } else {
        tag = this.getNamedTag();
    }
    tag.putString("author", author);
    tag.putString("title", title);
    tag.putList(pages);
    tag.putInt("generation", 0);
    long randomId = 1095216660480L + ThreadLocalRandom.current().nextLong(0L, 2147483647L);
    tag.putLong("id", randomId);
    this.isWritten = true;
    return this.setNamedTag(tag);
}
Also used : CompoundTag(cn.nukkit.nbt.tag.CompoundTag)

Example 32 with CompoundTag

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

the class ItemPainting method onActivate.

@Override
public boolean onActivate(Level level, Player player, Block block, Block target, BlockFace face, double fx, double fy, double fz) {
    FullChunk chunk = level.getChunk((int) block.getX() >> 4, (int) block.getZ() >> 4);
    if (chunk == null) {
        return false;
    }
    if (!target.isTransparent() && face.getIndex() > 1 && !block.isSolid()) {
        int[] direction = { 2, 0, 1, 3 };
        int[] right = { 4, 5, 3, 2 };
        List<EntityPainting.Motive> validMotives = new ArrayList<>();
        for (EntityPainting.Motive motive : EntityPainting.motives) {
            boolean valid = true;
            for (int x = 0; x < motive.width && valid; x++) {
                for (int z = 0; z < motive.height && valid; z++) {
                    if (target.getSide(BlockFace.fromIndex(right[face.getIndex() - 2]), x).isTransparent() || target.up(z).isTransparent() || block.getSide(BlockFace.fromIndex(right[face.getIndex() - 2]), x).isSolid() || block.up(z).isSolid()) {
                        valid = false;
                    }
                }
            }
            if (valid) {
                validMotives.add(motive);
            }
        }
        CompoundTag nbt = new CompoundTag().putByte("Direction", direction[face.getIndex() - 2]).putString("Motive", validMotives.get(ThreadLocalRandom.current().nextInt(validMotives.size())).title).putList(new ListTag<DoubleTag>("Pos").add(new DoubleTag("0", target.x)).add(new DoubleTag("1", target.y)).add(new DoubleTag("2", target.z))).putList(new ListTag<DoubleTag>("Motion").add(new DoubleTag("0", 0)).add(new DoubleTag("1", 0)).add(new DoubleTag("2", 0))).putList(new ListTag<FloatTag>("Rotation").add(new FloatTag("0", direction[face.getIndex() - 2] * 90)).add(new FloatTag("1", 0)));
        EntityPainting entity = new EntityPainting(chunk, nbt);
        if (player.isSurvival()) {
            Item item = player.getInventory().getItemInHand();
            item.setCount(item.getCount() - 1);
            player.getInventory().setItemInHand(item);
        }
        entity.spawnToAll();
        return true;
    }
    return false;
}
Also used : ArrayList(java.util.ArrayList) EntityPainting(cn.nukkit.entity.item.EntityPainting) DoubleTag(cn.nukkit.nbt.tag.DoubleTag) ListTag(cn.nukkit.nbt.tag.ListTag) FullChunk(cn.nukkit.level.format.FullChunk) FloatTag(cn.nukkit.nbt.tag.FloatTag) CompoundTag(cn.nukkit.nbt.tag.CompoundTag)

Example 33 with CompoundTag

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

the class ItemSpawnEgg method onActivate.

@Override
public boolean onActivate(Level level, Player player, Block block, Block target, BlockFace face, double fx, double fy, double fz) {
    FullChunk chunk = level.getChunk((int) block.getX() >> 4, (int) block.getZ() >> 4);
    if (chunk == null) {
        return false;
    }
    CompoundTag nbt = new CompoundTag().putList(new ListTag<DoubleTag>("Pos").add(new DoubleTag("", block.getX() + 0.5)).add(new DoubleTag("", block.getY())).add(new DoubleTag("", block.getZ() + 0.5))).putList(new ListTag<DoubleTag>("Motion").add(new DoubleTag("", 0)).add(new DoubleTag("", 0)).add(new DoubleTag("", 0))).putList(new ListTag<FloatTag>("Rotation").add(new FloatTag("", new Random().nextFloat() * 360)).add(new FloatTag("", 0)));
    if (this.hasCustomName()) {
        nbt.putString("CustomName", this.getCustomName());
    }
    Entity entity = Entity.createEntity(this.meta, chunk, nbt);
    if (entity != null) {
        if (player.isSurvival()) {
            Item item = player.getInventory().getItemInHand();
            item.setCount(item.getCount() - 1);
            player.getInventory().setItemInHand(item);
        }
        entity.spawnToAll();
        return true;
    }
    return false;
}
Also used : Entity(cn.nukkit.entity.Entity) FullChunk(cn.nukkit.level.format.FullChunk) FloatTag(cn.nukkit.nbt.tag.FloatTag) Random(java.util.Random) DoubleTag(cn.nukkit.nbt.tag.DoubleTag) ListTag(cn.nukkit.nbt.tag.ListTag) CompoundTag(cn.nukkit.nbt.tag.CompoundTag)

Example 34 with CompoundTag

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

the class ItemColorArmor method getColor.

/**
 * Get color of Leather Item
 *
 * @return - BlockColor, or null if item has no color
 */
public BlockColor getColor() {
    if (!this.hasCompoundTag())
        return null;
    CompoundTag tag = this.getNamedTag();
    if (!tag.exist("customColor"))
        return null;
    int rgb = tag.getInt("customColor");
    return new BlockColor(rgb);
}
Also used : BlockColor(cn.nukkit.utils.BlockColor) CompoundTag(cn.nukkit.nbt.tag.CompoundTag)

Example 35 with CompoundTag

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

the class ItemColorArmor method setColor.

/**
 * Set leather armor color
 *
 * @param r - red
 * @param g - green
 * @param b - blue
 * @return - Return colored item
 */
public ItemColorArmor setColor(int r, int g, int b) {
    int rgb = r << 16 | g << 8 | b;
    CompoundTag tag = this.hasCompoundTag() ? this.getNamedTag() : new CompoundTag();
    tag.putInt("customColor", rgb);
    this.setNamedTag(tag);
    return this;
}
Also used : CompoundTag(cn.nukkit.nbt.tag.CompoundTag)

Aggregations

CompoundTag (cn.nukkit.nbt.tag.CompoundTag)74 ListTag (cn.nukkit.nbt.tag.ListTag)28 BlockEntity (cn.nukkit.blockentity.BlockEntity)13 DoubleTag (cn.nukkit.nbt.tag.DoubleTag)13 FloatTag (cn.nukkit.nbt.tag.FloatTag)13 Tag (cn.nukkit.nbt.tag.Tag)13 StringTag (cn.nukkit.nbt.tag.StringTag)12 Map (java.util.Map)8 Entity (cn.nukkit.entity.Entity)7 IOException (java.io.IOException)7 Player (cn.nukkit.Player)5 BinaryStream (cn.nukkit.utils.BinaryStream)5 ArrayList (java.util.ArrayList)5 BlockRail (cn.nukkit.block.BlockRail)4 FullChunk (cn.nukkit.level.format.FullChunk)4 BaseFullChunk (cn.nukkit.level.format.generic.BaseFullChunk)4 Rail (cn.nukkit.utils.Rail)4 File (java.io.File)4 BlockEntityChest (cn.nukkit.blockentity.BlockEntityChest)3 BlockEntitySpawnable (cn.nukkit.blockentity.BlockEntitySpawnable)3