Search in sources :

Example 1 with PulseTask

use of net.glowstone.scheduler.PulseTask in project Glowstone by GlowstoneMC.

the class BlockObserver method onNearBlockChanged.

@Override
public void onNearBlockChanged(GlowBlock block, BlockFace face, GlowBlock changedBlock, Material oldType, byte oldData, Material newType, byte newData) {
    byte data = (byte) (block.getData() & ~(1 << POWERED_MASK));
    if (data != block.getData()) {
        return;
    }
    BlockFace direction = getFace(data);
    if (face != direction) {
        return;
    }
    block.setData((byte) (block.getData() | (1 << POWERED_MASK)));
    block.getWorld().requestPulse(block);
    new PulseTask(block, true, 4, true).startPulseTask();
}
Also used : BlockFace(org.bukkit.block.BlockFace) PulseTask(net.glowstone.scheduler.PulseTask)

Example 2 with PulseTask

use of net.glowstone.scheduler.PulseTask in project Glowstone by GlowstoneMC.

the class LavaDecorator 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 = flowing ? 4 + random.nextInt(120) : 10 + random.nextInt(108);
    Block block = world.getBlockAt(sourceX, sourceY, sourceZ);
    if ((block.getType() != Material.NETHERRACK && !block.isEmpty()) || block.getRelative(BlockFace.UP).getType() != Material.NETHERRACK) {
        return;
    }
    int netherrackBlockCount = 0;
    int airBlockCount = 0;
    for (BlockFace face : SIDES) {
        Block neighbor = block.getRelative(face);
        if (neighbor.getType() == Material.NETHERRACK) {
            netherrackBlockCount++;
        } else if (neighbor.isEmpty()) {
            airBlockCount++;
        }
    }
    if (netherrackBlockCount == 5 || flowing && airBlockCount == 1 && netherrackBlockCount == 4) {
        BlockState state = block.getState();
        state.setType(Material.LAVA);
        state.update(true);
        new PulseTask((GlowBlock) block, 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)

Example 3 with PulseTask

use of net.glowstone.scheduler.PulseTask in project Glowstone by GlowstoneMC.

the class BlockRedstone method updatePhysics.

@Override
public void updatePhysics(GlowBlock me) {
    super.updatePhysics(me);
    for (BlockFace face : ADJACENT) {
        GlowBlock target = me.getRelative(face);
        switch(target.getType()) {
            case LEVER:
                Lever lever = (Lever) target.getState().getData();
                if (lever.isPowered()) {
                    if (me.getData() != 15) {
                        me.setData((byte) 15);
                        extraUpdate(me);
                    }
                    return;
                }
                break;
            case STONE_BUTTON:
            case WOOD_BUTTON:
                Button button = (Button) target.getState().getData();
                if (button.isPowered()) {
                    if (me.getData() != 15) {
                        me.setData((byte) 15);
                        extraUpdate(me);
                    }
                    return;
                }
                break;
            case DIODE_BLOCK_ON:
                Diode diode = (Diode) target.getState().getData();
                if (face == diode.getFacing().getOppositeFace()) {
                    if (me.getData() != 15) {
                        me.setData((byte) 15);
                        extraUpdate(me);
                    }
                    return;
                }
                break;
            case REDSTONE_BLOCK:
            case REDSTONE_TORCH_ON:
                if (me.getData() != 15) {
                    me.setData((byte) 15);
                    extraUpdate(me);
                }
                return;
            case OBSERVER:
                boolean powered = BlockObserver.isPowered(target);
                BlockFace outputFace = BlockObserver.getFace(target).getOppositeFace();
                if (powered && target.getRelative(outputFace).getLocation().equals(me.getLocation())) {
                    if (me.getData() != 15) {
                        me.setData((byte) 15);
                        extraUpdate(me);
                    }
                    return;
                }
                break;
            default:
                if (target.getType().isSolid() && target.getRelative(BlockFace.DOWN).getType() == Material.REDSTONE_TORCH_ON) {
                    if (me.getData() != 15) {
                        me.setData((byte) 15);
                        extraUpdate(me);
                    }
                    return;
                }
                if (target.getType().isSolid()) {
                    for (BlockFace face2 : ADJACENT) {
                        GlowBlock target2 = target.getRelative(face2);
                        if (target2.getType() == Material.DIODE_BLOCK_ON && ((Diode) target2.getState().getData()).getFacing() == target2.getFace(target)) {
                            if (me.getData() != 15) {
                                me.setData((byte) 15);
                                extraUpdate(me);
                            }
                            return;
                        } else if (target2.getType() == Material.STONE_BUTTON || target2.getType() == Material.WOOD_BUTTON) {
                            Button button2 = (Button) target2.getState().getData();
                            if (button2.isPowered() && button2.getAttachedFace() == target2.getFace(target)) {
                                if (me.getData() != 15) {
                                    me.setData((byte) 15);
                                    extraUpdate(me);
                                }
                                return;
                            }
                        } else if (target2.getType() == Material.LEVER) {
                            Lever lever2 = (Lever) target2.getState().getData();
                            if (lever2.isPowered() && lever2.getAttachedFace() == target2.getFace(target)) {
                                if (me.getData() != 15) {
                                    me.setData((byte) 15);
                                    extraUpdate(me);
                                }
                                return;
                            }
                        }
                    }
                }
        }
    }
    byte power = 0;
    for (BlockFace face : calculateConnections(me)) {
        if (face == BlockFace.DOWN) {
            continue;
        }
        GlowBlock target = me.getRelative(face);
        if (target.getType() != Material.REDSTONE_WIRE) {
            if (!target.getType().isSolid()) {
                target = target.getRelative(BlockFace.DOWN);
            } else if (!me.getRelative(BlockFace.UP).getType().isSolid()) {
                target = target.getRelative(BlockFace.UP);
            }
            if (target.getType() != Material.REDSTONE_WIRE) {
                // There is no redstone wire here..
                continue;
            }
        }
        if (target.getData() > power) {
            power = (byte) (target.getData() - 1);
        }
    }
    if (power != me.getData()) {
        me.setData(power);
        extraUpdate(me);
        new PulseTask(me, true, 1, true).startPulseTask();
    }
}
Also used : GlowBlock(net.glowstone.block.GlowBlock) BlockFace(org.bukkit.block.BlockFace) PulseTask(net.glowstone.scheduler.PulseTask)

Example 4 with PulseTask

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

Example 5 with PulseTask

use of net.glowstone.scheduler.PulseTask in project Glowstone by GlowstoneMC.

the class BlockRedstone method updatePhysicsAfterEvent.

@Override
public void updatePhysicsAfterEvent(GlowBlock me) {
    super.updatePhysicsAfterEvent(me);
    for (BlockFace face : ADJACENT) {
        GlowBlock target = me.getRelative(face);
        switch(target.getType()) {
            case LEVER:
            case STONE_BUTTON:
            case OAK_BUTTON:
            case DARK_OAK_BUTTON:
            case ACACIA_BUTTON:
            case BIRCH_BUTTON:
            case JUNGLE_BUTTON:
            case SPRUCE_BUTTON:
            case REDSTONE_TORCH:
            case REDSTONE_WALL_TORCH:
                Redstone state = (Redstone) target.getState().getData();
                if (state.isPowered()) {
                    setFullyPowered(me);
                    return;
                }
                break;
            case REPEATER:
                Diode diode = (Diode) target.getState().getData();
                if (diode.isPowered() && face == diode.getFacing().getOppositeFace()) {
                    setFullyPowered(me);
                    return;
                }
                break;
            case REDSTONE_BLOCK:
                setFullyPowered(me);
                return;
            case OBSERVER:
                boolean powered = BlockObserver.isPowered(target);
                BlockFace outputFace = BlockObserver.getFace(target).getOppositeFace();
                if (powered && target.getRelative(outputFace).getLocation().equals(me.getLocation())) {
                    setFullyPowered(me);
                    return;
                }
                break;
            default:
                if (!target.getType().isSolid()) {
                    continue;
                }
                GlowBlock underTarget = target.getRelative(BlockFace.DOWN);
                if (underTarget.getType() == Material.REDSTONE_TORCH && ((Redstone) (underTarget.getState().getData())).isPowered()) {
                    setFullyPowered(me);
                    return;
                }
                for (BlockFace face2 : ADJACENT) {
                    GlowBlock target2 = target.getRelative(face2);
                    if (target2.getType() == Material.REPEATER) {
                        Repeater data = (Repeater) target2.getBlockData();
                        if (data.isPowered() && data.getFacing() == target2.getFace(target)) {
                            setFullyPowered(me);
                            return;
                        } else if (Tag.BUTTONS.isTagged(target2.getType())) {
                            Button button2 = (Button) target2.getState().getData();
                            if (button2.isPowered() && button2.getAttachedFace() == target2.getFace(target)) {
                                setFullyPowered(me);
                                return;
                            }
                        } else if (target2.getType() == Material.LEVER) {
                            Lever lever2 = (Lever) target2.getState().getData();
                            if (lever2.isPowered() && lever2.getAttachedFace() == target2.getFace(target)) {
                                setFullyPowered(me);
                                return;
                            }
                        }
                    } else if (Tag.BUTTONS.isTagged(target2.getType())) {
                        Button button2 = (Button) target2.getState().getData();
                        if (button2.isPowered() && button2.getAttachedFace() == target2.getFace(target)) {
                            setFullyPowered(me);
                            return;
                        }
                    } else if (target2.getType() == Material.LEVER) {
                        Lever lever2 = (Lever) target2.getState().getData();
                        if (lever2.isPowered() && lever2.getAttachedFace() == target2.getFace(target)) {
                            setFullyPowered(me);
                            return;
                        }
                    }
                }
        }
    }
    byte power = 0;
    for (BlockFace face : calculateConnections(me)) {
        if (face == BlockFace.DOWN) {
            continue;
        }
        GlowBlock target = me.getRelative(face);
        if (target.getType() != Material.REDSTONE_WIRE) {
            if (!target.getType().isSolid()) {
                target = target.getRelative(BlockFace.DOWN);
            } else if (!me.getRelative(BlockFace.UP).getType().isSolid()) {
                target = target.getRelative(BlockFace.UP);
            }
            if (target.getType() != Material.REDSTONE_WIRE) {
                // There is no redstone wire here..
                continue;
            }
        }
        if (target.getData() > power) {
            power = (byte) (target.getData() - 1);
        }
    }
    if (power != me.getData()) {
        BlockRedstoneEvent event = EventFactory.getInstance().callEvent(new BlockRedstoneEvent(me, me.getData(), power));
        power = (byte) event.getNewCurrent();
        me.setData(power);
        updateConnected(me);
        new PulseTask(me, true, 1, true).startPulseTask();
    }
}
Also used : Redstone(org.bukkit.material.Redstone) GlowBlock(net.glowstone.block.GlowBlock) Diode(org.bukkit.material.Diode) Lever(org.bukkit.material.Lever) Button(org.bukkit.material.Button) BlockFace(org.bukkit.block.BlockFace) Repeater(org.bukkit.block.data.type.Repeater) BlockRedstoneEvent(org.bukkit.event.block.BlockRedstoneEvent) PulseTask(net.glowstone.scheduler.PulseTask)

Aggregations

PulseTask (net.glowstone.scheduler.PulseTask)5 BlockFace (org.bukkit.block.BlockFace)5 GlowBlock (net.glowstone.block.GlowBlock)4 Block (org.bukkit.block.Block)2 BlockState (org.bukkit.block.BlockState)2 Repeater (org.bukkit.block.data.type.Repeater)1 BlockRedstoneEvent (org.bukkit.event.block.BlockRedstoneEvent)1 Button (org.bukkit.material.Button)1 Diode (org.bukkit.material.Diode)1 Lever (org.bukkit.material.Lever)1 Redstone (org.bukkit.material.Redstone)1