diff --git a/src/main/java/ru/octol1ttle/flightassistant/computers/AirDataComputer.java b/src/main/java/ru/octol1ttle/flightassistant/computers/AirDataComputer.java index b805ecad..33ab34a0 100644 --- a/src/main/java/ru/octol1ttle/flightassistant/computers/AirDataComputer.java +++ b/src/main/java/ru/octol1ttle/flightassistant/computers/AirDataComputer.java @@ -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; @@ -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()); @@ -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(); } @@ -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(); @@ -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; @@ -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); } @@ -202,5 +216,6 @@ public void reset() { roll = 0.0f; groundLevel = 0; elytraHealth = null; + isCurrentChunkLoaded = true; } } diff --git a/src/main/java/ru/octol1ttle/flightassistant/indicators/AltitudeIndicator.java b/src/main/java/ru/octol1ttle/flightassistant/indicators/AltitudeIndicator.java index 4e5d298e..205f4b2d 100644 --- a/src/main/java/ru/octol1ttle/flightassistant/indicators/AltitudeIndicator.java +++ b/src/main/java/ru/octol1ttle/flightassistant/indicators/AltitudeIndicator.java @@ -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) {