BREAKING: rename methods

This commit is contained in:
fullwall 2012-05-17 21:34:03 +08:00
parent 41ddb6851b
commit 22ced5b02b
15 changed files with 106 additions and 109 deletions

View File

@ -12,6 +12,7 @@ import net.citizensnpcs.api.CitizensPlugin;
import net.citizensnpcs.api.event.CitizensReloadEvent; import net.citizensnpcs.api.event.CitizensReloadEvent;
import net.citizensnpcs.api.exception.NPCLoadException; import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCRegistry;
import net.citizensnpcs.api.npc.character.CharacterManager; import net.citizensnpcs.api.npc.character.CharacterManager;
import net.citizensnpcs.api.scripting.EventRegistrar; import net.citizensnpcs.api.scripting.EventRegistrar;
import net.citizensnpcs.api.scripting.ObjectProvider; import net.citizensnpcs.api.scripting.ObjectProvider;
@ -37,7 +38,7 @@ import net.citizensnpcs.command.exception.WrappedCommandException;
import net.citizensnpcs.editor.Editor; import net.citizensnpcs.editor.Editor;
import net.citizensnpcs.npc.CitizensCharacterManager; import net.citizensnpcs.npc.CitizensCharacterManager;
import net.citizensnpcs.npc.CitizensNPC; import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager; import net.citizensnpcs.npc.CitizensNPCRegistry;
import net.citizensnpcs.npc.CitizensTraitManager; import net.citizensnpcs.npc.CitizensTraitManager;
import net.citizensnpcs.npc.NPCSelector; import net.citizensnpcs.npc.NPCSelector;
import net.citizensnpcs.util.Messaging; import net.citizensnpcs.util.Messaging;
@ -61,8 +62,8 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
private boolean compatible; private boolean compatible;
private Settings config; private Settings config;
private ClassLoader contextClassLoader; private ClassLoader contextClassLoader;
private CitizensNPCManager npcManager; private CitizensNPCRegistry npcRegistry;
private Storage saves; private Storage saves; // TODO: refactor this, it's used in too many places
private NPCSelector selector; private NPCSelector selector;
private TraitManager traitManager; private TraitManager traitManager;
@ -71,13 +72,13 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
return characterManager; return characterManager;
} }
public CommandManager getCommandManager() { public Iterable<net.citizensnpcs.command.Command> getCommands(String base) {
return commands; // TODO: this doesn't need to be exposed. return commands.getCommands(base);
} }
@Override @Override
public CitizensNPCManager getNPCManager() { public NPCRegistry getNPCRegistry() {
return npcManager; return npcRegistry;
} }
public NPCSelector getNPCSelector() { public NPCSelector getNPCSelector() {
@ -147,7 +148,7 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
if (compatible) { if (compatible) {
save(); save();
despawnNPCs(); despawnNPCs();
npcManager = null; npcRegistry = null;
getServer().getScheduler().cancelTasks(this); getServer().getScheduler().cancelTasks(this);
} }
@ -171,12 +172,12 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
setupStorage(); setupStorage();
npcManager = new CitizensNPCManager(saves); npcRegistry = new CitizensNPCRegistry(saves);
traitManager = new CitizensTraitManager(this); traitManager = new CitizensTraitManager(this);
selector = new NPCSelector(this); selector = new NPCSelector(this);
CitizensAPI.setImplementation(this); CitizensAPI.setImplementation(this);
getServer().getPluginManager().registerEvents(new EventListen(npcManager), this); getServer().getPluginManager().registerEvents(new EventListen(), this);
registerCommands(); registerCommands();
@ -231,7 +232,7 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
} }
private void despawnNPCs() { private void despawnNPCs() {
Iterator<NPC> itr = npcManager.iterator(); Iterator<NPC> itr = npcRegistry.iterator();
while (itr.hasNext()) { while (itr.hasNext()) {
NPC npc = itr.next(); NPC npc = itr.next();
itr.remove(); itr.remove();
@ -240,7 +241,7 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
} }
public void save() { public void save() {
for (NPC npc : npcManager) for (NPC npc : npcRegistry)
((CitizensNPC) npc).save(saves.getKey("npc." + npc.getId())); ((CitizensNPC) npc).save(saves.getKey("npc." + npc.getId()));
saves.save(); saves.save();
@ -267,7 +268,7 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
continue; continue;
} }
} }
NPC npc = npcManager.createNPC(type, id, key.getString("name"), null); NPC npc = npcRegistry.createNPC(type, id, key.getString("name"), null);
((CitizensNPC) npc).load(key); ((CitizensNPC) npc).load(key);
++created; ++created;
@ -315,7 +316,7 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
metrics.addCustomData(new Metrics.Plotter("Total NPCs") { metrics.addCustomData(new Metrics.Plotter("Total NPCs") {
@Override @Override
public int getValue() { public int getValue() {
return Iterators.size(npcManager.iterator()); return Iterators.size(npcRegistry.iterator());
} }
}); });
Metrics.Graph graph = metrics.createGraph("Character Type Usage"); Metrics.Graph graph = metrics.createGraph("Character Type Usage");

