scriptEntry.defaultObject("switchstate", new Element("TOGGLE"));
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
final dLocation interactLocation = (dLocation)scriptEntry.getObject("location");
int duration = ((Duration)scriptEntry.getObject("duration")).getSecondsAsInt();
final SwitchState switchState = SwitchState.valueOf(scriptEntry.getElement("switchstate").asString());
final Player player = ((BukkitScriptEntryData)scriptEntry.entryData).hasPlayer() ? ((BukkitScriptEntryData)scriptEntry.entryData).getPlayer().getPlayerEntity(): null;
// Switch the Block
final ScriptEntry se = scriptEntry;
switchBlock(se, interactLocation, switchState, player);
// If duration set, schedule a delayed task.
if (duration > 0) {
// If this block already had a delayed task, cancel it.
if (taskMap.containsKey(interactLocation))
try { DenizenAPI.getCurrentInstance().getServer().getScheduler().cancelTask(taskMap.get(interactLocation)); } catch (Exception e) { }
dB.log("Setting delayed task 'SWITCH' for " + interactLocation.identify());
// Store new delayed task ID, for checking against, then schedule new delayed task.
taskMap.put(interactLocation, DenizenAPI.getCurrentInstance().getServer().getScheduler().scheduleSyncDelayedTask(DenizenAPI.getCurrentInstance(),
new Runnable() {
public void run() {
// Check to see if the state of the block is what is expected. If switched during
// the duration, the switchback is cancelled.
if (switchState == SwitchState.OFF && !((interactLocation.getBlock().getData() & 0x8) > 0))
switchBlock(se, interactLocation, SwitchState.ON, player);
else if (switchState == SwitchState.ON && ((interactLocation.getBlock().getData() & 0x8) > 0))
switchBlock(se, interactLocation, SwitchState.OFF, player);
else if (switchState == SwitchState.TOGGLE) switchBlock(se, interactLocation, SwitchState.TOGGLE, player);
}
}, duration * 20));
}