Add head yaw persistence

This commit is contained in:
fullwall 2020-03-21 18:30:52 +08:00
parent 1e507a1d57
commit 1cd8e1913e
12 changed files with 81 additions and 34 deletions

View File

@ -154,6 +154,7 @@ public class CitizensNPC extends AbstractNPC {
CurrentLocation spawnLocation = getTrait(CurrentLocation.class);
if (getTrait(Spawned.class).shouldSpawn() && spawnLocation.getLocation() != null) {
spawn(spawnLocation.getLocation(), SpawnReason.RESPAWN);
NMS.setHeadYaw(getEntity(), spawnLocation.getHeadYaw());
}
if (getTrait(Spawned.class).shouldSpawn() && spawnLocation.getLocation() == null) {
Messaging.debug("Tried to spawn", getId(), "on load but world was null");
@ -261,6 +262,7 @@ public class CitizensNPC extends AbstractNPC {
getEntity().teleport(at);
NMS.setBodyYaw(getEntity(), at.getYaw());
NMS.setHeadYaw(getEntity(), at.getYaw());
// Set the spawned state

View File

@ -5,12 +5,15 @@ import org.bukkit.Location;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.util.NMS;
/**
* Persists the current {@link Location} of the {@link NPC}. Will cache last known location if despawned.
*/
@TraitName("location")
public class CurrentLocation extends Trait {
@Persist
private float headYaw;
@Persist(value = "", required = true)
private Location location = new Location(null, 0, 0, 0);
@ -18,6 +21,10 @@ public class CurrentLocation extends Trait {
super("location");
}
public float getHeadYaw() {
return headYaw;
}
public Location getLocation() {
return location.getWorld() == null ? null : location;
}
@ -27,6 +34,7 @@ public class CurrentLocation extends Trait {
if (!npc.isSpawned())
return;
location = npc.getEntity().getLocation(location);
headYaw = NMS.getHeadYaw(npc.getEntity());
}
public void setLocation(Location loc) {

View File

@ -358,6 +358,10 @@ public class NMS {
BRIDGE.sendTabListRemove(recipient, listPlayer);
}
public static void setBodyYaw(Entity entity, float yaw) {
BRIDGE.setBodyYaw(entity, yaw);
}
public static void setDestination(org.bukkit.entity.Entity entity, double x, double y, double z, float speed) {
BRIDGE.setDestination(entity, x, y, z, speed);
}

View File

@ -115,6 +115,8 @@ public interface NMSBridge {
public void sendTabListRemove(Player recipient, Player listPlayer);
public void setBodyYaw(Entity entity, float yaw);
public void setDestination(Entity entity, double x, double y, double z, float speed);
public void setDummyAdvancement(Player entity);

View File

@ -843,6 +843,11 @@ public class NMSImpl implements NMSBridge {
new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER, entity));
}
@Override
public void setBodyYaw(org.bukkit.entity.Entity entity, float yaw) {
getHandle(entity).yaw = yaw;
}
@Override
public void setDestination(org.bukkit.entity.Entity entity, double x, double y, double z, float speed) {
Entity handle = NMSImpl.getHandle(entity);

View File

@ -899,6 +899,11 @@ public class NMSImpl implements NMSBridge {
new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER, entity));
}
@Override
public void setBodyYaw(org.bukkit.entity.Entity entity, float yaw) {
getHandle(entity).yaw = yaw;
}
@Override
public void setDestination(org.bukkit.entity.Entity entity, double x, double y, double z, float speed) {
Entity handle = NMSImpl.getHandle(entity);

View File

@ -907,6 +907,11 @@ public class NMSImpl implements NMSBridge {
new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER, entity));
}
@Override
public void setBodyYaw(org.bukkit.entity.Entity entity, float yaw) {
getHandle(entity).yaw = yaw;
}
@Override
public void setDestination(org.bukkit.entity.Entity entity, double x, double y, double z, float speed) {
Entity handle = NMSImpl.getHandle(entity);

View File

@ -942,6 +942,11 @@ public class NMSImpl implements NMSBridge {
new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER, entity));
}
@Override
public void setBodyYaw(org.bukkit.entity.Entity entity, float yaw) {
getHandle(entity).yaw = yaw;
}
@Override
public void setDestination(org.bukkit.entity.Entity entity, double x, double y, double z, float speed) {
Entity handle = NMSImpl.getHandle(entity);

View File

@ -995,6 +995,11 @@ public class NMSImpl implements NMSBridge {
new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER, entity));
}
@Override
public void setBodyYaw(org.bukkit.entity.Entity entity, float yaw) {
getHandle(entity).yaw = yaw;
}
@Override
public void setDestination(org.bukkit.entity.Entity entity, double x, double y, double z, float speed) {
Entity handle = NMSImpl.getHandle(entity);

View File

@ -48,11 +48,8 @@ public class HorseZombieController extends MobEntityController {
public static class EntityHorseZombieNPC extends EntityHorseZombie implements NPCHolder {
private double baseMovementSpeed;
boolean calledNMSHeight = false;
private final CitizensNPC npc;
private boolean riding;
public EntityHorseZombieNPC(EntityTypes<? extends EntityHorseZombie> types, World world) {
@ -96,6 +93,13 @@ public class HorseZombieController extends MobEntityController {
return false;
}
@Override
public void checkDespawn() {
if (npc == null) {
super.checkDespawn();
}
}
@Override
public boolean cj() {
if (npc != null && riding) {
@ -104,13 +108,6 @@ public class HorseZombieController extends MobEntityController {
return super.cj();
}
@Override
public void checkDespawn() {
if (npc == null) {
super.checkDespawn();
}
}
@Override
public void collide(net.minecraft.server.v1_15_R1.Entity entity) {
// this method is called by both the entities involved - cancelling
@ -148,28 +145,6 @@ public class HorseZombieController extends MobEntityController {
}
}
@Override
public void h(double x, double y, double z) {
if (npc == null) {
super.h(x, y, z);
return;
}
if (NPCPushEvent.getHandlerList().getRegisteredListeners().length == 0) {
if (!npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true))
super.h(x, y, z);
return;
}
Vector vector = new Vector(x, y, z);
NPCPushEvent event = Util.callPushEvent(npc, vector);
if (!event.isCancelled()) {
vector = event.getCollisionVector();
super.h(vector.getX(), vector.getY(), vector.getZ());
}
// when another entity collides, this method is called to push the
// NPC so we prevent it from doing anything if the event is
// cancelled.
}
@Override
public CraftEntity getBukkitEntity() {
if (npc != null && !(super.getBukkitEntity() instanceof NPCHolder)) {
@ -198,6 +173,28 @@ public class HorseZombieController extends MobEntityController {
return NMSImpl.getSoundEffect(npc, super.getSoundHurt(damagesource), NPC.HURT_SOUND_METADATA);
}
@Override
public void h(double x, double y, double z) {
if (npc == null) {
super.h(x, y, z);
return;
}
if (NPCPushEvent.getHandlerList().getRegisteredListeners().length == 0) {
if (!npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true))
super.h(x, y, z);
return;
}
Vector vector = new Vector(x, y, z);
NPCPushEvent event = Util.callPushEvent(npc, vector);
if (!event.isCancelled()) {
vector = event.getCollisionVector();
super.h(vector.getX(), vector.getY(), vector.getZ());
}
// when another entity collides, this method is called to push the
// NPC so we prevent it from doing anything if the event is
// cancelled.
}
@Override
public boolean isClimbing() {
if (npc == null || !npc.isFlyable()) {

View File

@ -435,7 +435,7 @@ public class NMSImpl implements NMSBridge {
if (!(entity instanceof LivingEntity)) {
return entity.getLocation().getYaw();
}
return getHandle((LivingEntity) entity).aM;
return getHandle((LivingEntity) entity).aK;
}
@Override
@ -998,6 +998,11 @@ public class NMSImpl implements NMSBridge {
new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER, entity));
}
@Override
public void setBodyYaw(org.bukkit.entity.Entity entity, float yaw) {
getHandle(entity).yaw = yaw;
}
@Override
public void setDestination(org.bukkit.entity.Entity entity, double x, double y, double z, float speed) {
Entity handle = NMSImpl.getHandle(entity);
@ -1729,7 +1734,6 @@ public class NMSImpl implements NMSBridge {
private static final MethodHandle ADVANCEMENT_PLAYER_FIELD = NMS.getFinalSetter(EntityPlayer.class,
"advancementDataPlayer");
private static final Set<EntityType> BAD_CONTROLLER_LOOK = EnumSet.of(EntityType.POLAR_BEAR, EntityType.BEE,
EntityType.SILVERFISH, EntityType.SHULKER, EntityType.ENDERMITE, EntityType.ENDER_DRAGON, EntityType.BAT,
EntityType.SLIME, EntityType.DOLPHIN, EntityType.MAGMA_CUBE, EntityType.HORSE, EntityType.GHAST,

View File

@ -779,6 +779,11 @@ public class NMSImpl implements NMSBridge {
new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER, entity));
}
@Override
public void setBodyYaw(org.bukkit.entity.Entity entity, float yaw) {
getHandle(entity).yaw = yaw;
}
@Override
public void setDestination(org.bukkit.entity.Entity entity, double x, double y, double z, float speed) {
Entity handle = NMSImpl.getHandle(entity);