Search in sources :

Example 1 with BlockRedstoneEvent

use of org.bukkit.event.block.BlockRedstoneEvent in project BKCommonLib by bergerhealer.

the class BlockUtil method setLever.

/**
 * Sets the toggled state of a single lever<br>
 * <b>No Lever type check is performed</b>
 *
 * @param lever block
 * @param down state to set to
 */
@SuppressWarnings("deprecation")
public static void setLever(org.bukkit.block.Block lever, boolean down) {
    int data = getRawData(lever);
    Lever newMaterialData = (Lever) getData(Material.LEVER, data);
    newMaterialData.setPowered(down);
    if (getRawData(newMaterialData) != data) {
        // CraftBukkit start - Redstone event for lever
        int old = !down ? 1 : 0;
        int current = down ? 1 : 0;
        BlockRedstoneEvent eventRedstone = new BlockRedstoneEvent(lever, old, current);
        CommonUtil.callEvent(eventRedstone);
        if ((eventRedstone.getNewCurrent() > 0) != down) {
            return;
        }
        // CraftBukkit end
        setData(lever, newMaterialData, true);
        applyPhysics(getAttachedBlock(lever), Material.LEVER);
    }
}
Also used : Lever(org.bukkit.material.Lever) BlockRedstoneEvent(org.bukkit.event.block.BlockRedstoneEvent)

Example 2 with BlockRedstoneEvent

use of org.bukkit.event.block.BlockRedstoneEvent 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)

Example 3 with BlockRedstoneEvent

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

the class BlockRedstone method setFullyPowered.

/**
 * Sets a redstone dust block to the fully-powered state and, if it wasn't already in that
 * state, updates connected blocks so that power propagates.
 *
 * @param block the block to update
 */
protected static void setFullyPowered(GlowBlock block) {
    int newPower = 15;
    if (block.getData() != newPower) {
        BlockRedstoneEvent event = EventFactory.getInstance().callEvent(new BlockRedstoneEvent(block, block.getData(), newPower));
        newPower = event.getNewCurrent();
        block.setData((byte) newPower);
        updateConnected(block);
    }
}
Also used : BlockRedstoneEvent(org.bukkit.event.block.BlockRedstoneEvent)

Aggregations

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