Java Code Examples for cn.nukkit.nbt.tag.ListTag#add()

The following examples show how to use cn.nukkit.nbt.tag.ListTag#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ItemBookWritable.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Adds a new page with the given page ID.
 * Creates a new page for every page between the given ID and existing pages that doesn't yet exist.
 * @return boolean indicating success
 */
public boolean addPage(int pageId) {
    Preconditions.checkArgument(pageId >= 0 && pageId < 50, "Page number " + pageId + " is out of range");
    CompoundTag tag = this.hasCompoundTag() ? this.getNamedTag() : new CompoundTag();
    ListTag<CompoundTag> pages;
    if (!tag.contains("pages") || !(tag.get("pages") instanceof ListTag)) {
        pages = new ListTag<>("pages");
        tag.putList(pages);
    } else {
        pages = tag.getList("pages", CompoundTag.class);
    }

    for (int current = pages.size(); current <= pageId; current++) {
        pages.add(createPageTag());
    }
    this.setCompoundTag(tag);
    return true;
}
 
Example 2
Source File: ItemBookWritable.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Inserts a new page with the given text and moves other pages upwards.
 * @return boolean indicating success
 */
public boolean insertPage(int pageId, String pageText) {
    Preconditions.checkArgument(pageId >= 0 && pageId < 50, "Page number " + pageId + " is out of range");
    CompoundTag tag = this.hasCompoundTag() ? this.getNamedTag() : new CompoundTag();
    ListTag<CompoundTag> pages;
    if (!tag.contains("pages") || !(tag.get("pages") instanceof ListTag)) {
        pages = new ListTag<>("pages");
        tag.putList(pages);
    } else {
        pages = tag.getList("pages", CompoundTag.class);
    }

    if (pages.size() <= pageId) {
        for (int current = pages.size(); current <= pageId; current++) {
            pages.add(createPageTag());
        }
        pages.get(pageId).putString("text", pageText);
    } else {
        pages.add(pageId, createPageTag(pageText));
    }
    this.setCompoundTag(tag);
    return true;
}
 
Example 3
Source File: ItemBookWritten.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public Item writeBook (String author, String title, String[] pages) {
    ListTag<CompoundTag> pageList = new ListTag<>("pages");
    for (String page : pages){
        pageList.add(new CompoundTag().putString("photoname", "").putString("text", page));
    }
    return writeBook(author, title, pageList);
}
 
Example 4
Source File: ItemBookWritten.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public Item writeBook(String author, String title, String[] pages) {
    ListTag<CompoundTag> pageList = new ListTag<>("pages");
    for (String page : pages) {
        pageList.add(createPageTag(page));
    }
    return writeBook(author, title, pageList);
}
 
Example 5
Source File: ItemBanner.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void addPattern(BannerPattern pattern) {
    CompoundTag tag = this.hasCompoundTag() ? this.getNamedTag() : new CompoundTag();
    ListTag<CompoundTag> patterns = tag.getList("Patterns", CompoundTag.class);
    patterns.add(new CompoundTag("").
            putInt("Color", pattern.getColor().getDyeData() & 0x0f).
            putString("Pattern", pattern.getType().getName()));
    tag.putList(patterns);
    this.setNamedTag(tag);
}
 
Example 6
Source File: ItemFirework.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void addExplosion(FireworkExplosion explosion) {
    List<DyeColor> colors = explosion.getColors();
    List<DyeColor> fades = explosion.getFades();

    if (colors.isEmpty()) {
        return;
    }
    byte[] clrs = new byte[colors.size()];
    for (int i = 0; i < clrs.length; i++) {
        clrs[i] = (byte) colors.get(i).getDyeData();
    }

    byte[] fds = new byte[fades.size()];
    for (int i = 0; i < fds.length; i++) {
        fds[i] = (byte) fades.get(i).getDyeData();
    }

    ListTag<CompoundTag> explosions = this.getNamedTag().getCompound("Fireworks").getList("Explosions", CompoundTag.class);
    CompoundTag tag = new CompoundTag()
            .putByteArray("FireworkColor", clrs)
            .putByteArray("FireworkFade", fds)
            .putBoolean("FireworkFlicker", explosion.flicker)
            .putBoolean("FireworkTrail", explosion.trail)
            .putByte("FireworkType", explosion.type.ordinal());

    explosions.add(tag);
}
 
