use of cn.nukkit.block.Block in project Nukkit by Nukkit.
the class EntityLightning method initEntity.
@Override
protected void initEntity() {
super.initEntity();
this.setHealth(4);
this.setMaxHealth(4);
this.state = 2;
this.liveTime = ThreadLocalRandom.current().nextInt(3) + 1;
if (isEffect && this.level.gameRules.getBoolean(GameRule.DO_FIRE_TICK) && (this.server.getDifficulty() >= 2)) {
Block block = this.getLevelBlock();
if (block.getId() == 0 || block.getId() == Block.TALL_GRASS) {
BlockFire fire = new BlockFire();
fire.x = block.x;
fire.y = block.y;
fire.z = block.z;
fire.level = level;
this.getLevel().setBlock(fire, fire, true);
if (fire.isBlockTopFacingSurfaceSolid(fire.down()) || fire.canNeighborBurn()) {
BlockIgniteEvent e = new BlockIgniteEvent(block, null, this, BlockIgniteEvent.BlockIgniteCause.LIGHTNING);
getServer().getPluginManager().callEvent(e);
if (!e.isCancelled()) {
level.setBlock(fire, fire, true);
level.scheduleUpdate(fire, fire.tickRate() + ThreadLocalRandom.current().nextInt(10));
}
}
}
}
}
use of cn.nukkit.block.Block in project Nukkit by Nukkit.
the class Level method getBlock.
public Block getBlock(int x, int y, int z) {
int fullState;
if (y >= 0 && y < 256) {
int cx = x >> 4;
int cz = z >> 4;
BaseFullChunk chunk = getChunk(cx, cz);
if (chunk != null) {
fullState = chunk.getFullBlock(x & 0xF, y, z & 0xF);
} else {
fullState = 0;
}
} else {
fullState = 0;
}
Block block = this.blockStates[fullState & 0xFFF].clone();
block.x = x;
block.y = y;
block.z = z;
block.level = this;
return block;
}
use of cn.nukkit.block.Block in project Nukkit by Nukkit.
the class Level method hasCollision.
public boolean hasCollision(Entity entity, AxisAlignedBB bb, boolean entities) {
int minX = NukkitMath.floorDouble(bb.getMinX());
int minY = NukkitMath.floorDouble(bb.getMinY());
int minZ = NukkitMath.floorDouble(bb.getMinZ());
int maxX = NukkitMath.ceilDouble(bb.getMaxX());
int maxY = NukkitMath.ceilDouble(bb.getMaxY());
int maxZ = NukkitMath.ceilDouble(bb.getMaxZ());
for (int z = minZ; z <= maxZ; ++z) {
for (int x = minX; x <= maxX; ++x) {
for (int y = minY; y <= maxY; ++y) {
Block block = this.getBlock(this.temporalVector.setComponents(x, y, z));
if (!block.canPassThrough() && block.collidesWithBB(bb)) {
return true;
}
}
}
}
if (entities) {
return this.getCollidingEntities(bb.grow(0.25f, 0.25f, 0.25f), entity).length > 0;
}
return false;
}
use of cn.nukkit.block.Block in project Nukkit by Nukkit.
the class Level method setBlock.
public boolean setBlock(int x, int y, int z, Block block, boolean direct, boolean update) {
if (y < 0 || y >= 256) {
return false;
}
BaseFullChunk chunk = this.getChunk(x >> 4, z >> 4, true);
Block blockPrevious;
// synchronized (chunk) {
blockPrevious = chunk.getAndSetBlock(x & 0xF, y, z & 0xF, block);
if (blockPrevious.getFullId() == block.getFullId()) {
return false;
}
// }
block.x = x;
block.y = y;
block.z = z;
block.level = this;
int cx = x >> 4;
int cz = z >> 4;
long index = Level.chunkHash(cx, cz);
if (direct) {
this.sendBlocks(this.getChunkPlayers(cx, cz).values().stream().toArray(Player[]::new), new Block[] { block }, UpdateBlockPacket.FLAG_ALL_PRIORITY);
} else {
addBlockChange(index, x, y, z);
}
for (ChunkLoader loader : this.getChunkLoaders(cx, cz)) {
loader.onBlockChanged(block);
}
if (update) {
if (blockPrevious.isTransparent() != block.isTransparent() || blockPrevious.getLightLevel() != block.getLightLevel()) {
addLightUpdate(x, y, z);
}
BlockUpdateEvent ev = new BlockUpdateEvent(block);
this.server.getPluginManager().callEvent(ev);
if (!ev.isCancelled()) {
for (Entity entity : this.getNearbyEntities(new SimpleAxisAlignedBB(x - 1, y - 1, z - 1, x + 1, y + 1, z + 1))) {
entity.scheduleUpdate();
}
block = ev.getBlock();
block.onUpdate(BLOCK_UPDATE_NORMAL);
this.updateAround(x, y, z);
}
}
return true;
}
use of cn.nukkit.block.Block in project Nukkit by Nukkit.
the class Level method getSafeSpawn.
public Position getSafeSpawn(Vector3 spawn) {
if (spawn == null || spawn.y < 1) {
spawn = this.getSpawnLocation();
}
if (spawn != null) {
Vector3 v = spawn.floor();
FullChunk chunk = this.getChunk((int) v.x >> 4, (int) v.z >> 4, false);
int x = (int) v.x & 0x0f;
int z = (int) v.z & 0x0f;
if (chunk != null) {
int y = (int) Math.min(254, v.y);
boolean wasAir = chunk.getBlockId(x, y - 1, z) == 0;
for (; y > 0; --y) {
int b = chunk.getFullBlock(x, y, z);
Block block = Block.get(b >> 4, b & 0x0f);
if (this.isFullBlock(block)) {
if (wasAir) {
y++;
break;
}
} else {
wasAir = true;
}
}
for (; y >= 0 && y < 256; ++y) {
int b = chunk.getFullBlock(x, y + 1, z);
Block block = Block.get(b >> 4, b & 0x0f);
if (!this.isFullBlock(block)) {
b = chunk.getFullBlock(x, y, z);
block = Block.get(b >> 4, b & 0x0f);
if (!this.isFullBlock(block)) {
return new Position(spawn.x, y == (int) spawn.y ? spawn.y : y, spawn.z, this);
}
} else {
++y;
}
}
v.y = y;
}
return new Position(spawn.x, v.y, spawn.z, this);
}
return null;
}
Aggregations