cn.nukkit.math.BlockFace Java Examples

The following examples show how to use cn.nukkit.math.BlockFace. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BlockQuartz.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    if (this.getDamage() != QUARTZ_NORMAL) {
        short[] faces = new short[]{
                0,
                0,
                0b1000,
                0b1000,
                0b0100,
                0b0100
        };

        this.setDamage(((this.getDamage() & 0x03) | faces[face.getIndex()]));
    }
    this.getLevel().setBlock(block, this, true, true);

    return true;
}
 
Example #2
Source File: BlockTorch.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public int onUpdate(int type) {
    if (type == Level.BLOCK_UPDATE_NORMAL) {
        Block below = this.down();
        int side = this.getDamage();
        int[] faces = new int[]{
                0, //0
                4, //1
                5, //2
                2, //3
                3, //4
                0, //5
                0  //6
        };

        if (this.getSide(BlockFace.fromIndex(faces[side])).isTransparent() && !(side == 0 && (below instanceof BlockFence || below.getId() == COBBLE_WALL))) {
            this.getLevel().useBreakOn(this);

            return Level.BLOCK_UPDATE_NORMAL;
        }
    }

    return 0;
}
 
Example #3
Source File: BlockRail.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
private Orientation connect(BlockRail rail1, BlockFace face1, BlockRail rail2, BlockFace face2) {
    this.connect(rail1, face1);
    this.connect(rail2, face2);

    if (face1.getOpposite() == face2) {
        int delta1 = (int) (this.y - rail1.y);
        int delta2 = (int) (this.y - rail2.y);

        if (delta1 == -1) {
            return Orientation.ascending(face1);
        } else if (delta2 == -1) {
            return Orientation.ascending(face2);
        }
    }
    return straightOrCurved(face1, face2);
}
 
Example #4
Source File: BlockCactus.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    Block down = this.down();
    if (down.getId() == SAND || down.getId() == CACTUS) {
        Block block0 = north();
        Block block1 = south();
        Block block2 = west();
        Block block3 = east();
        if (block0.isTransparent() && block1.isTransparent() && block2.isTransparent() && block3.isTransparent()) {
            this.getLevel().setBlock(this, this, true);

            return true;
        }
    }
    return false;
}
 
Example #5
Source File: BlockLadder.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public int onUpdate(int type) {
    if (type == Level.BLOCK_UPDATE_NORMAL) {
        int[] faces = {
                0, //never use
                1, //never use
                3,
                2,
                5,
                4
        };
        if (!this.getSide(BlockFace.fromIndex(faces[this.getDamage()])).isSolid()) {
            this.getLevel().useBreakOn(this);
            return Level.BLOCK_UPDATE_NORMAL;
        }
    }
    return 0;
}
 
Example #6
Source File: Entity.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
public BlockFace getDirection() {
    double rotation = (this.yaw - 90) % 360;
    if (rotation < 0) {
        rotation += 360.0;
    }
    if ((0 <= rotation && rotation < 45) || (315 <= rotation && rotation < 360)) {
        return BlockFace.NORTH;//s
    } else if (45 <= rotation && rotation < 135) {
        return BlockFace.EAST;//w
    } else if (135 <= rotation && rotation < 225) {
        return BlockFace.SOUTH;//n
    } else if (225 <= rotation && rotation < 315) {
        return BlockFace.WEST;//e
    } else {
        return null;
    }
}
 
Example #7
Source File: BlockItemFrame.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    if (face.getIndex() > 1 && target.isSolid() && (!block.isSolid() || block.canBeReplaced())) {
        this.setDamage(FACING[face.getIndex()]);
        this.getLevel().setBlock(block, this, true, true);
        CompoundTag nbt = new CompoundTag()
                .putString("id", BlockEntity.ITEM_FRAME)
                .putInt("x", (int) block.x)
                .putInt("y", (int) block.y)
                .putInt("z", (int) block.z)
                .putByte("ItemRotation", 0)
                .putFloat("ItemDropChance", 1.0f);
        if (item.hasCustomBlockData()) {
            for (Tag aTag : item.getCustomBlockData().getAllTags()) {
                nbt.put(aTag.getName(), aTag);
            }
        }
        BlockEntityItemFrame frame = (BlockEntityItemFrame) BlockEntity.createBlockEntity(BlockEntity.ITEM_FRAME, this.getLevel().getChunk((int) this.x >> 4, (int) this.z >> 4), nbt);
        if (frame == null) {
            return false;
        }
        this.getLevel().addSound(this, Sound.BLOCK_ITEMFRAME_PLACE);
        return true;
    }
    return false;
}
 
