Skip to content

Commit

Permalink
switch no_physics
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed May 5, 2021
1 parent b26a466 commit 2acbc73
Showing 1 changed file with 25 additions and 15 deletions.
Expand Up @@ -26,16 +26,16 @@ public class SwitchCommand extends AbstractCommand {

public SwitchCommand() {
setName("switch");
setSyntax("switch [<location>|...] (state:[{toggle}/on/off]) (duration:<value>)");
setRequiredArguments(1, 3);
setSyntax("switch [<location>|...] (state:[{toggle}/on/off]) (duration:<value>) (no_physics)");
setRequiredArguments(1, 4);
isProcedural = false;
}

// <--[command]
// @Name Switch
// @Syntax switch [<location>|...] (state:[{toggle}/on/off]) (duration:<value>)
// @Syntax switch [<location>|...] (state:[{toggle}/on/off]) (duration:<value>) (no_physics)
// @Required 1
// @Maximum 3
// @Maximum 4
// @Short Switches state of the block.
// @Synonyms Toggle,Lever,Activate,Power,Redstone
// @Group world
Expand All @@ -58,6 +58,8 @@ public SwitchCommand() {
// This will generally (but not always) function equivalently to a user right-clicking the block
// (so it will open and close doors, flip levers on and off, press and depress buttons, ...).
//
// Optionally specify 'no_physics' to not apply a physics update after switching.
//
// @Tags
// <LocationTag.switched>
// <MaterialTag.switched>
Expand Down Expand Up @@ -90,7 +92,11 @@ public void addCustomTabCompletions(String arg, Consumer<String> addOne) {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!scriptEntry.hasObject("locations") &&
if (!scriptEntry.hasObject("no_physics") &&
arg.matches("no_physics")) {
scriptEntry.addObject("no_physics", new ElementTag(true));
}
else if (!scriptEntry.hasObject("locations") &&
arg.matchesArgumentList(LocationTag.class)) {
scriptEntry.addObject("locations", arg.asType(ListTag.class));
}
Expand All @@ -116,18 +122,18 @@ else if (!scriptEntry.hasObject("state") &&
@Override
public void execute(final ScriptEntry scriptEntry) {
final ListTag interactLocations = scriptEntry.getObjectTag("locations");
long duration = ((DurationTag) scriptEntry.getObject("duration")).getTicks();
DurationTag duration = scriptEntry.getObjectTag("duration");
final SwitchState switchState = SwitchState.valueOf(scriptEntry.getElement("switchstate").asString());
ElementTag noPhysics = scriptEntry.getElement("no_physics");
// Switch the Block
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), interactLocations.debug()
+ ArgumentHelper.debugObj("duration", duration + "t")
+ ArgumentHelper.debugObj("switchstate", switchState.name()));
Debug.report(scriptEntry, getName(), interactLocations, duration, noPhysics, ArgumentHelper.debugObj("switchstate", switchState.name()));
}
final boolean physics = noPhysics == null || !noPhysics.asBoolean();
for (final LocationTag interactLocation : interactLocations.filter(LocationTag.class, scriptEntry)) {
switchBlock(scriptEntry, interactLocation, switchState);
switchBlock(scriptEntry, interactLocation, switchState, physics);
// If duration set, schedule a delayed task.
if (duration > 0) {
if (duration.getTicks() > 0) {
// If this block already had a delayed task, cancel it.
if (taskMap.containsKey(interactLocation)) {
try {
Expand All @@ -138,7 +144,7 @@ public void execute(final ScriptEntry scriptEntry) {
}
Debug.echoDebug(scriptEntry, "Setting delayed task 'SWITCH' for " + interactLocation.identify());
// Store new delayed task ID, for checking against, then schedule new delayed task.
taskMap.put(interactLocation, Bukkit.getScheduler().scheduleSyncDelayedTask(Denizen.getInstance(), () -> switchBlock(scriptEntry, interactLocation, SwitchState.TOGGLE), duration));
taskMap.put(interactLocation, Bukkit.getScheduler().scheduleSyncDelayedTask(Denizen.getInstance(), () -> switchBlock(scriptEntry, interactLocation, SwitchState.TOGGLE, physics), duration.getTicks()));
}
}
}
Expand All @@ -152,7 +158,7 @@ public static boolean switchState(Block b) {
}

// 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) {
public void switchBlock(ScriptEntry scriptEntry, Location interactLocation, SwitchState switchState, boolean physics) {
MaterialSwitchable switchable = MaterialSwitchable.getFrom(new MaterialTag(interactLocation.getBlock().getBlockData()));
if (switchable == null) {
return;
Expand All @@ -172,9 +178,13 @@ public void switchBlock(ScriptEntry scriptEntry, Location interactLocation, Swit
MaterialSwitchable switchable2 = MaterialSwitchable.getFrom(new MaterialTag(other.getBlock().getBlockData()));
switchable2.setState(!currentState);
other.getBlock().setBlockData(switchable2.material.getModernData());
AdjustBlockCommand.applyPhysicsAt(other);
if (physics) {
AdjustBlockCommand.applyPhysicsAt(other);
}
}
if (physics) {
AdjustBlockCommand.applyPhysicsAt(interactLocation);
}
AdjustBlockCommand.applyPhysicsAt(interactLocation);
Debug.echoDebug(scriptEntry, "Switched " + interactLocation.getBlock().getType().toString() + "! Current state now: " + (switchState(interactLocation.getBlock()) ? "ON" : "OFF"));
}
}
Expand Down

0 comments on commit 2acbc73

Please sign in to comment.