Skip to content

Commit

Permalink
Fix Chat command
Browse files Browse the repository at this point in the history
  • Loading branch information
aufdemrand committed Mar 8, 2013
1 parent 2eb1a97 commit 8106c4e
Showing 1 changed file with 79 additions and 77 deletions.
Expand Up @@ -14,15 +14,15 @@
/**
* <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.
Expand All @@ -40,85 +40,87 @@
* - 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 {
// 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 (arg.equalsIgnoreCase("none")) {
dB.echoDebug("Removed TARGET(s).");
noTargets = true;
}

// Iterate through targets, make sure target is LivingEntity
for (String target : aH.getListFrom(arg)) {
if (aH.getLivingEntityFrom(target) != null) {
context.addRecipient(aH.getLivingEntityFrom(target));
} 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 (talker.startsWith("NPC.") && aH.getLivingEntityFrom(talker) != null) {
context.setTalker(aH.getLivingEntityFrom(talker));
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.getLivingEntityFrom(target) != null) {
context.addRecipient(aH.getLivingEntityFrom(target));
} 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 (talker.startsWith("NPC.") && aH.getLivingEntityFrom(talker) != null) {
context.setTalker(aH.getLivingEntityFrom(talker));
} else
//
// TODO: add hooking into Converse to handle player talking
//
dB.echoError("Invalid TALKER! Perhaps the NPC doesn't exist?");

} else {
context.setMessage(arg);
dB.echoDebug(Messages.DEBUG_SET_TEXT, arg);
}
}

// Add default recipient as the scriptEntry Player if no recipients set otherwise
if (!context.hasRecipients() && !noTargets && scriptEntry.getPlayer() != null)
context.addRecipient(scriptEntry.getPlayer());
// 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");

// 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

}
//
// TODO: add hooking into Converse to handle player talking
//
dB.echoError("Invalid TALKER! Perhaps the NPC doesn't exist?");

} else {
context.setMessage(arg);
dB.echoDebug(Messages.DEBUG_SET_TEXT, arg);
}
}

// Add default recipient as the scriptEntry Player if no recipients set otherwise
if (!context.hasRecipients() && !noTargets && scriptEntry.getPlayer() != null)
context.addRecipient(scriptEntry.getPlayer());

// 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");

// 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

}

}

0 comments on commit 8106c4e

Please sign in to comment.