Search in sources :

Example 41 with Vector3

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();
}
Also used : Vector3(cn.nukkit.math.Vector3)

Example 42 with Vector3

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;
}
Also used : Vector3(cn.nukkit.math.Vector3)

Example 43 with Vector3

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;
    }
}
Also used : BlockFace(cn.nukkit.math.BlockFace) ArrayList(java.util.ArrayList) Vector3(cn.nukkit.math.Vector3)

Example 44 with Vector3

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);
    }
}
Also used : BlockFace(cn.nukkit.math.BlockFace) Vector3(cn.nukkit.math.Vector3)

Example 45 with Vector3

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;
}
Also used : Random(java.util.Random) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Vector3(cn.nukkit.math.Vector3) BlockIgniteEvent(cn.nukkit.event.block.BlockIgniteEvent)

Aggregations

Vector3 (cn.nukkit.math.Vector3)62 BlockFace (cn.nukkit.math.BlockFace)16 Block (cn.nukkit.block.Block)5 Player (cn.nukkit.Player)3 Entity (cn.nukkit.entity.Entity)3 BlockIgniteEvent (cn.nukkit.event.block.BlockIgniteEvent)3 Random (java.util.Random)3 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)3 EntityLiving (cn.nukkit.entity.EntityLiving)2 BlockRedstoneEvent (cn.nukkit.event.block.BlockRedstoneEvent)2 BlockSpreadEvent (cn.nukkit.event.block.BlockSpreadEvent)2 VehicleMoveEvent (cn.nukkit.event.vehicle.VehicleMoveEvent)2 VehicleUpdateEvent (cn.nukkit.event.vehicle.VehicleUpdateEvent)2 ItemBlock (cn.nukkit.item.ItemBlock)2 TranslationContainer (cn.nukkit.lang.TranslationContainer)2 Level (cn.nukkit.level.Level)2 Location (cn.nukkit.level.Location)2 AxisAlignedBB (cn.nukkit.math.AxisAlignedBB)2 NukkitRandom (cn.nukkit.math.NukkitRandom)2 Rail (cn.nukkit.utils.Rail)2