From 8f0af02292b996ee16153afdcb82c35fc519452f Mon Sep 17 00:00:00 2001 From: aPunch Date: Fri, 2 Mar 2012 14:42:34 -0600 Subject: [PATCH] initial text editing using Conversations API --- src/main/java/net/citizensnpcs/Citizens.java | 4 +- .../java/net/citizensnpcs/EventListen.java | 1 + .../command/command/EditorCommands.java | 4 +- .../citizensnpcs/editor/EquipmentEditor.java | 10 +- .../net/citizensnpcs/editor/TextEditor.java | 12 -- .../net/citizensnpcs/trait/LookClose.java | 1 - .../citizensnpcs/{ => trait}/Toggleable.java | 2 +- .../net/citizensnpcs/trait/text/Text.java | 120 ++++++++++++++++++ .../trait/text/prompt/StartPrompt.java | 38 ++++++ .../trait/text/prompt/TextAddPrompt.java | 28 ++++ .../trait/text/prompt/TextEditPrompt.java | 32 +++++ .../prompt/TextEditSelectIndexPrompt.java | 39 ++++++ .../trait/text/prompt/TextRemovePrompt.java | 39 ++++++ .../waypoint/LinearWaypointProvider.java | 5 +- .../trait/waypoint/WaypointProvider.java | 1 + 15 files changed, 311 insertions(+), 25 deletions(-) delete mode 100644 src/main/java/net/citizensnpcs/editor/TextEditor.java rename src/main/java/net/citizensnpcs/{ => trait}/Toggleable.java (81%) create mode 100644 src/main/java/net/citizensnpcs/trait/text/Text.java create mode 100644 src/main/java/net/citizensnpcs/trait/text/prompt/StartPrompt.java create mode 100644 src/main/java/net/citizensnpcs/trait/text/prompt/TextAddPrompt.java create mode 100644 src/main/java/net/citizensnpcs/trait/text/prompt/TextEditPrompt.java create mode 100644 src/main/java/net/citizensnpcs/trait/text/prompt/TextEditSelectIndexPrompt.java create mode 100644 src/main/java/net/citizensnpcs/trait/text/prompt/TextRemovePrompt.java diff --git a/src/main/java/net/citizensnpcs/Citizens.java b/src/main/java/net/citizensnpcs/Citizens.java index e2f209580..4b611f561 100644 --- a/src/main/java/net/citizensnpcs/Citizens.java +++ b/src/main/java/net/citizensnpcs/Citizens.java @@ -38,6 +38,7 @@ import net.citizensnpcs.command.exception.WrappedCommandException; import net.citizensnpcs.editor.Editor; import net.citizensnpcs.npc.CitizensNPCManager; import net.citizensnpcs.trait.LookClose; +import net.citizensnpcs.trait.text.Text; import net.citizensnpcs.trait.waypoint.Waypoints; import net.citizensnpcs.util.Messaging; import net.citizensnpcs.util.Metrics; @@ -63,7 +64,8 @@ public class Citizens extends JavaPlugin { private volatile CitizensNPCManager npcManager; private Storage saves; private final InstanceFactory traitManager = DefaultInstanceFactory.create(Owner.class, Spawned.class, - LookClose.class, SpawnLocation.class, Inventory.class, MobType.class, Waypoints.class, Equipment.class); + LookClose.class, SpawnLocation.class, Inventory.class, MobType.class, Waypoints.class, Equipment.class, + Text.class); public InstanceFactory getCharacterManager() { return characterManager; diff --git a/src/main/java/net/citizensnpcs/EventListen.java b/src/main/java/net/citizensnpcs/EventListen.java index 16262d3f6..83a31f8e6 100644 --- a/src/main/java/net/citizensnpcs/EventListen.java +++ b/src/main/java/net/citizensnpcs/EventListen.java @@ -112,6 +112,7 @@ public class EventListen implements Listener { return; } } + // TODO NPC text if (npc.getCharacter() != null) npc.getCharacter().onRightClick(npc, player); } diff --git a/src/main/java/net/citizensnpcs/command/command/EditorCommands.java b/src/main/java/net/citizensnpcs/command/command/EditorCommands.java index b3be6d00e..ee57e8500 100644 --- a/src/main/java/net/citizensnpcs/command/command/EditorCommands.java +++ b/src/main/java/net/citizensnpcs/command/command/EditorCommands.java @@ -10,7 +10,7 @@ import net.citizensnpcs.command.CommandContext; import net.citizensnpcs.command.Requirements; import net.citizensnpcs.editor.Editor; import net.citizensnpcs.editor.EquipmentEditor; -import net.citizensnpcs.editor.TextEditor; +import net.citizensnpcs.trait.text.Text; import net.citizensnpcs.trait.waypoint.Waypoints; @Requirements(selected = true, ownership = true) @@ -55,6 +55,6 @@ public class EditorCommands { max = 1, permission = "npc.edit.text") public void text(CommandContext args, Player player, NPC npc) { - Editor.enterOrLeave(player, new TextEditor()); + Editor.enterOrLeave(player, npc.getTrait(Text.class).getEditor(player)); } } \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/editor/EquipmentEditor.java b/src/main/java/net/citizensnpcs/editor/EquipmentEditor.java index b1fb9b44b..8e3997311 100644 --- a/src/main/java/net/citizensnpcs/editor/EquipmentEditor.java +++ b/src/main/java/net/citizensnpcs/editor/EquipmentEditor.java @@ -35,7 +35,7 @@ public class EquipmentEditor extends Editor { @Override public void end() { - Messaging.send(player, "Exited equipment editor."); + Messaging.send(player, "Exited the equipment editor."); } @EventHandler @@ -100,17 +100,15 @@ public class EquipmentEditor extends Editor { Messaging.send(player, "" + npc.getName() + " had all of its items removed."); } // Now edit the equipment based on the slot - if (trait.get(slot) != null && trait.get(slot).getType() != Material.AIR) { + if (trait.get(slot) != null && trait.get(slot).getType() != Material.AIR) player.getWorld().dropItemNaturally(npc.getBukkitEntity().getLocation(), trait.get(slot)); - } ItemStack set = hand; if (set != null && set.getType() != Material.AIR) { - if (hand.getAmount() > 1) { + if (hand.getAmount() > 1) hand.setAmount(hand.getAmount() - 1); - } else { + else hand = null; - } player.setItemInHand(hand); set.setAmount(1); } diff --git a/src/main/java/net/citizensnpcs/editor/TextEditor.java b/src/main/java/net/citizensnpcs/editor/TextEditor.java deleted file mode 100644 index 4778c4a86..000000000 --- a/src/main/java/net/citizensnpcs/editor/TextEditor.java +++ /dev/null @@ -1,12 +0,0 @@ -package net.citizensnpcs.editor; - -public class TextEditor extends Editor { - - @Override - public void begin() { - } - - @Override - public void end() { - } -} \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/trait/LookClose.java b/src/main/java/net/citizensnpcs/trait/LookClose.java index 8158f32a3..3c8a39def 100644 --- a/src/main/java/net/citizensnpcs/trait/LookClose.java +++ b/src/main/java/net/citizensnpcs/trait/LookClose.java @@ -1,6 +1,5 @@ package net.citizensnpcs.trait; -import net.citizensnpcs.Toggleable; import net.citizensnpcs.api.exception.NPCLoadException; import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.trait.SaveId; diff --git a/src/main/java/net/citizensnpcs/Toggleable.java b/src/main/java/net/citizensnpcs/trait/Toggleable.java similarity index 81% rename from src/main/java/net/citizensnpcs/Toggleable.java rename to src/main/java/net/citizensnpcs/trait/Toggleable.java index cfef0d6f5..a2daec244 100644 --- a/src/main/java/net/citizensnpcs/Toggleable.java +++ b/src/main/java/net/citizensnpcs/trait/Toggleable.java @@ -1,4 +1,4 @@ -package net.citizensnpcs; +package net.citizensnpcs.trait; /** * Represents a two-state entity which can be toggled on and off. diff --git a/src/main/java/net/citizensnpcs/trait/text/Text.java b/src/main/java/net/citizensnpcs/trait/text/Text.java new file mode 100644 index 000000000..59eae8d64 --- /dev/null +++ b/src/main/java/net/citizensnpcs/trait/text/Text.java @@ -0,0 +1,120 @@ +package net.citizensnpcs.trait.text; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +import org.bukkit.Bukkit; +import org.bukkit.conversations.ConversationFactory; +import org.bukkit.entity.Player; + +import net.citizensnpcs.Citizens; +import net.citizensnpcs.api.exception.NPCLoadException; +import net.citizensnpcs.api.npc.NPC; +import net.citizensnpcs.api.trait.SaveId; +import net.citizensnpcs.api.trait.Trait; +import net.citizensnpcs.api.util.DataKey; +import net.citizensnpcs.editor.Editor; +import net.citizensnpcs.npc.CitizensNPC; +import net.citizensnpcs.trait.Toggleable; +import net.citizensnpcs.trait.text.prompt.StartPrompt; +import net.citizensnpcs.util.Messaging; +import net.citizensnpcs.util.Paginator; +import net.minecraft.server.EntityHuman; +import net.minecraft.server.EntityLiving; + +@SaveId("text") +public class Text extends Trait implements Runnable, Toggleable { + private final Citizens plugin; + private final NPC npc; + private final List text = new ArrayList(); + private boolean talkClose; + + public Text(NPC npc) { + this.npc = npc; + plugin = (Citizens) Bukkit.getPluginManager().getPlugin("Citizens"); + } + + @Override + public void load(DataKey key) throws NPCLoadException { + for (DataKey sub : key.getIntegerSubKeys()) + text.add(sub.getString("")); + + if (key.keyExists("talk-close")) + talkClose = key.getBoolean("talk-close"); + } + + @Override + public void save(DataKey key) { + key.setBoolean("talk-close", talkClose); + for (int i = 0; i < text.size(); i++) + key.setString(String.valueOf(i), text.get(i)); + } + + @Override + public void toggle() { + talkClose = !talkClose; + } + + @Override + public void run() { + EntityHuman search = null; + EntityLiving handle = ((CitizensNPC) npc).getHandle(); + if ((search = handle.world.findNearbyPlayer(handle, 5)) != null && talkClose) + sendRandomText((Player) search.getBukkitEntity()); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("Text{talk-close=" + talkClose + ",text="); + for (String line : text) + builder.append(line + ","); + builder.append("}"); + return builder.toString(); + } + + public Editor getEditor(final Player player) { + final StartPrompt startPrompt = new StartPrompt(this); + return new Editor() { + + @Override + public void begin() { + Messaging.send(player, "Entered the text editor!"); + + new ConversationFactory(plugin).withFirstPrompt(startPrompt).withEscapeSequence("/npc text") + .buildConversation(player).begin(); + } + + @Override + public void end() { + Messaging.send(player, "Exited the text editor."); + } + }; + } + + public void add(String string) { + text.add(string); + } + + public void remove(int index) { + text.remove(index); + } + + public void edit(int index, String newText) { + text.set(index, newText); + } + + public boolean sendPage(Player player, int page) { + Paginator paginator = new Paginator(); + paginator.setHeaderText(npc.getName() + "'s Text Entries"); + for (String line : text) + paginator.addLine(line); + + return paginator.sendPage(player, page); + } + + private void sendRandomText(Player player) { + npc.chat(player, text.get(new Random().nextInt(text.size()))); + } +} \ 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 new file mode 100644 index 000000000..bf5319c16 --- /dev/null +++ b/src/main/java/net/citizensnpcs/trait/text/prompt/StartPrompt.java @@ -0,0 +1,38 @@ +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 StartPrompt extends StringPrompt { + private Text text; + + public StartPrompt(Text text) { + this.text = text; + } + + @Override + public Prompt acceptInput(ConversationContext context, String string) { + if (string.equalsIgnoreCase("add")) + return new TextAddPrompt(text); + else if (string.equalsIgnoreCase("edit")) + return new TextEditSelectIndexPrompt(text); + else if (string.equalsIgnoreCase("remove")) + return new TextRemovePrompt(text); + else { + Messaging.sendError((Player) context.getForWhom(), "Invalid edit type."); + return new StartPrompt(text); + } + } + + @Override + public String getPromptText(ConversationContext context) { + return StringHelper + .parseColors("Type add to add an entry, edit to edit entries, and remove to remove entries."); + } +} \ 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 new file mode 100644 index 000000000..e219b0da9 --- /dev/null +++ b/src/main/java/net/citizensnpcs/trait/text/prompt/TextAddPrompt.java @@ -0,0 +1,28 @@ +package net.citizensnpcs.trait.text.prompt; + +import net.citizensnpcs.trait.text.Text; +import net.citizensnpcs.util.StringHelper; + +import org.bukkit.conversations.ConversationContext; +import org.bukkit.conversations.Prompt; +import org.bukkit.conversations.StringPrompt; + +public class TextAddPrompt extends StringPrompt { + private Text text; + + public TextAddPrompt(Text text) { + this.text = text; + } + + @Override + public Prompt acceptInput(ConversationContext context, String string) { + text.add(string); + context.getForWhom().sendRawMessage(StringHelper.parseColors("Added the entry " + string + ".")); + return new StartPrompt(text); + } + + @Override + public String getPromptText(ConversationContext context) { + return "Enter text to add to the NPC."; + } +} \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/trait/text/prompt/TextEditPrompt.java b/src/main/java/net/citizensnpcs/trait/text/prompt/TextEditPrompt.java new file mode 100644 index 000000000..57a08514d --- /dev/null +++ b/src/main/java/net/citizensnpcs/trait/text/prompt/TextEditPrompt.java @@ -0,0 +1,32 @@ +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 TextEditPrompt extends StringPrompt { + private Text text; + + public TextEditPrompt(Text text) { + this.text = text; + } + + @Override + public Prompt acceptInput(ConversationContext context, String string) { + text.edit((Integer) context.getSessionData("index"), string); + Messaging.send((Player) context.getForWhom(), "Changed entry at index " + context.getSessionData("index") + + " to " + string + "."); + 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") + "."); + } +} \ 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/TextEditSelectIndexPrompt.java new file mode 100644 index 000000000..ee705ebcd --- /dev/null +++ b/src/main/java/net/citizensnpcs/trait/text/prompt/TextEditSelectIndexPrompt.java @@ -0,0 +1,39 @@ +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.entity.Player; + +public class TextEditSelectIndexPrompt extends NumericPrompt { + private Text text; + + public TextEditSelectIndexPrompt(Text text) { + this.text = text; + } + + @Override + public Prompt acceptValidatedInput(ConversationContext context, Number number) { + context.setSessionData("index", number.intValue()); + Messaging.send((Player) context.getForWhom(), "Now editing the entry at index " + number.intValue() + + ". Enter text to replace the entry with."); + return new TextEditPrompt(text); + } + + @Override + public String getFailedValidationText(ConversationContext context, String invalidInput) { + return ChatColor.RED + invalidInput + " is not a valid index!"; + } + + @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."); + } +} \ 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 new file mode 100644 index 000000000..ecd015cb7 --- /dev/null +++ b/src/main/java/net/citizensnpcs/trait/text/prompt/TextRemovePrompt.java @@ -0,0 +1,39 @@ +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.entity.Player; + +public class TextRemovePrompt extends NumericPrompt { + private Text text; + + public TextRemovePrompt(Text text) { + this.text = text; + } + + @Override + public Prompt acceptValidatedInput(ConversationContext context, Number number) { + int index = number.intValue(); + text.remove(index); + Messaging.send((Player) context.getForWhom(), "Removed entry at index " + index + "."); + return new StartPrompt(text); + } + + @Override + public String getFailedValidationText(ConversationContext context, String invalidInput) { + return ChatColor.RED + invalidInput + " is not a valid index!"; + } + + @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 remove."); + } +} \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/trait/waypoint/LinearWaypointProvider.java b/src/main/java/net/citizensnpcs/trait/waypoint/LinearWaypointProvider.java index fab9905ae..c20c6bb3f 100644 --- a/src/main/java/net/citizensnpcs/trait/waypoint/LinearWaypointProvider.java +++ b/src/main/java/net/citizensnpcs/trait/waypoint/LinearWaypointProvider.java @@ -25,6 +25,7 @@ public class LinearWaypointProvider implements WaypointProvider, Iterable 0) { waypoints.remove(waypoints.size() - 1); - Messaging.send(player, - String.format("Removed a waypoint (%d remaining)", waypoints.size())); + Messaging.send(player, String.format("Removed a waypoint (%d remaining)", waypoints + .size())); } } }; diff --git a/src/main/java/net/citizensnpcs/trait/waypoint/WaypointProvider.java b/src/main/java/net/citizensnpcs/trait/waypoint/WaypointProvider.java index 062db0adb..a224776bd 100644 --- a/src/main/java/net/citizensnpcs/trait/waypoint/WaypointProvider.java +++ b/src/main/java/net/citizensnpcs/trait/waypoint/WaypointProvider.java @@ -8,6 +8,7 @@ import net.citizensnpcs.editor.Editor; import org.bukkit.entity.Player; public interface WaypointProvider { + /** * Creates an {@link Editor} with the given {@link Player}. *