Java Code Examples for cn.nukkit.math.BlockFace#getIndex()

The following examples show how to use cn.nukkit.math.BlockFace#getIndex() . 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: 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 2
Source File: BlockVine.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.isSolid()) {
        int[] faces = new int[]{
                0,
                0,
                1,
                4,
                8,
                2
        };
        this.meta = faces[face.getIndex()];
        this.getLevel().setBlock(block, this, true, true);
        return true;
    }
    return false;
}
 
Example 3
Source File: BlockCocoa.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.getId() == Block.WOOD && target.getDamage() == BlockWood.JUNGLE) {
        if (face != BlockFace.DOWN && face != BlockFace.UP) {
            int[] faces = new int[]{
                    0,
                    0,
                    0,
                    2,
                    3,
                    1,
            };

            this.meta = faces[face.getIndex()];
            this.level.setBlock(block, this, true, true);
            return true;
        }
    }
    return false;
}
 
Example 4
Source File: BlockTorch.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 below = this.down();

    if (!target.isTransparent() && face != BlockFace.DOWN) {
        int[] faces = new int[]{
                0, //0, nerver used
                5, //1
                4, //2
                3, //3
                2, //4
                1, //5
        };
        this.meta = faces[face.getIndex()];
        this.getLevel().setBlock(block, this, true, true);

        return true;
    } else if (!below.isTransparent() || below instanceof BlockFence || below.getId() == COBBLE_WALL) {
        this.meta = 0;
        this.getLevel().setBlock(block, this, true, true);

        return true;
    }
    return false;
}
 
Example 5
Source File: BlockRedstoneTorchUnlit.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) {
    Block below = this.down();

    if (!target.isTransparent() && face != BlockFace.DOWN) {
        int[] faces = new int[]{
                0, //0, nerver used
                5, //1
                4, //2
                3, //3
                2, //4
                1, //5
        };
        this.meta = faces[face.getIndex()];
        this.getLevel().setBlock(block, this, true, true);
        //Redstone.active(this);

        return true;
    } else if (!below.isTransparent() || below instanceof BlockFence || below.getId() == COBBLE_WALL) {
        this.meta = 0;
        this.getLevel().setBlock(block, this, true, true);
        //Redstone.active(this);

        return true;
    }
    return false;
}
 
Example 6
Source File: BlockItemFrame.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() && face.getIndex() > 1 && !block.isSolid()) {
        switch (face) {
            case NORTH:
                this.setDamage(3);
                break;
            case SOUTH:
                this.setDamage(2);
                break;
            case WEST:
                this.setDamage(1);
                break;
            case EAST:
                this.setDamage(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(this, Sound.BLOCK_ITEMFRAME_PLACE);
        return true;
    }
    return false;
}
 
Example 7
Source File: BlockLadder.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()) {
        if (face.getIndex() >= 2 && face.getIndex() <= 5) {
            this.setDamage(face.getIndex());
            this.getLevel().setBlock(block, this, true, true);
            return true;
        }
    }
    return false;
}
 
Example 8
Source File: BlockSignPost.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 (face != BlockFace.DOWN) {
        CompoundTag nbt = new CompoundTag()
                .putString("id", BlockEntity.SIGN)
                .putInt("x", (int) block.x)
                .putInt("y", (int) block.y)
                .putInt("z", (int) block.z)
                .putString("Text1", "")
                .putString("Text2", "")
                .putString("Text3", "")
                .putString("Text4", "");

        if (face == BlockFace.UP) {
            meta = (int) Math.floor(((player.yaw + 180) * 16 / 360) + 0.5) & 0x0f;
            getLevel().setBlock(block, new BlockSignPost(meta), true);
        } else {
            meta = face.getIndex();
            getLevel().setBlock(block, new BlockWallSign(meta), true);
        }

        if (player != null) {
            nbt.putString("Creator", player.getUniqueId().toString());
        }

        if (item.hasCustomBlockData()) {
            for (Tag aTag : item.getCustomBlockData().getAllTags()) {
                nbt.put(aTag.getName(), aTag);
            }
        }

        new BlockEntitySign(getLevel().getChunk((int) block.x >> 4, (int) block.z >> 4), nbt);

        return true;
    }

    return false;
}
 
