Add sitting height offset correction check

This commit is contained in:
fullwall 2023-11-01 01:24:23 +08:00
parent 483b6565cd
commit 2736f4ab53

View File

@ -12,6 +12,7 @@ import net.citizensnpcs.api.npc.NPCRegistry;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.api.util.SpigotUtil;
import net.citizensnpcs.util.NMS;
@TraitName("sittrait")
@ -38,7 +39,11 @@ public class SitTrait extends Trait {
if (chair != null) {
if (chair.getEntity() != null) {
chair.getEntity().eject();
npc.getEntity().teleport(npc.getEntity().getLocation().clone().add(0, 0.3, 0));
Location npcLoc = npc.getEntity().getLocation().clone();
if (requiresPassengerOffsetCorrection()) {
npcLoc = npcLoc.add(0, 0.3, 0);
}
npc.getEntity().teleport(npcLoc);
}
chair.destroy();
chair = null;
@ -50,6 +55,11 @@ public class SitTrait extends Trait {
onDespawn();
}
private boolean requiresPassengerOffsetCorrection() {
return SpigotUtil.getVersion() != null && SpigotUtil.getVersion().length >= 2
&& SpigotUtil.getVersion()[1] <= 19;
}
@Override
public void run() {
if (!npc.isSpawned() || !isSitting())
@ -86,7 +96,11 @@ public class SitTrait extends Trait {
}
public void setSitting(Location at) {
this.sittingAt = at != null ? at.clone().add(0, -0.3, 0) : null;
this.sittingAt = at != null ? at.clone() : null;
if (requiresPassengerOffsetCorrection()) {
sittingAt = sittingAt.add(0, 0.3, 0);
}
if (at == null) {
onDespawn();
}