Search in sources :

Example 11 with BlockFace

use of cn.nukkit.math.BlockFace in project Nukkit by Nukkit.

the class BlockRedstoneWire method getIndirectPower.

private int getIndirectPower() {
    int power = 0;
    Vector3 pos = getLocation();
    for (BlockFace face : BlockFace.values()) {
        int blockPower = this.getIndirectPower(pos.getSide(face), face);
        if (blockPower >= 15) {
            return 15;
        }
        if (blockPower > power) {
            power = blockPower;
        }
    }
    return power;
}
Also used : BlockFace(cn.nukkit.math.BlockFace) Vector3(cn.nukkit.math.Vector3)

Example 12 with BlockFace

use of cn.nukkit.math.BlockFace in project Nukkit by Nukkit.

the class BlockTripWireHook method calculateState.

public void calculateState(boolean onBreak, boolean updateAround, int pos, Block block) {
    BlockFace facing = getFacing();
    Vector3 v = this.getLocation();
    boolean attached = isAttached();
    boolean powered = isPowered();
    boolean canConnect = !onBreak;
    boolean nextPowered = false;
    int distance = 0;
    Block[] blocks = new Block[42];
    for (int i = 1; i < 42; ++i) {
        Vector3 vector = v.getSide(facing, i);
        Block b = this.level.getBlock(vector);
        if (b instanceof BlockTripWireHook) {
            if (((BlockTripWireHook) b).getFacing() == facing.getOpposite()) {
                distance = i;
            }
            break;
        }
        if (b.getId() != Block.TRIPWIRE && i != pos) {
            blocks[i] = null;
            canConnect = false;
        } else {
            if (i == pos) {
                b = block != null ? block : b;
            }
            if (b instanceof BlockTripWire) {
                boolean disarmed = !((BlockTripWire) b).isDisarmed();
                boolean wirePowered = ((BlockTripWire) b).isPowered();
                nextPowered |= disarmed && wirePowered;
                if (i == pos) {
                    this.level.scheduleUpdate(this, 10);
                    canConnect &= disarmed;
                }
            }
            blocks[i] = b;
        }
    }
    canConnect = canConnect & distance > 1;
    nextPowered = nextPowered & canConnect;
    BlockTripWireHook hook = new BlockTripWireHook();
    hook.setAttached(canConnect);
    hook.setPowered(nextPowered);
    if (distance > 0) {
        Vector3 vec = v.getSide(facing, distance);
        BlockFace face = facing.getOpposite();
        hook.setFace(face);
        this.level.setBlock(vec, hook, true, false);
        this.level.updateAroundRedstone(vec, null);
        this.level.updateAroundRedstone(vec.getSide(face.getOpposite()), null);
        this.addSound(vec, canConnect, nextPowered, attached, powered);
    }
    this.addSound(v, canConnect, nextPowered, attached, powered);
    if (!onBreak) {
        hook.setFace(facing);
        this.level.setBlock(v, hook, true, false);
        if (updateAround) {
            this.level.updateAroundRedstone(v, null);
            this.level.updateAroundRedstone(v.getSide(facing.getOpposite()), null);
        }
    }
    if (attached != canConnect) {
        for (int i = 1; i < distance; i++) {
            Vector3 vc = v.getSide(facing, i);
            block = blocks[i];
            if (block != null && this.level.getBlockIdAt(vc.getFloorX(), vc.getFloorY(), vc.getFloorZ()) != Block.AIR) {
                if (canConnect ^ ((block.getDamage() & 0x04) > 0)) {
                    block.setDamage(block.getDamage() ^ 0x04);
                }
                this.level.setBlock(vc, block, true, false);
            }
        }
    }
}
Also used : BlockFace(cn.nukkit.math.BlockFace) Vector3(cn.nukkit.math.Vector3)

Example 13 with BlockFace

use of cn.nukkit.math.BlockFace in project Nukkit by Nukkit.

the class ProjectileDispenseBehavior method dispense.

@Override
public void dispense(BlockDispenser source, Item item) {
    Position dispensePos = Position.fromObject(source.getDispensePosition(), source.getLevel());
    CompoundTag nbt = Entity.getDefaultNBT(dispensePos);
    this.correctNBT(nbt);
    BlockFace face = source.getFacing();
    Entity projectile = Entity.createEntity(getEntityType(), dispensePos.getLevel().getChunk(dispensePos.getFloorX(), dispensePos.getFloorZ()), nbt);
    if (projectile == null) {
        return;
    }
    projectile.setMotion(new Vector3(face.getXOffset(), face.getYOffset() + 0.1f, face.getZOffset()).multiply(6));
    projectile.spawnToAll();
}
Also used : Entity(cn.nukkit.entity.Entity) Position(cn.nukkit.level.Position) BlockFace(cn.nukkit.math.BlockFace) Vector3(cn.nukkit.math.Vector3) CompoundTag(cn.nukkit.nbt.tag.CompoundTag)