Example 7
Source File: BlockEntityBanner.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void addPattern(BannerPattern pattern) {
    ListTag<CompoundTag> patterns = this.namedTag.getList("Patterns", CompoundTag.class);
    patterns.add(new CompoundTag("").
            putInt("Color", pattern.getColor().getDyeData() & 0x0f).
            putString("Pattern", pattern.getType().getName()));
    this.namedTag.putList(patterns);
}
 
Example 8
Source File: ItemBookWritten.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public Item writeBook(String author, String title, String[] pages) {
    ListTag<CompoundTag> pageList = new ListTag<>("pages");
    for (String page : pages) {
        pageList.add(new CompoundTag().putString("photoname", "").putString("text", page));
    }
    return writeBook(author, title, pageList);
}
 
Example 9
Source File: Entity.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
public void saveNBT() {
    if (!(this instanceof Player)) {
        this.namedTag.putString("id", this.getSaveId());
        if (!this.getNameTag().equals("")) {
            this.namedTag.putString("CustomName", this.getNameTag());
            this.namedTag.putBoolean("CustomNameVisible", this.isNameTagVisible());
        } else {
            this.namedTag.remove("CustomName");
            this.namedTag.remove("CustomNameVisible");
        }
    }

    this.namedTag.putList(new ListTag<DoubleTag>("Pos")
            .add(new DoubleTag("0", this.x))
            .add(new DoubleTag("1", this.y))
            .add(new DoubleTag("2", this.z))
    );

    this.namedTag.putList(new ListTag<DoubleTag>("Motion")
            .add(new DoubleTag("0", this.motionX))
            .add(new DoubleTag("1", this.motionY))
            .add(new DoubleTag("2", this.motionZ))
    );

    this.namedTag.putList(new ListTag<FloatTag>("Rotation")
            .add(new FloatTag("0", (float) this.yaw))
            .add(new FloatTag("1", (float) this.pitch))
    );

    this.namedTag.putFloat("FallDistance", this.fallDistance);
    this.namedTag.putShort("Fire", this.fireTicks);
    this.namedTag.putShort("Air", this.getDataPropertyShort(DATA_AIR));
    this.namedTag.putBoolean("OnGround", this.onGround);
    this.namedTag.putBoolean("Invulnerable", this.invulnerable);
    this.namedTag.putFloat("Scale", this.scale);

    if (!this.effects.isEmpty()) {
        ListTag<CompoundTag> list = new ListTag<>("ActiveEffects");
        for (Effect effect : this.effects.values()) {
            list.add(new CompoundTag(String.valueOf(effect.getId()))
                    .putByte("Id", effect.getId())
                    .putByte("Amplifier", effect.getAmplifier())
                    .putInt("Duration", effect.getDuration())
                    .putBoolean("Ambient", false)
                    .putBoolean("ShowParticles", effect.isVisible())
            );
        }

        this.namedTag.putList(list);
    } else {
        this.namedTag.remove("ActiveEffects");
    }
}
 
