Search in sources :

Example 1 with MaterialSwitchable

use of com.denizenscript.denizen.objects.properties.material.MaterialSwitchable in project Denizen-For-Bukkit by DenizenScript.

the class SwitchCommand method switchBlock.

// Break off this portion of the code from execute() so it can be used in both execute and the delayed runnable
public void switchBlock(ScriptEntry scriptEntry, Location interactLocation, SwitchState switchState, boolean physics) {
    Block block = interactLocation.getBlock();
    BlockData data1 = block.getBlockData();
    MaterialTag materialTag = new MaterialTag(data1);
    MaterialSwitchable switchable = MaterialSwitchable.getFrom(materialTag);
    if (switchable == null) {
        Debug.echoError("Cannot switch block of type '" + materialTag.getMaterial().name() + "'");
        return;
    }
    if (materialTag.getMaterial() == Material.BELL) {
        NMSHandler.getBlockHelper().ringBell((Bell) block.getState());
        return;
    }
    boolean currentState = switchable.getState();
    if ((switchState.equals(SwitchState.ON) && !currentState) || (switchState.equals(SwitchState.OFF) && currentState) || switchState.equals(SwitchState.TOGGLE)) {
        switchable.setState(!currentState);
        if (physics) {
            block.setBlockData(switchable.material.getModernData());
        } else {
            ModifyBlockCommand.setBlock(block.getLocation(), materialTag, false, null);
        }
        if (data1 instanceof Bisected && !(data1 instanceof TrapDoor)) {
            // TrapDoor implements Bisected, but is not actually bisected???
            Location other = interactLocation.clone();
            if (((Bisected) data1).getHalf() == Bisected.Half.TOP) {
                other = other.add(0, -1, 0);
            } else {
                other = other.add(0, 1, 0);
            }
            BlockData data2 = other.getBlock().getBlockData();
            if (data2.getMaterial() == data1.getMaterial()) {
                MaterialSwitchable switchable2 = MaterialSwitchable.getFrom(new MaterialTag(data2));
                switchable2.setState(!currentState);
                other.getBlock().setBlockData(switchable2.material.getModernData());
                if (physics) {
                    AdjustBlockCommand.applyPhysicsAt(other);
                }
            }
        }
        if (physics) {
            AdjustBlockCommand.applyPhysicsAt(interactLocation);
        }
        if (scriptEntry.dbCallShouldDebug()) {
            Debug.echoDebug(scriptEntry, "Switched " + block.getType().toString() + "! Current state now: " + (switchState(block) ? "ON" : "OFF"));
        }
    }
}
Also used : MaterialTag(com.denizenscript.denizen.objects.MaterialTag) TrapDoor(org.bukkit.block.data.type.TrapDoor) Block(org.bukkit.block.Block) BlockData(org.bukkit.block.data.BlockData) MaterialSwitchable(com.denizenscript.denizen.objects.properties.material.MaterialSwitchable) Bisected(org.bukkit.block.data.Bisected) Location(org.bukkit.Location)

Aggregations

MaterialTag (com.denizenscript.denizen.objects.MaterialTag)1 MaterialSwitchable (com.denizenscript.denizen.objects.properties.material.MaterialSwitchable)1 Location (org.bukkit.Location)1 Block (org.bukkit.block.Block)1 Bisected (org.bukkit.block.data.Bisected)1 BlockData (org.bukkit.block.data.BlockData)1 TrapDoor (org.bukkit.block.data.type.TrapDoor)1