/
ChatCommand.java
129 lines (107 loc) · 5.5 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
package net.aufdemrand.denizen.scripts.commands.player;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.aH.ArgumentType;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.ai.speech.SpeechContext;
/**
* <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 {
// TODO: Make this class abstract to minimize code duplication for Whisper/Shout/etc.
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
// Create empty speech context
SpeechContext context = new SpeechContext("");
boolean noTargets = false;
if (scriptEntry.getNPC() != null)
context.setTalker(scriptEntry.getNPC().getEntity());
for (String arg : scriptEntry.getArguments()) {
if (aH.matchesValueArg("TARGET, TARGETS", arg, ArgumentType.Custom)) {
if (aH.getStringFrom(arg).equalsIgnoreCase("none")) {
dB.echoDebug("Removed TARGET(s).");
noTargets = true;
}
else {
// Iterate through targets, make sure target is LivingEntity
for (String target : aH.getListFrom(arg)) {
if (aH.getEntityFrom(target) != null) {
context.addRecipient(aH.getEntityFrom(target).getLivingEntity());
} else
dB.echoError("Invalid TARGET: '%s'", target);
}
dB.echoDebug("Set TARGET(s).");
}
} else if (aH.matchesValueArg("TALKER", arg, ArgumentType.LivingEntity)) {
String talker = aH.getStringFrom(arg);
if (aH.getNPCFrom(talker) != null) {
context.setTalker(aH.getEntityFrom(talker).getLivingEntity());
} else
//
// TODO: add hooking into Converse to handle player talking
//
dB.echoError("Invalid TALKER! Perhaps the NPC doesn't exist?");
} else {
context.setMessage(arg);
}
}
// Add default recipient as the scriptEntry Player if no recipients set otherwise
if (!context.hasRecipients() && !noTargets && scriptEntry.getPlayer() != null)
context.addRecipient(scriptEntry.getPlayer().getPlayerEntity());
// Verify essential fields are set
if (context.getTalker() == null)
throw new InvalidArgumentsException("Must specify a valid TALKER.");
if (context.getMessage().length() < 1)
throw new InvalidArgumentsException("Must specify a message.");
// Add context to the ScriptEntry to pass along to execute().
scriptEntry.addObject("context", context);
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
SpeechContext context = (SpeechContext) scriptEntry.getObject("context");
dB.report(getName(),
aH.debugObj("Talker", context.getTalker().getName())
+ aH.debugObj("Direct recipients?", String.valueOf(context.hasRecipients()))
+ aH.debugObj("Message", context.getMessage()));
// If the talker is an NPC, use the NPC object to speak
if (CitizensAPI.getNPCRegistry().isNPC(context.getTalker().getEntity()))
CitizensAPI.getNPCRegistry().getNPC(context.getTalker().getEntity())
.getDefaultSpeechController().speak(context, "chat");
// else
// TODO: Chat via Player with Converse
}
}