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

The following examples show how to use cn.nukkit.nbt.tag.CompoundTag#contains() . 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
/**
 * 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 2
Source File: EntityFirework.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public EntityFirework(FullChunk chunk, CompoundTag nbt) {
    super(chunk, nbt);

    this.fireworkAge = 0;
    Random rand = new Random();
    this.lifetime = 30 + rand.nextInt(6) + rand.nextInt(7);

    this.motionX = rand.nextGaussian() * 0.001D;
    this.motionZ = rand.nextGaussian() * 0.001D;
    this.motionY = 0.05D;

    if (nbt.contains("FireworkItem")) {
        firework = NBTIO.getItemHelper(nbt.getCompound("FireworkItem"));
    } else {
        firework = new ItemFirework();
    }

    this.setDataProperty(new NBTEntityData(Entity.DATA_DISPLAY_ITEM, firework.getNamedTag()));
    this.setDataProperty(new IntEntityData(Entity.DATA_DISPLAY_OFFSET, 1));
    this.setDataProperty(new ByteEntityData(Entity.DATA_HAS_DISPLAY, 1));
}
 
Example 3
Source File: Item.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
public CompoundTag getCustomBlockData() {
    if (!this.hasCompoundTag()) {
        return null;
    }

    CompoundTag tag = this.getNamedTag();

    if (tag.contains("BlockEntityTag")) {
        Tag bet = tag.get("BlockEntityTag");
        if (bet instanceof CompoundTag) {
            return (CompoundTag) bet;
        }
    }

    return null;
}
 
Example 4
Source File: Item.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
public boolean hasEnchantments() {
    if (!this.hasCompoundTag()) {
        return false;
    }

    CompoundTag tag = this.getNamedTag();

    if (tag.contains("ench")) {
        Tag enchTag = tag.get("ench");
        if (enchTag instanceof ListTag) {
            return true;
        }
    }

    return false;
}
 
Example 5
Source File: BlockEntityFlowerPot.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
public BlockEntityFlowerPot(FullChunk chunk, CompoundTag nbt) {
    super(chunk, nbt);
    if (!nbt.contains("item")) {
        nbt.putShort("item", 0);
    }

    if (!nbt.contains("data")) {
        if (nbt.contains("mData")) {
            nbt.putInt("data", nbt.getInt("mData"));
            nbt.remove("mData");
        } else {
            nbt.putInt("data", 0);
        }
    }

    this.namedTag = nbt;
}
 
Example 6
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 7
Source File: EntityArmorStand.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
public EntityArmorStand(FullChunk chunk, CompoundTag nbt) {
	super(chunk, nbt);

	if (!nbt.contains("HandItems")) {
		nbt.putCompound("HandItems", NBTIO.putItemHelper(Item.get(0)));
	}
	if (!nbt.contains("ArmorItems")) {
		ListTag<CompoundTag> tag = new ListTag<CompoundTag>("ArmorItems")
				.add(NBTIO.putItemHelper(Item.get(0)))
				.add(NBTIO.putItemHelper(Item.get(0)))
				.add(NBTIO.putItemHelper(Item.get(0)))
				.add(NBTIO.putItemHelper(Item.get(0)));
		nbt.putList(tag);
	}

	this.setHealth(2);
	this.setMaxHealth(2);
}
 
Example 8
Source File: BlockEntityMovingBlock.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
public BlockEntityMovingBlock(FullChunk chunk, CompoundTag nbt) {
    super(chunk, nbt);

    if (nbt.contains("movingBlockData") && nbt.contains("movingBlockId")) {
        this.block = Block.get(nbt.getInt("movingBlockId"), nbt.getInt("movingBlockData"));
    } else {
        this.close();
    }

    if (nbt.contains("pistonPosX") && nbt.contains("pistonPosY") && nbt.contains("pistonPosZ")) {
        this.piston = new BlockVector3(nbt.getInt("pistonPosX"), nbt.getInt("pistonPosY"), nbt.getInt("pistonPosZ"));
    } else {
        this.close();
    }

    this.isMovable = nbt.getBoolean("isMovable");
}
 
Example 9
Source File: Item.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public boolean hasEnchantments() {
    if (!this.hasCompoundTag()) {
        return false;
    }

    CompoundTag tag = this.getNamedTag();

    if (tag.contains("ench")) {
        Tag enchTag = tag.get("ench");
        if (enchTag instanceof ListTag) {
            return true;
        }
    }

    return false;
}
 
Example 10
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 11
Source File: ItemArmor.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public int getCustomColor(){
    if(!this.hasCompoundTag()) return 0;
    CompoundTag tag = this.getNamedTag();
    if(tag.contains("customColor")){
        return tag.getInt("customColor");
    }
    return 0;
}
 
Example 12
Source File: BlockEntityCommandBlock.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public BlockEntityCommandBlock(FullChunk chunk, CompoundTag nbt) {
	super(chunk, nbt);

	if (!nbt.contains("CustomName")) {
		nbt.putString("CustomName", "");
	}
	if (!nbt.contains("commandBlockMode")) {
		nbt.putInt("commandBlockMode", 0);
	}
	if (!nbt.contains("Command")) {
		nbt.putString("Command", "");
	}
	if (!nbt.contains("LastOutput")) {
		nbt.putString("LastOutput", "");
	}
	if (!nbt.contains("powered")) {
		nbt.putBoolean("powered", false);
	}
	if (!nbt.contains("auto")) {
		nbt.putBoolean("auto", false);
	}
	if (!nbt.contains("conditionalMode")) {
		nbt.putBoolean("conditionalMode", false);
	}
	this.perm = new PermissibleBase(this);

	this.scheduleUpdate();
}
 
Example 13
Source File: Item.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public Item clearCustomBlockData() {
    if (!this.hasCompoundTag()) {
        return this;
    }
    CompoundTag tag = this.getNamedTag();

    if (tag.contains("BlockEntityTag") && tag.get("BlockEntityTag") instanceof CompoundTag) {
        tag.remove("BlockEntityTag");
        this.setNamedTag(tag);
    }

    return this;
}
 
Example 14
Source File: BlockBannerStanding.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
	if (face == BlockFace.DOWN) {
		return false;
	}

	CompoundTag nbt = new CompoundTag()
			.putString("id", BlockEntity.BANNER)
            .putInt("x", (int) block.x)
            .putInt("y", (int) block.y)
            .putInt("z", (int) block.z)
            .putInt("Base", item.getDamage());

	if (item.hasCompoundTag()) {
		CompoundTag tag = item.getNamedTag();
		if (tag.contains("Patterns")) {
			nbt.putList(tag.getList("Patterns"));
		}
	}

    if (face == BlockFace.UP) {
        meta = (int) Math.floor(((player.yaw + 180) * 16 / 360) + 0.5) & 0x0f;
        getLevel().setBlock(block, new BlockBannerStanding(meta), true);
    } else {
        meta = face.getIndex();
        getLevel().setBlock(block, new BlockBannerWall(meta), true);
    }

    new BlockEntityBanner(getLevel().getChunk((int) block.x >> 4, (int) block.z >> 4), nbt);
    return true;
}
 
Example 15
Source File: ItemArmor.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public void clearCustomColor(){
    if(!this.hasCompoundTag()) return;
    CompoundTag tag = this.getNamedTag();
    if(tag.contains("customColor")){
        tag.remove("customColor");
    }
    this.setCompoundTag(tag);
}
 
Example 16
Source File: BlockEntityBanner.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public BlockEntityBanner(FullChunk chunk, CompoundTag nbt) {
	super(chunk, nbt);

	if (!nbt.contains("Base")) {
		nbt.putInt("Base", 0);
	}
	if (!nbt.contains("Patterns")) {
		nbt.putList(new ListTag<>("Patterns"));
	}
}
 
Example 17
Source File: Chunk.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public static Chunk fromBinary(byte[] data, LevelProvider provider) {
    try {
        CompoundTag chunk = NBTIO.read(new ByteArrayInputStream(Zlib.inflate(data)), ByteOrder.BIG_ENDIAN);

        if (!chunk.contains("Level") || !(chunk.get("Level") instanceof CompoundTag)) {
            return null;
        }
        return new Chunk(provider != null ? provider : McRegion.class.newInstance(), chunk.getCompound("Level"));
    } catch (Exception e) {
        return null;
    }
}
 
Example 18
Source File: Item.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public boolean hasCustomName() {
    if (!this.hasCompoundTag()) {
        return false;
    }

    CompoundTag tag = this.getNamedTag();
    if (tag.contains("display")) {
        Tag tag1 = tag.get("display");
        if (tag1 instanceof CompoundTag && ((CompoundTag) tag1).contains("Name") && ((CompoundTag) tag1).get("Name") instanceof StringTag) {
            return true;
        }
    }

    return false;
}
 
Example 19
Source File: Item.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public boolean hasCustomBlockData() {
    if (!this.hasCompoundTag()) {
        return false;
    }

    CompoundTag tag = this.getNamedTag();
    return tag.contains("BlockEntityTag") && tag.get("BlockEntityTag") instanceof CompoundTag;

}
 
Example 20
Source File: BlockEntityComparator.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public BlockEntityComparator(FullChunk chunk, CompoundTag nbt) {
    super(chunk, nbt);

    if (!nbt.contains("OutputSignal")) {
        nbt.putInt("OutputSignal", 0);
    }

    this.outputSignal = nbt.getInt("OutputSignal");
}