From 8106c4e12045b92329fc87cb78b597906b8f459b Mon Sep 17 00:00:00 2001 From: Jeremy Schroeder Date: Fri, 8 Mar 2013 15:06:09 -0500 Subject: [PATCH] Fix Chat command --- .../scripts/commands/core/ChatCommand.java | 156 +++++++++--------- 1 file changed, 79 insertions(+), 77 deletions(-) diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/ChatCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/ChatCommand.java index 26a3d7a9a0..f5b48c12cb 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/ChatCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/ChatCommand.java @@ -14,15 +14,15 @@ /** *

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.

- * + * * dScript Usage:
*
CHAT  ['message to chat.'] (TARGET(S):list_of_LivingEntities) (TALKER:NPC.#)
- * + * *
    Arguments: [] - Required
- * + * *
    ['message to chat']
    * The chat message the Talker will use. This will be seen by all entities within range.
- * + * *
    (TARGET(S):NONE|List of LivingEntities{Interact Player})
    * 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. @@ -40,85 +40,87 @@ * - CHAT TARGETS:PLAYER.aufdemrand|PLAYER.Jeebiss|PLAYER.DrBix 'Ah, a group of adventurers! Great!' * - CHAT TALKER:NPC.13 TARGET:NPC. 'Shut up, old man!' *
- * + * * @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 + + } } \ No newline at end of file