Skip to content

Commit

Permalink
Use cached ground height when inside unloaded chunks
Browse files Browse the repository at this point in the history
Show placeholder for radio altitude.
  • Loading branch information
zomabies committed Mar 17, 2024
1 parent 2845d86 commit 7ebe94c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class AirDataComputer implements ITickableComputer {
public float flightYaw;
public int groundLevel;
public Float elytraHealth;
public boolean isCurrentChunkLoaded;

public AirDataComputer(MinecraftClient mc) {
this.mc = mc;
Expand All @@ -39,6 +40,7 @@ public AirDataComputer(MinecraftClient mc) {
public void tick() {
velocity = player().getVelocity().multiply(TICKS_PER_SECOND);
roll = computeRoll(RenderSystem.getInverseViewRotationMatrix().invert());
isCurrentChunkLoaded = isCurrentChunkLoaded();
groundLevel = computeGroundLevel();
flightPitch = computeFlightPitch(velocity, pitch());
flightYaw = computeFlightYaw(velocity, yaw());
Expand Down Expand Up @@ -91,6 +93,9 @@ private Float computeElytraHealth() {
}

private int computeGroundLevel() {
if (!isCurrentChunkLoaded) {
return groundLevel; // last known cache
}
BlockPos ground = findGround(player().getBlockPos().mutableCopy());
return ground == null ? voidLevel() : ground.getY();
}
Expand All @@ -101,7 +106,7 @@ public boolean isGround(BlockPos pos) {
}

public BlockPos findGround(BlockPos.Mutable from) {
if (!world().getChunkManager().isChunkLoaded(ChunkSectionPos.getSectionCoord(from.getX()), ChunkSectionPos.getSectionCoord(from.getZ()))) {
if (!isChunkLoadedAt(from)) {
return null;
}
int start = from.getY();
Expand Down Expand Up @@ -159,7 +164,7 @@ public float flightHeading() {

public float heightAboveGround() {
float height = Math.max(0.0f, altitude() - groundLevel);
if (height < 1.0f) {
if (height < 1.0f && isCurrentChunkLoaded) {
throw new AssertionError(height);
}
return height;
Expand All @@ -177,6 +182,15 @@ public World world() {
return player().getWorld();
}

public boolean isChunkLoadedAt(BlockPos pos){
return world().getChunkManager().isChunkLoaded(ChunkSectionPos.getSectionCoord(pos.getX()), ChunkSectionPos.getSectionCoord(pos.getZ()));
}

private boolean isCurrentChunkLoaded(){
BlockPos pos = player().getBlockPos();
return isChunkLoadedAt(pos);
}

public static float validate(float f, float bounds) {
return validate(f, -bounds, bounds);
}
Expand All @@ -202,5 +216,6 @@ public void reset() {
roll = 0.0f;
groundLevel = 0;
elytraHealth = null;
isCurrentChunkLoaded = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,13 @@ public void render(DrawContext context, TextRenderer textRenderer) {

if (FAConfig.indicator().showGroundAltitude) {
Color color = data.altitude() < safeLevel ? FAConfig.indicator().warningColor : FAConfig.indicator().frameColor;
drawText(textRenderer, context, Text.translatable(data.groundLevel == data.voidLevel() ? "flightassistant.void_level" : "flightassistant.ground_level"), xAltText - 10, bottom, color);
drawText(textRenderer, context, asText("%d", MathHelper.floor(data.heightAboveGround())), xAltText, bottom, color);
drawBorder(context, xAltText - 2, bottom - 2, 28, color);
if (!data.isCurrentChunkLoaded) {
drawText(textRenderer, context, Text.translatable("flightassistant.altitude_short"), xAltText, bottom, FAConfig.indicator().warningColor);
} else {
drawText(textRenderer, context, Text.translatable(data.groundLevel == data.voidLevel() ? "flightassistant.void_level" : "flightassistant.ground_level"), xAltText - 10, bottom, color);
drawText(textRenderer, context, asText("%d", MathHelper.floor(data.heightAboveGround())), xAltText, bottom, color);
drawBorder(context, xAltText - 2, bottom - 2, 28, color);
}
}

if (FAConfig.indicator().showAltitudeScale) {
Expand Down

0 comments on commit 7ebe94c

Please sign in to comment.