Rework /npc text

This commit is contained in:
fullwall 2022-06-26 13:57:51 +08:00
parent e54e4434cc
commit 9ce4a9020f
7 changed files with 71 additions and 186 deletions

View File

@ -1,38 +0,0 @@
package net.citizensnpcs.trait.text;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.util.Messages;
import org.bukkit.ChatColor;
import org.bukkit.conversations.ConversationContext;
import org.bukkit.conversations.NumericPrompt;
import org.bukkit.conversations.Prompt;
import org.bukkit.entity.Player;
public class PageChangePrompt extends NumericPrompt {
private final Text text;
public PageChangePrompt(Text text) {
this.text = text;
}
@Override
public Prompt acceptValidatedInput(ConversationContext context, Number input) {
Player player = (Player) context.getForWhom();
if (!text.sendPage(player, input.intValue())) {
Messaging.sendErrorTr(player, Messages.TEXT_EDITOR_INVALID_PAGE);
return new TextBasePrompt(text);
}
return (Prompt) context.getSessionData("previous");
}
@Override
public String getFailedValidationText(ConversationContext context, String input) {
return ChatColor.RED + Messaging.tr(Messages.TEXT_EDITOR_INVALID_PAGE);
}
@Override
public String getPromptText(ConversationContext context) {
return Messaging.tr(Messages.TEXT_EDITOR_PAGE_PROMPT);
}
}

View File

@ -8,6 +8,7 @@ import java.util.UUID;
import java.util.concurrent.TimeUnit;
import org.bukkit.GameMode;
import org.bukkit.command.CommandSender;
import org.bukkit.conversations.Conversation;
import org.bukkit.conversations.ConversationAbandonedEvent;
import org.bukkit.conversations.ConversationAbandonedListener;
@ -32,7 +33,6 @@ import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.api.util.Paginator;
import net.citizensnpcs.editor.Editor;
import net.citizensnpcs.trait.HologramTrait;
import net.citizensnpcs.trait.Toggleable;
import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.Util;
@ -40,7 +40,7 @@ import net.citizensnpcs.util.Util;
* Persists text metadata, i.e. text that will be said by an NPC on certain triggers.
*/
@TraitName("text")
public class Text extends Trait implements Runnable, Toggleable, Listener, ConversationAbandonedListener {
public class Text extends Trait implements Runnable, Listener, ConversationAbandonedListener {
private int bubbleTicks;
private final Map<UUID, Long> cooldowns = Maps.newHashMap();
private int currentIndex;
@ -138,6 +138,14 @@ public class Text extends Trait implements Runnable, Toggleable, Listener, Conve
return index >= 0 && text.size() > index;
}
boolean hasPage(int page) {
return new Paginator(text.size()).hasPage(page);
}
public boolean isRandomTalker() {
return randomTalker;
}
@Override
public void load(DataKey key) throws NPCLoadException {
text.clear();
@ -230,11 +238,11 @@ public class Text extends Trait implements Runnable, Toggleable, Listener, Conve
}
}
boolean sendPage(Player player, int page) {
Paginator paginator = new Paginator().header("Current Texts");
boolean sendPage(CommandSender player, int page) {
Paginator paginator = new Paginator().header("Current Texts").enablePageSwitcher();
for (int i = 0; i < text.size(); i++)
paginator.addLine("<a>" + i + " <7>- <e>" + text.get(i));
paginator.addLine("<a>" + text.get(i) + " (<<&eedit:suggest(edit " + i + " )>>) (<<&c-:command(remove " + i
+ "):Remove this text>>)");
return paginator.sendPage(player, page);
}
@ -303,12 +311,9 @@ public class Text extends Trait implements Runnable, Toggleable, Listener, Conve
return talkClose;
}
/**
* Toggles talking to nearby Players.
*/
@Override
@Deprecated
public boolean toggle() {
return (talkClose = !talkClose);
return toggleTalkClose();
}
/**
@ -335,5 +340,20 @@ public class Text extends Trait implements Runnable, Toggleable, Listener, Conve
return (speechBubbles = !speechBubbles);
}
/**
* Toggles talking to nearby Players.
*/
public boolean toggleTalkClose() {
return (talkClose = !talkClose);
}
public boolean useRealisticLooking() {
return realisticLooker;
}
public boolean useSpeechBubbles() {
return speechBubbles;
}
private static Random RANDOM = Util.getFastRandom();
}

View File