View File

@ -1,13 +1,14 @@
package net.citizensnpcs; package net.citizensnpcs;
import net.citizensnpcs.Settings.Setting; import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.event.NPCDamageByEntityEvent; import net.citizensnpcs.api.event.NPCDamageByEntityEvent;
import net.citizensnpcs.api.event.NPCDamageEvent; import net.citizensnpcs.api.event.NPCDamageEvent;
import net.citizensnpcs.api.event.NPCLeftClickEvent; import net.citizensnpcs.api.event.NPCLeftClickEvent;
import net.citizensnpcs.api.event.NPCRightClickEvent; import net.citizensnpcs.api.event.NPCRightClickEvent;
import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCRegistry;
import net.citizensnpcs.editor.Editor; import net.citizensnpcs.editor.Editor;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.entity.EntityHumanNPC; import net.citizensnpcs.npc.entity.EntityHumanNPC;
import net.citizensnpcs.trait.CurrentLocation; import net.citizensnpcs.trait.CurrentLocation;
import net.citizensnpcs.trait.text.Text; import net.citizensnpcs.trait.text.Text;
@ -39,13 +40,9 @@ import com.google.common.collect.ListMultimap;
import com.google.gson.internal.Pair; import com.google.gson.internal.Pair;
public class EventListen implements Listener { public class EventListen implements Listener {
private volatile CitizensNPCManager npcManager; private final NPCRegistry npcManager = CitizensAPI.getNPCRegistry();
private final ListMultimap<Pair<Integer, Integer>, Integer> toRespawn = ArrayListMultimap.create(); private final ListMultimap<Pair<Integer, Integer>, Integer> toRespawn = ArrayListMultimap.create();
public EventListen(CitizensNPCManager npcManager) {
this.npcManager = npcManager;
}
/* /*
* Chunk events * Chunk events
*/ */

View File

@ -9,7 +9,6 @@ import net.citizensnpcs.Citizens;
import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.command.Command; import net.citizensnpcs.command.Command;
import net.citizensnpcs.command.CommandContext; import net.citizensnpcs.command.CommandContext;
import net.citizensnpcs.command.CommandManager;
import net.citizensnpcs.command.Requirements; import net.citizensnpcs.command.Requirements;
import net.citizensnpcs.command.ServerCommand; import net.citizensnpcs.command.ServerCommand;
import net.citizensnpcs.command.exception.CommandException; import net.citizensnpcs.command.exception.CommandException;
@ -19,10 +18,10 @@ import org.bukkit.command.CommandSender;
@Requirements @Requirements
public class HelpCommands { public class HelpCommands {
private final CommandManager cmdManager; private final Citizens plugin;
public HelpCommands(Citizens plugin) { public HelpCommands(Citizens plugin) {
cmdManager = plugin.getCommandManager(); this.plugin = plugin;
} }
@Command( @Command(
@ -48,7 +47,7 @@ public class HelpCommands {
// Ensures that commands with multiple modifiers are only added once // Ensures that commands with multiple modifiers are only added once
Set<Command> cmds = new HashSet<Command>(); Set<Command> cmds = new HashSet<Command>();
List<String> lines = new ArrayList<String>(); List<String> lines = new ArrayList<String>();
for (Command cmd : cmdManager.getCommands(baseCommand)) { for (Command cmd : plugin.getCommands(baseCommand)) {
if (cmds.contains(cmd) if (cmds.contains(cmd)
|| (!sender.hasPermission("citizens.admin") && !sender || (!sender.hasPermission("citizens.admin") && !sender
.hasPermission("citizens." + cmd.permission()))) .hasPermission("citizens." + cmd.permission())))

View File

@ -7,6 +7,7 @@ import net.citizensnpcs.Citizens;
import net.citizensnpcs.Settings.Setting; import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCRegistry;
import net.citizensnpcs.api.npc.character.Character; import net.citizensnpcs.api.npc.character.Character;
import net.citizensnpcs.api.npc.character.CharacterManager; import net.citizensnpcs.api.npc.character.CharacterManager;
import net.citizensnpcs.api.trait.trait.MobType; import net.citizensnpcs.api.trait.trait.MobType;
@ -18,7 +19,6 @@ import net.citizensnpcs.command.Requirements;
import net.citizensnpcs.command.ServerCommand; import net.citizensnpcs.command.ServerCommand;
import net.citizensnpcs.command.exception.CommandException; import net.citizensnpcs.command.exception.CommandException;
import net.citizensnpcs.command.exception.NoPermissionsException; import net.citizensnpcs.command.exception.NoPermissionsException;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.NPCSelector; import net.citizensnpcs.npc.NPCSelector;
import net.citizensnpcs.trait.Age; import net.citizensnpcs.trait.Age;
import net.citizensnpcs.trait.Behaviour; import net.citizensnpcs.trait.Behaviour;
@ -46,11 +46,11 @@ import com.google.common.base.Splitter;
@Requirements(selected = true, ownership = true) @Requirements(selected = true, ownership = true)
public class NPCCommands { public class NPCCommands {
private final CharacterManager characterManager = CitizensAPI.getCharacterManager(); private final CharacterManager characterManager = CitizensAPI.getCharacterManager();
private final CitizensNPCManager npcManager; private final NPCRegistry npcRegistry;
private final NPCSelector selector; private final NPCSelector selector;
public NPCCommands(Citizens plugin) { public NPCCommands(Citizens plugin) {
npcManager = plugin.getNPCManager(); npcRegistry = CitizensAPI.getNPCRegistry();
selector = plugin.getNPCSelector(); selector = plugin.getNPCSelector();
} }
@ -154,7 +154,7 @@ public class NPCCommands {
type = EntityType.PLAYER; type = EntityType.PLAYER;
} }
} }
npc = npcManager.createNPC(type, name); npc = npcRegistry.createNPC(type, name);
String msg = ChatColor.GREEN + "You created " + StringHelper.wrap(npc.getName()); String msg = ChatColor.GREEN + "You created " + StringHelper.wrap(npc.getName());
if (args.hasValueFlag("char")) { if (args.hasValueFlag("char")) {
String character = args.getFlag("char").toLowerCase(); String character = args.getFlag("char").toLowerCase();
@ -240,17 +240,17 @@ public class NPCCommands {
List<NPC> npcs = new ArrayList<NPC>(); List<NPC> npcs = new ArrayList<NPC>();
if (args.hasFlag('a')) { if (args.hasFlag('a')) {
for (NPC add : npcManager) for (NPC add : npcRegistry)
npcs.add(add); npcs.add(add);
} else if (args.getValueFlags().size() == 0 && sender instanceof Player) { } else if (args.getValueFlags().size() == 0 && sender instanceof Player) {
for (NPC add : npcManager) { for (NPC add : npcRegistry) {
if (!npcs.contains(add) && add.getTrait(Owner.class).isOwnedBy(sender)) if (!npcs.contains(add) && add.getTrait(Owner.class).isOwnedBy(sender))
npcs.add(add); npcs.add(add);
} }
} else { } else {
if (args.hasValueFlag("owner")) { if (args.hasValueFlag("owner")) {
String name = args.getFlag("owner"); String name = args.getFlag("owner");
for (NPC add : npcManager) { for (NPC add : npcRegistry) {
if (!npcs.contains(add) && add.getTrait(Owner.class).isOwnedBy(name)) if (!npcs.contains(add) && add.getTrait(Owner.class).isOwnedBy(name))
npcs.add(add); npcs.add(add);
} }
@ -262,7 +262,7 @@ public class NPCCommands {
if (EntityType.fromName(type.replace('-', '_')) == null) if (EntityType.fromName(type.replace('-', '_')) == null)
throw new CommandException("'" + type + "' is not a valid mob type."); throw new CommandException("'" + type + "' is not a valid mob type.");
for (NPC add : npcManager) { for (NPC add : npcRegistry) {
if (!npcs.contains(add) && add.getTrait(MobType.class).getType().equalsIgnoreCase(type)) if (!npcs.contains(add) && add.getTrait(MobType.class).getType().equalsIgnoreCase(type))
npcs.add(add); npcs.add(add);
} }
@ -273,7 +273,7 @@ public class NPCCommands {
if (characterManager.getCharacter(character) == null) if (characterManager.getCharacter(character) == null)
throw new CommandException("'" + character + "' is not a valid character."); throw new CommandException("'" + character + "' is not a valid character.");
for (NPC add : npcManager.getNPCs(characterManager.getCharacter(character).getClass())) { for (NPC add : npcRegistry.getNPCs(characterManager.getCharacter(character).getClass())) {
if (!npcs.contains(add)) if (!npcs.contains(add))
npcs.add(add); npcs.add(add);
} }
@ -394,7 +394,7 @@ public class NPCCommands {
throw new CommandException("Incorrect syntax. /npc remove (all)"); throw new CommandException("Incorrect syntax. /npc remove (all)");
if (!sender.hasPermission("citizens.npc.remove.all") && !sender.hasPermission("citizens.admin")) if (!sender.hasPermission("citizens.npc.remove.all") && !sender.hasPermission("citizens.admin"))
throw new NoPermissionsException(); throw new NoPermissionsException();
npcManager.removeAll(); npcRegistry.deregisterAll();
Messaging.send(sender, "<a>You permanently removed all NPCs."); Messaging.send(sender, "<a>You permanently removed all NPCs.");
return; return;
} }
@ -443,7 +443,7 @@ public class NPCCommands {
@Requirements(ownership = true) @Requirements(ownership = true)
@ServerCommand @ServerCommand
public void select(CommandContext args, CommandSender sender, NPC npc) throws CommandException { public void select(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
NPC toSelect = npcManager.getNPC(args.getInteger(1)); NPC toSelect = npcRegistry.getNPC(args.getInteger(1));
if (toSelect == null || !toSelect.getTrait(Spawned.class).shouldSpawn()) if (toSelect == null || !toSelect.getTrait(Spawned.class).shouldSpawn())
throw new CommandException("No NPC with the ID '" + args.getInteger(1) + "' is spawned."); throw new CommandException("No NPC with the ID '" + args.getInteger(1) + "' is spawned.");
if (npc != null && toSelect.getId() == npc.getId()) if (npc != null && toSelect.getId() == npc.getId())
@ -462,7 +462,7 @@ public class NPCCommands {
permission = "npc.spawn") permission = "npc.spawn")
@Requirements @Requirements
public void spawn(CommandContext args, Player player, NPC npc) throws CommandException { public void spawn(CommandContext args, Player player, NPC npc) throws CommandException {
NPC respawn = npcManager.getNPC(args.getInteger(1)); NPC respawn = npcRegistry.getNPC(args.getInteger(1));
if (respawn == null) if (respawn == null)
throw new CommandException("No NPC with the ID '" + args.getInteger(1) + "' exists."); throw new CommandException("No NPC with the ID '" + args.getInteger(1) + "' exists.");

View File

@ -2,7 +2,6 @@ package net.citizensnpcs.editor;
import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.util.Messaging; import net.citizensnpcs.util.Messaging;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -40,15 +39,12 @@ public class EquipmentEditor extends Editor {
@EventHandler @EventHandler
public void onPlayerInteractEntity(PlayerInteractEntityEvent event) { public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
if (!CitizensAPI.getNPCManager().isNPC(event.getRightClicked()) if (!npc.equals(CitizensAPI.getNPCRegistry().getNPC(event.getRightClicked()))
|| !CitizensAPI.getNPCManager().getNPC(event.getRightClicked()).equals(npc)
|| !event.getPlayer().equals(player)) || !event.getPlayer().equals(player))
return; return;
CitizensNPC handle = (CitizensNPC) npc; if (npc instanceof Equipable) {
if (!(handle instanceof Equipable)) ((Equipable) npc).equip(event.getPlayer());
return; }
((Equipable) handle).equip(event.getPlayer());
} }
} }

View File

@ -37,7 +37,7 @@ public class CitizensCharacterManager implements CharacterManager {
graph.addPlotter(new Metrics.Plotter(StringHelper.capitalize(character.getName())) { graph.addPlotter(new Metrics.Plotter(StringHelper.capitalize(character.getName())) {
@Override @Override
public int getValue() { public int getValue() {
return CitizensAPI.getNPCManager().getNPCs(character.getClass()).size(); return CitizensAPI.getNPCRegistry().getNPCs(character.getClass()).size();
} }
}); });
} }

View File

@ -95,6 +95,13 @@ public abstract class CitizensNPC extends AbstractNPC {
} }
public void load(DataKey root) { public void load(DataKey root) {
// Spawn the NPC
if (getTrait(Spawned.class).shouldSpawn()) {
Location spawnLoc = getTrait(CurrentLocation.class).getLocation();
if (spawnLoc != null)
spawn(spawnLoc);
}
Character character = CitizensAPI.getCharacterManager().getCharacter(root.getString("character")); Character character = CitizensAPI.getCharacterManager().getCharacter(root.getString("character"));
// Load the character if it exists // Load the character if it exists
@ -102,8 +109,8 @@ public abstract class CitizensNPC extends AbstractNPC {
try { try {
character.load(root.getRelative("characters." + character.getName())); character.load(root.getRelative("characters." + character.getName()));
} catch (NPCLoadException e) { } catch (NPCLoadException e) {
Messaging.severe(String.format("Unable to load character '%s'.", character.getName())); Messaging.severe(String.format("Unable to load character '%s': %s.", character.getName(),
e.printStackTrace(); e.getMessage()));
} }
setCharacter(character); setCharacter(character);
} }
@ -113,34 +120,25 @@ public abstract class CitizensNPC extends AbstractNPC {
Trait trait = traitManager.getTrait(traitKey.name(), this); Trait trait = traitManager.getTrait(traitKey.name(), this);
if (trait == null) { if (trait == null) {
Messaging.severe(String.format( Messaging.severe(String.format(
"Found missing trait '%s' while loading NPC ID: '%d' - skipped. Has the name changed?", "Skipped missing trait '%s' while loading NPC ID: '%d'. Has the name changed?",
traitKey.name(), getId())); traitKey.name(), getId()));
continue; continue;
} }
addTrait(trait); addTrait(trait);
try { try {
getTrait(trait.getClass()).load(traitKey); getTrait(trait.getClass()).load(traitKey);
} catch (Exception ex) { } catch (NPCLoadException ex) {
Messaging.log(String.format("The trait '%s' failed to load properly for NPC ID: '%d'.", Messaging.log(
traitKey.name(), getId()), ex.getMessage()); String.format("The trait '%s' failed to load for NPC ID: '%d'.", traitKey.name(), getId()),
ex.printStackTrace(); ex.getMessage());
} }
} }
// Spawn the NPC
if (getTrait(Spawned.class).shouldSpawn()) {
Location spawnLoc = getTrait(CurrentLocation.class).getLocation();
if (spawnLoc != null)
spawn(spawnLoc);
}
} }
@Override @Override
public void remove() { public void remove() {
super.remove(); super.remove();
CitizensAPI.getNPCManager().deregister(this); CitizensAPI.getNPCRegistry().deregister(this);
if (isSpawned())
despawn();
} }
public void save(DataKey root) { public void save(DataKey root) {

View File

@ -8,7 +8,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCManager; import net.citizensnpcs.api.npc.NPCRegistry;
import net.citizensnpcs.api.npc.character.Character; import net.citizensnpcs.api.npc.character.Character;
import net.citizensnpcs.api.util.Storage; import net.citizensnpcs.api.util.Storage;
import net.citizensnpcs.npc.ai.NPCHandle; import net.citizensnpcs.npc.ai.NPCHandle;
@ -45,13 +45,13 @@ import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
public class CitizensNPCManager implements NPCManager { public class CitizensNPCRegistry implements NPCRegistry {
private final ByIdArray<NPC> npcs = new ByIdArray<NPC>(); private final ByIdArray<NPC> npcs = new ByIdArray<NPC>();
private final Storage saves; private final Storage saves;
private final Map<EntityType, Class<? extends CitizensNPC>> types = new EnumMap<EntityType, Class<? extends CitizensNPC>>( private final Map<EntityType, Class<? extends CitizensNPC>> types = new EnumMap<EntityType, Class<? extends CitizensNPC>>(
EntityType.class); EntityType.class);
public CitizensNPCManager(Storage saves) { public CitizensNPCRegistry(Storage saves) {
this.saves = saves; this.saves = saves;
types.put(EntityType.BLAZE, CitizensBlazeNPC.class); types.put(EntityType.BLAZE, CitizensBlazeNPC.class);
@ -152,9 +152,11 @@ public class CitizensNPCManager implements NPCManager {
public void deregister(NPC npc) { public void deregister(NPC npc) {
npcs.remove(npc.getId()); npcs.remove(npc.getId());
saves.getKey("npc").removeKey(String.valueOf(npc.getId())); saves.getKey("npc").removeKey(String.valueOf(npc.getId()));
npc.despawn();
} }
public void removeAll() { @Override
public void deregisterAll() {
Iterator<NPC> itr = iterator(); Iterator<NPC> itr = iterator();
while (itr.hasNext()) { while (itr.hasNext()) {
NPC npc = itr.next(); NPC npc = itr.next();

View File

@ -98,8 +98,8 @@ public class CitizensTraitManager implements TraitManager {
Trait trait = create(subEntry.getValue(), npc); Trait trait = create(subEntry.getValue(), npc);
if (trait == null) if (trait == null)
return null; return null;
trait.setName(subEntry.getKey());
trait.setPlugin(entry.getKey()); trait.setPlugin(entry.getKey());
trait.setName(subEntry.getKey());
return (T) trait; return (T) trait;
} }
} }

View File

@ -76,7 +76,7 @@ public class NPCSelector implements Listener {
// Remove editor if the player has one // Remove editor if the player has one
Editor.leave(player); Editor.leave(player);
} else { } else {
this.consoleSelectedNPC = npc.getId(); consoleSelectedNPC = npc.getId();
npc.setMetadata("selectors", new FixedMetadataValue(plugin, "console")); npc.setMetadata("selectors", new FixedMetadataValue(plugin, "console"));
} }
@ -88,11 +88,11 @@ public class NPCSelector implements Listener {
List<MetadataValue> metadata = ((Player) sender).getMetadata("selected"); List<MetadataValue> metadata = ((Player) sender).getMetadata("selected");
if (metadata.size() == 0) if (metadata.size() == 0)
return null; return null;
return CitizensAPI.getNPCManager().getNPC(metadata.get(0).asInt()); return CitizensAPI.getNPCRegistry().getNPC(metadata.get(0).asInt());
} else { } else {
if (consoleSelectedNPC == -1) if (consoleSelectedNPC == -1)
return null; return null;
return CitizensAPI.getNPCManager().getNPC(consoleSelectedNPC); return CitizensAPI.getNPCRegistry().getNPC(consoleSelectedNPC);
} }
} }
} }

View File

@ -1,12 +1,12 @@
package net.citizensnpcs.trait; package net.citizensnpcs.trait;
import org.bukkit.entity.Ageable;
import net.citizensnpcs.api.exception.NPCLoadException; import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.Trait; import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.util.DataKey; import net.citizensnpcs.api.util.DataKey;
import org.bukkit.entity.Ageable;
public class Age extends Trait implements Runnable, Toggleable { public class Age extends Trait implements Runnable, Toggleable {
private int age = 0; private int age = 0;
private boolean locked = true; private boolean locked = true;
@ -18,17 +18,17 @@ public class Age extends Trait implements Runnable, Toggleable {
@Override @Override
public void load(DataKey key) throws NPCLoadException { public void load(DataKey key) throws NPCLoadException {
if (!(npc.getBukkitEntity() instanceof Ageable))
throw new NPCLoadException("NPC must be ageable");
age = key.getInt("age"); age = key.getInt("age");
locked = key.getBoolean("locked"); locked = key.getBoolean("locked");
} }
@Override @Override
public void onNPCSpawn() { public void onNPCSpawn() {
if (npc.getBukkitEntity() instanceof Ageable) { Ageable entity = (Ageable) npc.getBukkitEntity();
Ageable entity = (Ageable) npc.getBukkitEntity(); entity.setAge(age);
entity.setAge(age); entity.setAgeLock(locked);
entity.setAgeLock(locked);
}
} }
@Override @Override
@ -45,8 +45,7 @@ public class Age extends Trait implements Runnable, Toggleable {
public void setAge(int age) { public void setAge(int age) {
this.age = age; this.age = age;
if (npc.getBukkitEntity() instanceof Ageable) ((Ageable) npc.getBukkitEntity()).setAge(age);
((Ageable) npc.getBukkitEntity()).setAge(age);
} }
@Override @Override

View File

@ -1,11 +1,12 @@
package net.citizensnpcs.trait; package net.citizensnpcs.trait;
import org.bukkit.entity.Creeper; import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.Trait; import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.util.DataKey; import net.citizensnpcs.api.util.DataKey;
import org.bukkit.entity.Creeper;
public class Powered extends Trait implements Toggleable { public class Powered extends Trait implements Toggleable {
private final NPC npc; private final NPC npc;
private boolean powered; private boolean powered;
@ -15,14 +16,15 @@ public class Powered extends Trait implements Toggleable {
} }
@Override @Override
public void load(DataKey key) { public void load(DataKey key) throws NPCLoadException {
if (!(npc.getBukkitEntity() instanceof Creeper))
throw new NPCLoadException("NPC must be a creeper");
powered = key.getBoolean(""); powered = key.getBoolean("");
} }
@Override @Override
public void onNPCSpawn() { public void onNPCSpawn() {
if (npc.getBukkitEntity() instanceof Creeper) ((Creeper) npc.getBukkitEntity()).setPowered(powered);
((Creeper) npc.getBukkitEntity()).setPowered(powered);
} }
@Override @Override

View File

@ -1,16 +1,16 @@
package net.citizensnpcs.trait; package net.citizensnpcs.trait;
import org.bukkit.entity.Pig;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.exception.NPCLoadException; import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.Trait; import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.util.DataKey; import net.citizensnpcs.api.util.DataKey;
import org.bukkit.entity.Pig;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEntityEvent;
public class Saddle extends Trait implements Toggleable, Listener { public class Saddle extends Trait implements Toggleable, Listener {
private final NPC npc; private final NPC npc;
private boolean saddle; private boolean saddle;
@ -21,18 +21,19 @@ public class Saddle extends Trait implements Toggleable, Listener {
@Override @Override
public void load(DataKey key) throws NPCLoadException { public void load(DataKey key) throws NPCLoadException {
if (!(npc.getBukkitEntity() instanceof Pig))
throw new NPCLoadException("NPC must be a pig to have this trait");
saddle = key.getBoolean(""); saddle = key.getBoolean("");
} }
@Override @Override
public void onNPCSpawn() { public void onNPCSpawn() {
if (npc.getBukkitEntity() instanceof Pig) ((Pig) npc.getBukkitEntity()).setSaddle(saddle);
((Pig) npc.getBukkitEntity()).setSaddle(saddle);
} }
@EventHandler @EventHandler
public void onPlayerInteractEntity(PlayerInteractEntityEvent event) { public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
if (CitizensAPI.getNPCManager().isNPC(event.getRightClicked())) if (npc.equals(CitizensAPI.getNPCRegistry().getNPC(event.getRightClicked())))
event.setCancelled(true); event.setCancelled(true);
} }

View File

@ -1,16 +1,16 @@
package net.citizensnpcs.trait; package net.citizensnpcs.trait;
import org.bukkit.entity.Sheep;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerShearEntityEvent;
import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.exception.NPCLoadException; import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.Trait; import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.util.DataKey; import net.citizensnpcs.api.util.DataKey;
import org.bukkit.entity.Sheep;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerShearEntityEvent;
public class Sheared extends Trait implements Toggleable, Listener { public class Sheared extends Trait implements Toggleable, Listener {
private final NPC npc; private final NPC npc;
private boolean sheared; private boolean sheared;
@ -21,18 +21,19 @@ public class Sheared extends Trait implements Toggleable, Listener {
@Override @Override
public void load(DataKey key) throws NPCLoadException { public void load(DataKey key) throws NPCLoadException {
if (!(npc.getBukkitEntity() instanceof Sheep))
throw new NPCLoadException("NPC must be a sheep to be sheared");
sheared = key.getBoolean(""); sheared = key.getBoolean("");
} }
@Override @Override
public void onNPCSpawn() { public void onNPCSpawn() {
if (npc.getBukkitEntity() instanceof Sheep) ((Sheep) npc.getBukkitEntity()).setSheared(sheared);
((Sheep) npc.getBukkitEntity()).setSheared(sheared);
} }
@EventHandler @EventHandler
public void onPlayerShearEntityEvent(PlayerShearEntityEvent event) { public void onPlayerShearEntityEvent(PlayerShearEntityEvent event) {
if (CitizensAPI.getNPCManager().isNPC(event.getEntity())) if (npc.equals(CitizensAPI.getNPCRegistry().getNPC(event.getEntity())))
event.setCancelled(true); event.setCancelled(true);
} }

View File

@ -1,17 +1,17 @@
package net.citizensnpcs.trait; package net.citizensnpcs.trait;
import org.bukkit.DyeColor;
import org.bukkit.entity.Sheep;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.SheepDyeWoolEvent;
import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.exception.NPCLoadException; import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.Trait; import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.util.DataKey; import net.citizensnpcs.api.util.DataKey;
import org.bukkit.DyeColor;
import org.bukkit.entity.Sheep;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.SheepDyeWoolEvent;
public class WoolColor extends Trait implements Listener { public class WoolColor extends Trait implements Listener {
private DyeColor color = DyeColor.WHITE; private DyeColor color = DyeColor.WHITE;
private final NPC npc; private final NPC npc;
@ -22,6 +22,8 @@ public class WoolColor extends Trait implements Listener {
@Override @Override
public void load(DataKey key) throws NPCLoadException { public void load(DataKey key) throws NPCLoadException {
if (!(npc.getBukkitEntity() instanceof Sheep))
throw new NPCLoadException("NPC must be a sheep");
try { try {
color = DyeColor.valueOf(key.getString("")); color = DyeColor.valueOf(key.getString(""));
} catch (Exception ex) { } catch (Exception ex) {
@ -31,13 +33,12 @@ public class WoolColor extends Trait implements Listener {
@Override @Override
public void onNPCSpawn() { public void onNPCSpawn() {
if (npc.getBukkitEntity() instanceof Sheep) ((Sheep) npc.getBukkitEntity()).setColor(color);
((Sheep) npc.getBukkitEntity()).setColor(color);
} }
@EventHandler @EventHandler
public void onSheepDyeWool(SheepDyeWoolEvent event) { public void onSheepDyeWool(SheepDyeWoolEvent event) {
if (CitizensAPI.getNPCManager().isNPC(event.getEntity())) if (npc.equals(CitizensAPI.getNPCRegistry().getNPC(event.getEntity())))
event.setCancelled(true); event.setCancelled(true);
} }