Example 9
Source File: BlockHopper.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) {
    BlockFace facing = face.getOpposite();

    if (facing == BlockFace.UP) {
        facing = BlockFace.DOWN;
    }

    this.meta = facing.getIndex();

    boolean powered = this.level.isBlockPowered(this);

    if (powered == this.isEnabled()) {
        this.setEnabled(!powered);
    }

    this.level.setBlock(this, this);

    CompoundTag nbt = new CompoundTag()
            .putList(new ListTag<>("Items"))
            .putString("id", BlockEntity.HOPPER)
            .putInt("x", (int) this.x)
            .putInt("y", (int) this.y)
            .putInt("z", (int) this.z);

    new BlockEntityHopper(this.level.getChunk(this.getFloorX() >> 4, this.getFloorZ() >> 4), nbt);
    return true;
}
 
Example 10
Source File: BlockBannerStanding.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 (face == BlockFace.DOWN) {
		return false;
	}

	CompoundTag nbt = new CompoundTag()
			.putString("id", BlockEntity.BANNER)
            .putInt("x", (int) block.x)
            .putInt("y", (int) block.y)
            .putInt("z", (int) block.z)
            .putInt("Base", item.getDamage());

	if (item.hasCompoundTag()) {
		CompoundTag tag = item.getNamedTag();
		if (tag.contains("Patterns")) {
			nbt.putList(tag.getList("Patterns"));
		}
	}

    if (face == BlockFace.UP) {
        meta = (int) Math.floor(((player.yaw + 180) * 16 / 360) + 0.5) & 0x0f;
        getLevel().setBlock(block, new BlockBannerStanding(meta), true);
    } else {
        meta = face.getIndex();
        getLevel().setBlock(block, new BlockBannerWall(meta), true);
    }

    new BlockEntityBanner(getLevel().getChunk((int) block.x >> 4, (int) block.z >> 4), nbt);
    return true;
}
 
Example 11
Source File: BlockButton.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()) {
        return false;
    }

    this.meta = face.getIndex();
    this.level.setBlock(block, this, true, true);
    return true;
}
 
Example 12
Source File: BlockSkull.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) {
    switch (face) {
        case NORTH:
        case SOUTH:
        case EAST:
        case WEST:
        case UP:
            this.meta = face.getIndex();
            break;
        case DOWN:
        default:
            return false;
    }
    this.getLevel().setBlock(block, this, true, true);

    CompoundTag nbt = new CompoundTag()
            .putString("id", BlockEntity.SKULL)
            .putByte("SkullType", item.getDamage())
            .putInt("x", block.getFloorX())
            .putInt("y", block.getFloorY())
            .putInt("z", block.getFloorZ())
            .putByte("Rot", (int) Math.floor((player.yaw * 16 / 360) + 0.5) & 0x0f);
    if (item.hasCustomBlockData()) {
        for (Tag aTag : item.getCustomBlockData().getAllTags()) {
            nbt.put(aTag.getName(), aTag);
        }
    }

    new BlockEntitySkull(getLevel().getChunk((int) block.x >> 4, (int) block.z >> 4), nbt);

    return true;
}
 
Example 13
Source File: BlockEndRod.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) {
    int[] faces = {0, 1, 3, 2, 5, 4};
    this.meta = faces[player != null ? face.getIndex() : 0];
    this.getLevel().setBlock(block, this, true, true);

    return true;
}
 
Example 14
Source File: BlockLadder.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()) {
        if (face.getIndex() >= 2 && face.getIndex() <= 5) {
            this.meta = face.getIndex();
            this.getLevel().setBlock(block, this, true, true);
            return true;
        }
    }
    return false;
}
 
