From 7f06ef13466be80b534d6cf511e09d1cc5bf9fc6 Mon Sep 17 00:00:00 2001 From: Jordan Irwin Date: Thu, 13 Jun 2024 13:30:22 -0700 Subject: [PATCH] Add methods to force triggers to display as chat options even if not... ...registered in transition --- .../server/entity/npc/SpeakerNPC.java | 47 +++++++++++++++++-- .../server/events/ChatOptionsEvent.java | 7 ++- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/games/stendhal/server/entity/npc/SpeakerNPC.java b/src/games/stendhal/server/entity/npc/SpeakerNPC.java index 9401e69568..02a00ab45a 100644 --- a/src/games/stendhal/server/entity/npc/SpeakerNPC.java +++ b/src/games/stendhal/server/entity/npc/SpeakerNPC.java @@ -171,6 +171,8 @@ public class SpeakerNPC extends PassiveNPC { * a set of blue words used since the start of the conversation */ private Set learnedWordsInCurrentConversation = new HashSet<>(); + /** Chat options triggers to be shown even if not registered with transition. */ + private Set forcedWordsInCurrentConversation = new HashSet<>(); /** * alternative image for website @@ -349,6 +351,7 @@ public void setAttending(final RPEntity rpentity) { } setIdea(null); learnedWordsInCurrentConversation = new HashSet<>(); + forcedWordsInCurrentConversation = new HashSet<>(); // send chat option event when NPC becomes "idle" if (wasAttending instanceof Player) { engine.addChatOptionsEvent((Player) wasAttending); @@ -515,6 +518,44 @@ protected void say(final String text, final boolean turnToPlayer) { learnWordsInCurrentConversation(text); } + /** + * Adds a trigger that should be shown as chat option even if not registered in a transition. + * + * @param triggers + * Trigger word(s). + */ + public void forceWordsInCurrentConversation(String... triggers) { + for (String trigger: triggers) { + trigger = trigger.toLowerCase(Locale.ENGLISH); + if (!forcedWordsInCurrentConversation.contains(trigger)) { + forcedWordsInCurrentConversation.add(trigger); + } + } + } + + /** + * Retrieves triggers that should be shown as chat options even if not registered in a transition. + * + * @return + * Trigger words. + */ + public Set getForcedWordsInCurrentConversation() { + return forcedWordsInCurrentConversation; + } + + /** + * Adds a word to list of learned words. + * + * @param trigger + * Word to be added. + */ + private void addLearnedWordInCurrentConversation(String trigger) { + trigger = trigger.toLowerCase(Locale.ENGLISH); + if (!hasLearnedWordInCurrentConversation(trigger)) { + learnedWordsInCurrentConversation.add(trigger); + } + } + public boolean hasLearnedWordInCurrentConversation(String trigger) { return learnedWordsInCurrentConversation.contains(trigger); } @@ -535,7 +576,7 @@ private void learnWordsInCurrentConversation(String text) { continue; } else if (next == '\'') { int end = text.indexOf('\'', pos + 2); - learnedWordsInCurrentConversation.add(text.substring(pos + 2, end).toLowerCase(Locale.ENGLISH)); + addLearnedWordInCurrentConversation(text.substring(pos + 2, end)); pos = text.indexOf('#', end); continue; } else { @@ -543,12 +584,12 @@ private void learnWordsInCurrentConversation(String text) { for (i = pos + 1; i < text.length(); i++) { char endChar = text.charAt(i); if (!Character.isJavaIdentifierPart(endChar)) { - learnedWordsInCurrentConversation.add(text.substring(pos + 1, i).toLowerCase(Locale.ENGLISH)); + addLearnedWordInCurrentConversation(text.substring(pos + 1, i)); pos = text.indexOf('#', i); continue loop; } } - learnedWordsInCurrentConversation.add(text.substring(pos + 1).toLowerCase(Locale.ENGLISH)); + addLearnedWordInCurrentConversation(text.substring(pos + 1)); break; } } diff --git a/src/games/stendhal/server/events/ChatOptionsEvent.java b/src/games/stendhal/server/events/ChatOptionsEvent.java index f7c56d87c6..a1e84bf663 100644 --- a/src/games/stendhal/server/events/ChatOptionsEvent.java +++ b/src/games/stendhal/server/events/ChatOptionsEvent.java @@ -13,10 +13,12 @@ import java.util.Arrays; import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Set; import java.util.TreeSet; import com.google.common.base.Function; @@ -156,7 +158,10 @@ private TreeSet buildChatOptions(SpeakerNPC npc, Player player, Conv } if (npc.getAttending() instanceof Player) { - for (final String trigger: npc.getKnownChatOptions()) { + final Set temp = new HashSet<>(); + temp.addAll(npc.getForcedWordsInCurrentConversation()); + temp.addAll(npc.getKnownChatOptions()); + for (final String trigger: temp) { final ChatOption copt = new ChatOption(trigger); if (!res.contains(copt)) { res.add(copt);