cn.nukkit.item.ItemEndCrystal Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of powernukkit Show documentation
Show all versions of powernukkit Show documentation
A Minecraft Bedrock Edition server software implementation made in Java from scratch which supports all new features.
package cn.nukkit.item;
import cn.nukkit.block.BlockObsidian;
import cn.nukkit.block.BlockBedrock;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.entity.Entity;
import java.util.Random;
import cn.nukkit.nbt.tag.FloatTag;
import cn.nukkit.nbt.tag.DoubleTag;
import cn.nukkit.nbt.tag.ListTag;
import cn.nukkit.math.BlockFace;
import cn.nukkit.block.Block;
import cn.nukkit.Player;
import cn.nukkit.level.Level;
import cn.nukkit.level.format.FullChunk;
public class ItemEndCrystal extends Item {
public ItemEndCrystal() {
this(0, 1);
}
public ItemEndCrystal(Integer meta) {
this(meta, 1);
}
public ItemEndCrystal(Integer meta, int count) {
super(END_CRYSTAL, meta, count, "End Crystal");
}
@Override
public boolean canBeActivated() {
return true;
}
@Override
public boolean onActivate(Level level, Player player, Block block, Block target, BlockFace face, double fx, double fy, double fz) {
if ((!(target instanceof BlockBedrock) && !(target instanceof BlockObsidian)) || face != BlockFace.UP) return false;
FullChunk chunk = level.getChunk((int) block.getX() >> 4, (int) block.getZ() >> 4);
if (chunk == null) {
return false;
}
CompoundTag nbt = new CompoundTag()
.putList(new ListTag("Pos")
.add(new DoubleTag("", block.getX() + 0.5))
.add(new DoubleTag("", block.getY()))
.add(new DoubleTag("", block.getZ() + 0.5)))
.putList(new ListTag("Motion")
.add(new DoubleTag("", 0))
.add(new DoubleTag("", 0))
.add(new DoubleTag("", 0)))
.putList(new ListTag("Rotation")
.add(new FloatTag("", new Random().nextFloat() * 360))
.add(new FloatTag("", 0)));
if (this.hasCustomName()) {
nbt.putString("CustomName", this.getCustomName());
}
Entity entity = Entity.createEntity("EndCrystal", 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;
}
}