use of cn.nukkit.math.Vector3 in project Nukkit by Nukkit.
the class BlockRedstoneWire method calculateCurrentChanges.
private void calculateCurrentChanges(boolean force) {
Vector3 pos = this.getLocation();
int meta = this.getDamage();
int maxStrength = meta;
this.canProvidePower = false;
int power = this.getIndirectPower();
this.canProvidePower = true;
if (power > 0 && power > maxStrength - 1) {
maxStrength = power;
}
int strength = 0;
for (BlockFace face : Plane.HORIZONTAL) {
Vector3 v = pos.getSide(face);
boolean flag = v.getX() != this.getX() || v.getZ() != this.getZ();
if (flag) {
strength = this.getMaxCurrentStrength(v, strength);
}
if (this.level.getBlock(v).isNormalBlock() && !this.level.getBlock(pos.up()).isNormalBlock()) {
if (flag) {
strength = this.getMaxCurrentStrength(v.up(), strength);
}
} else if (flag && !this.level.getBlock(v).isNormalBlock()) {
strength = this.getMaxCurrentStrength(v.down(), strength);
}
}
if (strength > maxStrength) {
maxStrength = strength - 1;
} else if (maxStrength > 0) {
--maxStrength;
} else {
maxStrength = 0;
}
if (power > maxStrength - 1) {
maxStrength = power;
}
if (meta != maxStrength) {
this.level.getServer().getPluginManager().callEvent(new BlockRedstoneEvent(this, meta, maxStrength));
this.setDamage(maxStrength);
this.level.setBlock(this, this, false, false);
this.level.updateAroundRedstone(this, null);
for (BlockFace face : BlockFace.values()) {
this.level.updateAroundRedstone(pos.getSide(face), face.getOpposite());
}
} else if (force) {
for (BlockFace face : BlockFace.values()) {
this.level.updateAroundRedstone(pos.getSide(face), face.getOpposite());
}
}
}
use of cn.nukkit.math.Vector3 in project Nukkit by Nukkit.
the class ItemFlintSteel method onActivate.
@Override
public boolean onActivate(Level level, Player player, Block block, Block target, BlockFace face, double fx, double fy, double fz) {
if (block.getId() == AIR && (target instanceof BlockSolid || target instanceof BlockSolidMeta)) {
if (target.getId() == OBSIDIAN) {
int targetX = target.getFloorX();
int targetY = target.getFloorY();
int targetZ = target.getFloorZ();
int x_max = targetX;
int x_min = targetX;
int x;
for (x = targetX + 1; level.getBlock(new Vector3(x, targetY, targetZ)).getId() == OBSIDIAN; x++) {
x_max++;
}
for (x = targetX - 1; level.getBlock(new Vector3(x, targetY, targetZ)).getId() == OBSIDIAN; x--) {
x_min--;
}
int count_x = x_max - x_min + 1;
int z_max = targetZ;
int z_min = targetZ;
int z;
for (z = targetZ + 1; level.getBlock(new Vector3(targetX, targetY, z)).getId() == OBSIDIAN; z++) {
z_max++;
}
for (z = targetZ - 1; level.getBlock(new Vector3(targetX, targetY, z)).getId() == OBSIDIAN; z--) {
z_min--;
}
int count_z = z_max - z_min + 1;
int z_max_y = targetY;
int z_min_y = targetY;
int y;
for (y = targetY; level.getBlock(new Vector3(targetX, y, z_max)).getId() == OBSIDIAN; y++) {
z_max_y++;
}
for (y = targetY; level.getBlock(new Vector3(targetX, y, z_min)).getId() == OBSIDIAN; y++) {
z_min_y++;
}
int y_max = Math.min(z_max_y, z_min_y) - 1;
int count_y = y_max - targetY + 2;
if ((count_x >= 4 && count_x <= 23 || count_z >= 4 && count_z <= 23) && count_y >= 5 && count_y <= 23) {
int count_up = 0;
for (int up_z = z_min; level.getBlock(new Vector3(targetX, y_max, up_z)).getId() == OBSIDIAN && up_z <= z_max; up_z++) {
count_up++;
}
if (count_up == count_z) {
for (int block_z = z_min + 1; block_z < z_max; block_z++) {
for (int block_y = targetY + 1; block_y < y_max; block_y++) {
level.setBlock(new Vector3(targetX, block_y, block_z), new BlockNetherPortal());
}
}
return true;
}
}
}
BlockFire fire = new BlockFire();
fire.x = block.x;
fire.y = block.y;
fire.z = block.z;
fire.level = level;
if (fire.isBlockTopFacingSurfaceSolid(fire.down()) || fire.canNeighborBurn()) {
BlockIgniteEvent e = new BlockIgniteEvent(block, null, player, BlockIgniteEvent.BlockIgniteCause.FLINT_AND_STEEL);
block.getLevel().getServer().getPluginManager().callEvent(e);
if (!e.isCancelled()) {
level.setBlock(fire, fire, true);
level.scheduleUpdate(fire, fire.tickRate() + ThreadLocalRandom.current().nextInt(10));
}
return true;
}
if ((player.gamemode & 0x01) == 0 && this.useOn(block)) {
if (this.getDamage() >= this.getMaxDurability()) {
this.count = 0;
} else {
this.meta++;
}
}
return true;
}
return false;
}
use of cn.nukkit.math.Vector3 in project Nukkit by Nukkit.
the class ExplodePacket method encode.
@Override
public void encode() {
this.reset();
this.putVector3f(this.x, this.y, this.z);
this.putVarInt((int) (this.radius * 32));
this.putUnsignedVarInt(this.records.length);
if (this.records.length > 0) {
for (Vector3 record : records) {
this.putBlockVector3((int) record.x, (int) record.y, (int) record.z);
}
}
}
use of cn.nukkit.math.Vector3 in project Nukkit by Nukkit.
the class BlockLiquid method getOptimalFlowDirections.
private boolean[] getOptimalFlowDirections() {
if (this.temporalVector == null) {
this.temporalVector = new Vector3(0, 0, 0);
}
for (int j = 0; j < 4; ++j) {
this.flowinCost[j] = 1000;
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 block = this.getLevel().getBlock(this.temporalVector.setComponents(x, y, z));
if (!block.canBeFlowedInto() && !(block instanceof BlockLiquid)) {
continue;
} else if (block instanceof BlockLiquid && block.getDamage() == 0) {
continue;
} else if (this.getLevel().getBlock(this.temporalVector.setComponents(x, y - 1, z)).canBeFlowedInto()) {
this.flowinCost[j] = 0;
} else {
this.flowinCost[j] = this.calculateFlowCost(block, 1, j);
}
}
int minCost = this.flowinCost[0];
for (int i = 1; i < 4; ++i) {
if (this.flowinCost[i] < minCost) {
minCost = this.flowinCost[i];
}
}
for (int i = 0; i < 4; ++i) {
this.isOptimalFlowDirection[i] = (this.flowinCost[i] == minCost);
}
return this.isOptimalFlowDirection;
}
use of cn.nukkit.math.Vector3 in project Nukkit by Nukkit.
the class BlockLiquid method onUpdate.
@Override
public int onUpdate(int type) {
if (type == Level.BLOCK_UPDATE_NORMAL) {
this.checkForHarden();
this.getLevel().scheduleUpdate(this, this.tickRate());
} else if (type == Level.BLOCK_UPDATE_SCHEDULED) {
if (this.temporalVector == null) {
this.temporalVector = new Vector3(0, 0, 0);
}
int decay = this.getFlowDecay(this);
int multiplier = this instanceof BlockLava ? 2 : 1;
boolean flag = true;
int smallestFlowDecay;
if (decay > 0) {
smallestFlowDecay = -100;
this.adjacentSources = 0;
smallestFlowDecay = this.getSmallestFlowDecay(this.level.getBlock(this.temporalVector.setComponents(this.x, this.y, this.z - 1)), smallestFlowDecay);
smallestFlowDecay = this.getSmallestFlowDecay(this.level.getBlock(this.temporalVector.setComponents(this.x, this.y, this.z + 1)), smallestFlowDecay);
smallestFlowDecay = this.getSmallestFlowDecay(this.level.getBlock(this.temporalVector.setComponents(this.x - 1, this.y, this.z)), smallestFlowDecay);
smallestFlowDecay = this.getSmallestFlowDecay(this.level.getBlock(this.temporalVector.setComponents(this.x + 1, this.y, this.z)), smallestFlowDecay);
int k = smallestFlowDecay + multiplier;
if (k >= 8 || smallestFlowDecay < 0) {
k = -1;
}
int topFlowDecay;
if ((topFlowDecay = this.getFlowDecay(this.level.getBlock(this.level.getBlock(this.temporalVector.setComponents(this.x, this.y + 1, this.z))))) >= 0) {
if (topFlowDecay >= 8) {
k = topFlowDecay;
} else {
k = topFlowDecay | 0x08;
}
}
if (this.adjacentSources >= 2 && this instanceof BlockWater) {
Block bottomBlock = this.level.getBlock(this.level.getBlock(this.temporalVector.setComponents(this.x, this.y - 1, this.z)));
if (bottomBlock.isSolid()) {
k = 0;
} else if (bottomBlock instanceof BlockWater && bottomBlock.getDamage() == 0) {
k = 0;
}
}
if (this instanceof BlockLava && decay < 8 && k < 8 && k > 1 && new Random().nextInt(4) != 0) {
k = decay;
flag = false;
}
if (k != decay) {
decay = k;
if (decay < 0) {
this.getLevel().setBlock(this, new BlockAir(), true);
} else {
this.getLevel().setBlock(this, this.getBlock(decay), true);
}
} else if (flag) {
// this.getLevel().scheduleUpdate(this, this.tickRate());
// this.updateFlow();
}
} else {
// this.updateFlow();
}
Block bottomBlock = this.level.getBlock(this.temporalVector.setComponents(this.x, this.y - 1, this.z));
if (bottomBlock.canBeFlowedInto() || bottomBlock instanceof BlockLiquid) {
if (this instanceof BlockLava && bottomBlock instanceof BlockWater) {
Block to = new BlockStone();
to.setComponents(bottomBlock.getX(), bottomBlock.getY(), bottomBlock.getZ());
to.setLevel(bottomBlock.getLevel());
BlockFromToEvent ev = new BlockFromToEvent(bottomBlock, to);
this.getLevel().getServer().getPluginManager().callEvent(ev);
if (!ev.isCancelled()) {
this.getLevel().setBlock(bottomBlock, ev.getTo(), true);
this.triggerLavaMixEffects(bottomBlock);
return 0;
}
}
if (decay >= 8) {
this.flowIntoBlock(bottomBlock, decay);
} else {
this.flowIntoBlock(bottomBlock, decay | 0x08);
}
}
if (decay >= 0 && ((decay == 0 && this.getDamage() == 0) || (!bottomBlock.canBeFlowedInto() && !(bottomBlock instanceof BlockLiquid)))) {
boolean[] flags = this.getOptimalFlowDirections();
int l = decay + multiplier;
if (decay >= 8) {
l = 1;
}
if (l >= 8) {
this.checkForHarden();
return 0;
}
if (flags[0]) {
this.flowIntoBlock(this.level.getBlock(this.temporalVector.setComponents(this.x - 1, this.y, this.z)), l);
}
if (flags[1]) {
this.flowIntoBlock(this.level.getBlock(this.temporalVector.setComponents(this.x + 1, this.y, this.z)), l);
}
if (flags[2]) {
this.flowIntoBlock(this.level.getBlock(this.temporalVector.setComponents(this.x, this.y, this.z - 1)), l);
}
if (flags[3]) {
this.flowIntoBlock(this.level.getBlock(this.temporalVector.setComponents(this.x, this.y, this.z + 1)), l);
}
}
this.checkForHarden();
}
return 0;
}
Aggregations