Use onPreSpawn event to avoid 'FOUC' in armorstands. Fix invulnerable ticks being reduced too quickly.

This commit is contained in:
fullwall 2021-09-23 14:12:44 +08:00
parent 620cda6542
commit 3d3de6576d
9 changed files with 60 additions and 77 deletions

View File

@ -255,6 +255,16 @@ public class CitizensNPC extends AbstractNPC {
entityController.spawn(at.clone(), this); entityController.spawn(at.clone(), this);
getEntity().setMetadata(NPC_METADATA_MARKER, new FixedMetadataValue(CitizensAPI.getPlugin(), true)); getEntity().setMetadata(NPC_METADATA_MARKER, new FixedMetadataValue(CitizensAPI.getPlugin(), true));
Collection<Trait> onPreSpawn = traits.values();
for (Trait trait : onPreSpawn.toArray(new Trait[onPreSpawn.size()])) {
try {
trait.onPreSpawn();
} catch (Throwable ex) {
Messaging.severeTr(Messages.TRAIT_ONSPAWN_FAILED, trait.getName(), getId());
ex.printStackTrace();
}
}
boolean loaded = Util.isLoaded(at); boolean loaded = Util.isLoaded(at);
boolean couldSpawn = !loaded ? false : NMS.addEntityToWorld(getEntity(), CreatureSpawnEvent.SpawnReason.CUSTOM); boolean couldSpawn = !loaded ? false : NMS.addEntityToWorld(getEntity(), CreatureSpawnEvent.SpawnReason.CUSTOM);
@ -294,10 +304,7 @@ public class CitizensNPC extends AbstractNPC {
navigator.onSpawn(); navigator.onSpawn();
// Modify NPC using traits after the entity has been created
Collection<Trait> onSpawn = traits.values(); Collection<Trait> onSpawn = traits.values();
// work around traits modifying the map during this iteration.
for (Trait trait : onSpawn.toArray(new Trait[onSpawn.size()])) { for (Trait trait : onSpawn.toArray(new Trait[onSpawn.size()])) {
try { try {
trait.onSpawn(); trait.onSpawn();

View File

@ -65,54 +65,59 @@ public class ArmorStandTrait extends Trait {
return visible; return visible;
} }
@Override
public void onPreSpawn() {
onSpawn();
}
@Override @Override
public void onSpawn() { public void onSpawn() {
if (npc.getEntity() instanceof ArmorStand) { if (!(npc.getEntity() instanceof ArmorStand))
ArmorStand entity = (ArmorStand) npc.getEntity(); return;
if (leftArm != null) { ArmorStand entity = (ArmorStand) npc.getEntity();
entity.setLeftArmPose(leftArm); if (leftArm != null) {
} entity.setLeftArmPose(leftArm);
if (leftLeg != null) {
entity.setLeftLegPose(leftLeg);
}
if (rightArm != null) {
entity.setRightArmPose(rightArm);
}
if (rightLeg != null) {
entity.setRightLegPose(rightLeg);
}
if (body != null) {
entity.setBodyPose(body);
}
if (head != null) {
entity.setHeadPose(head);
}
entity.setVisible(visible);
entity.setGravity(gravity);
entity.setArms(hasarms);
entity.setBasePlate(hasbaseplate);
entity.setSmall(small);
entity.setMarker(marker);
} }
if (leftLeg != null) {
entity.setLeftLegPose(leftLeg);
}
if (rightArm != null) {
entity.setRightArmPose(rightArm);
}
if (rightLeg != null) {
entity.setRightLegPose(rightLeg);
}
if (body != null) {
entity.setBodyPose(body);
}
if (head != null) {
entity.setHeadPose(head);
}
entity.setVisible(visible);
entity.setGravity(gravity);
entity.setArms(hasarms);
entity.setBasePlate(hasbaseplate);
entity.setSmall(small);
entity.setMarker(marker);
} }
@Override @Override
public void run() { public void run() {
if (npc.getEntity() instanceof ArmorStand) { if (!(npc.getEntity() instanceof ArmorStand))
ArmorStand entity = (ArmorStand) npc.getEntity(); return;
body = entity.getBodyPose(); ArmorStand entity = (ArmorStand) npc.getEntity();
leftArm = entity.getLeftArmPose(); body = entity.getBodyPose();
leftLeg = entity.getLeftLegPose(); leftArm = entity.getLeftArmPose();
rightArm = entity.getRightArmPose(); leftLeg = entity.getLeftLegPose();
rightLeg = entity.getRightLegPose(); rightArm = entity.getRightArmPose();
head = entity.getHeadPose(); rightLeg = entity.getRightLegPose();
entity.setVisible(visible); head = entity.getHeadPose();
entity.setGravity(gravity); entity.setVisible(visible);
entity.setArms(hasarms); entity.setGravity(gravity);
entity.setBasePlate(hasbaseplate); entity.setArms(hasarms);
entity.setSmall(small); entity.setBasePlate(hasbaseplate);
entity.setMarker(marker); entity.setSmall(small);
} entity.setMarker(marker);
} }
/** /**

View File

@ -127,10 +127,6 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
boolean navigating = npc.getNavigator().isNavigating(); boolean navigating = npc.getNavigator().isNavigating();
updatePackets(navigating); updatePackets(navigating);
if (noDamageTicks > 0) {
--noDamageTicks;
}
npc.update(); npc.update();
} }

View File

@ -140,10 +140,6 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
boolean navigating = npc.getNavigator().isNavigating(); boolean navigating = npc.getNavigator().isNavigating();
updatePackets(navigating); updatePackets(navigating);
if (noDamageTicks > 0) {
--noDamageTicks;
}
npc.update(); npc.update();
} }

View File

@ -421,10 +421,6 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
boolean navigating = npc.getNavigator().isNavigating(); boolean navigating = npc.getNavigator().isNavigating();
updatePackets(navigating); updatePackets(navigating);
if (noDamageTicks > 0) {
--noDamageTicks;
}
npc.update(); npc.update();
} }

View File

@ -424,11 +424,6 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
boolean navigating = npc.getNavigator().isNavigating(); boolean navigating = npc.getNavigator().isNavigating();
updatePackets(navigating); updatePackets(navigating);
if (noDamageTicks > 0) {
--noDamageTicks;
}
npc.update(); npc.update();
} }

View File

@ -170,7 +170,7 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
public void run() { public void run() {
((WorldServer) world).removeEntity(EntityHumanNPC.this); ((WorldServer) world).removeEntity(EntityHumanNPC.this);
} }
}, 35); // give enough time for death and smoke animation }, 15); // give enough time for death and smoke animation
} }
@Override @Override
@ -425,10 +425,6 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
boolean navigating = npc.getNavigator().isNavigating(); boolean navigating = npc.getNavigator().isNavigating();
updatePackets(navigating); updatePackets(navigating);
if (noDamageTicks > 0) {
--noDamageTicks;
}
npc.update(); npc.update();
/* /*
double diff = this.yaw - this.aK; double diff = this.yaw - this.aK;

View File

@ -183,7 +183,7 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
public void run() { public void run() {
((WorldServer) world).removeEntity(EntityHumanNPC.this); ((WorldServer) world).removeEntity(EntityHumanNPC.this);
} }
}, 35); // give enough time for death and smoke animation }, 15); // give enough time for death and smoke animation
} }
@Override @Override
@ -455,10 +455,6 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
boolean navigating = npc.getNavigator().isNavigating(); boolean navigating = npc.getNavigator().isNavigating();
updatePackets(navigating); updatePackets(navigating);
if (noDamageTicks > 0) {
--noDamageTicks;
}
npc.update(); npc.update();
/* /*
double diff = this.yaw - this.aK; double diff = this.yaw - this.aK;

View File

@ -197,11 +197,11 @@ public class EntityHumanNPC extends ServerPlayer implements NPCHolder, Skinnable
if (this.hurtTime > 0) if (this.hurtTime > 0)
this.hurtTime--; this.hurtTime--;
if (this.invulnerableTime > 0)
this.invulnerableTime--;
if (isDeadOrDying()) { if (isDeadOrDying()) {
tickDeath(); tickDeath();
} }
if (this.lastHurtByPlayerTime > 0) { if (this.lastHurtByPlayerTime > 0) {
this.lastHurtByPlayerTime--; this.lastHurtByPlayerTime--;
} else { } else {
@ -482,10 +482,6 @@ public class EntityHumanNPC extends ServerPlayer implements NPCHolder, Skinnable
updatePackets(npc.getNavigator().isNavigating()); updatePackets(npc.getNavigator().isNavigating());
if (invulnerableTime > 0) {
--invulnerableTime;
}
npc.update(); npc.update();
} }