/
SwitchCommand.java
152 lines (120 loc) · 6.32 KB
/
SwitchCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package net.aufdemrand.denizen.scripts.commands.world;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.aH.ArgumentType;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;
import net.minecraft.server.v1_6_R2.Block;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_6_R2.CraftWorld;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* Switches a button or lever.
*
* @author Jeremy Schroeder, Mason Adkins, David Cernat
*/
public class SwitchCommand extends AbstractCommand {
@Override
public void onEnable() {
// Nothing to do here
}
/* 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);
SwitchState switchState;
Location interactLocation;
int duration = -1;
@Override
public void parseArgs(ScriptEntry theEntry) throws InvalidArgumentsException {
/* Initialize variables */
interactLocation = null;
duration = -1;
switchState = SwitchState.TOGGLE;
for (String arg : theEntry.getArguments()) {
if (aH.matchesDuration(arg)) {
duration = Integer.valueOf(arg.split(":")[1]);
dB.echoDebug(Messages.DEBUG_SET_DURATION, arg);
} else if (aH.matchesValueArg("STATE", arg, ArgumentType.Custom)) {
String state = aH.getStringFrom(arg).toUpperCase();
if (state.matches("ON|OPEN")) {
switchState = SwitchState.ON;
}
else if (state.matches("OFF|CLOSE")) {
switchState = SwitchState.OFF;
}
else if (state.matches("TOGGLE")) {
switchState = SwitchState.TOGGLE;
}
dB.echoDebug("...set STATE: " + switchState.toString());
} else if (aH.matchesLocation(arg)) {
interactLocation = aH.getLocationFrom(arg);
if (interactLocation != null) dB.echoDebug("...switch LOCATION now: '%s'", arg);
} else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg);
}
if (interactLocation == null) throw new InvalidArgumentsException(Messages.ERROR_MISSING_LOCATION);
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
// Switch the Block
switchBlock(interactLocation, switchState);
// 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 { denizen.getServer().getScheduler().cancelTask(taskMap.get(interactLocation)); } catch (Exception e) { }
dB.echoDebug(Messages.DEBUG_RUNNING_DELAYED_TASK, "SWITCH");
// Store new delayed task ID, for checking against, then schedule new delayed task.
taskMap.put(interactLocation, denizen.getServer().getScheduler().scheduleSyncDelayedTask(denizen,
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(interactLocation, SwitchState.ON);
else if (switchState == SwitchState.ON && ((interactLocation.getBlock().getData() & 0x8) > 0))
switchBlock(interactLocation, SwitchState.OFF);
else if (switchState == SwitchState.TOGGLE) switchBlock(interactLocation, SwitchState.TOGGLE);
}
}, duration * 20));
}
}
// Break off this portion of the code from execute() so it can be used in both execute and the delayed runnable
public void switchBlock(Location interactLocation, SwitchState switchState) {
World world = interactLocation.getWorld();
boolean currentState = (interactLocation.getBlock().getData() & 0x8) > 0;
String state = switchState.toString();
if ((state.equals("ON") && !currentState) ||
(state.equals("OFF") && currentState) ||
state.equals("TOGGLE")) {
try {
Block.byId[interactLocation.getBlock().getType().getId()]
.interact(((CraftWorld)world).getHandle(),
interactLocation.getBlockX(),
interactLocation.getBlockY(),
interactLocation.getBlockZ(),
null, 0, 0f, 0f, 0f);
dB.echoDebug("Switched " + interactLocation.getBlock().getType().toString() + "! Current state now: " +
((interactLocation.getBlock().getData() & 0x8) > 0 ? "ON" : "OFF"));
} catch (NullPointerException e) {
dB.echoDebug("Cannot switch " + interactLocation.getBlock().getType().toString() + "!");
}
}
}
}