/
TriggerCommand.java
159 lines (135 loc) · 6.57 KB
/
TriggerCommand.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
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.TriggerTrait;
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 TriggerCommand extends AbstractCommand {
// <--[command]
// @Name Trigger
// @Syntax trigger [name:<trigger>] (state:{toggle}/true/false) (cooldown:<duration>) (radius:<#>)
// @Required 1
// @Maximum 4
// @Short Enables or disables a trigger.
// @Group npc
// @Guide https://guide.denizenscript.com/guides/npcs/interact-scripts.html
//
// @Description
// This command enables or disables an interact script trigger for the linked NPC.
// This is generally meant to be used within the 'on assignment' action in an assignment script.
// This might also be useful on timed activations or other special events (such as an NPC that "goes to bed" at the end of the day,
// you might disable the proximity trigger that would otherwise normally show a greeting message).
//
// The "name" argument is required, and can have any supported trigger name.
// The 4 triggers available by default are chat, click, damage, and proximity.
// For more details of the available trigger types, refer to <@link language Interact Script Triggers>.
//
// The "state" argument can be 'true' (to enable it), 'false' (to disable it),
// or unspecified to toggle it (that is, enable if it's currently off, or disable if it's currently on).
//
// You can specify the "cooldown" argument to set how long the trigger must wait
// after any firing before it can be fired again.
//
// You can specify the "radius" argument to set how far away a player can be when activating it.
// Note that the way this applies varies from trigger to trigger.
// For the "chat" trigger, a large radius can be easily accidentally triggered by unrelated chatter.
// For the "proximity" trigger, the radius argument should almost always be specified, as you generally want to control this with care.
// For the "click" and "damage" trigger, the radius argument will be ignored.
//
// @Tags
// <NPCTag.has_trigger[<trigger>]>
//
// @Usage
// Use to enable the click trigger.
// - trigger name:click state:true
//
// @Usage
// Use to enable the chat trigger with a 10-second cooldown and a radius of 5 blocks.
// - trigger name:chat state:true cooldown:10s radius:5
//
// @Usage
// Use to disable the proximity trigger.
// - trigger name:proximity state:false
// -->
private enum Toggle {TOGGLE, TRUE, FALSE}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!scriptEntry.hasObject("cooldown")
&& arg.matchesPrefix("cooldown", "c")
&& arg.matchesArgumentType(DurationTag.class)) {
scriptEntry.addObject("cooldown", arg.asType(DurationTag.class));
}
else if (!scriptEntry.hasObject("radius")
&& arg.matchesPrefix("radius", "r")
&& arg.matchesInteger()) {
scriptEntry.addObject("radius", arg.asElement());
}
else if (!scriptEntry.hasObject("toggle")
&& arg.matchesEnum(Toggle.values())) {
scriptEntry.addObject("toggle", arg.asElement());
}
else if (!scriptEntry.hasObject("npc")
&& arg.matchesArgumentType(NPCTag.class)) {
scriptEntry.addObject("npc", arg.asType(NPCTag.class));
}
else if (!scriptEntry.hasObject("trigger")) {
scriptEntry.addObject("trigger", arg.asElement());
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("trigger")) {
throw new InvalidArgumentsException("Missing name argument!");
}
if (!scriptEntry.hasObject("toggle")) {
scriptEntry.addObject("toggle", new ElementTag("TOGGLE"));
}
if (!Utilities.entryHasNPC(scriptEntry) && !scriptEntry.hasObject("npc")) {
throw new InvalidArgumentsException("This command requires a linked NPC!");
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag toggle = scriptEntry.getElement("toggle");
ElementTag trigger = scriptEntry.getElement("trigger");
ElementTag radius = scriptEntry.getElement("radius");
DurationTag cooldown = (DurationTag) scriptEntry.getObject("cooldown");
NPCTag npc = scriptEntry.hasObject("npc") ? (NPCTag) scriptEntry.getObject("npc") : Utilities.getEntryNPC(scriptEntry);
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(),
trigger.debug() + toggle.debug() +
(radius != null ? radius.debug() : "") +
(cooldown != null ? cooldown.debug() : "") +
npc.debug());
}
// Add trigger trait
if (!npc.getCitizen().hasTrait(TriggerTrait.class)) {
npc.getCitizen().addTrait(TriggerTrait.class);
}
TriggerTrait trait = npc.getCitizen().getTrait(TriggerTrait.class);
switch (Toggle.valueOf(toggle.asString().toUpperCase())) {
case TOGGLE:
trait.toggleTrigger(trigger.asString());
break;
case TRUE:
trait.toggleTrigger(trigger.asString(), true);
break;
case FALSE:
trait.toggleTrigger(trigger.asString(), false);
break;
}
if (radius != null) {
trait.setLocalRadius(trigger.asString(), radius.asInt());
}
if (cooldown != null) {
trait.setLocalCooldown(trigger.asString(), cooldown.getSeconds());
}
}
}