Updates to SpeechController.

(Not working for some reason, will debug tomorrow.)
This commit is contained in:
Jeremy Schroeder 2012-12-22 22:44:33 -05:00
parent 1baa5c0f01
commit 2be6437a63
5 changed files with 308 additions and 279 deletions

View File

@ -11,7 +11,7 @@
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<craftbukkit.version>LATEST</craftbukkit.version> <craftbukkit.version>1.4.5-R1.0</craftbukkit.version>
<citizensapi.version>2.0.6-SNAPSHOT</citizensapi.version> <citizensapi.version>2.0.6-SNAPSHOT</citizensapi.version>
<vault.version>1.2.19-SNAPSHOT</vault.version> <vault.version>1.2.19-SNAPSHOT</vault.version>
<powermock.version>1.4.12</powermock.version> <powermock.version>1.4.12</powermock.version>

View File

@ -46,7 +46,6 @@ public class Settings {
CHAT_FORMAT("npc.chat.format.no-targets", "[<npc>]: <text>"), CHAT_FORMAT("npc.chat.format.no-targets", "[<npc>]: <text>"),
CHAT_FORMAT_TO_TARGET("npc.chat.format.to-target", "[<npc>] -> You: <text>"), CHAT_FORMAT_TO_TARGET("npc.chat.format.to-target", "[<npc>] -> You: <text>"),
CHAT_FORMAT_TO_BYSTANDERS("npc.chat.prefix.to-bystanders", "[<npc>] -> [<target>]: <text>"), CHAT_FORMAT_TO_BYSTANDERS("npc.chat.prefix.to-bystanders", "[<npc>] -> [<target>]: <text>"),
CHAT_FORMAT_WITH_TARGETS_TO_TARGET("npc.chat.format.with-target-to-target", "[<npc>] -> [<targets>]: <text>"),
CHAT_FORMAT_WITH_TARGETS_TO_BYSTANDERS("npc.chat.format.with-target-to-bystanders", "[<npc>] -> [<target>]: <text>"), CHAT_FORMAT_WITH_TARGETS_TO_BYSTANDERS("npc.chat.format.with-target-to-bystanders", "[<npc>] -> [<target>]: <text>"),
CHAT_RANGE("npc.chat.options.range", 5), CHAT_RANGE("npc.chat.options.range", 5),
CHAT_BYSTANDERS_HEAR_TARGETED_CHAT("npc.chat.options.bystanders-hear-targeted-chat", true), CHAT_BYSTANDERS_HEAR_TARGETED_CHAT("npc.chat.options.bystanders-hear-targeted-chat", true),

View File

@ -1,6 +1,8 @@
package net.citizensnpcs.npc.ai.speech; package net.citizensnpcs.npc.ai.speech;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.logging.Level;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
@ -9,79 +11,102 @@ import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.ai.speech.Talkable; import net.citizensnpcs.api.ai.speech.Talkable;
import net.citizensnpcs.api.ai.speech.TalkableEntity; import net.citizensnpcs.api.ai.speech.TalkableEntity;
import net.citizensnpcs.api.ai.speech.Tongue; import net.citizensnpcs.api.ai.speech.SpeechContext;
import net.citizensnpcs.api.ai.speech.VocalChord; import net.citizensnpcs.api.ai.speech.VocalChord;
import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.util.Messaging;
public class Chat implements VocalChord { public class Chat implements VocalChord {
/*
CHAT_FORMAT("npc.chat.format.no-targets", "[<npc>]: <text>"),
CHAT_FORMAT_TO_TARGET("npc.chat.format.to-target", "[<npc>] -> You: <text>"),
CHAT_FORMAT_TO_BYSTANDERS("npc.chat.prefix.to-bystanders", "[<npc>] -> [<target>]: <text>"),
CHAT_FORMAT_WITH_TARGETS_TO_BYSTANDERS("npc.chat.format.with-target-to-bystanders", "[<npc>] -> [<targets>]: <text>"),
CHAT_RANGE("npc.chat.options.range", 5),
CHAT_BYSTANDERS_HEAR_TARGETED_CHAT("npc.chat.options.bystanders-hear-targeted-chat", true),
CHAT_MAX_NUMBER_OF_TARGETS("npc.chat.options.max-number-of-targets-to-show", 2),
CHAT_MULTIPLE_TARGETS_FORMAT("npc.chat.options.multiple-targets-format", "<target>,|<target>|& <target>|& others"),
*/
public final String VOCAL_CHORD_NAME = "chat"; public final String VOCAL_CHORD_NAME = "chat";
@Override @Override
public void talk(Tongue tongue) { public void talk(SpeechContext context) {
NPC npc = CitizensAPI.getNPCRegistry().getNPC(tongue.getTalker().getEntity()); NPC npc = CitizensAPI.getNPCRegistry().getNPC(context.getTalker().getEntity());
// If no recipients, chat to the world with CHAT_FORMAT and CHAT_RANGE settings // If no recipients, chat to the world with CHAT_FORMAT and CHAT_RANGE settings
if (!tongue.isTargeted()) { if (!context.hasRecipients()) {
String text = Setting.CHAT_FORMAT.asString().replace("<npc>", npc.getName()).replace("<text>", tongue.getContents()); String text = Setting.CHAT_FORMAT.asString().replace("<npc>", npc.getName()).replace("<text>", context.getMessage());
talkToBystanders(npc, text, tongue); talkToBystanders(npc, text, context);
return; return;
} }
// Assumed recipients at this point // Assumed recipients at this point
else if (tongue.getRecipients().size() <= 1) { // One recipient else if (context.size() <= 1) { // One recipient
String text = Setting.CHAT_FORMAT_TO_TARGET.asString().replace("<npc>", npc.getName()).replace("<text>", tongue.getContents()); String text = Setting.CHAT_FORMAT_TO_TARGET.asString().replace("<npc>", npc.getName()).replace("<text>", context.getMessage());
tongue.getRecipients().get(0).talkTo(tongue, text, this); String targetName = "";
// For each recipient
for (Talkable entity : context) {
entity.talkTo(context, text, this);
targetName = entity.getName();
}
// Check if bystanders hear targeted chat
if (!Setting.CHAT_BYSTANDERS_HEAR_TARGETED_CHAT.asBoolean()) return; if (!Setting.CHAT_BYSTANDERS_HEAR_TARGETED_CHAT.asBoolean()) return;
String bystanderText = Setting.CHAT_FORMAT_TO_BYSTANDERS.asString().replace("<npc>", npc.getName()).replace("<target>", tongue.getRecipients().get(0).getName()).replace("<text>", tongue.getContents()); // Format message with config setting and send to bystanders
talkToBystanders(npc, bystanderText, tongue); String bystanderText = Setting.CHAT_FORMAT_TO_BYSTANDERS.asString().replace("<npc>", npc.getName()).replace("<target>", targetName).replace("<text>", context.getMessage());
talkToBystanders(npc, bystanderText, context);
return; return;
} }
else { // Multiple recipients else { // Multiple recipients
// Set up text String text = Setting.CHAT_FORMAT_TO_TARGET.asString().replace("<npc>", npc.getName()).replace("<text>", context.getMessage());
String text = Setting.CHAT_FORMAT_TO_TARGET.asString().replace("<npc>", npc.getName()).replace("<text>", tongue.getContents()); List<String> targetNames = Collections.emptyList();
tongue.getRecipients().get(0).talkTo(tongue, text, this); // Talk to each recipient
for (Talkable entity : context) {
entity.talkTo(context, text, this);
targetNames.add(entity.getName());
}
if (!Setting.CHAT_BYSTANDERS_HEAR_TARGETED_CHAT.asBoolean()) return; if (!Setting.CHAT_BYSTANDERS_HEAR_TARGETED_CHAT.asBoolean()) return;
String bystanders = null; String targets = "";
bystanders = bystanders + ""; int max = Setting.CHAT_MAX_NUMBER_OF_TARGETS.asInt();
String bystanderText = Setting.CHAT_FORMAT_WITH_TARGETS_TO_BYSTANDERS.asString().replace("<npc>", npc.getName()).replace("<targets>", tongue.getRecipients().get(0).getName()).replace("<text>", tongue.getContents()); String[] format = Setting.CHAT_FORMAT_WITH_TARGETS_TO_BYSTANDERS.asString().split("\\|");
talkToBystanders(npc, bystanderText, tongue); if (format.length != 4) Messaging.log(Level.WARNING, "npc.chat.format.with-target-to-bystanders invalid!");
if (max == 1) {
targets = format[0].replace("<npc>", targetNames.get(0)) + format[3];
}
else if (max == 2 || targetNames.size() == 2) {
if (targetNames.size() == 2)
targets = format[0].replace("<npc>", targetNames.get(0)) + format[2].replace("<npc>", targetNames.get(1));
else
targets = format[0].replace("<npc>", targetNames.get(0)) + format[1].replace("<npc>", targetNames.get(1)) + format[3];
}
else if (max >= 3) {
targets = format[0].replace("<npc>", targetNames.get(0));
// TODO: Finish multiple recipients int x = 1;
for (x = 1; x < max - 1; x++) {
if (targetNames.size() - 1 == x) break;
targets = targets + format[1].replace("<npc>", targetNames.get(x));
}
if (targetNames.size() == max)
targets = targets + format[2].replace("<npc>", targetNames.get(x));
else targets = targets + format[3];
}
String bystanderText = Setting.CHAT_FORMAT_WITH_TARGETS_TO_BYSTANDERS.asString().replace("<npc>", npc.getName()).replace("<targets>", targets).replace("<text>", context.getMessage());
talkToBystanders(npc, bystanderText, context);
} }
} }
private void talkToBystanders(NPC npc, String text, Tongue tongue) { private void talkToBystanders(NPC npc, String text, SpeechContext context) {
// Get list of nearby entities // Get list of nearby entities
List<Entity> bystanderEntities = npc.getBukkitEntity().getNearbyEntities(Setting.CHAT_RANGE.asDouble(), Setting.CHAT_RANGE.asDouble(), Setting.CHAT_RANGE.asDouble()); List<Entity> bystanderEntities = npc.getBukkitEntity().getNearbyEntities(Setting.CHAT_RANGE.asDouble(), Setting.CHAT_RANGE.asDouble(), Setting.CHAT_RANGE.asDouble());
for (Entity bystander : bystanderEntities) for (Entity bystander : bystanderEntities)
// Continue if a LivingEntity, which is compatible with TalkableEntity // Continue if a LivingEntity, which is compatible with TalkableEntity
if (bystander instanceof LivingEntity) { if (bystander instanceof LivingEntity) {
// Exclude Targets // Exclude targeted recipients
if (tongue.isTargeted()) { if (context.hasRecipients()) {
for (Talkable target : tongue.getRecipients()) for (Talkable target : context)
if (target.getEntity() == bystander) continue; if (target.getEntity() == bystander) continue;
} else } else
// Found a nearby LivingEntity, make it Talkable and talkNear it // Found a nearby LivingEntity, make it Talkable and talkNear it
new TalkableEntity((LivingEntity) bystander).talkNear(tongue, text, this); new TalkableEntity((LivingEntity) bystander).talkNear(context, text, this);
} }
} }
@Override @Override
public String getName() { public String getName() {
return VOCAL_CHORD_NAME; return VOCAL_CHORD_NAME;

View File

@ -1,6 +1,6 @@
package net.citizensnpcs.npc.ai.speech; package net.citizensnpcs.npc.ai.speech;
import java.util.Collections; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -9,7 +9,7 @@ import net.citizensnpcs.api.ai.speech.VocalChord;
public class CitizensSpeechFactory implements SpeechFactory { public class CitizensSpeechFactory implements SpeechFactory {
Map<String, Class<? extends VocalChord>> registered = Collections.emptyMap(); Map<String, Class<? extends VocalChord>> registered = new HashMap<String, Class <? extends VocalChord>>();
@Override @Override
public void register(Class<? extends VocalChord> clazz, String name) { public void register(Class<? extends VocalChord> clazz, String name) {

View File

@ -10,6 +10,9 @@ import java.util.concurrent.TimeUnit;
import net.citizensnpcs.Settings.Setting; import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.ai.speech.SpeechContext;
import net.citizensnpcs.api.ai.speech.Talkable;
import net.citizensnpcs.api.ai.speech.TalkableEntity;
import net.citizensnpcs.api.event.NPCRightClickEvent; import net.citizensnpcs.api.event.NPCRightClickEvent;
import net.citizensnpcs.api.exception.NPCLoadException; import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.trait.Trait; import net.citizensnpcs.api.trait.Trait;
@ -189,7 +192,9 @@ public class Text extends Trait implements Runnable, Toggleable, Listener, Conve
currentIndex = 0; currentIndex = 0;
index = currentIndex++; index = currentIndex++;
} }
Messaging.sendWithNPC(player, Setting.CHAT_PREFIX.asString() + text.get(index), npc);
npc.getDefaultSpeechController().speak(new SpeechContext(text.get(index), new TalkableEntity(player)));
// Messaging.sendWithNPC(player, Setting.CHAT_PREFIX.asString() + text.get(index), npc);
return true; return true;
} }