2012-03-08 02:03:32 +01:00
|
|
|
package net.citizensnpcs.npc;
|
|
|
|
|
|
|
|
import java.lang.reflect.Constructor;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2012-03-12 11:17:28 +01:00
|
|
|
import java.util.Map.Entry;
|
2012-03-08 02:03:32 +01:00
|
|
|
|
|
|
|
import net.citizensnpcs.api.npc.NPC;
|
|
|
|
import net.citizensnpcs.api.trait.Trait;
|
|
|
|
import net.citizensnpcs.api.trait.TraitFactory;
|
|
|
|
import net.citizensnpcs.api.trait.TraitManager;
|
|
|
|
import net.citizensnpcs.api.trait.trait.Equipment;
|
|
|
|
import net.citizensnpcs.api.trait.trait.Inventory;
|
|
|
|
import net.citizensnpcs.api.trait.trait.MobType;
|
|
|
|
import net.citizensnpcs.api.trait.trait.Owner;
|
|
|
|
import net.citizensnpcs.api.trait.trait.Spawned;
|
2012-03-18 22:14:17 +01:00
|
|
|
import net.citizensnpcs.trait.Age;
|
2012-04-30 15:38:18 +02:00
|
|
|
import net.citizensnpcs.trait.Behaviour;
|
2012-04-19 05:52:07 +02:00
|
|
|
import net.citizensnpcs.trait.Controllable;
|
2012-03-12 07:26:08 +01:00
|
|
|
import net.citizensnpcs.trait.CurrentLocation;
|
2012-03-08 02:03:32 +01:00
|
|
|
import net.citizensnpcs.trait.LookClose;
|
2012-03-11 23:39:18 +01:00
|
|
|
import net.citizensnpcs.trait.Powered;
|
2012-03-19 00:11:13 +01:00
|
|
|
import net.citizensnpcs.trait.Saddle;
|
2012-03-17 16:43:43 +01:00
|
|
|
import net.citizensnpcs.trait.Sheared;
|
2012-03-15 12:02:53 +01:00
|
|
|
import net.citizensnpcs.trait.VillagerProfession;
|
2012-03-17 16:43:43 +01:00
|
|
|
import net.citizensnpcs.trait.WoolColor;
|
2012-03-08 02:03:32 +01:00
|
|
|
import net.citizensnpcs.trait.text.Text;
|
|
|
|
import net.citizensnpcs.trait.waypoint.Waypoints;
|
|
|
|
|
2012-04-19 05:52:07 +02:00
|
|
|
import org.bukkit.plugin.Plugin;
|
|
|
|
|
2012-03-08 02:03:32 +01:00
|
|
|
public class CitizensTraitManager implements TraitManager {
|
2012-03-12 20:13:20 +01:00
|
|
|
private final Map<Class<? extends Trait>, Constructor<? extends Trait>> CACHED_CTORS = new HashMap<Class<? extends Trait>, Constructor<? extends Trait>>();
|
2012-03-27 16:42:15 +02:00
|
|
|
private final Map<Plugin, Map<String, Class<? extends Trait>>> registered = new HashMap<Plugin, Map<String, Class<? extends Trait>>>();
|
2012-03-08 02:03:32 +01:00
|
|
|
|
2012-05-11 17:13:10 +02:00
|
|
|
// TODO: handle Plugin-setting/names better and avoid cruft. also find a
|
|
|
|
// way to avoid naming conflicts
|
|
|
|
public CitizensTraitManager(Plugin plugin) {
|
2012-03-23 12:18:10 +01:00
|
|
|
registerTrait(new TraitFactory(Age.class).withName("age").withPlugin(plugin));
|
|
|
|
registerTrait(new TraitFactory(CurrentLocation.class).withName("location").withPlugin(plugin));
|
|
|
|
registerTrait(new TraitFactory(Equipment.class).withName("equipment").withPlugin(plugin));
|
|
|
|
registerTrait(new TraitFactory(Inventory.class).withName("inventory").withPlugin(plugin));
|
2012-04-22 11:57:50 +02:00
|
|
|
registerTrait(new TraitFactory(LookClose.class).withName("lookclose").withPlugin(plugin));
|
2012-03-23 12:18:10 +01:00
|
|
|
registerTrait(new TraitFactory(MobType.class).withName("type").withPlugin(plugin));
|
|
|
|
registerTrait(new TraitFactory(Owner.class).withName("owner").withPlugin(plugin));
|
|
|
|
registerTrait(new TraitFactory(Powered.class).withName("powered").withPlugin(plugin));
|
|
|
|
registerTrait(new TraitFactory(Saddle.class).withName("saddle").withPlugin(plugin));
|
|
|
|
registerTrait(new TraitFactory(Sheared.class).withName("sheared").withPlugin(plugin));
|
|
|
|
registerTrait(new TraitFactory(Spawned.class).withName("spawned").withPlugin(plugin));
|
|
|
|
registerTrait(new TraitFactory(Text.class).withName("text").withPlugin(plugin));
|
|
|
|
registerTrait(new TraitFactory(VillagerProfession.class).withName("profession").withPlugin(plugin));
|
|
|
|
registerTrait(new TraitFactory(Waypoints.class).withName("waypoints").withPlugin(plugin));
|
2012-04-22 11:57:50 +02:00
|
|
|
registerTrait(new TraitFactory(WoolColor.class).withName("woolcolor").withPlugin(plugin));
|
2012-04-19 05:52:07 +02:00
|
|
|
registerTrait(new TraitFactory(Controllable.class).withName("controllable").withPlugin(plugin));
|
2012-04-30 15:38:18 +02:00
|
|
|
registerTrait(new TraitFactory(Behaviour.class).withName("behaviour").withPlugin(plugin));
|
2012-03-08 02:03:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
2012-03-27 16:42:15 +02:00
|
|
|
private <T extends Trait> T create(Class<T> trait, NPC npc) {
|
|
|
|
Constructor<? extends Trait> constructor;
|
|
|
|
|
|
|
|
if (!CACHED_CTORS.containsKey(trait)) {
|
|
|
|
try {
|
2012-05-11 17:13:10 +02:00
|
|
|
// TODO: replace this fixed constructor with a context class
|
|
|
|
// which can have extra environment variables.
|
2012-03-27 16:42:15 +02:00
|
|
|
constructor = trait.getConstructor(NPC.class);
|
2012-04-20 16:19:03 +02:00
|
|
|
if (constructor == null)
|
|
|
|
constructor = trait.getConstructor(CitizensNPC.class);
|
2012-03-27 16:42:15 +02:00
|
|
|
constructor.setAccessible(true);
|
|
|
|
} catch (Exception ex) {
|
|
|
|
constructor = null;
|
|
|
|
}
|
|
|
|
CACHED_CTORS.put(trait, constructor);
|
|
|
|
} else
|
|
|
|
constructor = CACHED_CTORS.get(trait);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (constructor == null || npc == null)
|
|
|
|
return trait.newInstance();
|
|
|
|
return (T) constructor.newInstance(npc);
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
return null;
|
2012-03-23 12:18:10 +01:00
|
|
|
}
|
2012-03-08 02:03:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-03-27 16:42:15 +02:00
|
|
|
public <T extends Trait> T getTrait(Class<T> clazz) {
|
|
|
|
return getTrait(clazz, null);
|
2012-03-08 02:03:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public <T extends Trait> T getTrait(Class<T> clazz, NPC npc) {
|
2012-03-23 12:18:10 +01:00
|
|
|
for (Entry<Plugin, Map<String, Class<? extends Trait>>> entry : registered.entrySet()) {
|
|
|
|
for (Entry<String, Class<? extends Trait>> subEntry : entry.getValue().entrySet()) {
|
|
|
|
if (!subEntry.getValue().equals(clazz))
|
|
|
|
continue;
|
2012-03-23 12:55:53 +01:00
|
|
|
Trait trait = create(subEntry.getValue(), npc);
|
2012-04-20 16:19:03 +02:00
|
|
|
if (trait == null)
|
|
|
|
return null;
|
2012-03-23 12:55:53 +01:00
|
|
|
trait.setName(subEntry.getKey());
|
|
|
|
trait.setPlugin(entry.getKey());
|
|
|
|
return (T) trait;
|
2012-03-23 12:18:10 +01:00
|
|
|
}
|
2012-03-12 11:17:28 +01:00
|
|
|
}
|
2012-03-08 02:03:32 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2012-03-27 16:42:15 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
@Override
|
|
|
|
public <T extends Trait> T getTrait(String name) {
|
2012-05-11 17:13:10 +02:00
|
|
|
for (Map<String, Class<? extends Trait>> entry : registered.values()) {
|
|
|
|
if (!entry.containsKey(name))
|
|
|
|
continue;
|
|
|
|
return (T) create(entry.get(name), null);
|
2012-03-27 16:42:15 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2012-03-08 02:03:32 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
2012-03-10 20:41:09 +01:00
|
|
|
public <T extends Trait> T getTrait(String name, NPC npc) {
|
2012-05-11 17:13:10 +02:00
|
|
|
for (Map<String, Class<? extends Trait>> entry : registered.values()) {
|
|
|
|
Class<? extends Trait> clazz = entry.get(name);
|
2012-03-23 12:18:10 +01:00
|
|
|
if (clazz == null)
|
|
|
|
continue;
|
|
|
|
return (T) getTrait(clazz, npc);
|
|
|
|
}
|
|
|
|
return null;
|
2012-03-08 02:03:32 +01:00
|
|
|
}
|
|
|
|
|
2012-03-27 16:42:15 +02:00
|
|
|
@Override
|
|
|
|
public void registerTrait(TraitFactory factory) {
|
|
|
|
Map<String, Class<? extends Trait>> map = registered.get(factory.getTraitPlugin());
|
|
|
|
if (map == null)
|
|
|
|
map = new HashMap<String, Class<? extends Trait>>();
|
|
|
|
map.put(factory.getTraitName(), factory.getTraitClass());
|
|
|
|
registered.put(factory.getTraitPlugin(), map);
|
2012-03-08 02:03:32 +01:00
|
|
|
}
|
|
|
|
}
|