/
SwitchCommand.java
254 lines (222 loc) · 12.9 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package net.aufdemrand.denizen.scripts.commands.core;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.arguments.aH;
import net.aufdemrand.denizen.utilities.arguments.aH.ArgumentType;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;
import net.aufdemrand.denizen.utilities.runnables.Runnable2;
import net.minecraft.server.v1_5_R2.Block;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_5_R2.CraftWorld;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* Switches a button or lever.
*
* @author Jeremy Schroeder, Mason Adkins
*/
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>();
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)) {
if (aH.getStringFrom(arg).equalsIgnoreCase("ON") || aH.getStringFrom(arg).equalsIgnoreCase("OPEN")) {
switchState = SwitchState.ON;
dB.echoDebug("...set STATE: 'ON'.");
} else if (aH.getStringFrom(arg).equalsIgnoreCase("OFF") || aH.getStringFrom(arg).equalsIgnoreCase("CLOSE")) {
switchState = SwitchState.OFF;
dB.echoDebug("...set STATE: 'OFF'.");
} else if (aH.getStringFrom(arg).equalsIgnoreCase("TOGGLE")) {
switchState = SwitchState.TOGGLE;
dB.echoDebug("...set STATE: 'TOGGLE'.");
} else dB.echoError("Unknown STATE! Valid: ON, OFF, TOGGLE");
} else if (aH.matchesLocation(arg)) {
interactLocation = aH.getLocationFrom(arg);
if (interactLocation != null) dB.echoError("...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 Runnable2<Location, SwitchState>(interactLocation, switchState) {
@Override public void run(Location iLocation, SwitchState sState) {
// Check to see if the state of the block is what is expected. If switched during
// the duration, the switchback is cancelled.
if (sState == SwitchState.OFF && !((iLocation.getBlock().getData() & 0x8) > 0))
switchBlock(iLocation, SwitchState.ON);
else if (sState == SwitchState.ON && ((iLocation.getBlock().getData() & 0x8) > 0))
switchBlock(iLocation, SwitchState.OFF);
else if (sState == SwitchState.TOGGLE) switchBlock(iLocation, 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;
// Might be a way to generalize this portion and negate the need for a switch all-together.
switch (interactLocation.getBlock().getType()) {
case LEVER:
switch (switchState) {
case TOGGLE:
Block.LEVER.interact(((CraftWorld)world).getHandle(), interactLocation.getBlockX(), interactLocation.getBlockY(), interactLocation.getBlockZ(), null, 0, 0f, 0f, 0f);
break;
case ON:
if (currentState != true)
Block.LEVER.interact(((CraftWorld)world).getHandle(), interactLocation.getBlockX(), interactLocation.getBlockY(), interactLocation.getBlockZ(), null, 0, 0f, 0f, 0f);
break;
case OFF:
if (currentState != false)
Block.LEVER.interact(((CraftWorld)world).getHandle(), interactLocation.getBlockX(), interactLocation.getBlockY(), interactLocation.getBlockZ(), null, 0, 0f, 0f, 0f);
break;
}
dB.echoDebug("SWITCHED! Current state now: " + ((interactLocation.getBlock().getData() & 0x8) > 0 ? "ON" : "OFF"));
break;
case STONE_BUTTON:
switch (switchState) {
case TOGGLE:
Block.STONE_BUTTON.interact(((CraftWorld)world).getHandle(), interactLocation.getBlockX(), interactLocation.getBlockY(), interactLocation.getBlockZ(), null, 0, 0f, 0f, 0f);
break;
case ON:
if (currentState != true)
Block.STONE_BUTTON.interact(((CraftWorld)world).getHandle(), interactLocation.getBlockX(), interactLocation.getBlockY(), interactLocation.getBlockZ(), null, 0, 0f, 0f, 0f);
break;
case OFF:
if (currentState != false)
Block.STONE_BUTTON.interact(((CraftWorld)world).getHandle(), interactLocation.getBlockX(), interactLocation.getBlockY(), interactLocation.getBlockZ(), null, 0, 0f, 0f, 0f);
break;
}
dB.echoDebug("SWITCHED! Current state now: " + ((interactLocation.getBlock().getData() & 0x8) > 0 ? "ON" : "OFF"));
break;
case STONE_PLATE:
switch (switchState) {
case TOGGLE:
Block.STONE_PLATE.interact(((CraftWorld)world).getHandle(), interactLocation.getBlockX(), interactLocation.getBlockY(), interactLocation.getBlockZ(), null, 0, 0f, 0f, 0f);
break;
case ON:
if (currentState != true)
Block.STONE_PLATE.interact(((CraftWorld)world).getHandle(), interactLocation.getBlockX(), interactLocation.getBlockY(), interactLocation.getBlockZ(), null, 0, 0f, 0f, 0f);
break;
case OFF:
if (currentState != false)
Block.STONE_PLATE.interact(((CraftWorld)world).getHandle(), interactLocation.getBlockX(), interactLocation.getBlockY(), interactLocation.getBlockZ(), null, 0, 0f, 0f, 0f);
break;
}
dB.echoDebug("SWITCHED! Current state now: " + ((interactLocation.getBlock().getData() & 0x8) > 0 ? "ON" : "OFF"));
break;
case WOOD_PLATE:
switch (switchState) {
case TOGGLE:
Block.WOOD_PLATE.interact(((CraftWorld)world).getHandle(), interactLocation.getBlockX(), interactLocation.getBlockY(), interactLocation.getBlockZ(), null, 0, 0f, 0f, 0f);
break;
case ON:
if (currentState != true)
Block.WOOD_PLATE.interact(((CraftWorld)world).getHandle(), interactLocation.getBlockX(), interactLocation.getBlockY(), interactLocation.getBlockZ(), null, 0, 0f, 0f, 0f);
break;
case OFF:
if (currentState != false)
Block.WOOD_PLATE.interact(((CraftWorld)world).getHandle(), interactLocation.getBlockX(), interactLocation.getBlockY(), interactLocation.getBlockZ(), null, 0, 0f, 0f, 0f);
break;
}
dB.echoDebug("SWITCHED! Current state now: " + ((interactLocation.getBlock().getData() & 0x8) > 0 ? "ON" : "OFF"));
break;
case WOODEN_DOOR:
switch (switchState) {
case TOGGLE:
Block.WOODEN_DOOR.interact(((CraftWorld)world).getHandle(), interactLocation.getBlockX(), interactLocation.getBlockY(), interactLocation.getBlockZ(), null, 0, 0f, 0f, 0f);
break;
case ON:
if (currentState != true)
Block.WOODEN_DOOR.interact(((CraftWorld)world).getHandle(), interactLocation.getBlockX(), interactLocation.getBlockY(), interactLocation.getBlockZ(), null, 0, 0f, 0f, 0f);
break;
case OFF:
if (currentState != false)
Block.WOODEN_DOOR.interact(((CraftWorld)world).getHandle(), interactLocation.getBlockX(), interactLocation.getBlockY(), interactLocation.getBlockZ(), null, 0, 0f, 0f, 0f);
break;
}
dB.echoDebug("SWITCHED! Current state now: " + ((interactLocation.getBlock().getData() & 0x8) > 0 ? "ON" : "OFF"));
break;
case IRON_DOOR_BLOCK:
switch (switchState) {
case TOGGLE:
Block.IRON_DOOR_BLOCK.interact(((CraftWorld)world).getHandle(), interactLocation.getBlockX(), interactLocation.getBlockY(), interactLocation.getBlockZ(), null, 0, 0f, 0f, 0f);
break;
case ON:
if (currentState != true)
Block.WOODEN_DOOR.interact(((CraftWorld)world).getHandle(), interactLocation.getBlockX(), interactLocation.getBlockY(), interactLocation.getBlockZ(), null, 0, 0f, 0f, 0f);
break;
case OFF:
if (currentState != false)
Block.WOODEN_DOOR.interact(((CraftWorld)world).getHandle(), interactLocation.getBlockX(), interactLocation.getBlockY(), interactLocation.getBlockZ(), null, 0, 0f, 0f, 0f);
break;
}
dB.echoDebug("SWITCHED! Current state now: " + ((interactLocation.getBlock().getData() & 0x8) > 0 ? "ON" : "OFF"));
break;
case TRAP_DOOR:
switch (switchState) {
case TOGGLE:
Block.TRAP_DOOR.interact(((CraftWorld)world).getHandle(), interactLocation.getBlockX(), interactLocation.getBlockY(), interactLocation.getBlockZ(), null, 0, 0f, 0f, 0f);
break;
case ON:
if (currentState != true)
Block.TRAP_DOOR.interact(((CraftWorld)world).getHandle(), interactLocation.getBlockX(), interactLocation.getBlockY(), interactLocation.getBlockZ(), null, 0, 0f, 0f, 0f);
break;
case OFF:
if (currentState != false)
Block.TRAP_DOOR.interact(((CraftWorld)world).getHandle(), interactLocation.getBlockX(), interactLocation.getBlockY(), interactLocation.getBlockZ(), null, 0, 0f, 0f, 0f);
break;
}
dB.echoDebug("SWITCHED! Current state now: " + ((interactLocation.getBlock().getData() & 0x8) > 0 ? "ON" : "OFF"));
break;
// If block isn't any of the above...
default:
dB.echoError("UNSWITCHABLE! Not a valid type of block!");
break;
}
}
}