Example #8
Source File: BlockCauldron.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    CompoundTag nbt = new CompoundTag("")
            .putString("id", BlockEntity.CHEST)
            .putInt("x", (int) this.x)
            .putInt("y", (int) this.y)
            .putInt("z", (int) this.z)
            .putShort("PotionId", 0xffff)
            .putByte("SplashPotion", 0);

    if (item.hasCustomBlockData()) {
        Map<String, Tag> customData = item.getCustomBlockData().getTags();
        for (Map.Entry<String, Tag> tag : customData.entrySet()) {
            nbt.put(tag.getKey(), tag.getValue());
        }
    }

    new BlockEntityCauldron(this.level.getChunk((int) this.x >> 4, (int) this.z >> 4), nbt);
    this.getLevel().setBlock(block, this, true, true);
    return true;
}
 
Example #9
Source File: BlockLadder.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public int onUpdate(int type) {
    if (type == Level.BLOCK_UPDATE_NORMAL) {
        int[] faces = {
                0, //never use
                1, //never use
                3,
                2,
                5,
                4
        };
        if (!this.getSide(BlockFace.fromIndex(faces[this.getDamage()])).isSolid()) {
            this.getLevel().useBreakOn(this);
            return Level.BLOCK_UPDATE_NORMAL;
        }
    }
    return 0;
}
 
Example #10
Source File: BlockPurpur.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    if (this.meta != PURPUR_NORMAL) {
        short[] faces = new short[]{
                0,
                0,
                0b1000,
                0b1000,
                0b0100,
                0b0100
        };

        this.meta = ((this.meta & 0x03) | faces[face.getIndex()]);
    }
    this.getLevel().setBlock(block, this, true, true);

    return true;
}
 
Example #11
Source File: ProjectileDispenseBehavior.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@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();
}
 
Example #12
Source File: BlockPurpur.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    if (this.getDamage() != PURPUR_NORMAL) {
        short[] faces = new short[]{
                0,
                0,
                0b1000,
                0b1000,
                0b0100,
                0b0100
        };

        this.setDamage(((this.getDamage() & 0x03) | faces[face.getIndex()]));
    }
    this.getLevel().setBlock(block, this, true, true);

    return true;
}
 
Example #13
Source File: BlockAnvil.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    if (!target.isTransparent()) {
        int faces[] = {0, 1, 2, 3};
        int damage = this.getDamage();
        this.meta = faces[player != null ? player.getDirection().getHorizontalIndex() : 0] & 0x04;
        if (damage >= 0 && damage <= 3) {
            this.meta = faces[player != null ? player.getDirection().getHorizontalIndex() : 0];
        } else if (damage >= 4 && damage <= 7) {
            this.meta = faces[player != null ? player.getDirection().getHorizontalIndex() : 0] | 0x04;
        } else if (damage >= 8 && damage <= 11) {
            this.meta = faces[player != null ? player.getDirection().getHorizontalIndex() : 0] | 0x08;
        }
        this.getLevel().setBlock(block, this, true);
        return true;
    }
    return false;
}
 
Example #14
Source File: BlockCactus.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    Block down = this.down();
    if (down.getId() == SAND || down.getId() == CACTUS) {
        Block block0 = north();
        Block block1 = south();
        Block block2 = west();
        Block block3 = east();
        if (block0.isTransparent() && block1.isTransparent() && block2.isTransparent() && block3.isTransparent()) {
            this.getLevel().setBlock(this, this, true);

            return true;
        }
    }
    return false;
}
 
Example #15
Source File: BlockLadder.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
@Override
public int onUpdate(int type) {
    if (type == Level.BLOCK_UPDATE_NORMAL) {
        int[] faces = {
                0, //never use
                1, //never use
                3,
                2,
                5,
                4
        };
        if (!this.getSide(BlockFace.fromIndex(faces[this.meta])).isSolid()) {
            this.getLevel().useBreakOn(this);
            return Level.BLOCK_UPDATE_NORMAL;
        }
    }
    return 0;
}
 
