This commit is contained in:
fullwall 2012-02-28 15:39:21 +08:00
commit bb37e033b9
9 changed files with 85 additions and 37 deletions

Binary file not shown.

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

@ -8,11 +8,17 @@ 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;
@Requirements(selected = true, ownership = true)
public class EditorCommands {
private final Citizens plugin;
public EditorCommands(Citizens plugin) {
this.plugin = plugin;
}
@Command(
@ -25,17 +31,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) {
Editor.enterOrLeave(player, new EquipmentEditor(plugin, player, npc));
}
@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) {
Editor.enterOrLeave(player, npc.getTrait(Waypoints.class).getEditor(player));
}
@Command(
@ -47,5 +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());
}
}

View File

@ -1,49 +1,51 @@
package net.citizensnpcs.editor;
import java.util.HashMap;
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 abstract class Editor implements Listener {
private static final Map<String, Editor> editing = new HashMap<String, Editor>();
public abstract void begin();
public abstract void end();
public static void enter(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 enterOrLeave(Player player, Editor editor) {
Editor edit = editing.get(player.getName());
if (edit == null) {
if (edit == null)
enter(player, editor);
} else if (edit.getClass() == editor.getClass()) {
else if (edit.getClass() == editor.getClass())
leave(player);
} else {
else
Messaging.sendError(player, "You're already in an editor!");
}
}
public static void leave(Player player) {
if (!editing.containsKey(player.getName()))
if (!hasEditor(player))
return;
Editor editor = editing.remove(player.getName());
HandlerList.unregisterAll(editor);
editor.end();
}
private static final Map<String, Editor> editing = Maps.newHashMap();
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

@ -1,12 +1,52 @@
package net.citizensnpcs.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.block.Action;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
public class EquipmentEditor extends 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() {
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.");
Messaging.send(player, "<a>Left click to remove all armor and items.");
}
@Override
public void end() {
Messaging.send(player, "<a>Exited equipment editor.");
}
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
if (event.getAction() == Action.RIGHT_CLICK_AIR && Editor.hasEditor(event.getPlayer()))
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!");
}
}
}

View File

@ -1,12 +0,0 @@
package net.citizensnpcs.editor;
public class PathEditor extends Editor {
@Override
public void begin() {
}
@Override
public void end() {
}
}

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

@ -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);

View File

@ -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) {