@ -31,9 +31,19 @@ public class TextBasePrompt extends StringPrompt {
text.add(Joiner.on(' ').join(Arrays.copyOfRange(parts, 1, parts.length)));
return this;
} else if (input.equalsIgnoreCase("edit")) {
return new TextEditStartPrompt(text);
int index = Integer.parseInt(parts[1]);
if (!text.hasIndex(index)) {
Messaging.sendErrorTr(sender, Messages.TEXT_EDITOR_INVALID_INDEX, index);
} else {
text.edit(index, Joiner.on(' ').join(Arrays.copyOfRange(parts, 2, parts.length)));
}
} else if (input.equalsIgnoreCase("remove")) {
return new TextRemovePrompt(text);
int index = Integer.parseInt(parts[1]);
if (!text.hasIndex(index)) {
Messaging.sendErrorTr(sender, Messages.TEXT_EDITOR_INVALID_INDEX);
} else {
text.remove(index);
}
} else if (input.equalsIgnoreCase("delay")) {
try {
int delay = Integer.parseInt(parts[1]);
@ -45,13 +55,13 @@ public class TextBasePrompt extends StringPrompt {
Messaging.sendErrorTr(sender, Messages.TEXT_EDITOR_INVALID_DELAY);
}
} else if (input.equalsIgnoreCase("random")) {
Messaging.sendTr(sender, Messages.TEXT_EDITOR_RANDOM_TALKER_SET, text.toggleRandomTalker());
text.toggleRandomTalker();
} else if (original.trim().equalsIgnoreCase("realistic looking")) {
Messaging.sendTr(sender, Messages.TEXT_EDITOR_REALISTIC_LOOKING_SET, text.toggleRealisticLooking());
text.toggleRealisticLooking();
} else if (original.trim().equalsIgnoreCase("speech bubbles")) {
Messaging.sendTr(sender, Messages.TEXT_EDITOR_SPEECH_BUBBLES_SET, text.toggleSpeechBubbles());
text.toggleSpeechBubbles();
} else if (input.equalsIgnoreCase("close") || original.trim().equalsIgnoreCase("talk close")) {
Messaging.sendTr(sender, Messages.TEXT_EDITOR_CLOSE_TALKER_SET, text.toggle());
text.toggleTalkClose();
} else if (input.equalsIgnoreCase("range")) {
try {
double range = Math.min(Math.max(0, Double.parseDouble(parts[1])), Setting.MAX_TEXT_RANGE.asDouble());
@ -69,25 +79,38 @@ public class TextBasePrompt extends StringPrompt {
} else {
Messaging.sendErrorTr(sender, Messages.TEXT_EDITOR_MISSING_ITEM_PATTERN);
}
} else if (input.equalsIgnoreCase("help")) {
context.setSessionData("said-text", false);
Messaging.send(sender, getPromptText(context));
} else if (input.equalsIgnoreCase("page")) {
try {
int page = Integer.parseInt(parts[1]);
if (!text.hasPage(page)) {
Messaging.sendErrorTr(sender, Messages.TEXT_EDITOR_INVALID_PAGE);
}
context.setSessionData("page", page);
return this;
} catch (NumberFormatException e) {
Messaging.sendErrorTr(sender, Messages.TEXT_EDITOR_INVALID_PAGE);
}
} else {
Messaging.sendErrorTr(sender, Messages.TEXT_EDITOR_INVALID_EDIT_TYPE);
return this;
}
Messaging.send(sender, getPromptText(context));
return this;
}
private String colorToggleableText(boolean enabled) {
return (enabled ? ChatColor.GREEN : ChatColor.RED).toString();
}
@Override
public String getPromptText(ConversationContext context) {
if (Boolean.TRUE == context.getSessionData("said-text")) {
text.sendPage(((Player) context.getForWhom()), 1);
} else {
Messaging.send((Player) context.getForWhom(), Messaging.tr(Messages.TEXT_EDITOR_START_PROMPT));
text.sendPage(((Player) context.getForWhom()), 1);
context.setSessionData("said-text", Boolean.TRUE);
}
Messaging.send((Player) context.getForWhom(),
Messaging.tr(Messages.TEXT_EDITOR_START_PROMPT, colorToggleableText(text.shouldTalkClose()),
colorToggleableText(text.isRandomTalker()), colorToggleableText(text.useSpeechBubbles()),
colorToggleableText(text.useRealisticLooking())));
int page = context.getSessionData("page") == null ? 1 : (int) context.getSessionData("page");
text.sendPage(((Player) context.getForWhom()), page);
return "";
}
}

View File

@ -1,31 +0,0 @@
package net.citizensnpcs.trait.text;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.util.Messages;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.conversations.ConversationContext;
import org.bukkit.conversations.Prompt;
import org.bukkit.conversations.StringPrompt;
public class TextEditPrompt extends StringPrompt {
private final Text text;
public TextEditPrompt(Text text) {
this.text = text;
}
@Override
public Prompt acceptInput(ConversationContext context, String input) {
int index = (Integer) context.getSessionData("index");
text.edit(index, input);
Messaging.sendTr((CommandSender) context.getForWhom(), Messages.TEXT_EDITOR_EDITED_TEXT, index, input);
return new TextBasePrompt(text);
}
@Override
public String getPromptText(ConversationContext context) {
return ChatColor.GREEN + Messaging.tr(Messages.TEXT_EDITOR_EDIT_PROMPT);
}
}

