diff --git a/lib/CitizensAPI.jar b/lib/CitizensAPI.jar index 94c9799f9..f2d6b1bad 100644 Binary files a/lib/CitizensAPI.jar and b/lib/CitizensAPI.jar differ diff --git a/src/main/java/net/citizensnpcs/command/command/EditorCommands.java b/src/main/java/net/citizensnpcs/command/command/EditorCommands.java index e0461f2a2..bea1fc105 100644 --- a/src/main/java/net/citizensnpcs/command/command/EditorCommands.java +++ b/src/main/java/net/citizensnpcs/command/command/EditorCommands.java @@ -1,5 +1,8 @@ package net.citizensnpcs.command.command; +import java.util.HashMap; +import java.util.Map; + import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; @@ -8,11 +11,19 @@ import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.command.Command; 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.waypoint.Waypoints; +import net.citizensnpcs.util.Messaging; @Requirements(selected = true, ownership = true) public class EditorCommands { + private final Citizens plugin; + private static final Map editors = new HashMap(); public EditorCommands(Citizens plugin) { + this.plugin = plugin; } @Command( @@ -25,17 +36,19 @@ public class EditorCommands { permission = "npc.edit.equip") @Requirements(selected = true, ownership = true, type = EntityType.PLAYER) public void equip(CommandContext args, Player player, NPC npc) { + toggleEditor(player, npc, "equip"); } @Command( aliases = { "npc" }, usage = "path", desc = "Toggle the waypoint editor", - modifiers = { "path", "waypoints" }, + modifiers = { "path" }, min = 1, max = 1, permission = "npc.edit.path") public void path(CommandContext args, Player player, NPC npc) { + toggleEditor(player, npc, "path"); } @Command( @@ -47,5 +60,38 @@ public class EditorCommands { max = 1, permission = "npc.edit.text") public void text(CommandContext args, Player player, NPC npc) { + toggleEditor(player, npc, "text"); + } + + public static void removeEditor(Player player) { + if (editors.containsKey(player.getName())) + editors.remove(player.getName()); + } + + private void toggleEditor(Player player, NPC npc, String name) { + if (editors.containsKey(player.getName())) { + if (editors.get(player.getName()).getName().equals(name)) { + editors.get(player.getName()).end(); + editors.remove(player.getName()); + } else { + Messaging.sendError(player, "You can only be in one editor at a time."); + Messaging.sendError(player, "Type /npc " + name + " to exit the current editor."); + } + } else { + Editor editor = getEditor(player, npc, name); + editors.put(player.getName(), editor); + editor.begin(); + } + } + + private Editor getEditor(Player player, NPC npc, String name) { + if (name.equals("equip")) + return new EquipmentEditor(plugin, player, npc); + else if (name.equals("path")) + return npc.getTrait(Waypoints.class).getEditor(player); + else if (name.equals("text")) + return new TextEditor(); + + return null; } } \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/editor/Editor.java b/src/main/java/net/citizensnpcs/editor/Editor.java index 81a26a47f..530891831 100644 --- a/src/main/java/net/citizensnpcs/editor/Editor.java +++ b/src/main/java/net/citizensnpcs/editor/Editor.java @@ -1,38 +1,12 @@ package net.citizensnpcs.editor; -import java.util.Map; - -import net.citizensnpcs.util.Messaging; - -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; -import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; -import com.google.common.collect.Maps; +public interface Editor extends Listener { -public abstract class Editor implements Listener { - public abstract void begin(); + public void begin(); - public abstract void end(); + public void end(); - public static void enterEditor(Player player, Editor editor) { - if (editing.containsKey(player.getName())) { - Messaging.sendError(player, "You're already in an editor!"); - return; - } - editor.begin(); - Bukkit.getPluginManager().registerEvents(editor, Bukkit.getPluginManager().getPlugin("Citizens")); - editing.put(player.getName(), editor); - } - - public static void leaveEditor(Player player) { - if (!editing.containsKey(player.getName())) - return; - Editor editor = editing.remove(player.getName()); - HandlerList.unregisterAll(editor); - editor.end(); - } - - private static final Map editing = Maps.newHashMap(); + public String getName(); } \ 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 becc2d36b..3ca57e509 100644 --- a/src/main/java/net/citizensnpcs/editor/EquipmentEditor.java +++ b/src/main/java/net/citizensnpcs/editor/EquipmentEditor.java @@ -1,12 +1,60 @@ package net.citizensnpcs.editor; -public class EquipmentEditor extends Editor { +import net.citizensnpcs.Citizens; +import net.citizensnpcs.api.npc.NPC; +import net.citizensnpcs.util.Messaging; + +import org.bukkit.entity.Player; +import org.bukkit.event.Event.Result; +import org.bukkit.event.EventHandler; +import org.bukkit.event.HandlerList; +import org.bukkit.event.block.Action; +import org.bukkit.event.player.PlayerInteractEntityEvent; +import org.bukkit.event.player.PlayerInteractEvent; + +public class EquipmentEditor implements Editor { + private final Citizens plugin; + private final Player player; + private final NPC npc; + + public EquipmentEditor(Citizens plugin, Player player, NPC npc) { + this.plugin = plugin; + this.player = player; + this.npc = npc; + } @Override public void begin() { + plugin.getServer().getPluginManager().registerEvents(this, plugin); + Messaging.send(player, "Entered the equipment editor!"); + Messaging.send(player, "Right click to equip armor and items."); + Messaging.send(player, "Right click while crouching to equip armor in the NPC's hand."); + Messaging.send(player, "Left click to remove all armor and items."); } @Override public void end() { + HandlerList.unregisterAll(this); + Messaging.send(player, "Exited equipment editor."); + } + + @Override + public String getName() { + return "equip"; + } + + @EventHandler + public void onPlayerInteract(PlayerInteractEvent event) { + if (event.getAction() == Action.RIGHT_CLICK_AIR && event.getPlayer().getName().equals(player.getName())) + event.setUseItemInHand(Result.DENY); + } + + @EventHandler + public void onPlayerInteractEntity(PlayerInteractEntityEvent event) { + if (plugin.getNPCManager().isNPC(event.getRightClicked()) + && plugin.getNPCManager().getNPC(event.getRightClicked()).equals(npc) + && event.getPlayer().getName().equals(player.getName())) { + npc.chat("You clicked me!"); + } } } \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/editor/PathEditor.java b/src/main/java/net/citizensnpcs/editor/PathEditor.java deleted file mode 100644 index 4c1357b66..000000000 --- a/src/main/java/net/citizensnpcs/editor/PathEditor.java +++ /dev/null @@ -1,12 +0,0 @@ -package net.citizensnpcs.editor; - -public class PathEditor extends Editor { - - @Override - public void begin() { - } - - @Override - public void end() { - } -} \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/editor/TextEditor.java b/src/main/java/net/citizensnpcs/editor/TextEditor.java index 4778c4a86..9a3d50fc1 100644 --- a/src/main/java/net/citizensnpcs/editor/TextEditor.java +++ b/src/main/java/net/citizensnpcs/editor/TextEditor.java @@ -1,6 +1,6 @@ package net.citizensnpcs.editor; -public class TextEditor extends Editor { +public class TextEditor implements Editor { @Override public void begin() { @@ -9,4 +9,9 @@ public class TextEditor extends Editor { @Override public void end() { } + + @Override + public String getName() { + return "text"; + } } \ 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 c6790bf88..635b9f943 100644 --- a/src/main/java/net/citizensnpcs/trait/waypoint/LinearWaypointProvider.java +++ b/src/main/java/net/citizensnpcs/trait/waypoint/LinearWaypointProvider.java @@ -33,6 +33,11 @@ public class LinearWaypointProvider implements WaypointProvider { player.sendMessage(ChatColor.GREEN + "Exited linear waypoint editor."); } + @Override + public String getName() { + return "path"; + } + @EventHandler @SuppressWarnings("unused") public void onPlayerInteract(PlayerInteractEvent event) { diff --git a/src/main/java/net/citizensnpcs/trait/waypoint/WaypointProvider.java b/src/main/java/net/citizensnpcs/trait/waypoint/WaypointProvider.java index 5553b36c5..f517325ed 100644 --- a/src/main/java/net/citizensnpcs/trait/waypoint/WaypointProvider.java +++ b/src/main/java/net/citizensnpcs/trait/waypoint/WaypointProvider.java @@ -7,6 +7,7 @@ import net.citizensnpcs.editor.Editor; import org.bukkit.entity.Player; public interface WaypointProvider { + public Editor createEditor(Player player); public void load(DataKey key); diff --git a/src/main/java/net/citizensnpcs/util/StorageUtils.java b/src/main/java/net/citizensnpcs/util/StorageUtils.java index d5bdf6272..298bd9aa1 100644 --- a/src/main/java/net/citizensnpcs/util/StorageUtils.java +++ b/src/main/java/net/citizensnpcs/util/StorageUtils.java @@ -8,16 +8,17 @@ import org.bukkit.Material; import org.bukkit.inventory.ItemStack; public class StorageUtils { + public static Location loadLocation(DataKey root) { root = root.getRelative("location"); - return new Location(Bukkit.getWorld(root.getString("world")), root.getDouble("x"), root.getDouble("y"), - root.getDouble("z"), (float) root.getDouble("yaw", 0), (float) root.getDouble("pitch", 0)); + return new Location(Bukkit.getWorld(root.getString("world")), root.getDouble("x"), root.getDouble("y"), root + .getDouble("z"), (float) root.getDouble("yaw", 0), (float) root.getDouble("pitch", 0)); } public static ItemStack loadItemStack(DataKey root) { root = root.getRelative("item"); - return new ItemStack(Material.matchMaterial(root.getString("id")), root.getInt("amount"), - (short) (root.keyExists("data") ? root.getInt("data") : 0)); + return new ItemStack(Material.matchMaterial(root.getString("id")), root.getInt("amount"), (short) (root + .keyExists("data") ? root.getInt("data") : 0)); } public static void saveLocation(DataKey key, Location location) {