Example 14 with BlockFace

use of cn.nukkit.math.BlockFace in project Nukkit by Nukkit.

the class BlockTrappedChest method place.

@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    int[] faces = { 2, 5, 3, 4 };
    BlockEntityChest chest = null;
    this.setDamage(faces[player != null ? player.getDirection().getHorizontalIndex() : 0]);
    for (BlockFace side : Plane.HORIZONTAL) {
        if ((this.getDamage() == 4 || this.getDamage() == 5) && (side == BlockFace.WEST || side == BlockFace.EAST)) {
            continue;
        } else if ((this.getDamage() == 3 || this.getDamage() == 2) && (side == BlockFace.NORTH || side == BlockFace.SOUTH)) {
            continue;
        }
        Block c = this.getSide(side);
        if (c instanceof BlockTrappedChest && c.getDamage() == this.getDamage()) {
            BlockEntity blockEntity = this.getLevel().getBlockEntity(c);
            if (blockEntity instanceof BlockEntityChest && !((BlockEntityChest) blockEntity).isPaired()) {
                chest = (BlockEntityChest) blockEntity;
                break;
            }
        }
    }
    this.getLevel().setBlock(block, this, true, true);
    CompoundTag nbt = new CompoundTag("").putList(new ListTag<>("Items")).putString("id", BlockEntity.CHEST).putInt("x", (int) this.x).putInt("y", (int) this.y).putInt("z", (int) this.z);
    if (item.hasCustomName()) {
        nbt.putString("CustomName", item.getCustomName());
    }
    if (item.hasCustomBlockData()) {
        Map<String, Tag> customData = item.getCustomBlockData().getTags();
        for (Map.Entry<String, Tag> tag : customData.entrySet()) {
            nbt.put(tag.getKey(), tag.getValue());
        }
    }
    BlockEntity blockEntity = new BlockEntityChest(this.getLevel().getChunk((int) (this.x) >> 4, (int) (this.z) >> 4), nbt);
    if (chest != null) {
        chest.pairWith(((BlockEntityChest) blockEntity));
        ((BlockEntityChest) blockEntity).pairWith(chest);
    }
    return true;
}
Also used : BlockFace(cn.nukkit.math.BlockFace) BlockEntityChest(cn.nukkit.blockentity.BlockEntityChest) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) Tag(cn.nukkit.nbt.tag.Tag) ListTag(cn.nukkit.nbt.tag.ListTag) Map(java.util.Map) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 15 with BlockFace

use of cn.nukkit.math.BlockFace in project Nukkit by Nukkit.

the class BlockRedstoneDiode method onBreak.

@Override
public boolean onBreak(Item item) {
    Vector3 pos = getLocation();
    this.level.setBlock(this, new BlockAir(), true, true);
    for (BlockFace face : BlockFace.values()) {
        this.level.updateAroundRedstone(pos.getSide(face), null);
    }
    return true;
}
Also used : BlockFace(cn.nukkit.math.BlockFace) Vector3(cn.nukkit.math.Vector3)

Aggregations

BlockFace (cn.nukkit.math.BlockFace)29 Vector3 (cn.nukkit.math.Vector3)16 CompoundTag (cn.nukkit.nbt.tag.CompoundTag)3 BlockRedstoneEvent (cn.nukkit.event.block.BlockRedstoneEvent)2 Player (cn.nukkit.Player)1 Block (cn.nukkit.block.Block)1 BlockAir (cn.nukkit.block.BlockAir)1 BlockLiquid (cn.nukkit.block.BlockLiquid)1 BlockWater (cn.nukkit.block.BlockWater)1 BlockEntity (cn.nukkit.blockentity.BlockEntity)1 BlockEntityChest (cn.nukkit.blockentity.BlockEntityChest)1 BlockEntityHopper (cn.nukkit.blockentity.BlockEntityHopper)1 Entity (cn.nukkit.entity.Entity)1 BlockFromToEvent (cn.nukkit.event.block.BlockFromToEvent)1 PlayerBucketEmptyEvent (cn.nukkit.event.player.PlayerBucketEmptyEvent)1 PlayerBucketFillEvent (cn.nukkit.event.player.PlayerBucketFillEvent)1 Item (cn.nukkit.item.Item)1 ItemTool (cn.nukkit.item.ItemTool)1 Level (cn.nukkit.level.Level)1 Position (cn.nukkit.level.Position)1