Example #16
Source File: BlockFire.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public boolean canNeighborBurn() {
    for (BlockFace face : BlockFace.values()) {
        if (this.getSide(face).getBurnChance() > 0) {
            return true;
        }
    }

    return false;
}
 
Example #17
Source File: BlockTallGrass.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    Block down = this.down();
    if (down.getId() == Block.GRASS || down.getId() == Block.DIRT || down.getId() == Block.PODZOL) {
        this.getLevel().setBlock(block, this, true);
        return true;
    }
    return false;
}
 
Example #18
Source File: BlockLava.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    boolean ret = this.getLevel().setBlock(this, this, true, false);
    this.getLevel().scheduleUpdate(this, this.tickRate());

    return ret;
}
 
Example #19
Source File: BlockRedstoneDiode.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
protected int calculateInputStrength() {
    BlockFace face = getFacing();
    Vector3 pos = this.getLocation().getSide(face);
    int power = this.level.getRedstonePower(pos, face);

    if (power >= 15) {
        return power;
    } else {
        Block block = this.level.getBlock(pos);
        return Math.max(power, block.getId() == Block.REDSTONE_WIRE ? block.getDamage() : 0);
    }
}
 
Example #20
Source File: BlockRedstoneLamp.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    if (this.level.isBlockPowered(this.getLocation())) {
        this.level.setBlock(this, Block.get(BlockID.LIT_REDSTONE_LAMP), false, true);
    } else {
        this.level.setBlock(this, this, false, true);
    }
    return true;
}
 
Example #21
Source File: BlockItemFrame.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    if (!target.isTransparent() && face.getIndex() > 1 && !block.isSolid()) {
        switch (face) {
            case NORTH:
                this.meta = 3;
                break;
            case SOUTH:
                this.meta = 2;
                break;
            case WEST:
                this.meta = 1;
                break;
            case EAST:
                this.meta = 0;
                break;
            default:
                return false;
        }
        this.getLevel().setBlock(block, this, true, true);
        CompoundTag nbt = new CompoundTag()
                .putString("id", BlockEntity.ITEM_FRAME)
                .putInt("x", (int) block.x)
                .putInt("y", (int) block.y)
                .putInt("z", (int) block.z)
                .putByte("ItemRotation", 0)
                .putFloat("ItemDropChance", 1.0f);
        if (item.hasCustomBlockData()) {
            for (Tag aTag : item.getCustomBlockData().getAllTags()) {
                nbt.put(aTag.getName(), aTag);
            }
        }
        new BlockEntityItemFrame(this.getLevel().getChunk((int) this.x >> 4, (int) this.z >> 4), nbt);
        this.getLevel().addSound(new ItemFramePlacedSound(this));
        return true;
    }
    return false;
}
 
Example #22
Source File: BlockButton.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    if (target.isTransparent()) {
        return false;
    }

    this.setDamage(face.getIndex());
    this.level.setBlock(block, this, true, true);
    return true;
}
 
Example #23
Source File: BlockRedstoneDiode.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int onUpdate(int type) {
    if (type == Level.BLOCK_UPDATE_SCHEDULED) {
        if (!this.isLocked()) {
            Vector3 pos = getLocation();
            boolean shouldBePowered = this.shouldBePowered();

            if (this.isPowered && !shouldBePowered) {
                this.level.setBlock(pos, this.getUnpowered(), true, true);

                this.level.updateAroundRedstone(this.getLocation().getSide(getFacing().getOpposite()), null);
            } else if (!this.isPowered) {
                this.level.setBlock(pos, this.getPowered(), true, true);
                this.level.updateAroundRedstone(this.getLocation().getSide(getFacing().getOpposite()), null);

                if (!shouldBePowered) {
                    level.scheduleUpdate(getPowered(), this, this.getDelay());
                }
            }
        }
    } else if (type == Level.BLOCK_UPDATE_NORMAL || type == Level.BLOCK_UPDATE_REDSTONE) {
        if (type == Level.BLOCK_UPDATE_NORMAL && this.getSide(BlockFace.DOWN).isTransparent()) {
            this.level.useBreakOn(this);
            return Level.BLOCK_UPDATE_NORMAL;
        } else {
            this.updateState();
            return Level.BLOCK_UPDATE_NORMAL;
        }
    }
    return 0;
}
 
