-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
TriggerCommand.java
104 lines (79 loc) · 3.89 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
package net.aufdemrand.denizen.scripts.commands.npc;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.npc.traits.TriggerTrait;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.objects.Duration;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.aH.ArgumentType;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.citizensnpcs.api.npc.NPC;
/**
* Configures the TriggerTrait for a NPC.
*
* @author Jeremy Schroeder
*/
public class TriggerCommand extends AbstractCommand {
private enum Toggle {TOGGLE, TRUE, FALSE}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
// Initialize required fields
String trigger = null;
Toggle toggle = Toggle.TOGGLE;
Duration cooldown = new Duration(-1d);
int radius = -1;
// Parse arguments
for (String arg : scriptEntry.getArguments()) {
if (aH.matchesValueArg("COOLDOWN", arg, ArgumentType.Duration)) {
cooldown = aH.getDurationFrom(arg);
cooldown.setPrefix("Cooldown");
}
else if (aH.matchesValueArg("RADIUS", arg, ArgumentType.Integer))
radius = aH.getIntegerFrom(arg);
else if (aH.matchesState(arg))
toggle = Toggle.valueOf(aH.getStringFrom(arg.toUpperCase()));
else if (aH.matchesValueArg("NAME", arg, ArgumentType.String))
trigger = aH.getStringFrom(arg);
else if (denizen.getTriggerRegistry().get(arg) != null)
trigger = aH.getStringFrom(arg);
else
dB.echoError("Unknown argument '" + arg + "'");
}
// Check required arguments
if (trigger == null) throw new InvalidArgumentsException("Missing name argument!");
// Store objects in ScriptEntry for execute()
scriptEntry.addObject("trigger", trigger);
scriptEntry.addObject("cooldown", cooldown);
scriptEntry.addObject("toggle", toggle);
scriptEntry.addObject("radius", radius);
if (!scriptEntry.hasNPC())
throw new InvalidArgumentsException("This command requires a linked NPC!");
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
Toggle toggle = (Toggle) scriptEntry.getObject("toggle");
String trigger = (String) scriptEntry.getObject("trigger");
Integer radius = (Integer) scriptEntry.getObject("radius");
Duration cooldown = (Duration) scriptEntry.getObject("cooldown");
NPC npc = scriptEntry.getNPC().getCitizen();
dB.echoApproval("Executing '" + getName() + "': "
+ "Trigger='" + trigger + "', "
+ "Toggle='" + toggle.toString() + "', "
+ (radius > 0 ? "Radius='" + radius + "', " : "Radius='Unchanged', ")
+ (cooldown.getSeconds() > 0 ? "Cooldown='" + cooldown.debug() + "', " : "Cooldown='Unchanged', ")
+ "NPC='" + scriptEntry.getNPC() + "'");
// Add trigger trait
if (!npc.hasTrait(TriggerTrait.class)) npc.addTrait(TriggerTrait.class);
TriggerTrait trait = npc.getTrait(TriggerTrait.class);
if (toggle == Toggle.TOGGLE)
trait.toggleTrigger(trigger);
else if (toggle == Toggle.TRUE)
trait.toggleTrigger(trigger, true);
else trait.toggleTrigger(trigger, false);
if (radius > 0)
trait.setLocalRadius(trigger, radius);
if (cooldown.getSeconds() > 0)
trait.setLocalCooldown(trigger, cooldown.getSeconds());
}
}