Refactor getHeight for NMS

This commit is contained in:
fullwall 2020-07-06 08:42:46 +08:00
parent 942b354967
commit d333c98823
12 changed files with 111 additions and 4 deletions

View File

@ -9,6 +9,7 @@ import net.citizensnpcs.api.astar.pathfinder.BlockSource;
import net.citizensnpcs.api.astar.pathfinder.MinecraftBlockExaminer;
import net.citizensnpcs.api.astar.pathfinder.PathPoint;
import net.citizensnpcs.api.util.BoundingBox;
import net.citizensnpcs.util.NMS;
public class BoundingBoxExaminer implements BlockExaminer {
private double height;
@ -16,8 +17,8 @@ public class BoundingBoxExaminer implements BlockExaminer {
public BoundingBoxExaminer(Entity entity) {
if (entity != null) {
this.height = entity.getHeight();
this.width = entity.getWidth();
this.height = NMS.getHeight(entity);
this.width = NMS.getWidth(entity);
}
}

View File

@ -18,6 +18,7 @@ import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.api.util.Placeholders;
import net.citizensnpcs.util.NMS;
/**
* Persists a hologram attached to the NPC.
@ -42,6 +43,17 @@ public class HologramTrait extends Trait {
load();
}
private double getEntityHeight() {
if (SUPPORT_GET_HEIGHT) {
try {
return npc.getEntity().getHeight();
} catch (NoSuchMethodError err) {
SUPPORT_GET_HEIGHT = false;
}
}
return NMS.getHeight(npc.getEntity());
}
private double getHeight(int lineNumber) {
return (lineHeight == -1 ? Setting.DEFAULT_NPC_HOLOGRAM_LINE_HEIGHT.asDouble() : lineHeight) * (lineNumber + 1);
}
@ -62,7 +74,7 @@ public class HologramTrait extends Trait {
trait.setGravity(false);
trait.setHasArms(false);
trait.setHasBaseplate(false);
hologramNPC.spawn(currentLoc.clone().add(0, npc.getEntity().getHeight() + getHeight(i), 0));
hologramNPC.spawn(currentLoc.clone().add(0, getEntityHeight() + getHeight(i), 0));
hologramNPC.getEntity().setInvulnerable(true);
hologramNPCs.add(hologramNPC);
i++;
@ -106,7 +118,7 @@ public class HologramTrait extends Trait {
if (hologram == null)
continue;
if (update) {
hologramNPC.teleport(currentLoc.clone().add(0, npc.getEntity().getHeight() + getHeight(i), 0),
hologramNPC.teleport(currentLoc.clone().add(0, getEntityHeight() + getHeight(i), 0),
TeleportCause.PLUGIN);
}
String text = lines.get(i);
@ -133,4 +145,6 @@ public class HologramTrait extends Trait {
}
hologramNPCs.clear();
}
private static boolean SUPPORT_GET_HEIGHT = true;
}

View File

@ -187,6 +187,10 @@ public class NMS {
return BRIDGE.getHeadYaw(entity);
}
public static double getHeight(Entity entity) {
return BRIDGE.getHeight(entity);
}
public static float getHorizontalMovement(org.bukkit.entity.Entity bukkitEntity) {
return BRIDGE.getHorizontalMovement(bukkitEntity);
}
@ -279,6 +283,10 @@ public class NMS {
return BRIDGE.getVerticalMovement(bukkitEntity);
}
public static double getWidth(Entity entity) {
return BRIDGE.getWidth(entity);
}
public static float getYaw(Entity entity) {
return BRIDGE.getYaw(entity);
}

View File

@ -55,6 +55,8 @@ public interface NMSBridge {
public float getHeadYaw(Entity entity);
public double getHeight(Entity entity);
public float getHorizontalMovement(Entity entity);
public NPC getNPC(Entity entity);
@ -79,6 +81,8 @@ public interface NMSBridge {
public float getVerticalMovement(Entity entity);
public double getWidth(Entity entity);
public float getYaw(Entity entity);
public boolean isOnGround(Entity entity);

View File

@ -369,6 +369,11 @@ public class NMSImpl implements NMSBridge {
return getHandle((LivingEntity) entity).aQ;
}
@Override
public double getHeight(org.bukkit.entity.Entity entity) {
return getHandle(entity).length;
}
@Override
public float getHorizontalMovement(org.bukkit.entity.Entity entity) {
if (!entity.getType().isAlive())
@ -564,6 +569,11 @@ public class NMSImpl implements NMSBridge {
return handle.bf;
}
@Override
public double getWidth(org.bukkit.entity.Entity entity) {
return getHandle(entity).width;
}
@Override
public float getYaw(org.bukkit.entity.Entity entity) {
return getHandle(entity).yaw;

View File

@ -388,6 +388,11 @@ public class NMSImpl implements NMSBridge {
return getHandle((LivingEntity) entity).aP;
}
@Override
public double getHeight(org.bukkit.entity.Entity entity) {
return getHandle(entity).length;
}
@Override
public float getHorizontalMovement(org.bukkit.entity.Entity entity) {
if (!entity.getType().isAlive())
@ -602,6 +607,11 @@ public class NMSImpl implements NMSBridge {
return handle.be;
}
@Override
public double getWidth(org.bukkit.entity.Entity entity) {
return getHandle(entity).width;
}
@Override
public float getYaw(org.bukkit.entity.Entity entity) {
return getHandle(entity).yaw;

View File

@ -392,6 +392,11 @@ public class NMSImpl implements NMSBridge {
return getHandle((LivingEntity) entity).aP;
}
@Override
public double getHeight(org.bukkit.entity.Entity entity) {
return entity.getHeight();
}
@Override
public float getHorizontalMovement(org.bukkit.entity.Entity entity) {
if (!entity.getType().isAlive())
@ -606,6 +611,11 @@ public class NMSImpl implements NMSBridge {
return handle.be;
}
@Override
public double getWidth(org.bukkit.entity.Entity entity) {
return entity.getWidth();
}
@Override
public float getYaw(org.bukkit.entity.Entity entity) {
return getHandle(entity).yaw;

View File

@ -415,6 +415,11 @@ public class NMSImpl implements NMSBridge {
return getHandle((LivingEntity) entity).aS;
}
@Override
public double getHeight(org.bukkit.entity.Entity entity) {
return entity.getHeight();
}
@Override
public float getHorizontalMovement(org.bukkit.entity.Entity entity) {
if (!entity.getType().isAlive())
@ -629,6 +634,11 @@ public class NMSImpl implements NMSBridge {
return handle.bh;
}
@Override
public double getWidth(org.bukkit.entity.Entity entity) {
return entity.getWidth();
}
@Override
public float getYaw(org.bukkit.entity.Entity entity) {
return getHandle(entity).yaw;

View File

@ -451,6 +451,11 @@ public class NMSImpl implements NMSBridge {
return getHandle((LivingEntity) entity).aM;
}
@Override
public double getHeight(org.bukkit.entity.Entity entity) {
return entity.getHeight();
}
@Override
public float getHorizontalMovement(org.bukkit.entity.Entity entity) {
if (!entity.getType().isAlive())
@ -678,6 +683,11 @@ public class NMSImpl implements NMSBridge {
return handle.bb;
}
@Override
public double getWidth(org.bukkit.entity.Entity entity) {
return entity.getWidth();
}
@Override
public float getYaw(org.bukkit.entity.Entity entity) {
return getHandle(entity).yaw;

View File

@ -458,6 +458,11 @@ public class NMSImpl implements NMSBridge {
return getHandle((LivingEntity) entity).aK;
}
@Override
public double getHeight(org.bukkit.entity.Entity entity) {
return entity.getHeight();
}
@Override
public float getHorizontalMovement(org.bukkit.entity.Entity entity) {
if (!entity.getType().isAlive())
@ -686,6 +691,11 @@ public class NMSImpl implements NMSBridge {
return handle.aZ;
}
@Override
public double getWidth(org.bukkit.entity.Entity entity) {
return entity.getWidth();
}
@Override
public float getYaw(org.bukkit.entity.Entity entity) {
return getHandle(entity).yaw;

View File

@ -464,6 +464,11 @@ public class NMSImpl implements NMSBridge {
return getHandle((LivingEntity) entity).getHeadRotation();
}
@Override
public double getHeight(org.bukkit.entity.Entity entity) {
return entity.getHeight();
}
@Override
public float getHorizontalMovement(org.bukkit.entity.Entity entity) {
if (!entity.getType().isAlive())
@ -692,6 +697,11 @@ public class NMSImpl implements NMSBridge {
return handle.aY;
}
@Override
public double getWidth(org.bukkit.entity.Entity entity) {
return entity.getWidth();
}
@Override
public float getYaw(org.bukkit.entity.Entity entity) {
return getHandle(entity).yaw;

View File

@ -320,6 +320,11 @@ public class NMSImpl implements NMSBridge {
return getHandle((LivingEntity) entity).aK;
}
@Override
public double getHeight(org.bukkit.entity.Entity entity) {
return getHandle(entity).length;
}
@Override
public float getHorizontalMovement(org.bukkit.entity.Entity entity) {
if (!entity.getType().isAlive())
@ -501,6 +506,11 @@ public class NMSImpl implements NMSBridge {
return handle.aZ;
}
@Override
public double getWidth(org.bukkit.entity.Entity entity) {
return getHandle(entity).width;
}
@Override
public float getYaw(org.bukkit.entity.Entity entity) {
return getHandle(entity).yaw;