Example 10
Source File: Chunk.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
@Override
public byte[] toFastBinary() {
    CompoundTag nbt = this.getNBT().copy();
    nbt.putInt("xPos", this.x);
    nbt.putInt("zPos", this.z);

    nbt.putIntArray("BiomeColors", this.getBiomeColorArray());
    nbt.putIntArray("HeightMap", this.getHeightMapArray());

    for (cn.nukkit.level.format.ChunkSection section : this.getSections()) {
        if (section instanceof EmptyChunkSection) {
            continue;
        }
        CompoundTag s = new CompoundTag(null);
        s.putByte("Y", section.getY());
        s.putByteArray("Blocks", section.getIdArray());
        s.putByteArray("Data", section.getDataArray());
        s.putByteArray("BlockLight", section.getLightArray());
        s.putByteArray("SkyLight", section.getSkyLightArray());
        nbt.getList("Sections", CompoundTag.class).add(s);
    }

    ArrayList<CompoundTag> entities = new ArrayList<>();
    for (Entity entity : this.getEntities().values()) {
        if (!(entity instanceof Player) && !entity.closed) {
            entity.saveNBT();
            entities.add(entity.namedTag);
        }
    }
    ListTag<CompoundTag> entityListTag = new ListTag<>("Entities");
    entityListTag.setAll(entities);
    nbt.putList(entityListTag);

    ArrayList<CompoundTag> tiles = new ArrayList<>();
    for (BlockEntity blockEntity : this.getBlockEntities().values()) {
        blockEntity.saveNBT();
        tiles.add(blockEntity.namedTag);
    }
    ListTag<CompoundTag> tileListTag = new ListTag<>("TileEntities");
    tileListTag.setAll(tiles);
    nbt.putList(tileListTag);

    List<BlockUpdateEntry> entries = this.provider.getLevel().getPendingBlockUpdates(this);

    if (entries != null) {
        ListTag<CompoundTag> tileTickTag = new ListTag<>("TileTicks");
        long totalTime = this.provider.getLevel().getCurrentTick();

        for (BlockUpdateEntry entry : entries) {
            CompoundTag entryNBT = new CompoundTag()
                    .putString("i", entry.block.getClass().getSimpleName())
                    .putInt("x", entry.pos.getFloorX())
                    .putInt("y", entry.pos.getFloorY())
                    .putInt("z", entry.pos.getFloorZ())
                    .putInt("t", (int) (entry.delay - totalTime))
                    .putInt("p", entry.priority);
            tileTickTag.add(entryNBT);
        }

        nbt.putList(tileTickTag);
    }

    BinaryStream extraData = new BinaryStream();
    Map<Integer, Integer> extraDataArray = this.getBlockExtraDataArray();
    extraData.putInt(extraDataArray.size());
    for (Integer key : extraDataArray.keySet()) {
        extraData.putInt(key);
        extraData.putShort(extraDataArray.get(key));
    }

    nbt.putByteArray("ExtraData", extraData.getBuffer());

    CompoundTag chunk = new CompoundTag("");
    chunk.putCompound("Level", nbt);

    try {
        return NBTIO.write(chunk, ByteOrder.BIG_ENDIAN);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

}
 
Example 11
Source File: Chunk.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
@Override
public byte[] toBinary() {
    CompoundTag nbt = this.getNBT().copy();

    nbt.putInt("xPos", this.x);
    nbt.putInt("zPos", this.z);

    ListTag<CompoundTag> sectionList = new ListTag<>("Sections");
    for (cn.nukkit.level.format.ChunkSection section : this.getSections()) {
        if (section instanceof EmptyChunkSection) {
            continue;
        }
        CompoundTag s = new CompoundTag(null);
        s.putByte("Y", (section.getY()));
        s.putByteArray("Blocks", section.getIdArray());
        s.putByteArray("Data", section.getDataArray());
        s.putByteArray("BlockLight", section.getLightArray());
        s.putByteArray("SkyLight", section.getSkyLightArray());
        sectionList.add(s);
    }
    nbt.putList(sectionList);

    nbt.putIntArray("BiomeColors", this.getBiomeColorArray());
    nbt.putIntArray("HeightMap", this.getHeightMapArray());

    ArrayList<CompoundTag> entities = new ArrayList<>();
    for (Entity entity : this.getEntities().values()) {
        if (!(entity instanceof Player) && !entity.closed) {
            entity.saveNBT();
            entities.add(entity.namedTag);
        }
    }
    ListTag<CompoundTag> entityListTag = new ListTag<>("Entities");
    entityListTag.setAll(entities);
    nbt.putList(entityListTag);

    ArrayList<CompoundTag> tiles = new ArrayList<>();
    for (BlockEntity blockEntity : this.getBlockEntities().values()) {
        blockEntity.saveNBT();
        tiles.add(blockEntity.namedTag);
    }
    ListTag<CompoundTag> tileListTag = new ListTag<>("TileEntities");
    tileListTag.setAll(tiles);
    nbt.putList(tileListTag);

    List<BlockUpdateEntry> entries = this.provider.getLevel().getPendingBlockUpdates(this);

    if (entries != null) {
        ListTag<CompoundTag> tileTickTag = new ListTag<>("TileTicks");
        long totalTime = this.provider.getLevel().getCurrentTick();

        for (BlockUpdateEntry entry : entries) {
            CompoundTag entryNBT = new CompoundTag()
                    .putString("i", entry.block.getClass().getSimpleName())
                    .putInt("x", entry.pos.getFloorX())
                    .putInt("y", entry.pos.getFloorY())
                    .putInt("z", entry.pos.getFloorZ())
                    .putInt("t", (int) (entry.delay - totalTime))
                    .putInt("p", entry.priority);
            tileTickTag.add(entryNBT);
        }

        nbt.putList(tileTickTag);
    }

    BinaryStream extraData = new BinaryStream();
    Map<Integer, Integer> extraDataArray = this.getBlockExtraDataArray();
    extraData.putInt(extraDataArray.size());
    for (Integer key : extraDataArray.keySet()) {
        extraData.putInt(key);
        extraData.putShort(extraDataArray.get(key));
    }

    nbt.putByteArray("ExtraData", extraData.getBuffer());

    CompoundTag chunk = new CompoundTag("");
    chunk.putCompound("Level", nbt);

    try {
        return Zlib.deflate(NBTIO.write(chunk, ByteOrder.BIG_ENDIAN), RegionLoader.COMPRESSION_LEVEL);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 12
Source File: EntityHuman.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void saveNBT() {
    super.saveNBT();

    if (skin != null) {
        CompoundTag skinTag = new CompoundTag()
                .putByteArray("Data", this.getSkin().getSkinData().data)
                .putInt("SkinImageWidth", this.getSkin().getSkinData().width)
                .putInt("SkinImageHeight", this.getSkin().getSkinData().height)
                .putString("ModelId", this.getSkin().getSkinId())
                .putString("CapeId", this.getSkin().getCapeId())
                .putByteArray("CapeData", this.getSkin().getCapeData().data)
                .putInt("CapeImageWidth", this.getSkin().getCapeData().width)
                .putInt("CapeImageHeight", this.getSkin().getCapeData().height)
                .putByteArray("SkinResourcePatch", this.getSkin().getSkinResourcePatch().getBytes(StandardCharsets.UTF_8))
                .putByteArray("GeometryData", this.getSkin().getGeometryData().getBytes(StandardCharsets.UTF_8))
                .putByteArray("AnimationData", this.getSkin().getAnimationData().getBytes(StandardCharsets.UTF_8))
                .putBoolean("PremiumSkin", this.getSkin().isPremium())
                .putBoolean("PersonaSkin", this.getSkin().isPersona())
                .putBoolean("CapeOnClassicSkin", this.getSkin().isCapeOnClassic())
                .putString("ArmSize", this.getSkin().getArmSize())
                .putString("SkinColor", this.getSkin().getSkinColor())
                .putBoolean("IsTrustedSkin", this.getSkin().isTrusted());
        List<SkinAnimation> animations = this.getSkin().getAnimations();
        if (!animations.isEmpty()) {
            ListTag<CompoundTag> animationsTag = new ListTag<>("AnimationImageData");
            for (SkinAnimation animation : animations) {
                animationsTag.add(new CompoundTag()
                        .putFloat("Frames", animation.frames)
                        .putInt("Type", animation.type)
                        .putInt("ImageWidth", animation.image.width)
                        .putInt("ImageHeight", animation.image.height)
                        .putByteArray("Image", animation.image.data));
            }
            skinTag.putList(animationsTag);
        }
        List<PersonaPiece> personaPieces = this.getSkin().getPersonaPieces();
        if (!personaPieces.isEmpty()) {
            ListTag<CompoundTag> piecesTag = new ListTag<>("PersonaPieces");
            for (PersonaPiece piece : personaPieces) {
                piecesTag.add(new CompoundTag().putString("PieceId", piece.id)
                        .putString("PieceType", piece.type)
                        .putString("PackId", piece.packId)
                        .putBoolean("IsDefault", piece.isDefault)
                        .putString("ProductId", piece.productId));
            }
        }
        List<PersonaPieceTint> tints = this.getSkin().getTintColors();
        if (!tints.isEmpty()) {
            ListTag<CompoundTag> tintsTag = new ListTag<>("PieceTintColors");
            for (PersonaPieceTint tint : tints) {
                ListTag<StringTag> colors = new ListTag<>("Colors");
                colors.setAll(tint.colors.stream().map(s -> new StringTag("", s)).collect(Collectors.toList()));
                tintsTag.add(new CompoundTag()
                        .putString("PieceType", tint.pieceType)
                        .putList(colors));
            }
        }
        this.namedTag.putCompound("Skin", skinTag);
    }
}
 
Example 13
Source File: Entity.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public void saveNBT() {
    if (!(this instanceof Player)) {
        this.namedTag.putString("id", this.getSaveId());
        if (!this.getNameTag().equals("")) {
            this.namedTag.putString("CustomName", this.getNameTag());
            this.namedTag.putBoolean("CustomNameVisible", this.isNameTagVisible());
            this.namedTag.putBoolean("CustomNameAlwaysVisible", this.isNameTagAlwaysVisible());
        } else {
            this.namedTag.remove("CustomName");
            this.namedTag.remove("CustomNameVisible");
            this.namedTag.remove("CustomNameAlwaysVisible");
        }
    }

    this.namedTag.putList(new ListTag<DoubleTag>("Pos")
            .add(new DoubleTag("0", this.x))
            .add(new DoubleTag("1", this.y))
            .add(new DoubleTag("2", this.z))
    );

    this.namedTag.putList(new ListTag<DoubleTag>("Motion")
            .add(new DoubleTag("0", this.motionX))
            .add(new DoubleTag("1", this.motionY))
            .add(new DoubleTag("2", this.motionZ))
    );

    this.namedTag.putList(new ListTag<FloatTag>("Rotation")
            .add(new FloatTag("0", (float) this.yaw))
            .add(new FloatTag("1", (float) this.pitch))
    );

    this.namedTag.putFloat("FallDistance", this.fallDistance);
    this.namedTag.putShort("Fire", this.fireTicks);
    this.namedTag.putShort("Air", this.getDataPropertyShort(DATA_AIR));
    this.namedTag.putBoolean("OnGround", this.onGround);
    this.namedTag.putBoolean("Invulnerable", this.invulnerable);
    this.namedTag.putFloat("Scale", this.scale);

    if (!this.effects.isEmpty()) {
        ListTag<CompoundTag> list = new ListTag<>("ActiveEffects");
        for (Effect effect : this.effects.values()) {
            list.add(new CompoundTag(String.valueOf(effect.getId()))
                    .putByte("Id", effect.getId())
                    .putByte("Amplifier", effect.getAmplifier())
                    .putInt("Duration", effect.getDuration())
                    .putBoolean("Ambient", false)
                    .putBoolean("ShowParticles", effect.isVisible())
            );
        }

        this.namedTag.putList(list);
    } else {
        this.namedTag.remove("ActiveEffects");
    }
}
 
Example 14
Source File: Entity.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public void saveNBT() {
    if (!(this instanceof Player)) {
        this.namedTag.putString("id", this.getSaveId());
        if (!this.getNameTag().equals("")) {
            this.namedTag.putString("CustomName", this.getNameTag());
            this.namedTag.putBoolean("CustomNameVisible", this.isNameTagVisible());
        } else {
            this.namedTag.remove("CustomName");
            this.namedTag.remove("CustomNameVisible");
        }
    }

    this.namedTag.putList(new ListTag<DoubleTag>("Pos")
            .add(new DoubleTag("0", this.x))
            .add(new DoubleTag("1", this.y))
            .add(new DoubleTag("2", this.z))
    );

    this.namedTag.putList(new ListTag<DoubleTag>("Motion")
            .add(new DoubleTag("0", this.motionX))
            .add(new DoubleTag("1", this.motionY))
            .add(new DoubleTag("2", this.motionZ))
    );

    this.namedTag.putList(new ListTag<FloatTag>("Rotation")
            .add(new FloatTag("0", (float) this.yaw))
            .add(new FloatTag("1", (float) this.pitch))
    );

    this.namedTag.putFloat("FallDistance", this.fallDistance);
    this.namedTag.putShort("Fire", this.fireTicks);
    this.namedTag.putShort("Air", this.getDataPropertyShort(DATA_AIR));
    this.namedTag.putBoolean("OnGround", this.onGround);
    this.namedTag.putBoolean("Invulnerable", this.invulnerable);
    this.namedTag.putFloat("Scale", this.scale);

    if (!this.effects.isEmpty()) {
        ListTag<CompoundTag> list = new ListTag<>("ActiveEffects");
        for (Effect effect : this.effects.values()) {
            list.add(new CompoundTag(String.valueOf(effect.getId()))
                    .putByte("Id", effect.getId())
                    .putByte("Amplifier", effect.getAmplifier())
                    .putInt("Duration", effect.getDuration())
                    .putBoolean("Ambient", false)
                    .putBoolean("ShowParticles", effect.isVisible())
            );
        }

        this.namedTag.putList(list);
    } else {
        this.namedTag.remove("ActiveEffects");
    }
}