Citizens2/src/main/java/net/citizensnpcs/npc/CitizensTraitManager.java

139 lines
6.1 KiB
Java
Raw Normal View History

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;
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;
import net.citizensnpcs.trait.Age;
import net.citizensnpcs.trait.Behaviour;
2012-04-19 05:52:07 +02:00
import net.citizensnpcs.trait.Controllable;
import net.citizensnpcs.trait.CurrentLocation;
2012-03-08 02:03:32 +01:00
import net.citizensnpcs.trait.LookClose;
import net.citizensnpcs.trait.Powered;
import net.citizensnpcs.trait.Saddle;
import net.citizensnpcs.trait.Sheared;
import net.citizensnpcs.trait.VillagerProfession;
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
// 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));
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));
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));
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 {
// 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);
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);
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-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) {
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) {
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
}
}