cn.nukkit.block.BlockFallable 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.block;
import cn.nukkit.entity.Entity;
import cn.nukkit.entity.item.EntityFallingBlock;
import cn.nukkit.event.block.BlockFallEvent;
import cn.nukkit.level.Level;
import cn.nukkit.nbt.tag.*;
/**
* @author rcsuperman (Nukkit Project)
*/
public abstract class BlockFallable extends BlockSolid {
protected BlockFallable() {
}
public int onUpdate(int type) {
if (type == Level.BLOCK_UPDATE_NORMAL) {
Block down = this.down();
if (down.getId() == AIR || down instanceof BlockFire || down instanceof BlockLiquid || down.getLevelBlockAtLayer(1) instanceof BlockLiquid) {
BlockFallEvent event = new BlockFallEvent(this);
this.level.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
return type;
}
this.level.setBlock(this, Block.get(Block.AIR), true, true);
EntityFallingBlock fall = createFallingEntity(new CompoundTag());
fall.spawnToAll();
}
}
return type;
}
protected EntityFallingBlock createFallingEntity(CompoundTag customNbt) {
CompoundTag nbt = new CompoundTag()
.putList(new ListTag("Pos")
.add(new DoubleTag("", this.x + 0.5))
.add(new DoubleTag("", this.y))
.add(new DoubleTag("", this.z + 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("", 0))
.add(new FloatTag("", 0)))
.putInt("TileID", this.getId())
.putByte("Data", this.getDamage());
for (Tag customTag : customNbt.getAllTags()) {
nbt.put(customTag.getName(), customTag.copy());
}
EntityFallingBlock fall = (EntityFallingBlock) Entity.createEntity("FallingSand", this.getLevel().getChunk((int) this.x >> 4, (int) this.z >> 4), nbt);
if (fall != null) {
fall.spawnToAll();
}
return fall;
}
}