Fix some bugs

This commit is contained in:
fullwall 2012-07-21 23:19:54 +08:00
parent 6a991d7c2c
commit 91424d425c
5 changed files with 34 additions and 5 deletions

View File

@ -72,6 +72,7 @@ public abstract class CitizensNPC extends AbstractNPC {
}
public void load(DataKey root) {
metadata.loadFrom(root.getRelative("metadata"));
// Load traits
for (DataKey traitKey : root.getRelative("traits").getSubKeys()) {
Trait trait = CitizensAPI.getTraitFactory().getTrait(traitKey.name());
@ -101,6 +102,8 @@ public abstract class CitizensNPC extends AbstractNPC {
public void save(DataKey root) {
root.setString("name", getFullName());
metadata.saveTo(root.getRelative("metadata"));
// Save all existing traits
for (Trait trait : traits.values()) {
trait.save(root.getRelative("traits." + trait.getName()));
@ -133,6 +136,7 @@ public abstract class CitizensNPC extends AbstractNPC {
// Modify NPC using traits after the entity has been created
for (Trait trait : traits.values())
trait.onSpawn();
navigator.onSpawn();
return true;
}

View File

@ -35,6 +35,7 @@ import net.citizensnpcs.npc.entity.CitizensVillagerNPC;
import net.citizensnpcs.npc.entity.CitizensWolfNPC;
import net.citizensnpcs.npc.entity.CitizensZombieNPC;
import net.citizensnpcs.util.ByIdArray;
import net.citizensnpcs.util.Messaging;
import org.apache.commons.lang.Validate;
import org.bukkit.craftbukkit.entity.CraftEntity;
@ -132,6 +133,7 @@ public class CitizensNPCRegistry implements NPCRegistry {
try {
return npcClass.getConstructor(int.class, String.class).newInstance(id, name);
} catch (Exception ex) {
Messaging.log(ex);
return null;
}
}

View File

@ -2,6 +2,7 @@ package net.citizensnpcs.npc.ai;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.Set;
import net.citizensnpcs.api.ai.EntityTarget;
import net.citizensnpcs.api.ai.Navigator;
@ -10,6 +11,7 @@ import net.citizensnpcs.api.ai.event.NavigationBeginEvent;
import net.citizensnpcs.api.ai.event.NavigationCancelEvent;
import net.citizensnpcs.api.ai.event.NavigationReplaceEvent;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.util.Messaging;
import net.minecraft.server.EntityLiving;
import org.bukkit.Bukkit;
@ -18,15 +20,15 @@ import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
public class CitizensNavigator implements Navigator {
private PathStrategy executing;
private final CitizensNPC npc;
private float speed;
private float speed = -1;
public CitizensNavigator(CitizensNPC npc) {
this.npc = npc;
this.speed = getSpeedFor(npc.getHandle());
}
@Override
@ -88,25 +90,38 @@ public class CitizensNavigator implements Navigator {
@Override
public void setTarget(LivingEntity target, boolean aggressive) {
if (!npc.isSpawned())
throw new IllegalStateException("npc is not spawned");
PathStrategy newStrategy = new MCTargetStrategy(npc, target, aggressive, speed);
switchStrategyTo(newStrategy);
}
@Override
public void setTarget(Location target) {
if (!npc.isSpawned())
throw new IllegalStateException("npc is not spawned");
if (!logged.contains(target)) {
Messaging.log(target);
logged.add(target);
}
PathStrategy newStrategy = new MCNavigationStrategy(npc, target, speed);
switchStrategyTo(newStrategy);
}
private final Set<Location> logged = Sets.newHashSet();
private void switchStrategyTo(PathStrategy newStrategy) {
if (executing != null)
Bukkit.getPluginManager().callEvent(new NavigationReplaceEvent(this));
executing = newStrategy;
Bukkit.getPluginManager().callEvent(new NavigationBeginEvent(this));
}
public void onSpawn() {
if (speed == -1)
this.speed = getSpeedFor(npc.getHandle());
}
public void update() {
if (executing == null)
return;
@ -127,6 +142,7 @@ public class CitizensNavigator implements Navigator {
MOVEMENT_SPEEDS.put(EntityType.SHEEP, 0.25F);
MOVEMENT_SPEEDS.put(EntityType.VILLAGER, 0.3F);
MOVEMENT_SPEEDS.put(EntityType.SNOWMAN, 0.25F);
MOVEMENT_SPEEDS.put(EntityType.CREEPER, 0.3F);
try {
SPEED_FIELD = EntityLiving.class.getDeclaredField("bb");
SPEED_FIELD.setAccessible(true);

View File

@ -56,7 +56,8 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder {
navigation.d();
moveOnCurrentHeading();
} else if (motX != 0 || motZ != 0 || motY != 0) {
a(0, 0);
// a(0, 0);
}
if (noDamageTicks > 0)
--noDamageTicks;

View File

@ -36,6 +36,12 @@ public class Messaging {
log(Level.INFO, msg);
}
public static void log(Throwable ex) {
if (ex.getCause() != null)
ex = ex.getCause();
ex.printStackTrace();
}
public static void logF(Object... msg) {
log(getFormatted(msg));
}