/
ChatCommand.java
144 lines (117 loc) · 6.2 KB
/
ChatCommand.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
package net.aufdemrand.denizen.scripts.commands.player;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.npc.speech.DenizenSpeechContext;
import net.aufdemrand.denizen.npc.speech.DenizenSpeechController;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.tags.TagManager;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.entity.Entity;
/**
* <p>Uses the Citizens SpeechController to 'chat', the default VocalChord for
* of an NPC. Chat prefixes and setup is found in Citizen's config.yml file.</p>
*
* <b>dScript Usage:</b><br>
* <pre>CHAT ['message to chat.'] (TARGET(S):list_of_LivingEntities) (TALKER:NPC.#)</pre>
*
* <ol><tt>Arguments: [] - Required</ol></tt>
*
* <ol><tt>['message to chat']</tt><br>
* The chat message the Talker will use. This will be seen by all entities within range.</ol>
*
* <ol><tt>(TARGET(S):NONE|List of LivingEntities{Interact Player})</tt><br>
* The LivingEntities that the message is addressed to. Uses the dScript List format
* (item1|item2|etc). Valid entities are: PLAYER.player_name, NPC.npcid, or ENTITY.entity_name.
* If NONE is specified, the NPC speaking will have no target. Default target is set to
* the Player doing the interaction (if that information is available to the command).</ol>
*
* <ol><tt>(TALKER:NPC.npcid{Interact NPC})</tt><br>
* The NPC that will be doing the chatting. Defaults to the NPC interacted with (if that information is
* available to the command), but can be changed by using the NPC LivingEntity format (NPC.npcid).</ol>
*
* <br><b>Example Usage:</b><br>
* <ol><tt>
* - CHAT 'Be careful out there! The road is long and dark.' <br>
* - CHAT TARGET:NONE 'Beer here! Beer for sale! ...anybody need a beer?' <br>
* - CHAT TARGETS:PLAYER.aufdemrand|PLAYER.Jeebiss|PLAYER.DrBix 'Ah, a group of adventurers! Great!'
* - CHAT TALKER:NPC.13 TARGET:NPC.<NPC.ID> 'Shut up, old man!'
* </ol></tt>
*
* @author Jeremy Schroeder
*
*/
public class ChatCommand extends AbstractCommand {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
boolean specified_targets = false;
boolean specified_talker = false;
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
// Default target is the attached Player, if none specified otherwise.
if (arg.matchesPrefix("target", "targets", "t")) {
if (arg.matchesArgumentList(dEntity.class))
scriptEntry.addObject("targets", arg.asType(dList.class));
specified_targets = true;
}
else if (arg.matches("no_target"))
scriptEntry.addObject("targets", new dList());
// Default talker is the attached NPC, if none specified otherwise.
else if (arg.matchesPrefix("talker", "talkers")) {
if (arg.matchesArgumentList(dEntity.class))
scriptEntry.addObject("talkers", arg.asType(dList.class));
specified_talker = true;
}
else if (arg.matchesPrefix("range", "r")) {
if (arg.matchesPrimitive(aH.PrimitiveType.Double))
scriptEntry.addObject("range", arg.asElement());
}
else if (!scriptEntry.hasObject("message"))
scriptEntry.addObject("message", new Element(arg.raw_value));
else
arg.reportUnhandled();
}
// Add default recipient as the attached Player if no recipients set otherwise
if (!scriptEntry.hasObject("targets") && ((BukkitScriptEntryData)scriptEntry.entryData).hasPlayer() && !specified_targets)
scriptEntry.defaultObject("targets", new dList(((BukkitScriptEntryData)scriptEntry.entryData).getPlayer().identify()));
// Add default talker as the attached NPC if no recipients set otherwise
if (!scriptEntry.hasObject("talkers") && ((BukkitScriptEntryData)scriptEntry.entryData).hasNPC() && !specified_talker)
scriptEntry.defaultObject("talkers", new dList(((BukkitScriptEntryData)scriptEntry.entryData).getNPC().identify()));
// Verify essential fields are set
if (!scriptEntry.hasObject("targets"))
throw new InvalidArgumentsException("Must specify valid targets!");
if (!scriptEntry.hasObject("talkers"))
throw new InvalidArgumentsException("Must specify valid talkers!");
if (!scriptEntry.hasObject("message"))
throw new InvalidArgumentsException("Must specify a message!");
scriptEntry.defaultObject("range", new Element(Settings.chatBystandersRange()));
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
dList talkers = scriptEntry.getdObject("talkers");
dList targets = scriptEntry.getdObject("targets");
Element message = scriptEntry.getElement("message");
Element chatRange = scriptEntry.getElement("range");
dB.report(scriptEntry, getName(), talkers.debug() + targets.debug() + message.debug() + chatRange.debug());
// Create new speech context
DenizenSpeechContext context = new DenizenSpeechContext(TagManager.cleanOutputFully(message.asString()),
scriptEntry, chatRange.asDouble());
if (!targets.isEmpty()) {
for (dEntity ent : targets.filter(dEntity.class)) {
context.addRecipient(ent.getBukkitEntity());
}
}
for (dEntity talker : talkers.filter(dEntity.class)) {
Entity entity = talker.getBukkitEntity();
if (entity != null) {
context.setTalker(entity);
new DenizenSpeechController(entity).speak(context);
}
else {
dB.echoDebug(scriptEntry, "Chat Talker is not spawned! Cannot talk.");
}
}
}
}