use of cn.nukkit.math.Vector3 in project Nukkit by Nukkit.
the class BlockLiquid method getFlowVector.
public Vector3 getFlowVector() {
Vector3 vector = new Vector3(0, 0, 0);
if (this.temporalVector == null) {
this.temporalVector = new Vector3(0, 0, 0);
}
int decay = this.getEffectiveFlowDecay(this);
for (int j = 0; j < 4; ++j) {
double x = this.x;
double y = this.y;
double z = this.z;
if (j == 0) {
--x;
} else if (j == 1) {
++x;
} else if (j == 2) {
--z;
} else if (j == 3) {
++z;
}
Block sideBlock = this.getLevel().getBlock(this.temporalVector.setComponents(x, y, z));
int blockDecay = this.getEffectiveFlowDecay(sideBlock);
if (blockDecay < 0) {
if (!sideBlock.canBeFlowedInto()) {
continue;
}
blockDecay = this.getEffectiveFlowDecay(this.getLevel().getBlock(this.temporalVector.setComponents(x, y - 1, z)));
if (blockDecay >= 0) {
int realDecay = blockDecay - (decay - 8);
vector.x += (sideBlock.x - this.x) * realDecay;
vector.y += (sideBlock.y - this.y) * realDecay;
vector.z += (sideBlock.z - this.z) * realDecay;
}
continue;
} else {
int realDecay = blockDecay - decay;
vector.x += (sideBlock.x - this.x) * realDecay;
vector.y += (sideBlock.y - this.y) * realDecay;
vector.z += (sideBlock.z - this.z) * realDecay;
}
}
if (this.getDamage() >= 8) {
boolean falling = false;
if (!this.getLevel().getBlock(this.temporalVector.setComponents(this.x, this.y, this.z - 1)).canBeFlowedInto()) {
falling = true;
} else if (!this.getLevel().getBlock(this.temporalVector.setComponents(this.x, this.y, this.z + 1)).canBeFlowedInto()) {
falling = true;
} else if (!this.getLevel().getBlock(this.temporalVector.setComponents(this.x - 1, this.y, this.z)).canBeFlowedInto()) {
falling = true;
} else if (!this.getLevel().getBlock(this.temporalVector.setComponents(this.x + 1, this.y, this.z)).canBeFlowedInto()) {
falling = true;
} else if (!this.getLevel().getBlock(this.temporalVector.setComponents(this.x, this.y + 1, this.z - 1)).canBeFlowedInto()) {
falling = true;
} else if (!this.getLevel().getBlock(this.temporalVector.setComponents(this.x, this.y + 1, this.z + 1)).canBeFlowedInto()) {
falling = true;
} else if (!this.getLevel().getBlock(this.temporalVector.setComponents(this.x - 1, this.y + 1, this.z)).canBeFlowedInto()) {
falling = true;
} else if (!this.getLevel().getBlock(this.temporalVector.setComponents(this.x + 1, this.y + 1, this.z)).canBeFlowedInto()) {
falling = true;
}
if (falling) {
vector = vector.normalize().add(0, -6, 0);
}
}
return vector.normalize();
}
use of cn.nukkit.math.Vector3 in project Nukkit by Nukkit.
the class BlockLiquid method addVelocityToEntity.
@Override
public void addVelocityToEntity(Entity entity, Vector3 vector) {
Vector3 flow = this.getFlowVector();
vector.x += flow.x;
vector.y += flow.y;
vector.z += flow.z;
}
use of cn.nukkit.math.Vector3 in project Nukkit by Nukkit.
the class BlockPistonBase method doMove.
private boolean doMove(boolean extending) {
Vector3 pos = this.getLocation();
BlockFace direction = getFacing();
if (!extending) {
this.level.setBlock(pos.getSide(direction), new BlockAir(), true, false);
}
BlocksCalculator calculator = new BlocksCalculator(this.level, this, direction, extending);
if (!calculator.canMove()) {
return false;
} else {
List<Block> blocks = calculator.getBlocksToMove();
List<Block> newBlocks = new ArrayList<>();
for (int i = 0; i < blocks.size(); ++i) {
Block block = blocks.get(i);
newBlocks.add(block);
}
List<Block> destroyBlocks = calculator.getBlocksToDestroy();
BlockFace side = extending ? direction : direction.getOpposite();
for (int i = destroyBlocks.size() - 1; i >= 0; --i) {
Block block = destroyBlocks.get(i);
this.level.useBreakOn(block);
}
for (int i = blocks.size() - 1; i >= 0; --i) {
Block block = blocks.get(i);
this.level.setBlock(block, new BlockAir());
Vector3 newPos = block.getLocation().getSide(side);
// TODO: change this to block entity
this.level.setBlock(newPos, newBlocks.get(i));
}
Vector3 pistonHead = pos.getSide(direction);
if (extending) {
// extension block entity
this.level.setBlock(pistonHead, new BlockPistonHead(this.getDamage()));
}
return true;
}
}
use of cn.nukkit.math.Vector3 in project Nukkit by Nukkit.
the class BlockPistonBase method checkState.
private void checkState() {
BlockFace facing = getFacing();
boolean isPowered = this.isPowered();
if (isPowered && !isExtended()) {
if ((new BlocksCalculator(this.level, this, facing, true)).canMove()) {
if (!this.doMove(true)) {
return;
}
this.level.addSound(this, Sound.TILE_PISTON_OUT);
} else {
}
} else if (!isPowered && isExtended()) {
if (this.sticky) {
Vector3 pos = this.add(facing.getXOffset() * 2, facing.getYOffset() * 2, facing.getZOffset() * 2);
Block block = this.level.getBlock(pos);
if (block.getId() == AIR) {
this.level.setBlock(this.getLocation().getSide(facing), new BlockAir(), true, true);
}
if (canPush(block, facing.getOpposite(), false) && (!(block instanceof BlockFlowable) || block.getId() == PISTON || block.getId() == STICKY_PISTON)) {
this.doMove(false);
}
} else {
this.level.setBlock(getLocation().getSide(facing), new BlockAir(), true, false);
}
this.level.addSound(this, Sound.TILE_PISTON_IN);
}
}
use of cn.nukkit.math.Vector3 in project Nukkit by Nukkit.
the class BlockLava method onUpdate.
@Override
public int onUpdate(int type) {
int result = super.onUpdate(type);
if (type == Level.BLOCK_UPDATE_RANDOM && this.level.gameRules.getBoolean(GameRule.DO_FIRE_TICK)) {
Random random = ThreadLocalRandom.current();
int i = random.nextInt(3);
if (i > 0) {
for (int k = 0; k < i; ++k) {
Vector3 v = this.add(random.nextInt(3) - 1, 1, random.nextInt(3) - 1);
Block block = this.getLevel().getBlock(v);
if (block.getId() == AIR) {
if (this.isSurroundingBlockFlammable(block)) {
BlockIgniteEvent e = new BlockIgniteEvent(block, this, null, BlockIgniteEvent.BlockIgniteCause.LAVA);
this.level.getServer().getPluginManager().callEvent(e);
if (!e.isCancelled()) {
BlockFire fire = new BlockFire();
this.getLevel().setBlock(v, fire, true);
this.getLevel().scheduleUpdate(fire, fire.tickRate());
return Level.BLOCK_UPDATE_RANDOM;
}
return 0;
}
} else if (block.isSolid()) {
return Level.BLOCK_UPDATE_RANDOM;
}
}
} else {
for (int k = 0; k < 3; ++k) {
Vector3 v = this.add(random.nextInt(3) - 1, 0, random.nextInt(3) - 1);
Block block = this.getLevel().getBlock(v);
if (block.up().getId() == AIR && block.getBurnChance() > 0) {
BlockIgniteEvent e = new BlockIgniteEvent(block, this, null, BlockIgniteEvent.BlockIgniteCause.LAVA);
this.level.getServer().getPluginManager().callEvent(e);
if (!e.isCancelled()) {
BlockFire fire = new BlockFire();
this.getLevel().setBlock(v, fire, true);
this.getLevel().scheduleUpdate(fire, fire.tickRate());
}
}
}
}
}
return result;
}
Aggregations