Search in sources :

Example 21 with GlowWorld

use of net.glowstone.GlowWorld in project Glowstone by GlowstoneMC.

the class BlockMycel method updateBlock.

@Override
public void updateBlock(GlowBlock block) {
    GlowBlock blockAbove = block.getRelative(BlockFace.UP);
    if (blockAbove.getLightLevel() < 4 && blockAbove.getMaterialValues().getLightOpacity() > 2) {
        // mycel block turns into dirt block
        GlowBlockState state = block.getState();
        state.setType(Material.DIRT);
        BlockFadeEvent fadeEvent = new BlockFadeEvent(block, state);
        EventFactory.callEvent(fadeEvent);
        if (!fadeEvent.isCancelled()) {
            state.update(true);
        }
    } else if (blockAbove.getLightLevel() >= 9) {
        GlowWorld world = block.getWorld();
        int sourceX = block.getX();
        int sourceY = block.getY();
        int sourceZ = block.getZ();
        // mycel spread randomly around
        for (int i = 0; i < 4; i++) {
            int x = sourceX + random.nextInt(3) - 1;
            int z = sourceZ + random.nextInt(3) - 1;
            int y = sourceY + random.nextInt(5) - 3;
            GlowBlock targetBlock = world.getBlockAt(x, y, z);
            GlowBlock targetAbove = targetBlock.getRelative(BlockFace.UP);
            if (targetBlock.getType() == Material.DIRT && // only spread on normal dirt
            targetBlock.getData() == 0 && targetAbove.getMaterialValues().getLightOpacity() <= 2 && targetAbove.getLightLevel() >= 4) {
                GlowBlockState state = targetBlock.getState();
                state.setType(Material.MYCEL);
                state.setRawData((byte) 0);
                BlockSpreadEvent spreadEvent = new BlockSpreadEvent(targetBlock, block, state);
                EventFactory.callEvent(spreadEvent);
                if (!spreadEvent.isCancelled()) {
                    state.update(true);
                }
            }
        }
    }
}
Also used : GlowBlock(net.glowstone.block.GlowBlock) BlockFadeEvent(org.bukkit.event.block.BlockFadeEvent) BlockSpreadEvent(org.bukkit.event.block.BlockSpreadEvent) GlowBlockState(net.glowstone.block.GlowBlockState) GlowWorld(net.glowstone.GlowWorld)

Example 22 with GlowWorld

use of net.glowstone.GlowWorld 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)

Example 23 with GlowWorld

use of net.glowstone.GlowWorld in project Glowstone by GlowstoneMC.

the class BlockLeaves method blockDestroy.

@Override
public void blockDestroy(GlowPlayer player, GlowBlock block, BlockFace face) {
    // vanilla set decay check in a 3x3x3 neighboring when a leaves block is removed
    GlowWorld world = block.getWorld();
    for (int x = 0; x < 3; x++) {
        for (int z = 0; z < 3; z++) {
            for (int y = 0; y < 3; y++) {
                GlowBlock b = world.getBlockAt(block.getLocation().add(x - 1, y - 1, z - 1));
                if (b.getType() == Material.LEAVES || b.getType() == Material.LEAVES_2) {
                    GlowBlockState state = b.getState();
                    if ((state.getRawData() & 0x08) == 0 && (state.getRawData() & 0x04) == 0) {
                        // check decay is off and decay is on
                        // set decay check on for this leaves block
                        state.setRawData((byte) (state.getRawData() | 0x08));
                        state.update(true);
                    }
                }
            }
        }
    }
}
Also used : GlowBlock(net.glowstone.block.GlowBlock) GlowBlockState(net.glowstone.block.GlowBlockState) GlowWorld(net.glowstone.GlowWorld)

Aggregations

GlowWorld (net.glowstone.GlowWorld)23 GlowBlock (net.glowstone.block.GlowBlock)14 GlowBlockState (net.glowstone.block.GlowBlockState)10 GlowPlayer (net.glowstone.entity.GlowPlayer)3 Location (org.bukkit.Location)3 BlockSpreadEvent (org.bukkit.event.block.BlockSpreadEvent)3 Entry (java.util.Map.Entry)2 BlockType (net.glowstone.block.blocktype.BlockType)2 Material (org.bukkit.Material)2 BlockFace (org.bukkit.block.BlockFace)2 BlockFadeEvent (org.bukkit.event.block.BlockFadeEvent)2 PlayerInteractEvent (org.bukkit.event.player.PlayerInteractEvent)2 ItemStack (org.bukkit.inventory.ItemStack)2 MaterialData (org.bukkit.material.MaterialData)2 Vector (org.bukkit.util.Vector)2 Message (com.flowpowered.networking.Message)1 Arrays (java.util.Arrays)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 BlockContainer (net.glowstone.block.blocktype.BlockContainer)1 ItemTimedUsage (net.glowstone.block.itemtype.ItemTimedUsage)1