/
LookcloseCommand.java
104 lines (97 loc) · 4.11 KB
/
LookcloseCommand.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 com.denizenscript.denizen.scripts.commands.npc;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import net.citizensnpcs.trait.LookClose;
public class LookcloseCommand extends AbstractCommand {
public LookcloseCommand() {
setName("lookclose");
setSyntax("lookclose (<npc>) (state:<true/false>) (range:<#>) (realistic)");
setRequiredArguments(0, 4);
isProcedural = false;
}
// <--[command]
// @Name LookClose
// @Syntax lookclose (<npc>) (state:<true/false>) (range:<#>) (realistic)
// @Required 0
// @Maximum 4
// @Plugin Citizens
// @Short Interacts with an NPCs 'lookclose' trait as provided by Citizens.
// @Group npc
//
// @Description
// Use this command with any NPC to alter the state and options of its 'lookclose' trait.
// When an NPC's 'lookclose' trait is toggled to true, the NPC's head will follow nearby players.
// Specifying realistic will enable a higher precision and detection of players, while taking into account 'line-of-sight', however can use more CPU cycles.
// You may also specify a range integer to specify the number of blocks that will trigger the NPC's attention.
//
// @Tags
// <NPCTag.lookclose>
//
// @Usage
// Use to cause the NPC to begin looking at nearby players.
// - lookclose true
//
// @Usage
// Use to cause the NPC to stop looking at nearby players.
// - lookclose false
//
// @Usage
// Use to change the range and make the NPC more realistic
// - lookclose true range:10 realistic
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry) {
if (arg.matches("realistic", "realistically")) {
scriptEntry.addObject("realistic", new ElementTag(true));
}
else if (arg.matchesInteger()) {
scriptEntry.addObject("range", arg.asElement());
}
else if (arg.matchesBoolean()) {
scriptEntry.addObject("toggle", arg.asElement());
}
else if (arg.matchesArgumentType(NPCTag.class)) {
scriptEntry.addObject("npc", arg.asType(NPCTag.class));
((BukkitScriptEntryData) scriptEntry.entryData).setNPC(arg.asType(NPCTag.class));
}
else {
arg.reportUnhandled();
}
}
scriptEntry.defaultObject("npc", Utilities.getEntryNPC(scriptEntry));
if (!scriptEntry.hasObject("npc")) {
throw new InvalidArgumentsException("NPC linked was missing or invalid.");
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag realistic = scriptEntry.getElement("realistic");
ElementTag range = scriptEntry.getElement("range");
ElementTag toggle = scriptEntry.getElement("toggle");
NPCTag npc = scriptEntry.getObjectTag("npc");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), npc, realistic, range, toggle);
}
LookClose trait = npc.getCitizen().getOrAddTrait(LookClose.class);
if (toggle != null) {
trait.lookClose(toggle.asBoolean());
}
if (realistic != null && realistic.asBoolean()) {
trait.setRealisticLooking(true);
}
else {
trait.setRealisticLooking(false);
}
if (range != null) {
trait.setRange(range.asInt());
}
}
}