Search in sources :

Example 1 with StructureGrowEvent

use of org.bukkit.event.world.StructureGrowEvent in project Glowstone by GlowstoneMC.

the class BlockMushroom method grow.

@Override
public void grow(GlowPlayer player, GlowBlock block) {
    TreeType type;
    if (mushroomType == Material.BROWN_MUSHROOM) {
        type = TreeType.BROWN_MUSHROOM;
    } else if (mushroomType == Material.RED_MUSHROOM) {
        type = TreeType.RED_MUSHROOM;
    } else {
        return;
    }
    Location loc = block.getLocation();
    BlockStateDelegate blockStateDelegate = new BlockStateDelegate();
    if (GlowTree.newInstance(type, random, loc, blockStateDelegate).generate()) {
        List<BlockState> blockStates = new ArrayList<>(blockStateDelegate.getBlockStates());
        StructureGrowEvent growEvent = new StructureGrowEvent(loc, type, true, player, blockStates);
        EventFactory.callEvent(growEvent);
        if (!growEvent.isCancelled()) {
            for (BlockState state : blockStates) {
                state.update(true);
            }
        }
    }
}
Also used : TreeType(org.bukkit.TreeType) BlockStateDelegate(net.glowstone.util.BlockStateDelegate) BlockState(org.bukkit.block.BlockState) GlowBlockState(net.glowstone.block.GlowBlockState) ArrayList(java.util.ArrayList) StructureGrowEvent(org.bukkit.event.world.StructureGrowEvent) Location(org.bukkit.Location)

Example 2 with StructureGrowEvent

use of org.bukkit.event.world.StructureGrowEvent 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)

Aggregations

ArrayList (java.util.ArrayList)2 BlockStateDelegate (net.glowstone.util.BlockStateDelegate)2 Location (org.bukkit.Location)2 BlockState (org.bukkit.block.BlockState)2 StructureGrowEvent (org.bukkit.event.world.StructureGrowEvent)2 GlowBlockState (net.glowstone.block.GlowBlockState)1 TreeType (org.bukkit.TreeType)1