/
PushableCommand.java
120 lines (98 loc) · 4.03 KB
/
PushableCommand.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
package com.denizenscript.denizen.scripts.commands.npc;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.npc.traits.PushableTrait;
import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
public class PushableCommand extends AbstractCommand {
// <--[command]
// @Name Pushable
// @Syntax pushable (state:true/false/{toggle}) (delay:<duration>) (returnable:true/false)
// @Required 0
// @Maximum 3
// @Short Edits the pushable trait for NPCs.
// @Group npc
//
// @Description
// Enables, disables, toggles, or edits the Pushable trait on the attached NPC.
//
// @Tags
// None
//
// @Usage
// Use to toggle the Pushable trait for a specified NPC.
// - pushable npc:<[some_npc]>
//
// @Usage
// Use to enable the Pushable trait and return after 2 seconds.
// - pushable state:true delay:2s returnable:true
// -->
private enum Toggle {TOGGLE, TRUE, FALSE, ON, OFF}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!scriptEntry.hasObject("state")
&& arg.matchesPrefix("state", "s")
&& arg.matchesEnum(Toggle.values())) {
scriptEntry.addObject("state", arg.asElement());
}
else if (!scriptEntry.hasObject("delay")
&& arg.matchesPrefix("delay", "d")
&& arg.matchesArgumentType(DurationTag.class)) {
scriptEntry.addObject("delay", arg.asType(DurationTag.class));
}
else if (!scriptEntry.hasObject("return")
&& arg.matchesPrefix("return", "r")
&& arg.matchesBoolean()) {
scriptEntry.addObject("return", arg.asElement());
}
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
NPCTag denizenNPC = Utilities.getEntryNPC(scriptEntry);
if (denizenNPC == null) {
Debug.echoError("No valid NPC attached to this queue!");
return;
}
PushableTrait trait = denizenNPC.getPushableTrait();
ElementTag state = scriptEntry.getElement("state");
DurationTag delay = scriptEntry.getObjectTag("delay");
ElementTag returnable = scriptEntry.getElement("return");
if (state == null && delay == null && returnable == null) {
state = new ElementTag("TOGGLE");
}
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(),
(state != null ? state.debug() : "") +
(delay != null ? delay.debug() : "") +
(returnable != null ? returnable.debug() : ""));
}
if (delay != null) {
trait.setDelay(delay.getSecondsAsInt());
}
if (returnable != null) {
trait.setReturnable(returnable.asBoolean());
}
if (state != null) {
switch (Toggle.valueOf(state.asString().toUpperCase())) {
case TRUE:
case ON:
trait.setPushable(true);
break;
case FALSE:
case OFF:
trait.setPushable(false);
break;
case TOGGLE:
trait.setPushable(!trait.isPushable());
break;
}
}
}
}