Example #24
Source File: BlockCocoa.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int onUpdate(int type) {
    if (type == Level.BLOCK_UPDATE_NORMAL) {
        int[] faces = new int[]{
                3, 4, 2, 5, 3, 4, 2, 5, 3, 4, 2, 5
        };

        Block side = this.getSide(BlockFace.fromIndex(faces[this.meta]));

        if (side.getId() != Block.WOOD && side.getDamage() != BlockWood.JUNGLE) {
            this.getLevel().useBreakOn(this);
            return Level.BLOCK_UPDATE_NORMAL;
        }
    } else if (type == Level.BLOCK_UPDATE_RANDOM) {
        if (new Random().nextInt(2) == 1) {
            if (this.meta / 4 < 2) {
                BlockCocoa block = (BlockCocoa) this.clone();
                block.meta += 4;
                BlockGrowEvent ev = new BlockGrowEvent(this, block);
                Server.getInstance().getPluginManager().callEvent(ev);

                if (!ev.isCancelled()) {
                    this.getLevel().setBlock(this, ev.getNewState(), true, true);
                } else {
                    return Level.BLOCK_UPDATE_RANDOM;
                }
            }
        } else {
            return Level.BLOCK_UPDATE_RANDOM;
        }
    }

    return 0;
}
 
Example #25
Source File: BlockLever.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    if (target.isNormalBlock()) {
        this.setDamage(LeverOrientation.forFacings(face, player.getHorizontalFacing()).getMetadata());
        this.getLevel().setBlock(block, this, true, true);
        return true;
    }
    return false;
}
 
Example #26
Source File: BlockHayBale.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    int[] faces = new int[]{
            0,
            0,
            0b1000,
            0b1000,
            0b0100,
            0b0100,
    };
    this.setDamage((this.getDamage() & 0x03) | faces[face.getIndex()]);
    this.getLevel().setBlock(block, this, true, true);

    return true;
}
 
Example #27
Source File: BlockLever.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onActivate(Item item, Player player) {
    this.level.getServer().getPluginManager().callEvent(new BlockRedstoneEvent(this, isPowerOn() ? 15 : 0, isPowerOn() ? 0 : 15));
    this.setDamage(this.getDamage() ^ 0x08);

    this.getLevel().setBlock(this, this, false, true);
    this.getLevel().addSound(this, Sound.RANDOM_CLICK); //TODO: coorect pitcj

    LeverOrientation orientation = LeverOrientation.byMetadata(this.isPowerOn() ? this.getDamage() ^ 0x08 : this.getDamage());
    BlockFace face = orientation.getFacing();
    //this.level.updateAroundRedstone(this, null);
    this.level.updateAroundRedstone(this.getLocation().getSide(face.getOpposite()), isPowerOn() ? face : null);
    return true;
}
 
Example #28
Source File: BlockVine.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    if (target.isSolid() && face.getHorizontalIndex() != -1) {
        this.setDamage(getMetaFromFace(face.getOpposite()));
        this.getLevel().setBlock(block, this, true, true);
        return true;
    }

    return false;
}
 
Example #29
Source File: Rail.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public static Orientation curved(BlockFace f1, BlockFace f2) {
    for (Orientation o : new Orientation[]{CURVED_SOUTH_EAST, CURVED_SOUTH_WEST, CURVED_NORTH_WEST, CURVED_NORTH_EAST}) {
        if (o.connectingDirections.contains(f1) && o.connectingDirections.contains(f2)) {
            return o;
        }
    }
    return CURVED_SOUTH_EAST;
}
 
Example #30
Source File: BlockRedstoneWire.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
private int getIndirectPower(Vector3 pos, BlockFace face) {
    Block block = this.level.getBlock(pos);
    if (block.getId() == Block.REDSTONE_WIRE) {
        return 0;
    }
    return block.isNormalBlock() ? getStrongPower(pos.getSide(face), face) : block.getWeakPower(face);
}