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

The following examples show how to use cn.nukkit.nbt.tag.ListTag#size() . 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: 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 2
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 3
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 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: 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 6
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, ListTag<CompoundTag> pages) {
    if (pages.size() > 50 || pages.size() <= 0) return this; //Minecraft does not support more than 50 pages
    CompoundTag tag = this.hasCompoundTag() ? this.getNamedTag() : new CompoundTag();

    tag.putString("author", author);
    tag.putString("title", title);
    tag.putList(pages);

    tag.putInt("generation", GENERATION_ORIGINAL);
    tag.putString("xuid", "");

    return this.setNamedTag(tag);
}
 
Example 7
Source File: BlockEntityHopper.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
protected int getSlotIndex(int index) {
    ListTag<CompoundTag> list = this.namedTag.getList("Items", CompoundTag.class);
    for (int i = 0; i < list.size(); i++) {
        if (list.get(i).getByte("Slot") == index) {
            return i;
        }
    }

    return -1;
}
 
Example 8
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);
}
 
Example 9
Source File: ItemBookWritten.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public String[] getPages() {
    if (!this.isWritten) return new String[0];
    ListTag<CompoundTag> tag = (ListTag<CompoundTag>) this.getNamedTag().getList("pages");
    String[] pages = new String[tag.size()];
    int i = 0;
    for (CompoundTag pageCompound : tag.getAll()) {
        pages[i] = pageCompound.getString("text");
        i++;
    }
    return pages;
}
 
Example 10
Source File: BlockEntityChest.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
protected int getSlotIndex(int index) {
    ListTag<CompoundTag> list = this.namedTag.getList("Items", CompoundTag.class);
    for (int i = 0; i < list.size(); i++) {
        if (list.get(i).getByte("Slot") == index) {
            return i;
        }
    }

    return -1;
}
 
Example 11
Source File: BlockEntityHopper.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
protected int getSlotIndex(int index) {
    ListTag<CompoundTag> list = this.namedTag.getList("Items", CompoundTag.class);
    for (int i = 0; i < list.size(); i++) {
        if (list.get(i).getByte("Slot") == index) {
            return i;
        }
    }

    return -1;
}
 
Example 12
Source File: ItemBookWritten.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public String[] getPages(){
    if (!this.isWritten) return new String[0];
    ListTag<CompoundTag> tag = (ListTag<CompoundTag>) this.getNamedTag().getList("pages");
    String[] pages = new String[tag.size()];
    int i = 0;
    for (CompoundTag pageCompound : tag.getAll()) {
        pages[i] = pageCompound.getString("text");
        i++;
    }
    return pages;
}
 
Example 13
Source File: BlockEntityChest.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
protected int getSlotIndex(int index) {
    ListTag<CompoundTag> list = this.namedTag.getList("Items", CompoundTag.class);
    for (int i = 0; i < list.size(); i++) {
        if (list.get(i).getByte("Slot") == index) {
            return i;
        }
    }

    return -1;
}
 
Example 14
Source File: BlockEntityDispenser.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
protected int getSlotIndex(int index) {
    ListTag<CompoundTag> list = this.namedTag.getList("Items", CompoundTag.class);
    for (int i = 0; i < list.size(); i++) {
        if (list.get(i).getByte("Slot") == index) {
            return i;
        }
    }

    return -1;
}
 
Example 15
Source File: BlockEntityBrewingStand.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
protected int getSlotIndex(int index) {
    ListTag<CompoundTag> list = this.namedTag.getList("Items", CompoundTag.class);
    for (int i = 0; i < list.size(); i++) {
        if (list.get(i).getByte("Slot") == index) {
            return i;
        }
    }

    return -1;
}
 
Example 16
Source File: BlockEntityFurnace.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
protected int getSlotIndex(int index) {
    ListTag<CompoundTag> list = this.namedTag.getList("Items", CompoundTag.class);
    for (int i = 0; i < list.size(); i++) {
        if (list.get(i).getByte("Slot") == index) {
            return i;
        }
    }

    return -1;
}
 
Example 17
Source File: BlockEntityShulkerBox.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
protected int getSlotIndex(int index) {
    ListTag<CompoundTag> list = this.namedTag.getList("Items", CompoundTag.class);
    for (int i = 0; i < list.size(); i++) {
        if (list.get(i).getByte("Slot") == index) {
            return i;
        }
    }

    return -1;
}
 
Example 18
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 19
Source File: BlockEntityPistonArm.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void initBlockEntity() {
    if (namedTag.contains("Progress")) {
        this.progress = namedTag.getFloat("Progress");
    }

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

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

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

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

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

    super.initBlockEntity();
}
 
Example 20
Source File: BlockEntityChest.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
protected int getSlotIndex(int index) {
    ListTag<CompoundTag> list = this.namedTag.getList("Items", CompoundTag.class);
    for (int i = 0; i < list.size(); i++) {
        if (list.get(i).getByte("Slot") == index) {
            return i;
        }
    }

    return -1;
}