Search in sources :

Example 51 with BlockState

use of org.bukkit.block.BlockState in project Glowstone by GlowstoneMC.

the class SugarCane method generate.

public void generate(World world, Random random, int x, int y, int z) {
    if (world.getBlockAt(x, y, z).isEmpty()) {
        Block block = world.getBlockAt(x, y, z).getRelative(BlockFace.DOWN);
        boolean adjacentWater = false;
        for (BlockFace face : FACES) {
            // needs a directly adjacent water block
            if (block.getRelative(face).getType() == Material.STATIONARY_WATER || block.getRelative(face).getType() == Material.WATER) {
                adjacentWater = true;
                break;
            }
        }
        if (adjacentWater) {
            for (int n = 0; n <= random.nextInt(random.nextInt(3) + 1) + 1; n++) {
                block = world.getBlockAt(x, y + n, z).getRelative(BlockFace.DOWN);
                if (block.getType() == Material.SUGAR_CANE_BLOCK || block.getType() == Material.GRASS || block.getType() == Material.DIRT && block.getState().getData() instanceof Dirt && ((Dirt) block.getState().getData()).getType() == DirtType.NORMAL || block.getType() == Material.SAND) {
                    Block caneBlock = block.getRelative(BlockFace.UP);
                    if (!caneBlock.isEmpty() && !caneBlock.getRelative(BlockFace.UP).isEmpty()) {
                        return;
                    }
                    BlockState state = caneBlock.getState();
                    state.setType(Material.SUGAR_CANE_BLOCK);
                    state.setData(new MaterialData(Material.SUGAR_CANE_BLOCK));
                    state.update(true);
                }
            }
        }
    }
}
Also used : BlockState(org.bukkit.block.BlockState) BlockFace(org.bukkit.block.BlockFace) Dirt(org.bukkit.material.Dirt) Block(org.bukkit.block.Block) MaterialData(org.bukkit.material.MaterialData)

Example 52 with BlockState

use of org.bukkit.block.BlockState in project Glowstone by GlowstoneMC.

the class StructureBuilder method createRandomItemsContainer.

public boolean createRandomItemsContainer(Vector pos, Random random, RandomItemsContent content, DirectionalContainer container, int maxStacks) {
    Vector vec = translate(pos);
    if (boundingBox.isVectorInside(vec)) {
        BlockState state = world.getBlockAt(vec.getBlockX(), vec.getBlockY(), vec.getBlockZ()).getState();
        delegate.backupBlockState(state.getBlock());
        state.setType(container.getItemType());
        state.setData(container);
        state.update(true);
        return content.fillContainer(random, container, state, maxStacks);
    }
    return false;
}
Also used : BlockState(org.bukkit.block.BlockState) Vector(org.bukkit.util.Vector)

Example 53 with BlockState

use of org.bukkit.block.BlockState in project Glowstone by GlowstoneMC.

the class BlockSapling method generateTree.

private void generateTree(TreeType type, GlowBlock block, GlowPlayer player) {
    // get data but filters sapling age
    int data = block.getData() & 0x7;
    // replaces the sapling block(s)
    block.setType(Material.AIR);
    if (type == TreeType.JUNGLE || type == TreeType.MEGA_REDWOOD || type == TreeType.DARK_OAK) {
        block.getRelative(BlockFace.SOUTH).setType(Material.AIR);
        block.getRelative(BlockFace.EAST).setType(Material.AIR);
        block.getRelative(BlockFace.SOUTH_EAST).setType(Material.AIR);
    }
    // try to generate a tree
    Location loc = block.getLocation();
    BlockStateDelegate blockStateDelegate = new BlockStateDelegate();
    boolean canGrow = false;
    if (GlowTree.newInstance(type, random, loc, blockStateDelegate).generate()) {
        List<BlockState> blockStates = new ArrayList<>(blockStateDelegate.getBlockStates());
        StructureGrowEvent growEvent = new StructureGrowEvent(loc, type, player != null, player, blockStates);
        EventFactory.callEvent(growEvent);
        if (!growEvent.isCancelled()) {
            canGrow = true;
            for (BlockState state : blockStates) {
                state.update(true);
            }
        }
    }
    if (!canGrow) {
        // places the sapling block(s) back if the tree was not generated
        // the sapling ages are overwritten but this is an expected
        // vanilla behavior
        block.setType(Material.SAPLING);
        block.setData((byte) data);
        if (type == TreeType.JUNGLE || type == TreeType.MEGA_REDWOOD || type == TreeType.DARK_OAK) {
            block.getRelative(BlockFace.SOUTH).setType(Material.SAPLING);
            block.getRelative(BlockFace.SOUTH).setData((byte) data);
            block.getRelative(BlockFace.EAST).setType(Material.SAPLING);
            block.getRelative(BlockFace.EAST).setData((byte) data);
            block.getRelative(BlockFace.SOUTH_EAST).setType(Material.SAPLING);
            block.getRelative(BlockFace.SOUTH_EAST).setData((byte) data);
        }
    }
}
Also used : BlockStateDelegate(net.glowstone.util.BlockStateDelegate) BlockState(org.bukkit.block.BlockState) ArrayList(java.util.ArrayList) StructureGrowEvent(org.bukkit.event.world.StructureGrowEvent) Location(org.bukkit.Location)