Example 15
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 16
Source File: ItemPainting.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean onActivate(Level level, Player player, Block block, Block target, BlockFace face, double fx, double fy, double fz) {
    FullChunk chunk = level.getChunk((int) block.getX() >> 4, (int) block.getZ() >> 4);

    if (chunk == null) {
        return false;
    }

    if (!target.isTransparent() && face.getIndex() > 1 && !block.isSolid()) {
        int[] direction = {2, 0, 1, 3};
        int[] right = {4, 5, 3, 2};

        List<EntityPainting.Motive> validMotives = new ArrayList<>();
        for (EntityPainting.Motive motive : EntityPainting.motives) {
            boolean valid = true;
            for (int x = 0; x < motive.width && valid; x++) {
                for (int z = 0; z < motive.height && valid; z++) {
                    if (target.getSide(BlockFace.fromIndex(right[face.getIndex() - 2]), x).isTransparent() ||
                            target.up(z).isTransparent() ||
                            block.getSide(BlockFace.fromIndex(right[face.getIndex() - 2]), x).isSolid() ||
                            block.up(z).isSolid()) {
                        valid = false;
                    }
                }
            }

            if (valid) {
                validMotives.add(motive);
            }
        }

        CompoundTag nbt = new CompoundTag()
                .putByte("Direction", direction[face.getIndex() - 2])
                .putString("Motive", validMotives.get(ThreadLocalRandom.current().nextInt(validMotives.size())).title)
                .putList(new ListTag<DoubleTag>("Pos")
                        .add(new DoubleTag("0", target.x))
                        .add(new DoubleTag("1", target.y))
                        .add(new DoubleTag("2", target.z)))
                .putList(new ListTag<DoubleTag>("Motion")
                        .add(new DoubleTag("0", 0))
                        .add(new DoubleTag("1", 0))
                        .add(new DoubleTag("2", 0)))
                .putList(new ListTag<FloatTag>("Rotation")
                        .add(new FloatTag("0", direction[face.getIndex() - 2] * 90))
                        .add(new FloatTag("1", 0)));

        EntityPainting entity = new EntityPainting(chunk, nbt);

        if (player.isSurvival()) {
            Item item = player.getInventory().getItemInHand();
            item.setCount(item.getCount() - 1);
            player.getInventory().setItemInHand(item);
        }
        entity.spawnToAll();

        return true;
    }

    return false;
}
 
Example 17
Source File: PunchBlockParticle.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public PunchBlockParticle(Vector3 pos, int blockId, int blockDamage, BlockFace face) {
    super(pos.x, pos.y, pos.z);
    this.data = GlobalBlockPalette.getOrCreateRuntimeId(blockId, blockDamage) | (face.getIndex() << 24);
}
 
Example 18
Source File: ItemPainting.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean onActivate(Level level, Player player, Block block, Block target, BlockFace face, double fx, double fy, double fz) {
    FullChunk chunk = level.getChunk((int) block.getX() >> 4, (int) block.getZ() >> 4);

    if (chunk == null) {
        return false;
    }

    if (!target.isTransparent() && face.getIndex() > 1 && !block.isSolid()) {
        int[] direction = {2, 0, 1, 3};
        int[] right = {4, 5, 3, 2};

        List<EntityPainting.Motive> validMotives = new ArrayList<>();
        for (EntityPainting.Motive motive : EntityPainting.motives) {
            boolean valid = true;
            for (int x = 0; x < motive.width && valid; x++) {
                for (int z = 0; z < motive.height && valid; z++) {
                    if (target.getSide(BlockFace.fromIndex(right[face.getIndex() - 2]), x).isTransparent() ||
                            target.up(z).isTransparent() ||
                            block.getSide(BlockFace.fromIndex(right[face.getIndex() - 2]), x).isSolid() ||
                            block.up(z).isSolid()) {
                        valid = false;
                    }
                }
            }

            if (valid) {
                validMotives.add(motive);
            }
        }

        CompoundTag nbt = new CompoundTag()
                .putByte("Direction", direction[face.getIndex() - 2])
                .putString("Motive", validMotives.get(ThreadLocalRandom.current().nextInt(validMotives.size())).title)
                .putList(new ListTag<DoubleTag>("Pos")
                        .add(new DoubleTag("0", target.x))
                        .add(new DoubleTag("1", target.y))
                        .add(new DoubleTag("2", target.z)))
                .putList(new ListTag<DoubleTag>("Motion")
                        .add(new DoubleTag("0", 0))
                        .add(new DoubleTag("1", 0))
                        .add(new DoubleTag("2", 0)))
                .putList(new ListTag<FloatTag>("Rotation")
                        .add(new FloatTag("0", direction[face.getIndex() - 2] * 90))
                        .add(new FloatTag("1", 0)));

        EntityPainting entity = new EntityPainting(chunk, nbt);

        if (player.isSurvival()) {
            Item item = player.getInventory().getItemInHand();
            item.setCount(item.getCount() - 1);
            player.getInventory().setItemInHand(item);
        }
        entity.spawnToAll();

        return true;
    }

    return false;
}
 
Example 19
Source File: PunchBlockParticle.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public PunchBlockParticle(Vector3 pos, int blockId, int blockDamage, BlockFace face) {
    super(pos.x, pos.y, pos.z);
    this.data = blockId | (blockDamage << 8) | (face.getIndex() << 16);
}
 
Example 20
Source File: PunchBlockParticle.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
public PunchBlockParticle(Vector3 pos, int blockId, int blockDamage, BlockFace face) {
    super(pos.x, pos.y, pos.z);
    this.data = blockId | (blockDamage << 8) | (face.getIndex() << 16);
}