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,9 +65,15 @@ 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))
return;
ArmorStand entity = (ArmorStand) npc.getEntity(); ArmorStand entity = (ArmorStand) npc.getEntity();
if (leftArm != null) { if (leftArm != null) {
entity.setLeftArmPose(leftArm); entity.setLeftArmPose(leftArm);
@ -94,11 +100,11 @@ public class ArmorStandTrait extends Trait {
entity.setSmall(small); entity.setSmall(small);
entity.setMarker(marker); entity.setMarker(marker);
} }
}
@Override @Override
public void run() { public void run() {
if (npc.getEntity() instanceof ArmorStand) { if (!(npc.getEntity() instanceof ArmorStand))
return;
ArmorStand entity = (ArmorStand) npc.getEntity(); ArmorStand entity = (ArmorStand) npc.getEntity();
body = entity.getBodyPose(); body = entity.getBodyPose();
leftArm = entity.getLeftArmPose(); leftArm = entity.getLeftArmPose();
@ -113,7 +119,6 @@ public class ArmorStandTrait extends Trait {
entity.setSmall(small); entity.setSmall(small);
entity.setMarker(marker); entity.setMarker(marker);
} }
}
/** /**
* @see ArmorStand#setGravity(boolean) * @see ArmorStand#setGravity(boolean)

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();
} }