Java Code Examples for cn.nukkit.nbt.tag.CompoundTag#getList()

The following examples show how to use cn.nukkit.nbt.tag.CompoundTag#getList() . 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: Item.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
public String[] getLore() {
    Tag tag = this.getNamedTagEntry("display");
    ArrayList<String> lines = new ArrayList<>();

    if (tag instanceof CompoundTag) {
        CompoundTag nbt = (CompoundTag) tag;
        ListTag<StringTag> lore = nbt.getList("Lore", StringTag.class);

        if (lore.size() > 0) {
            for (StringTag stringTag : lore.getAll()) {
                lines.add(stringTag.data);
            }
        }
    }

    return lines.toArray(new String[0]);
}
 
Example 2
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 3
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 4
Source File: ItemBookWritable.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Switches the text of two pages with each other.
 * @return boolean indicating success
 */
public boolean swapPages(int pageId1, int pageId2) {
    Preconditions.checkArgument(pageId1 >= 0 && pageId1 < 50, "Page number " + pageId1 + " is out of range");
    Preconditions.checkArgument(pageId2 >= 0 && pageId2 < 50, "Page number " + pageId2 + " is out of range");
    if (this.hasCompoundTag()) {
        CompoundTag tag = this.getNamedTag();
        if (tag.contains("pages") && tag.get("pages") instanceof ListTag) {
            ListTag<CompoundTag> pages = tag.getList("pages", CompoundTag.class);
            if (pages.size() > pageId1 && pages.size() > pageId2) {
                String pageContents1 = pages.get(pageId1).getString("text");
                String pageContents2 = pages.get(pageId2).getString("text");
                pages.get(pageId1).putString("text", pageContents2);
                pages.get(pageId2).putString("text", pageContents1);
                return true;
            }
        }
    }
    return false;
}
 
Example 5
Source File: Item.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public String[] getLore() {
    Tag tag = this.getNamedTagEntry("display");
    ArrayList<String> lines = new ArrayList<>();

    if (tag instanceof CompoundTag) {
        CompoundTag nbt = (CompoundTag) tag;
        ListTag<StringTag> lore = nbt.getList("Lore", StringTag.class);

        if (lore.size() > 0) {
            for (StringTag stringTag : lore.getAll()) {
                lines.add(stringTag.data);
            }
        }
    }

    return lines.toArray(new String[0]);
}
 
Example 6
Source File: BinaryStream.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
private List<String> extractStringList(Item item, String tagName) {
    CompoundTag namedTag = item.getNamedTag();
    if (namedTag == null) {
        return Collections.emptyList();
    }

    ListTag<StringTag> listTag = namedTag.getList(tagName, StringTag.class);
    if (listTag == null) {
        return Collections.emptyList();
    }

    int size = listTag.size();
    List<String> values = new ArrayList<>(size);
    for (int i = 0; i < size; i++) {
        StringTag stringTag = listTag.get(i);
        if (stringTag != null) {
            values.add(stringTag.data);
        }
    }

    return values;
}
 
Example 7
Source File: Item.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public String[] getLore() {
    Tag tag = this.getNamedTagEntry("display");
    ArrayList<String> lines = new ArrayList<>();

    if (tag instanceof CompoundTag) {
        CompoundTag nbt = (CompoundTag) tag;
        ListTag<StringTag> lore = nbt.getList("Lore", StringTag.class);

        if (lore.size() > 0) {
            for (StringTag stringTag : lore.getAll()) {
                lines.add(stringTag.data);
            }
        }
    }

    return lines.toArray(new String[0]);
}
 
Example 8
Source File: BlockEntityPistonArm.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public BlockEntityPistonArm(FullChunk chunk, CompoundTag nbt) {
    super(chunk, nbt);
    if (nbt.contains("Progress")) {
        this.progress = nbt.getFloat("Progress");
    }

    if (nbt.contains("LastProgress")) {
        this.lastProgress = (float) nbt.getInt("LastProgress");
    }

    if (nbt.contains("Sticky")) {
        this.sticky = nbt.getBoolean("Sticky");
    }

    if (nbt.contains("Extending")) {
        this.extending = nbt.getBoolean("Extending");
    }

    if (nbt.contains("powered")) {
        this.powered = nbt.getBoolean("powered");
    }

    if (nbt.contains("AttachedBlocks")) {
        ListTag blocks = nbt.getList("AttachedBlocks", IntTag.class);
        if (blocks != null && blocks.size() > 0) {
            this.attachedBlock = new Vector3((double) ((IntTag) blocks.get(0)).getData().intValue(), (double) ((IntTag) blocks.get(1)).getData().intValue(), (double) ((IntTag) blocks.get(2)).getData().intValue());
        }
    } else {
        nbt.putList(new ListTag("AttachedBlocks"));
    }

}
 
Example 9
Source File: ItemBookWritable.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns a string containing the content of a page (which could be empty), or null if the page doesn't exist.
 */
public String getPageText(int pageId) {
    Preconditions.checkArgument(pageId >= 0 && pageId < 50, "Page number " + pageId + " is out of range");
    if (this.hasCompoundTag()) {
        CompoundTag tag = this.getNamedTag();
        if (tag.contains("pages") && tag.get("pages") instanceof ListTag) {
            ListTag<CompoundTag> pages = tag.getList("pages", CompoundTag.class);
            if (pages.size() > pageId) {
                return pages.get(pageId).getString("text");
            }
        }
    }
    return null;
}
 
Example 10
Source File: ItemBookWritable.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Deletes an existing page with the given page ID.
 * @return boolean indicating success
 */
public boolean deletePage(int pageId) {
    Preconditions.checkArgument(pageId >= 0 && pageId < 50, "Page number " + pageId + " is out of range");
    if (this.hasCompoundTag()) {
        CompoundTag tag = this.getNamedTag();
        if (tag.contains("pages") && tag.get("pages") instanceof ListTag) {
            ListTag<CompoundTag> pages = tag.getList("pages", CompoundTag.class);
            if (pages.size() > pageId) {
                pages.remove(pageId);
                this.setCompoundTag(tag);
            }
        }
    }
    return true;
}
 
Example 11
Source File: ItemBookWritable.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns an list containing all pages of this book.
 */
public List getPages() {
    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);
    }
    return pages.parseValue();
}
 
Example 12
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 13
Source File: ItemBanner.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void removePattern(int index) {
    CompoundTag tag = this.hasCompoundTag() ? this.getNamedTag() : new CompoundTag();
    ListTag<CompoundTag> patterns = tag.getList("Patterns", CompoundTag.class);
    if(patterns.size() > index && index >= 0) {
        patterns.remove(index);
    }
    this.setNamedTag(tag);
}