/
TraitCommand.java
151 lines (142 loc) · 5.86 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
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
package com.denizenscript.denizen.scripts.commands.npc;
import com.denizenscript.denizen.objects.NPCTag;
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.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
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;
import net.citizensnpcs.api.trait.TraitInfo;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
public class TraitCommand extends AbstractCommand {
public TraitCommand() {
setName("trait");
setSyntax("trait (state:true/false/{toggle}) [<trait>] (to:<npc>|...)");
setRequiredArguments(1, 3);
isProcedural = false;
}
// <--[command]
// @Name Trait
// @Syntax trait (state:true/false/{toggle}) [<trait>] (to:<npc>|...)
// @Required 1
// @Maximum 3
// @Plugin Citizens
// @Short Adds or removes a trait from an NPC.
// @Group npc
//
// @Description
// This command adds or removes a trait from an NPC.
//
// Use "state:true" to add or "state:false" to remove.
// If neither is specified, the default is "toggle", which means remove if already present or add if not.
//
// Note that a redundant instruction, like adding a trait that the NPC already has, will give an error message.
//
// The trait input is simply the name of the trait, like "sentinel".
//
// Optionally, specify a list of NPCs to apply the trait to. If unspecified, the linked NPC will be used.
//
// @Tags
// <NPCTag.has_trait[<trait>]>
// <NPCTag.traits>
// <server.traits>
//
// @Usage
// Use to add the Sentinel trait to the linked NPC.
// - trait state:true sentinel
//
// @Usage
// Use to toggle the MobProx trait on the linked NPC.
// - trait mobprox
//
// -->
private enum Toggle {TOGGLE, TRUE, FALSE, ON, OFF}
@Override
public void addCustomTabCompletions(String arg, Consumer<String> addOne) {
for (TraitInfo trait : CitizensAPI.getTraitFactory().getRegisteredTraits()) {
addOne.accept(trait.getTraitName());
}
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry) {
if (!scriptEntry.hasObject("state")
&& arg.matchesPrefix("state", "s")
&& arg.matchesEnum(Toggle.values())) {
scriptEntry.addObject("state", new ElementTag(arg.getValue().toUpperCase()));
}
else if (!scriptEntry.hasObject("trait")) {
scriptEntry.addObject("trait", new ElementTag(arg.getValue()));
}
else if (!scriptEntry.hasObject("npcs")
&& arg.matchesArgumentList(NPCTag.class)) {
scriptEntry.addObject("npcs", arg.asType(ListTag.class).filter(NPCTag.class, scriptEntry));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("trait")) {
throw new InvalidArgumentsException("Missing trait argument!");
}
if (!scriptEntry.hasObject("npcs")) {
if (!Utilities.entryHasNPC(scriptEntry)) {
throw new InvalidArgumentsException("This command requires a linked NPC!");
}
scriptEntry.addObject("npcs", Collections.singletonList(Utilities.getEntryNPC(scriptEntry)));
}
scriptEntry.defaultObject("state", new ElementTag("TOGGLE"));
}
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag toggle = scriptEntry.getElement("state");
ElementTag traitName = scriptEntry.getElement("trait");
List<NPCTag> npcs = (List<NPCTag>) scriptEntry.getObject("npcs");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), traitName, toggle, db("npc", npcs));
}
Class<? extends Trait> trait = CitizensAPI.getTraitFactory().getTraitClass(traitName.asString());
if (trait == null) {
Debug.echoError(scriptEntry.getResidingQueue(), "Trait not found: " + traitName.asString());
return;
}
for (NPCTag npcTag : npcs) {
NPC npc = npcTag.getCitizen();
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;
}
}
}
}