Skip to content

Commit

Permalink
Add methods to force triggers to display as chat options even if not...
Browse files Browse the repository at this point in the history
...registered in transition
  • Loading branch information
AntumDeluge committed Jun 13, 2024
1 parent 4e4fa1a commit 7f06ef1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
47 changes: 44 additions & 3 deletions src/games/stendhal/server/entity/npc/SpeakerNPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ public class SpeakerNPC extends PassiveNPC {
* a set of blue words used since the start of the conversation
*/
private Set<String> learnedWordsInCurrentConversation = new HashSet<>();
/** Chat options triggers to be shown even if not registered with transition. */
private Set<String> forcedWordsInCurrentConversation = new HashSet<>();

/**
* alternative image for website
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<String> 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);
}
Expand All @@ -535,20 +576,20 @@ 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 {
int i;
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;
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/games/stendhal/server/events/ChatOptionsEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -156,7 +158,10 @@ private TreeSet<ChatOption> buildChatOptions(SpeakerNPC npc, Player player, Conv
}

if (npc.getAttending() instanceof Player) {
for (final String trigger: npc.getKnownChatOptions()) {
final Set<String> 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);
Expand Down

0 comments on commit 7f06ef1

Please sign in to comment.