From de6d1b7e18be08b078db0a68b507fd9f5a00f1fe Mon Sep 17 00:00:00 2001 From: aPunch Date: Sat, 3 Mar 2012 08:15:45 -0600 Subject: [PATCH] changes to text editing, added click to talk and some talk settings --- .../java/net/citizensnpcs/EventListen.java | 6 ++- src/main/java/net/citizensnpcs/Settings.java | 4 ++ .../command/command/NPCCommands.java | 21 +------- .../net/citizensnpcs/trait/LookClose.java | 24 ++++----- .../net/citizensnpcs/trait/Toggleable.java | 2 +- .../net/citizensnpcs/trait/text/Text.java | 54 +++++++++++++------ ...IndexPrompt.java => PageChangePrompt.java} | 25 ++++----- .../trait/text/prompt/StartPrompt.java | 20 ++++--- .../trait/text/prompt/TextAddPrompt.java | 5 +- .../trait/text/prompt/TextEditPrompt.java | 12 ++--- .../text/prompt/TextEditStartPrompt.java | 46 ++++++++++++++++ .../trait/text/prompt/TextRemovePrompt.java | 44 ++++++++------- src/main/resources/plugin.yml | 1 + 13 files changed, 162 insertions(+), 102 deletions(-) rename src/main/java/net/citizensnpcs/trait/text/prompt/{TextEditSelectIndexPrompt.java => PageChangePrompt.java} (52%) create mode 100644 src/main/java/net/citizensnpcs/trait/text/prompt/TextEditStartPrompt.java diff --git a/src/main/java/net/citizensnpcs/EventListen.java b/src/main/java/net/citizensnpcs/EventListen.java index 83a31f8e6..ef34b8237 100644 --- a/src/main/java/net/citizensnpcs/EventListen.java +++ b/src/main/java/net/citizensnpcs/EventListen.java @@ -7,6 +7,7 @@ import net.citizensnpcs.api.trait.trait.SpawnLocation; import net.citizensnpcs.editor.Editor; import net.citizensnpcs.npc.CitizensNPCManager; import net.citizensnpcs.resource.lib.EntityHumanNPC; +import net.citizensnpcs.trait.text.Text; import net.citizensnpcs.util.Messaging; import org.bukkit.Bukkit; @@ -112,7 +113,10 @@ public class EventListen implements Listener { return; } } - // TODO NPC text + if ((player.hasPermission("citizens.npc.talk") || player.hasPermission("citizens.admin")) + && player.getItemInHand().getTypeId() == Setting.TALK_ITEM.asInt()) + npc.getTrait(Text.class).sendText(player); + if (npc.getCharacter() != null) npc.getCharacter().onRightClick(npc, player); } diff --git a/src/main/java/net/citizensnpcs/Settings.java b/src/main/java/net/citizensnpcs/Settings.java index a9b5e8165..124f6a726 100644 --- a/src/main/java/net/citizensnpcs/Settings.java +++ b/src/main/java/net/citizensnpcs/Settings.java @@ -36,11 +36,15 @@ public class Settings { DATABASE_URL("database.url", ""), DATABASE_USERNAME("database.username", ""), DEBUG_MODE("general.debug-mode", false), + DEFAULT_LOOK_CLOSE("npc.default.look-close", false), + DEFAULT_RANDOM_TALKER("npc.default.random-talker", true), + DEFAULT_TALK_CLOSE("npc.default.talk-close", false), QUICK_SELECT("npc.selection.quick-select", false), SELECTION_ITEM("npc.selection.item", 280), SELECTION_MESSAGE("npc.selection.message", "You selected !"), TALK_CLOSE_MAXIMUM_COOLDOWN("npc.talk.max-cooldown", 60), TALK_CLOSE_MINIMUM_COOLDOWN("npc.talk.min-cooldown", 30), + TALK_ITEM("npc.talk-item", 340), USE_DATABASE("use-database", false); private String path; diff --git a/src/main/java/net/citizensnpcs/command/command/NPCCommands.java b/src/main/java/net/citizensnpcs/command/command/NPCCommands.java index 89bd2fb78..1a3f43a46 100644 --- a/src/main/java/net/citizensnpcs/command/command/NPCCommands.java +++ b/src/main/java/net/citizensnpcs/command/command/NPCCommands.java @@ -19,7 +19,6 @@ import net.citizensnpcs.command.exception.CommandException; import net.citizensnpcs.command.exception.NoPermissionsException; import net.citizensnpcs.npc.CitizensNPCManager; import net.citizensnpcs.trait.LookClose; -import net.citizensnpcs.trait.text.Text; import net.citizensnpcs.util.Messaging; import net.citizensnpcs.util.Paginator; import net.citizensnpcs.util.StringHelper; @@ -207,10 +206,8 @@ public class NPCCommands { max = 1, permission = "npc.lookclose") public void lookClose(CommandContext args, Player player, NPC npc) { - LookClose trait = npc.getTrait(LookClose.class); - trait.toggle(); String msg = StringHelper.wrap(npc.getName()) + " will " - + (trait.shouldLookClose() ? "now rotate" : "no longer rotate"); + + (npc.getTrait(LookClose.class).toggle() ? "now rotate" : "no longer rotate"); Messaging.send(player, msg += " when a player is nearby."); } @@ -333,22 +330,6 @@ public class NPCCommands { + " Use '/npc tphere' to teleport the NPC to your location."); } - @Command( - aliases = { "npc" }, - usage = "talkclose", - desc = "Toggle whether an NPC talks when a player is near", - modifiers = { "talkclose", "talk" }, - min = 1, - max = 1, - permission = "npc.talkclose") - public void talkClose(CommandContext args, Player player, NPC npc) { - Text trait = npc.getTrait(Text.class); - trait.toggle(); - String msg = StringHelper.wrap(npc.getName()) + " will " - + (trait.shouldTalkClose() ? "now talk" : "no longer talk"); - Messaging.send(player, msg += " when a player is nearby."); - } - @Command( aliases = { "npc" }, usage = "tp", diff --git a/src/main/java/net/citizensnpcs/trait/LookClose.java b/src/main/java/net/citizensnpcs/trait/LookClose.java index 3c8a39def..9514d5b05 100644 --- a/src/main/java/net/citizensnpcs/trait/LookClose.java +++ b/src/main/java/net/citizensnpcs/trait/LookClose.java @@ -1,5 +1,6 @@ package net.citizensnpcs.trait; +import net.citizensnpcs.Settings.Setting; import net.citizensnpcs.api.exception.NPCLoadException; import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.trait.SaveId; @@ -14,7 +15,7 @@ import org.bukkit.entity.Entity; @SaveId("look-close") public class LookClose extends Trait implements Runnable, Toggleable { private final NPC npc; - private boolean shouldLookClose; + private boolean lookClose = Setting.DEFAULT_LOOK_CLOSE.asBoolean(); public LookClose(NPC npc) { this.npc = npc; @@ -45,37 +46,30 @@ public class LookClose extends Trait implements Runnable, Toggleable { @Override public void load(DataKey key) throws NPCLoadException { - shouldLookClose = key.getBoolean(""); + lookClose = key.getBoolean(""); } @Override public void run() { EntityLiving search = null; CitizensNPC handle = (CitizensNPC) npc; - if ((search = handle.getHandle().world.findNearbyPlayer(handle.getHandle(), 5)) != null && shouldLookClose) + if ((search = handle.getHandle().world.findNearbyPlayer(handle.getHandle(), 5)) != null && lookClose) faceEntity(handle, search.getBukkitEntity()); } @Override public void save(DataKey key) { - key.setBoolean("", shouldLookClose); - } - - public void setLookClose(boolean shouldLookClose) { - this.shouldLookClose = shouldLookClose; - } - - public boolean shouldLookClose() { - return shouldLookClose; + key.setBoolean("", lookClose); } @Override - public void toggle() { - shouldLookClose = !shouldLookClose; + public boolean toggle() { + lookClose = !lookClose; + return lookClose; } @Override public String toString() { - return "LookClose{" + shouldLookClose + "}"; + return "LookClose{" + lookClose + "}"; } } \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/trait/Toggleable.java b/src/main/java/net/citizensnpcs/trait/Toggleable.java index a2f2e2673..471b437fd 100644 --- a/src/main/java/net/citizensnpcs/trait/Toggleable.java +++ b/src/main/java/net/citizensnpcs/trait/Toggleable.java @@ -2,5 +2,5 @@ package net.citizensnpcs.trait; public interface Toggleable { - public void toggle(); + public boolean toggle(); } \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/trait/text/Text.java b/src/main/java/net/citizensnpcs/trait/text/Text.java index 5f596ad7f..c2290d148 100644 --- a/src/main/java/net/citizensnpcs/trait/text/Text.java +++ b/src/main/java/net/citizensnpcs/trait/text/Text.java @@ -33,7 +33,9 @@ public class Text extends Trait implements Runnable, Toggleable { private final NPC npc; private final List text = new ArrayList(); private final Map cooldowns = new HashMap(); - private boolean talkClose; + private boolean talkClose = Setting.DEFAULT_TALK_CLOSE.asBoolean(); + private boolean randomTalker = Setting.DEFAULT_RANDOM_TALKER.asBoolean(); + private int currentIndex; public Text(NPC npc) { this.npc = npc; @@ -47,18 +49,22 @@ public class Text extends Trait implements Runnable, Toggleable { if (key.keyExists("talk-close")) talkClose = key.getBoolean("talk-close"); + if (key.keyExists("random-talker")) + randomTalker = key.getBoolean("random-talker"); } @Override public void save(DataKey key) { key.setBoolean("talk-close", talkClose); + key.setBoolean("random-talker", randomTalker); for (int i = 0; i < text.size(); i++) key.setString(String.valueOf(i), text.get(i)); } @Override - public void toggle() { + public boolean toggle() { talkClose = !talkClose; + return talkClose; } @Override @@ -73,14 +79,15 @@ public class Text extends Trait implements Runnable, Toggleable { return; cooldowns.remove(player.getName()); } - sendRandomText(player); - // Add a cooldown if the text was successfully sent - Calendar wait = Calendar.getInstance(); - wait.add( - Calendar.SECOND, - (new Random().nextInt(Setting.TALK_CLOSE_MAXIMUM_COOLDOWN.asInt()) + Setting.TALK_CLOSE_MINIMUM_COOLDOWN - .asInt())); - cooldowns.put(player.getName(), wait); + if (sendText(player)) { + // Add a cooldown if the text was successfully sent + Calendar wait = Calendar.getInstance(); + wait.add( + Calendar.SECOND, + (new Random().nextInt(Setting.TALK_CLOSE_MAXIMUM_COOLDOWN.asInt()) + Setting.TALK_CLOSE_MINIMUM_COOLDOWN + .asInt())); + cooldowns.put(player.getName(), wait); + } } } @@ -94,10 +101,6 @@ public class Text extends Trait implements Runnable, Toggleable { return builder.toString(); } - public boolean shouldTalkClose() { - return talkClose; - } - public Editor getEditor(final Player player) { final StartPrompt startPrompt = new StartPrompt(this); return new Editor() { @@ -142,7 +145,26 @@ public class Text extends Trait implements Runnable, Toggleable { return paginator.sendPage(player, page); } - private void sendRandomText(Player player) { - npc.chat(player, text.get(new Random().nextInt(text.size()))); + public boolean sendText(Player player) { + if (text.size() == 0) + return false; + + int index = 0; + if (randomTalker) + index = new Random().nextInt(text.size()); + else { + if (currentIndex > text.size() - 1) + currentIndex = 0; + index = currentIndex++; + } + Messaging.log("current: " + currentIndex); + Messaging.log("index: " + index); + npc.chat(player, text.get(index)); + return true; + } + + public boolean toggleRandomTalker() { + randomTalker = !randomTalker; + return randomTalker; } } \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/trait/text/prompt/TextEditSelectIndexPrompt.java b/src/main/java/net/citizensnpcs/trait/text/prompt/PageChangePrompt.java similarity index 52% rename from src/main/java/net/citizensnpcs/trait/text/prompt/TextEditSelectIndexPrompt.java rename to src/main/java/net/citizensnpcs/trait/text/prompt/PageChangePrompt.java index 566c779fd..3f11f8811 100644 --- a/src/main/java/net/citizensnpcs/trait/text/prompt/TextEditSelectIndexPrompt.java +++ b/src/main/java/net/citizensnpcs/trait/text/prompt/PageChangePrompt.java @@ -10,35 +10,30 @@ import org.bukkit.conversations.NumericPrompt; import org.bukkit.conversations.Prompt; import org.bukkit.entity.Player; -public class TextEditSelectIndexPrompt extends NumericPrompt { +public class PageChangePrompt extends NumericPrompt { private Text text; - public TextEditSelectIndexPrompt(Text text) { + public PageChangePrompt(Text text) { this.text = text; } @Override public Prompt acceptValidatedInput(ConversationContext context, Number input) { - context.setSessionData("index", input.intValue()); - Messaging.send((Player) context.getForWhom(), "Now editing the entry at index " + input.intValue() - + "."); - return new TextEditPrompt(text); + Player player = (Player) context.getForWhom(); + if (!text.sendPage(player, input.intValue())) { + Messaging.sendError(player, "Invalid page number."); + return new StartPrompt(text); + } + return (Prompt) context.getSessionData("previous"); } @Override public String getFailedValidationText(ConversationContext context, String input) { - return ChatColor.RED + "'" + input + "' is not a valid index!"; + return ChatColor.RED + "Invalid page number."; } @Override public String getPromptText(ConversationContext context) { - Player player = (Player) context.getForWhom(); - text.sendPage(player, 1); - return StringHelper.parseColors("Enter the index of the entry you wish to edit."); - } - - @Override - public boolean isNumberValid(ConversationContext context, Number input) { - return text.hasIndex(input.intValue()); + return StringHelper.parseColors("Enter a page number to view more text entries."); } } \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/trait/text/prompt/StartPrompt.java b/src/main/java/net/citizensnpcs/trait/text/prompt/StartPrompt.java index bf5319c16..6d60c4ed7 100644 --- a/src/main/java/net/citizensnpcs/trait/text/prompt/StartPrompt.java +++ b/src/main/java/net/citizensnpcs/trait/text/prompt/StartPrompt.java @@ -17,15 +17,21 @@ public class StartPrompt extends StringPrompt { } @Override - public Prompt acceptInput(ConversationContext context, String string) { - if (string.equalsIgnoreCase("add")) + public Prompt acceptInput(ConversationContext context, String input) { + if (input.equalsIgnoreCase("add")) return new TextAddPrompt(text); - else if (string.equalsIgnoreCase("edit")) - return new TextEditSelectIndexPrompt(text); - else if (string.equalsIgnoreCase("remove")) + else if (input.equalsIgnoreCase("edit")) + return new TextEditStartPrompt(text); + else if (input.equalsIgnoreCase("remove")) return new TextRemovePrompt(text); else { - Messaging.sendError((Player) context.getForWhom(), "Invalid edit type."); + if (input.equalsIgnoreCase("random")) + Messaging.send((Player) context.getForWhom(), "Random talker set to " + + text.toggleRandomTalker() + "."); + else if (input.equalsIgnoreCase("close")) { + Messaging.send((Player) context.getForWhom(), "Close talker set to " + text.toggle() + "."); + } else + Messaging.sendError((Player) context.getForWhom(), "Invalid edit type."); return new StartPrompt(text); } } @@ -33,6 +39,6 @@ public class StartPrompt extends StringPrompt { @Override public String getPromptText(ConversationContext context) { return StringHelper - .parseColors("Type add to add an entry, edit to edit entries, and remove to remove entries."); + .parseColors("Type add to add an entry, edit to edit entries, remove to remove entries, close to toggle the NPC as a close talker, and random to toggle the NPC as a random talker."); } } \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/trait/text/prompt/TextAddPrompt.java b/src/main/java/net/citizensnpcs/trait/text/prompt/TextAddPrompt.java index bed867294..5ece6ae82 100644 --- a/src/main/java/net/citizensnpcs/trait/text/prompt/TextAddPrompt.java +++ b/src/main/java/net/citizensnpcs/trait/text/prompt/TextAddPrompt.java @@ -1,12 +1,14 @@ package net.citizensnpcs.trait.text.prompt; import net.citizensnpcs.trait.text.Text; +import net.citizensnpcs.util.Messaging; import net.citizensnpcs.util.StringHelper; import org.bukkit.ChatColor; import org.bukkit.conversations.ConversationContext; import org.bukkit.conversations.Prompt; import org.bukkit.conversations.StringPrompt; +import org.bukkit.entity.Player; public class TextAddPrompt extends StringPrompt { private Text text; @@ -18,7 +20,8 @@ public class TextAddPrompt extends StringPrompt { @Override public Prompt acceptInput(ConversationContext context, String input) { text.add(input); - context.getForWhom().sendRawMessage(StringHelper.parseColors("Added the entry " + input + ".")); + Messaging.send((Player) context.getForWhom(), StringHelper.parseColors("Added the entry " + input + + ".")); return new StartPrompt(text); } diff --git a/src/main/java/net/citizensnpcs/trait/text/prompt/TextEditPrompt.java b/src/main/java/net/citizensnpcs/trait/text/prompt/TextEditPrompt.java index e754e7289..88e7dbc41 100644 --- a/src/main/java/net/citizensnpcs/trait/text/prompt/TextEditPrompt.java +++ b/src/main/java/net/citizensnpcs/trait/text/prompt/TextEditPrompt.java @@ -2,8 +2,8 @@ package net.citizensnpcs.trait.text.prompt; import net.citizensnpcs.trait.text.Text; import net.citizensnpcs.util.Messaging; -import net.citizensnpcs.util.StringHelper; +import org.bukkit.ChatColor; import org.bukkit.conversations.ConversationContext; import org.bukkit.conversations.Prompt; import org.bukkit.conversations.StringPrompt; @@ -18,15 +18,15 @@ public class TextEditPrompt extends StringPrompt { @Override public Prompt acceptInput(ConversationContext context, String input) { - text.edit((Integer) context.getSessionData("index"), input); - Messaging.send((Player) context.getForWhom(), "Changed entry at index " + context.getSessionData("index") - + " to " + input + "."); + int index = (Integer) context.getSessionData("index"); + text.edit(index, input); + Messaging.send((Player) context.getForWhom(), "Changed entry at index " + index + " to " + input + + "."); return new StartPrompt(text); } @Override public String getPromptText(ConversationContext context) { - return StringHelper.parseColors("Enter text to change the entry at the index " - + context.getSessionData("index") + "."); + return ChatColor.GREEN + "Enter text to edit the entry."; } } \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/trait/text/prompt/TextEditStartPrompt.java b/src/main/java/net/citizensnpcs/trait/text/prompt/TextEditStartPrompt.java new file mode 100644 index 000000000..b92d9f7d4 --- /dev/null +++ b/src/main/java/net/citizensnpcs/trait/text/prompt/TextEditStartPrompt.java @@ -0,0 +1,46 @@ +package net.citizensnpcs.trait.text.prompt; + +import net.citizensnpcs.trait.text.Text; +import net.citizensnpcs.util.Messaging; +import net.citizensnpcs.util.StringHelper; + +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 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.sendError(player, "'" + index + "' is not a valid index!"); + return new StartPrompt(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.sendError(player, "Invalid input."); + return new StartPrompt(text); + } + + @Override + public String getPromptText(ConversationContext context) { + text.sendPage(((Player) context.getForWhom()), 1); + return StringHelper + .parseColors("Enter the index of the entry you wish to edit or page to view more pages."); + } +} \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/trait/text/prompt/TextRemovePrompt.java b/src/main/java/net/citizensnpcs/trait/text/prompt/TextRemovePrompt.java index 99d2bfaae..540543d7d 100644 --- a/src/main/java/net/citizensnpcs/trait/text/prompt/TextRemovePrompt.java +++ b/src/main/java/net/citizensnpcs/trait/text/prompt/TextRemovePrompt.java @@ -2,14 +2,14 @@ package net.citizensnpcs.trait.text.prompt; import net.citizensnpcs.trait.text.Text; import net.citizensnpcs.util.Messaging; +import net.citizensnpcs.util.StringHelper; -import org.bukkit.ChatColor; import org.bukkit.conversations.ConversationContext; -import org.bukkit.conversations.NumericPrompt; import org.bukkit.conversations.Prompt; +import org.bukkit.conversations.StringPrompt; import org.bukkit.entity.Player; -public class TextRemovePrompt extends NumericPrompt { +public class TextRemovePrompt extends StringPrompt { private Text text; public TextRemovePrompt(Text text) { @@ -17,27 +17,31 @@ public class TextRemovePrompt extends NumericPrompt { } @Override - public Prompt acceptValidatedInput(ConversationContext context, Number input) { - int index = input.intValue(); - text.remove(index); - Messaging.send((Player) context.getForWhom(), "Removed entry at index " + index + "."); + public Prompt acceptInput(ConversationContext context, String input) { + Player player = (Player) context.getForWhom(); + try { + int index = Integer.parseInt(input); + if (!text.hasIndex(index)) { + Messaging.sendError(player, "'" + index + "' is not a valid index!"); + return new StartPrompt(text); + } + text.remove(index); + Messaging.send(player, "Removed entry at index " + index + "."); + return new StartPrompt(text); + } catch (NumberFormatException ex) { + if (input.equalsIgnoreCase("page")) { + context.setSessionData("previous", this); + return new PageChangePrompt(text); + } + } + Messaging.sendError(player, "Invalid input."); return new StartPrompt(text); } - @Override - public String getFailedValidationText(ConversationContext context, String input) { - return ChatColor.RED + "'" + input + "' is not a valid index!"; - } - @Override public String getPromptText(ConversationContext context) { - Player player = (Player) context.getForWhom(); - text.sendPage(player, 1); - return ChatColor.GREEN + "Enter the index of the entry you wish to remove."; - } - - @Override - public boolean isNumberValid(ConversationContext context, Number input) { - return text.hasIndex(input.intValue()); + text.sendPage(((Player) context.getForWhom()), 1); + return StringHelper + .parseColors("Enter the index of the entry you wish to remove or page to view more pages."); } } \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 6d7865a67..d85ff1eb9 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -32,6 +32,7 @@ permissions: citizens.npc.rename: true citizens.npc.select: true citizens.npc.spawn: true + citizens.npc.talk: true citizens.npc.tp: true citizens.npc.tphere: true citizens.npc.lookclose: true \ No newline at end of file