Search in sources :

Example 1 with LeavesDecayEvent

use of org.bukkit.event.block.LeavesDecayEvent in project Glowstone by GlowstoneMC.

the class BlockLeaves method updateBlock.

@Override
public void updateBlock(GlowBlock block) {
    GlowBlockState state = block.getState();
    if ((state.getRawData() & 0x08) != 0 && (state.getRawData() & 0x04) == 0) {
        // check decay is on and decay is on
        GlowWorld world = block.getWorld();
        // build a 9x9x9 box to map neighboring blocks
        for (int x = 0; x < 9; x++) {
            for (int z = 0; z < 9; z++) {
                for (int y = 0; y < 9; y++) {
                    GlowBlock b = world.getBlockAt(block.getLocation().add(x - 4, y - 4, z - 4));
                    byte val = 127;
                    if (b.getType() == Material.LOG || b.getType() == Material.LOG_2) {
                        val = 0;
                    } else if (b.getType() == Material.LEAVES || b.getType() == Material.LEAVES_2) {
                        val = -1;
                    }
                    setBlockInMap(val, x, y, z);
                }
            }
        }
        // to another connected leaves block will decay
        for (int i = 0; i < 4; i++) {
            for (int x = 0; x < 9; x++) {
                for (int z = 0; z < 9; z++) {
                    for (int y = 0; y < 9; y++) {
                        if (getBlockInMap(x, y, z) == i) {
                            if (getBlockInMap(x - 1, y, z) == -1) {
                                setBlockInMap((byte) (i + 1), x - 1, y, z);
                            }
                            if (getBlockInMap(x, y - 1, z) == -1) {
                                setBlockInMap((byte) (i + 1), x, y - 1, z);
                            }
                            if (getBlockInMap(x, y, z - 1) == -1) {
                                setBlockInMap((byte) (i + 1), x, y, z - 1);
                            }
                            if (getBlockInMap(x + 1, y, z) == -1) {
                                setBlockInMap((byte) (i + 1), x + 1, y, z);
                            }
                            if (getBlockInMap(x, y + 1, z) == -1) {
                                setBlockInMap((byte) (i + 1), x, y + 1, z);
                            }
                            if (getBlockInMap(x, y, z + 1) == -1) {
                                setBlockInMap((byte) (i + 1), x, y, z + 1);
                            }
                        }
                    }
                }
            }
        }
        if (getBlockInMap(4, 4, 4) < 0) {
            // leaf decay
            LeavesDecayEvent decayEvent = new LeavesDecayEvent(block);
            EventFactory.callEvent(decayEvent);
            if (!decayEvent.isCancelled()) {
                block.breakNaturally();
            }
        } else {
            // cancel decay check on this leaves block
            state.setRawData((byte) (state.getRawData() & -0x09));
            state.update(true);
        }
    }
}
Also used : GlowBlock(net.glowstone.block.GlowBlock) GlowBlockState(net.glowstone.block.GlowBlockState) GlowWorld(net.glowstone.GlowWorld) LeavesDecayEvent(org.bukkit.event.block.LeavesDecayEvent)

Aggregations

GlowWorld (net.glowstone.GlowWorld)1 GlowBlock (net.glowstone.block.GlowBlock)1 GlowBlockState (net.glowstone.block.GlowBlockState)1 LeavesDecayEvent (org.bukkit.event.block.LeavesDecayEvent)1