cn.nukkit.block.BlockDragonEgg Maven / Gradle / Ivy
package cn.nukkit.block;
import cn.nukkit.Player;
import cn.nukkit.api.PowerNukkitOnly;
import cn.nukkit.api.Since;
import cn.nukkit.event.block.BlockFromToEvent;
import cn.nukkit.event.player.PlayerInteractEvent.Action;
import cn.nukkit.level.Level;
import cn.nukkit.network.protocol.LevelEventPacket;
import cn.nukkit.utils.BlockColor;
import javax.annotation.Nullable;
import java.util.concurrent.ThreadLocalRandom;
public class BlockDragonEgg extends BlockFallable {
public BlockDragonEgg() {
}
@Override
public String getName() {
return "Dragon Egg";
}
@Override
public int getId() {
return DRAGON_EGG;
}
@Override
public double getHardness() {
return 3;
}
@Override
public double getResistance() {
return 45;
}
@Override
public int getLightLevel() {
return 1;
}
@Override
public BlockColor getColor() {
return BlockColor.OBSIDIAN_BLOCK_COLOR;
}
@Override
public boolean isTransparent() {
return true;
}
@PowerNukkitOnly
@Override
public int getWaterloggingLevel() {
return 1;
}
@Override
public int onUpdate(int type) {
if (type == Level.BLOCK_UPDATE_TOUCH) {
this.teleport();
}
return super.onUpdate(type);
}
@Since("1.4.0.0-PN")
@PowerNukkitOnly
@Override
public int onTouch(@Nullable Player player, Action action) {
switch (action) {
case RIGHT_CLICK_BLOCK:
break;
case LEFT_CLICK_BLOCK:
if (player != null && player.isCreative()) {
return 0;
}
break;
default:
return 0;
}
return super.onTouch(player, action);
}
public void teleport() {
ThreadLocalRandom random = ThreadLocalRandom.current();
for (int i = 0; i < 1000; ++i) {
Block to = this.getLevel().getBlock(this.add(random.nextInt(-16, 16), random.nextInt(-16, 16), random.nextInt(-16, 16)));
if (to.getId() == AIR) {
BlockFromToEvent event = new BlockFromToEvent(this, to);
this.level.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) return;
to = event.getTo();
int diffX = this.getFloorX() - to.getFloorX();
int diffY = this.getFloorY() - to.getFloorY();
int diffZ = this.getFloorZ() - to.getFloorZ();
LevelEventPacket pk = new LevelEventPacket();
pk.evid = LevelEventPacket.EVENT_PARTICLE_DRAGON_EGG_TELEPORT;
pk.data = (((((Math.abs(diffX) << 16) | (Math.abs(diffY) << 8)) | Math.abs(diffZ)) | ((diffX < 0 ? 1 : 0) << 24)) | ((diffY < 0 ? 1 : 0) << 25)) | ((diffZ < 0 ? 1 : 0) << 26);
pk.x = this.getFloorX();
pk.y = this.getFloorY();
pk.z = this.getFloorZ();
this.getLevel().addChunkPacket(this.getFloorX() >> 4, this.getFloorZ() >> 4, pk);
this.getLevel().setBlock(this, get(AIR), true);
this.getLevel().setBlock(to, this, true);
return;
}
}
}
@Override
@PowerNukkitOnly
public boolean breaksWhenMoved() {
return true;
}
@Override
@PowerNukkitOnly
public boolean sticksToPiston() {
return false;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy