Skip to content

Commit

Permalink
Update the switch command
Browse files Browse the repository at this point in the history
- allow below-a-second durations
- report the switch info
- don't try to guess whether a switch-back is reasonable here, just
switch back when a duration is given.
  • Loading branch information
mcmonkey4eva committed Dec 10, 2014
1 parent b7b4cc2 commit 0ec177a
Showing 1 changed file with 7 additions and 26 deletions.
Expand Up @@ -36,26 +36,10 @@

public class SwitchCommand extends AbstractCommand {

/* SWITCH [LOCATION:x,y,z,world] (STATE:ON|OFF|TOGGLE) (DURATION:#) */

/*
* Arguments: [] - Required, () - Optional
* [LOCATION:x,y,z,world] specifies location of a switch, lever, or pressure plate.
* (STATE:ON|OFF|TOGGLE) can be used on locations with switches. Default: TOGGLE
* (DURATION:#) Reverts to the previous head position after # amount of seconds.
*
* Example Usage:
* SWITCH LOCATION:<BOOKMARK:Lever_1> STATE:ON
* SWITCH LOCATION:99,64,125,world 'DURATION:15'
* SWITCH LOCATION:<ANCHOR:button_location>
*
*/

private enum SwitchState { ON, OFF, TOGGLE }

private Map<Location, Integer> taskMap = new ConcurrentHashMap<Location, Integer>(8, 0.9f, 1);


@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
Expand Down Expand Up @@ -85,12 +69,15 @@ else if (!scriptEntry.hasObject("state") &&
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
final dLocation interactLocation = (dLocation)scriptEntry.getObject("location");
int duration = ((Duration)scriptEntry.getObject("duration")).getSecondsAsInt();
long duration = ((Duration)scriptEntry.getObject("duration")).getTicks();
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;
dB.report(scriptEntry, getName(), interactLocation.debug()
+ aH.debugObj("duration", duration + "t")
+ aH.debugObj("switchstate", switchState.name()));
switchBlock(se, interactLocation, switchState, player);

// If duration set, schedule a delayed task.
Expand All @@ -103,15 +90,9 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
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));
switchBlock(se, interactLocation, SwitchState.TOGGLE, player);
}
}, duration));
}

}
Expand Down

0 comments on commit 0ec177a

Please sign in to comment.