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);
}
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;
}
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;
}
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);
}
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;
}
Aggregations