-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
SwitchCommand.java
215 lines (195 loc) · 10.4 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
package net.aufdemrand.denizen.scripts.commands.world;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.nms.NMSHandler;
import net.aufdemrand.denizen.nms.NMSVersion;
import net.aufdemrand.denizen.nms.interfaces.BlockData;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.MaterialCompat;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.depends.Depends;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Duration;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.objects.dList;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.data.Openable;
import org.bukkit.block.data.Powerable;
import org.bukkit.entity.Player;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class SwitchCommand extends AbstractCommand {
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())) {
if (!scriptEntry.hasObject("locations") &&
arg.matchesArgumentList(dLocation.class)) {
scriptEntry.addObject("locations", arg.asType(dList.class));
}
else if (!scriptEntry.hasObject("duration") &&
arg.matchesArgumentType(Duration.class)) {
scriptEntry.addObject("duration", arg.asType(Duration.class));
}
else if (!scriptEntry.hasObject("state") &&
arg.matchesEnum(SwitchState.values())) {
scriptEntry.addObject("switchstate", new Element(arg.getValue().toUpperCase()));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("locations")) {
throw new InvalidArgumentsException("Must specify a location!");
}
scriptEntry.defaultObject("duration", new Duration(0));
scriptEntry.defaultObject("switchstate", new Element("TOGGLE"));
}
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
final dList interactLocations = scriptEntry.getdObject("locations");
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
if (scriptEntry.dbCallShouldDebug()) {
dB.report(scriptEntry, getName(), interactLocations.debug()
+ aH.debugObj("duration", duration + "t")
+ aH.debugObj("switchstate", switchState.name()));
}
for (final dLocation interactLocation : interactLocations.filter(dLocation.class)) {
switchBlock(scriptEntry, 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 {
Bukkit.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, Bukkit.getScheduler().scheduleSyncDelayedTask(DenizenAPI.getCurrentInstance(),
new Runnable() {
public void run() {
switchBlock(scriptEntry, interactLocation, SwitchState.TOGGLE, player);
}
}, duration));
}
}
}
public static boolean switchState(Block b) {
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13_R2)) {
if (b.getBlockData() instanceof Openable) {
return ((Openable) b.getBlockData()).isOpen();
}
else if (b.getBlockData() instanceof Powerable) {
return ((Powerable) b.getBlockData()).isPowered(); // TODO: is this valid for levers?
}
}
//return (b.getData() & 0x8) > 0;
Material type = b.getType();
if (type == MaterialCompat.IRON_DOOR
|| type == MaterialCompat.OAK_DOOR
|| type == Material.DARK_OAK_DOOR
|| type == Material.BIRCH_DOOR
|| type == Material.ACACIA_DOOR
|| type == Material.JUNGLE_DOOR
|| type == Material.SPRUCE_DOOR) {
Location location = b.getLocation();
int data = b.getData();
if (data >= 8) {
location = b.getLocation().clone().add(0, -1, 0);
}
return (location.getBlock().getData() & 0x4) > 0;
}
else if ((type == MaterialCompat.OAK_TRAPDOOR
|| type == Material.IRON_TRAPDOOR)
|| (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13_R2)
&& (type == Material.DARK_OAK_TRAPDOOR
|| type == Material.BIRCH_TRAPDOOR
|| type == Material.ACACIA_TRAPDOOR
|| type == Material.JUNGLE_TRAPDOOR
|| type == Material.SPRUCE_TRAPDOOR))) {
return (b.getData() & 0x4) > 0;
}
else {
return (b.getData() & 0x8) > 0;
}
}
// 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, Player player) {
boolean currentState = switchState(interactLocation.getBlock());
if ((switchState.equals(SwitchState.ON) && !currentState) ||
(switchState.equals(SwitchState.OFF) && currentState) ||
switchState.equals(SwitchState.TOGGLE)) {
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13_R2)) {
if (interactLocation.getBlock().getBlockData() instanceof Openable) {
Openable newState = ((Openable) interactLocation.getBlock().getBlockData());
newState.setOpen(!currentState);
interactLocation.getBlock().setBlockData(newState, true);
interactLocation.getBlock().getState().update(true, true);
}
else if (interactLocation.getBlock().getBlockData() instanceof Powerable) {
Powerable newState = ((Powerable) interactLocation.getBlock().getBlockData());
newState.setPowered(!currentState);
interactLocation.getBlock().setBlockData(newState, true);
interactLocation.getBlock().getState().update(true, true);
}
}
else {
try {
if (interactLocation.getBlock().getType() == MaterialCompat.IRON_DOOR) {
Location block;
if (interactLocation.clone().add(0, -1, 0).getBlock().getType() == MaterialCompat.IRON_DOOR) {
block = interactLocation.clone().add(0, -1, 0);
}
else {
block = interactLocation;
}
BlockData blockData = NMSHandler.getInstance().getBlockHelper().getBlockData(MaterialCompat.IRON_DOOR, (byte) (block.getBlock().getData() ^ 4));
blockData.setBlock(block.getBlock(), false);
}
else {
// Try for a linked player
if (player == null && Bukkit.getOnlinePlayers().size() > 0) {
// If there's none, link any player
if (Bukkit.getOnlinePlayers().size() > 0) {
player = (Player) Bukkit.getOnlinePlayers().toArray()[0];
}
else if (Depends.citizens != null) {
// If there are no players, link any Human NPC
for (NPC npc : CitizensAPI.getNPCRegistry()) {
if (npc.isSpawned() && npc.getEntity() instanceof Player) {
player = (Player) npc.getEntity();
break;
}
}
// TODO: backup if no human NPC available? (Fake EntityPlayer instance?)
}
}
NMSHandler.getInstance().getEntityHelper().forceInteraction(player, interactLocation);
}
}
catch (NullPointerException e) {
dB.echoError("Cannot switch " + interactLocation.getBlock().getType().toString() + "!");
return;
}
}
dB.echoDebug(scriptEntry, "Switched " + interactLocation.getBlock().getType().toString() + "! Current state now: " +
(switchState(interactLocation.getBlock()) ? "ON" : "OFF"));
}
}
}