-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
LookcloseCommand.java
115 lines (98 loc) · 4.38 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
105
106
107
108
109
110
111
112
113
114
115
package com.denizenscript.denizen.scripts.commands.npc;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.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.objects.ArgumentHelper;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import net.citizensnpcs.trait.LookClose;
public class LookcloseCommand extends AbstractCommand {
// <--[command]
// @Name LookClose
// @Syntax lookclose (<npc>) (state:<true/false>) (range:<#>) (realistic)
// @Required 0
// @Plugin Citizens
// @Short Interacts with an NPCs 'lookclose' trait as provided by Citizens2.
// @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
// None
//
// @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 {
// Parse Arguments
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (arg.matches("realistic", "realistically")) {
scriptEntry.addObject("realistic", new ElementTag(true));
}
else if (arg.matchesPrimitive(ArgumentHelper.PrimitiveType.Integer)) {
scriptEntry.addObject("range", arg.asElement());
}
else if (arg.matchesPrimitive(ArgumentHelper.PrimitiveType.Boolean)) {
scriptEntry.addObject("toggle", arg.asElement());
}
else if (arg.matchesArgumentType(NPCTag.class)) // TODO: better way of handling this?
{
((BukkitScriptEntryData) scriptEntry.entryData).setNPC(arg.asType(NPCTag.class));
}
else {
arg.reportUnhandled();
}
}
// Only required thing is a valid NPC. This may be an already linked
// NPC, or one specified by arguments
if (Utilities.getEntryNPC(scriptEntry) == null) {
throw new InvalidArgumentsException("NPC linked was missing or invalid.");
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), Utilities.getEntryNPC(scriptEntry).debug()
+ ArgumentHelper.debugObj("realistic", scriptEntry.getObject("realistic"))
+ ArgumentHelper.debugObj("range", scriptEntry.getObject("range"))
+ ArgumentHelper.debugObj("toggle", scriptEntry.getObject("toggle")));
}
// Get the instance of the trait that belongs to the target NPC
LookClose trait = Utilities.getEntryNPC(scriptEntry).getCitizen().getTrait(LookClose.class);
// Handle toggle
if (scriptEntry.hasObject("toggle")) {
trait.lookClose(scriptEntry.getElement("toggle").asBoolean());
}
// Handle realistic
if (scriptEntry.hasObject("realistic")) {
trait.setRealisticLooking(true);
}
else {
trait.setRealisticLooking(false);
}
// Handle range
if (scriptEntry.hasObject("range")) {
trait.setRange(scriptEntry.getElement("range").asInt());
}
}
}