-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
TraitCommand.java
123 lines (98 loc) · 3.98 KB
/
TraitCommand.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
package com.denizenscript.denizen.scripts.commands.npc;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.Element;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.Trait;
public class TraitCommand extends AbstractCommand {
// <--[command]
// @Name Trait
// @Syntax trait (state:true/false/{toggle}) [<trait>]
// @Required 1
// @Plugin Citizens
// @Short Adds or removes a trait from an NPC.
// @Group npc
//
// @Description
// TODO: Document Command Details
//
// @Tags
// <n@npc.has_trait[<trait>]>
// <n@npc.list_traits>
//
// @Usage
// TODO: Document Command Details
// -->
private enum Toggle {TOGGLE, TRUE, FALSE, ON, OFF}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : ArgumentHelper.interpretArguments(scriptEntry.aHArgs)) {
if (!scriptEntry.hasObject("state")
&& arg.matchesPrefix("state", "s")
&& arg.matchesEnum(Toggle.values())) {
scriptEntry.addObject("state", new Element(arg.getValue().toUpperCase()));
}
else if (!scriptEntry.hasObject("trait")) {
scriptEntry.addObject("trait", new Element(arg.getValue()));
}
}
if (!scriptEntry.hasObject("trait")) {
throw new InvalidArgumentsException("Missing trait argument!");
}
if (!Utilities.entryHasNPC(scriptEntry)) {
throw new InvalidArgumentsException("This command requires a linked NPC!");
}
scriptEntry.defaultObject("state", new Element("TOGGLE"));
}
@Override
public void execute(ScriptEntry scriptEntry) {
Element toggle = scriptEntry.getElement("state");
Element traitName = scriptEntry.getElement("trait");
NPC npc = Utilities.getEntryNPC(scriptEntry).getCitizen();
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(),
traitName.debug() +
toggle.debug() +
Utilities.getEntryNPC(scriptEntry).debug());
}
Class<? extends Trait> trait = CitizensAPI.getTraitFactory().getTraitClass(traitName.asString());
if (trait == null) {
Debug.echoError(scriptEntry.getResidingQueue(), "Trait not found: " + traitName.asString());
return;
}
switch (Toggle.valueOf(toggle.asString())) {
case TRUE:
case ON:
if (npc.hasTrait(trait)) {
Debug.echoError(scriptEntry.getResidingQueue(), "NPC already has trait '" + traitName.asString() + "'");
}
else {
npc.addTrait(trait);
}
break;
case FALSE:
case OFF:
if (!npc.hasTrait(trait)) {
Debug.echoError(scriptEntry.getResidingQueue(), "NPC does not have trait '" + traitName.asString() + "'");
}
else {
npc.removeTrait(trait);
}
break;
case TOGGLE:
if (npc.hasTrait(trait)) {
npc.removeTrait(trait);
}
else {
npc.addTrait(trait);
}
break;
}
}
}