Switch to new methods in TraitInfo

This commit is contained in:
fullwall 2013-01-05 23:48:47 +08:00
parent 73b962cb20
commit 8c3e50a25c
2 changed files with 20 additions and 21 deletions

View File

@ -199,7 +199,7 @@ public class CitizensNPC extends AbstractNPC {
} }
entityController.spawn(at, this); entityController.spawn(at, this);
EntityLiving mcEntity = getHandle(); EntityLiving mcEntity = ((CraftLivingEntity) getBukkitEntity()).getHandle();
boolean couldSpawn = !Util.isLoaded(at) ? false : mcEntity.world.addEntity(mcEntity, SpawnReason.CUSTOM); boolean couldSpawn = !Util.isLoaded(at) ? false : mcEntity.world.addEntity(mcEntity, SpawnReason.CUSTOM);
if (!couldSpawn) { if (!couldSpawn) {
Messaging.debug("Retrying spawn of", getId(), "later due to chunk being unloaded."); Messaging.debug("Retrying spawn of", getId(), "later due to chunk being unloaded.");

View File

@ -40,7 +40,7 @@ import com.google.common.collect.Maps;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
public class CitizensTraitFactory implements TraitFactory { public class CitizensTraitFactory implements TraitFactory {
private final Map<String, Class<? extends Trait>> registered = Maps.newHashMap(); private final Map<String, TraitInfo> registered = Maps.newHashMap();
public CitizensTraitFactory() { public CitizensTraitFactory() {
registerTrait(TraitInfo.create(Age.class).withName("age")); registerTrait(TraitInfo.create(Age.class).withName("age"));
@ -68,15 +68,16 @@ public class CitizensTraitFactory implements TraitFactory {
registerTrait(TraitInfo.create(WoolColor.class).withName("woolcolor")); registerTrait(TraitInfo.create(WoolColor.class).withName("woolcolor"));
registerTrait(TraitInfo.create(ZombieModifier.class).withName("zombiemodifier")); registerTrait(TraitInfo.create(ZombieModifier.class).withName("zombiemodifier"));
for (String trait : registered.keySet()) for (String trait : registered.keySet()) {
INTERNAL_TRAITS.add(trait); INTERNAL_TRAITS.add(trait);
}
} }
public void addPlotters(Graph graph) { public void addPlotters(Graph graph) {
for (Map.Entry<String, Class<? extends Trait>> entry : registered.entrySet()) { for (Map.Entry<String, TraitInfo> entry : registered.entrySet()) {
if (INTERNAL_TRAITS.contains(entry.getKey())) if (INTERNAL_TRAITS.contains(entry.getKey()))
continue; continue;
final Class<? extends Trait> traitClass = entry.getValue(); final Class<? extends Trait> traitClass = entry.getValue().getTraitClass();
graph.addPlotter(new Metrics.Plotter(entry.getKey()) { graph.addPlotter(new Metrics.Plotter(entry.getKey()) {
@Override @Override
public int getValue() { public int getValue() {
@ -91,34 +92,32 @@ public class CitizensTraitFactory implements TraitFactory {
} }
} }
private <T extends Trait> T create(Class<T> trait) { private <T extends Trait> T create(TraitInfo info) {
try { return info.tryCreateInstance();
return trait.newInstance();
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
} }
@Override @Override
public <T extends Trait> T getTrait(Class<T> clazz) { public <T extends Trait> T getTrait(Class<T> clazz) {
if (!registered.containsValue(clazz)) for (TraitInfo entry : registered.values()) {
return null; if (clazz == entry.getTraitClass())
return create(clazz); return create(entry);
}
return null;
} }
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T extends Trait> T getTrait(String name) { public <T extends Trait> T getTrait(String name) {
Class<? extends Trait> clazz = registered.get(name); TraitInfo info = registered.get(name);
if (clazz == null) if (info == null)
return null; return null;
return (T) create(clazz); return (T) create(info);
} }
@Override @Override
public Class<? extends Trait> getTraitClass(String name) { public Class<? extends Trait> getTraitClass(String name) {
return registered.get(name.toLowerCase()); TraitInfo info = registered.get(name.toLowerCase());
return info == null ? null : info.getTraitClass();
} }
@Override @Override
@ -129,9 +128,9 @@ public class CitizensTraitFactory implements TraitFactory {
@Override @Override
public void registerTrait(TraitInfo info) { public void registerTrait(TraitInfo info) {
Preconditions.checkNotNull(info, "info cannot be null"); Preconditions.checkNotNull(info, "info cannot be null");
if (registered.containsKey(info)) if (registered.containsKey(info.getTraitName()))
throw new IllegalArgumentException("trait name already registered"); throw new IllegalArgumentException("trait name already registered");
registered.put(info.getTraitName(), info.getTraitClass()); registered.put(info.getTraitName(), info);
} }
private static final Set<String> INTERNAL_TRAITS = Sets.newHashSet(); private static final Set<String> INTERNAL_TRAITS = Sets.newHashSet();