Example 54 with BlockState

use of org.bukkit.block.BlockState in project Glowstone by GlowstoneMC.

the class DeadBushDecorator method decorate.

@Override
public void decorate(World world, Random random, Chunk source) {
    int sourceX = (source.getX() << 4) + random.nextInt(16);
    int sourceZ = (source.getZ() << 4) + random.nextInt(16);
    int sourceY = random.nextInt(world.getHighestBlockYAt(sourceX, sourceZ) << 1);
    while ((world.getBlockAt(sourceX, sourceY, sourceZ).isEmpty() || world.getBlockAt(sourceX, sourceY, sourceZ).getType() == Material.LEAVES) && sourceY > 0) {
        sourceY--;
    }
    for (int i = 0; i < 4; i++) {
        int x = sourceX + random.nextInt(8) - random.nextInt(8);
        int z = sourceZ + random.nextInt(8) - random.nextInt(8);
        int y = sourceY + random.nextInt(4) - random.nextInt(4);
        if (world.getBlockAt(x, y, z).isEmpty()) {
            Block blockBelow = world.getBlockAt(x, y - 1, z);
            for (Material soil : SOIL_TYPES) {
                if (soil == blockBelow.getType()) {
                    BlockState state = world.getBlockAt(x, y, z).getState();
                    state.setType(Material.DEAD_BUSH);
                    state.setData(new LongGrass(GrassSpecies.DEAD));
                    state.update(true);
                    break;
                }
            }
        }
    }
}
Also used : BlockState(org.bukkit.block.BlockState) Block(org.bukkit.block.Block) Material(org.bukkit.Material) LongGrass(org.bukkit.material.LongGrass)

Example 55 with BlockState

use of org.bukkit.block.BlockState in project Glowstone by GlowstoneMC.

the class FlowingLiquidDecorator method decorate.

@Override
public void decorate(World world, Random random, Chunk source) {
    int sourceX = (source.getX() << 4) + random.nextInt(16);
    int sourceZ = (source.getZ() << 4) + random.nextInt(16);
    int sourceY = random.nextInt(random.nextInt(type == Material.LAVA ? random.nextInt(240) + 8 : 248) + 8);
    Block block = world.getBlockAt(sourceX, sourceY, sourceZ);
    if ((block.getType() == Material.STONE || block.getType() == Material.AIR) && block.getRelative(BlockFace.DOWN).getType() == Material.STONE && block.getRelative(BlockFace.UP).getType() == Material.STONE) {
        int stoneBlockCount = 0;
        for (BlockFace face : SIDES) {
            if (block.getRelative(face).getType() == Material.STONE) {
                stoneBlockCount++;
            }
        }
        if (stoneBlockCount == 3) {
            int airBlockCount = 0;
            for (BlockFace face : SIDES) {
                if (block.getRelative(face).isEmpty()) {
                    airBlockCount++;
                }
            }
            if (airBlockCount == 1) {
                BlockState state = block.getState();
                state.setType(type);
                state.update(true);
                new PulseTask((GlowBlock) state.getBlock(), true, 1, true).startPulseTask();
            }
        }
    }
}
Also used : GlowBlock(net.glowstone.block.GlowBlock) BlockState(org.bukkit.block.BlockState) BlockFace(org.bukkit.block.BlockFace) GlowBlock(net.glowstone.block.GlowBlock) Block(org.bukkit.block.Block) PulseTask(net.glowstone.scheduler.PulseTask)

Aggregations

BlockState (org.bukkit.block.BlockState)62 Block (org.bukkit.block.Block)27 EventHandler (org.bukkit.event.EventHandler)16 Location (org.bukkit.Location)12 MaterialData (org.bukkit.material.MaterialData)12 BlockFace (org.bukkit.block.BlockFace)8 Material (org.bukkit.Material)7 World (org.bukkit.World)6 Player (org.bukkit.entity.Player)6 ArrayList (java.util.ArrayList)5 ItemStack (org.bukkit.inventory.ItemStack)5 BlockStateChange (me.botsko.prism.events.BlockStateChange)4 GlowBlockState (net.glowstone.block.GlowBlockState)4 CreatureSpawner (org.bukkit.block.CreatureSpawner)4 Sign (org.bukkit.block.Sign)4 Vector (org.bukkit.util.Vector)4 GlowBlock (net.glowstone.block.GlowBlock)3 TreeType (org.bukkit.TreeType)3 InventoryHolder (org.bukkit.inventory.InventoryHolder)3 HashMap (java.util.HashMap)2