From e43a233ade2350b7f85c7a54eee372eed6cd1f19 Mon Sep 17 00:00:00 2001 From: fullwall Date: Mon, 29 Oct 2012 22:45:03 +0800 Subject: [PATCH] NPC stairclimbing: still not working --- .../net/citizensnpcs/npc/CitizensNPC.java | 23 +++++++++++++++++-- .../npc/entity/CitizensHumanNPC.java | 6 ++--- .../npc/entity/EntityHumanNPC.java | 15 ++++++------ 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/main/java/net/citizensnpcs/npc/CitizensNPC.java b/src/main/java/net/citizensnpcs/npc/CitizensNPC.java index 1878aacdf..41b432c6d 100644 --- a/src/main/java/net/citizensnpcs/npc/CitizensNPC.java +++ b/src/main/java/net/citizensnpcs/npc/CitizensNPC.java @@ -2,6 +2,8 @@ package net.citizensnpcs.npc; import java.util.List; +import javax.annotation.Nullable; + import net.citizensnpcs.EventListen; import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.ai.Navigator; @@ -27,6 +29,9 @@ import org.bukkit.entity.LivingEntity; import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; import org.bukkit.metadata.FixedMetadataValue; +import com.google.common.base.Function; +import com.google.common.base.Splitter; +import com.google.common.collect.Iterables; import com.google.common.collect.Lists; public abstract class CitizensNPC extends AbstractNPC { @@ -83,10 +88,19 @@ public abstract class CitizensNPC extends AbstractNPC { return getHandle() != null; } - public void load(DataKey root) { + public void load(final DataKey root) { metadata.loadFrom(root.getRelative("metadata")); // Load traits - for (DataKey traitKey : root.getRelative("traits").getSubKeys()) { + + String traitNames = root.getString("traitnames"); + Iterable keys = traitNames.isEmpty() ? root.getRelative("traits").getSubKeys() : Iterables + .transform(Splitter.on(',').split(traitNames), new Function() { + @Override + public DataKey apply(@Nullable String input) { + return root.getRelative("traits." + input); + } + }); + for (DataKey traitKey : keys) { if (traitKey.keyExists("enabled") && !traitKey.getBoolean("enabled")) continue; Class clazz = CitizensAPI.getTraitFactory().getTraitClass(traitKey.name()); @@ -141,11 +155,16 @@ public abstract class CitizensNPC extends AbstractNPC { navigator.save(root.getRelative("navigator")); // Save all existing traits + StringBuilder traitNames = new StringBuilder(); for (Trait trait : traits.values()) { DataKey traitKey = root.getRelative("traits." + trait.getName()); trait.save(traitKey); PersistenceLoader.save(trait, traitKey); removedTraits.remove(trait.getName()); + traitNames.append(trait.getName() + ","); + } + if (traitNames.length() > 0) { + root.setString("traitnames", traitNames.substring(0, traitNames.length() - 1)); } removeTraitData(root); } diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensHumanNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensHumanNPC.java index 8d3703914..27503f899 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensHumanNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensHumanNPC.java @@ -140,11 +140,9 @@ public class CitizensHumanNPC extends CitizensNPC implements Equipable { public void update() { super.update(); if (isSpawned() && getBukkitEntity().getLocation().getChunk().isLoaded()) { - if (NMS.inWater(mcEntity)) { - mcEntity.motY += 0.08F; - } else if (!getNavigator().isNavigating()) + if (!getNavigator().isNavigating() && !NMS.inWater(mcEntity)) mcEntity.move(0, -0.2, 0); - // gravity! also works around an entity.onGround not updating issue + // gravity. also works around an entity.onGround not updating issue // (onGround is normally updated by the client) } } diff --git a/src/main/java/net/citizensnpcs/npc/entity/EntityHumanNPC.java b/src/main/java/net/citizensnpcs/npc/entity/EntityHumanNPC.java index 9477a5950..bd1f27085 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/EntityHumanNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/EntityHumanNPC.java @@ -124,8 +124,8 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder { navigation.e(); moveOnCurrentHeading(); } else if (motX != 0 || motZ != 0 || motY != 0) - e(0, 0); // is this necessary? it does gravity/controllable but - // sometimes players sink into the ground + e(0, 0); // is this necessary? it does controllable but sometimes + // players sink into the ground if (noDamageTicks > 0) --noDamageTicks; @@ -136,17 +136,15 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder { getControllerMove().c(); getControllerLook().a(); getControllerJump().b(); - e(npc.getNavigator().getDefaultParameters().speed()); // taken from EntityLiving update method if (bG) { /* boolean inLiquid = H() || J(); if (inLiquid) { motY += 0.04; - } else (handled elsewhere)*/ + } else //(handled elsewhere)*/ if (onGround && bW == 0) { - // this.aZ(); - this doesn't jump high enough - motY = 0.6; + bf(); // jump bW = 10; } } else @@ -155,9 +153,10 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder { bD *= 0.98F; bE *= 0.98F; bF *= 0.9F; - + float speed = npc.getNavigator().getDefaultParameters().speed(); + e(speed); float prev = aM; - aM *= by() * npc.getNavigator().getDefaultParameters().speed(); + aM *= by() * speed; e(bD, bE); // movement method aM = prev; NMS.setHeadYaw(this, yaw);