editor changes, bug fixes

This commit is contained in:
aPunch 2012-02-27 09:59:39 -06:00
commit c188d2833e
8 changed files with 58 additions and 67 deletions

View File

@ -35,8 +35,10 @@ import net.citizensnpcs.command.exception.RequirementMissingException;
import net.citizensnpcs.command.exception.ServerCommandException;
import net.citizensnpcs.command.exception.UnhandledCommandException;
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.waypoint.Waypoints;
import net.citizensnpcs.util.Messaging;
import net.citizensnpcs.util.Metrics;
import net.citizensnpcs.util.StringHelper;
@ -58,7 +60,7 @@ public class Citizens extends JavaPlugin {
private volatile CitizensNPCManager npcManager;
private final InstanceFactory<Character> characterManager = DefaultInstanceFactory.create();
private final InstanceFactory<Trait> traitManager = DefaultInstanceFactory.create(Owner.class, Spawned.class,
LookClose.class, SpawnLocation.class, Inventory.class, MobType.class);
LookClose.class, SpawnLocation.class, Inventory.class, MobType.class, Waypoints.class);
private final CommandManager commands = new CommandManager();
private Settings config;
private Storage saves;
@ -232,6 +234,7 @@ public class Citizens extends JavaPlugin {
public void reload() throws NPCLoadException {
getServer().getScheduler().cancelTasks(this);
Editor.leaveAll();
config.load();
for (NPC npc : npcManager)
npc.despawn();

View File

@ -156,7 +156,7 @@ public class EventListen implements Listener {
*/
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
Editor.leaveEditor(event.getPlayer());
Editor.leave(event.getPlayer());
}
@EventHandler

View File

@ -1,8 +1,5 @@
package net.citizensnpcs.command.command;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
@ -15,12 +12,10 @@ 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<String, Editor> editors = new HashMap<String, Editor>();
public EditorCommands(Citizens plugin) {
this.plugin = plugin;
@ -36,7 +31,7 @@ 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");
Editor.enterOrLeave(player, new EquipmentEditor(plugin, player, npc));
}
@Command(
@ -48,7 +43,7 @@ public class EditorCommands {
max = 1,
permission = "npc.edit.path")
public void path(CommandContext args, Player player, NPC npc) {
toggleEditor(player, npc, "path");
Editor.enterOrLeave(player, npc.getTrait(Waypoints.class).getEditor(player));
}
@Command(
@ -60,38 +55,6 @@ 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;
Editor.enterOrLeave(player, new TextEditor());
}
}

View File

@ -1,12 +1,51 @@
package net.citizensnpcs.editor;
import java.util.HashMap;
import java.util.Map;
import net.citizensnpcs.util.Messaging;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
public interface Editor extends Listener {
public abstract class Editor implements Listener {
private static final Map<String, Editor> editing = new HashMap<String, Editor>();
public void begin();
public abstract void begin();
public void end();
public abstract void end();
public String getName();
public static void enterOrLeave(Player player, Editor editor) {
Editor edit = editing.get(player.getName());
if (edit == null)
enter(player, editor);
else if (edit.getClass() == editor.getClass())
leave(player);
else
Messaging.sendError(player, "You're already in an editor!");
}
public static void leave(Player player) {
if (!hasEditor(player))
return;
Editor editor = editing.remove(player.getName());
HandlerList.unregisterAll(editor);
editor.end();
}
public static void leaveAll() {
editing.clear();
}
private static void enter(Player player, Editor editor) {
editor.begin();
player.getServer().getPluginManager().registerEvents(editor,
player.getServer().getPluginManager().getPlugin("Citizens"));
editing.put(player.getName(), editor);
}
public static boolean hasEditor(Player player) {
return editing.containsKey(player.getName());
}
}

View File

@ -7,12 +7,11 @@ 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 {
public class EquipmentEditor extends Editor {
private final Citizens plugin;
private final Player player;
private final NPC npc;
@ -25,7 +24,6 @@ public class EquipmentEditor implements Editor {
@Override
public void begin() {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
Messaging.send(player, "<a>Entered the equipment editor!");
Messaging.send(player, "<a>Right click to equip armor and items.");
Messaging.send(player, "<a>Right click while crouching to equip armor in the NPC's hand.");
@ -34,18 +32,12 @@ public class EquipmentEditor implements Editor {
@Override
public void end() {
HandlerList.unregisterAll(this);
Messaging.send(player, "<a>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()))
if (event.getAction() == Action.RIGHT_CLICK_AIR && Editor.hasEditor(event.getPlayer()))
event.setUseItemInHand(Result.DENY);
}

View File

@ -1,6 +1,6 @@
package net.citizensnpcs.editor;
public class TextEditor implements Editor {
public class TextEditor extends Editor {
@Override
public void begin() {
@ -9,9 +9,4 @@ public class TextEditor implements Editor {
@Override
public void end() {
}
@Override
public String getName() {
return "text";
}
}

View File

@ -11,6 +11,7 @@ import net.citizensnpcs.api.npc.NPCManager;
import net.citizensnpcs.api.trait.Character;
import net.citizensnpcs.api.trait.trait.SpawnLocation;
import net.citizensnpcs.api.util.Storage;
import net.citizensnpcs.editor.Editor;
import net.citizensnpcs.util.ByIdArray;
import net.citizensnpcs.util.NPCBuilder;
@ -137,6 +138,9 @@ public class CitizensNPCManager implements NPCManager {
selected.get(existing.getId()).remove(player.getName());
selected.put(npc.getId(), player.getName());
// Remove editor if the player has one
Editor.leave(player);
// Call selection event
player.getServer().getPluginManager().callEvent(new NPCSelectEvent(npc, player));
}

View File

@ -33,11 +33,6 @@ 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) {