View File

@ -1,44 +0,0 @@
package net.citizensnpcs.trait.text;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.util.Messages;
import org.bukkit.conversations.ConversationContext;
import org.bukkit.conversations.Prompt;
import org.bukkit.conversations.StringPrompt;
import org.bukkit.entity.Player;
public class TextEditStartPrompt extends StringPrompt {
private final Text text;
public TextEditStartPrompt(Text text) {
this.text = text;
}
@Override
public Prompt acceptInput(ConversationContext context, String input) {
Player player = (Player) context.getForWhom();
try {
int index = Integer.parseInt(input);
if (!text.hasIndex(index)) {
Messaging.sendErrorTr(player, Messages.TEXT_EDITOR_INVALID_INDEX, index);
return new TextBasePrompt(text);
}
context.setSessionData("index", index);
return new TextEditPrompt(text);
} catch (NumberFormatException ex) {
if (input.equalsIgnoreCase("page")) {
context.setSessionData("previous", this);
return new PageChangePrompt(text);
}
}
Messaging.sendErrorTr(player, Messages.TEXT_EDITOR_INVALID_INPUT);
return new TextBasePrompt(text);
}
@Override
public String getPromptText(ConversationContext context) {
text.sendPage(((Player) context.getForWhom()), 1);
return Messaging.tr(Messages.TEXT_EDITOR_EDIT_BEGIN_PROMPT);
}
}

View File

@ -1,45 +0,0 @@
package net.citizensnpcs.trait.text;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.util.Messages;
import org.bukkit.conversations.ConversationContext;
import org.bukkit.conversations.Prompt;
import org.bukkit.conversations.StringPrompt;
import org.bukkit.entity.Player;
public class TextRemovePrompt extends StringPrompt {
private final Text text;
public TextRemovePrompt(Text text) {
this.text = text;
}
@Override
public Prompt acceptInput(ConversationContext context, String input) {
Player player = (Player) context.getForWhom();
try {
int index = Integer.parseInt(input);
if (!text.hasIndex(index)) {
Messaging.sendErrorTr(player, Messages.TEXT_EDITOR_INVALID_INDEX, index);
return new TextBasePrompt(text);
}
text.remove(index);
Messaging.sendTr(player, Messages.TEXT_EDITOR_REMOVED_ENTRY, index);
return new TextBasePrompt(text);
} catch (NumberFormatException ex) {
if (input.equalsIgnoreCase("page")) {
context.setSessionData("previous", this);
return new PageChangePrompt(text);
}
}
Messaging.sendErrorTr(player, Messages.TEXT_EDITOR_INVALID_INPUT);
return new TextBasePrompt(text);
}
@Override
public String getPromptText(ConversationContext context) {
text.sendPage(((Player) context.getForWhom()), 1);
return Messaging.tr(Messages.TEXT_EDITOR_REMOVE_PROMPT);
}
}

View File

@ -342,7 +342,7 @@ citizens.editors.text.realistic-looking-set=[[Realistic looking]] set to [[{0}]]
citizens.editors.text.speech-bubbles-set=[[Speech bubbles]] set to [[{0}]].
citizens.editors.text.remove-prompt=Enter the index of the entry you wish to remove or [[page]] to view more pages.
citizens.editors.text.removed-entry=[[Removed]] entry at index [[{0}]].
citizens.editors.text.start-prompt=Type [[add]] <text> to add an entry, [[edit]] to edit entries, [[remove]] to remove entries, [[close]] to toggle the NPC to send messages when players get close, [[item]] to set the talk item in hand pattern (set to [[default]] to clear), [[range]] to set the talking range, [[delay]] to set the talking delay in seconds and [[random]] to toggle the NPC as a random talker. Type [[help]] to show this again.
citizens.editors.text.start-prompt=<<[[add:command(add ):Add text>> | <<[[item:suggest(item ):Set the talk item in hand pattern (set to ''default'' to clear)>> | <<[[range:suggest(range ):Set the talking range in blocks>> | <<[[delay:suggest(delay ):Set the talking delay in seconds>><br><<{0}talk close:command(close):Toggle sending messages when players get close>> | <<{1}random:command(random):Toggle random talking>> | <<{2}speech bubbles:command(speech bubbles):Toggle showing text as holograms instead of messages>> | <<{3}realistic:command(realistic looking):Toggle requiring line of sight before speaking>>
citizens.editors.text.talk-item-set=[[Talk item pattern]] set to [[{0}]].
citizens.editors.text.text-list-header=Current text:
citizens.editors.waypoints.wander.editing-regions-stop=Exited the region editor.