diff --git a/src/main/java/com/connorcode/sigmautils/modules/rendering/LightLevel.java b/src/main/java/com/connorcode/sigmautils/modules/rendering/LightLevel.java index 0ba587f..2360c79 100644 --- a/src/main/java/com/connorcode/sigmautils/modules/rendering/LightLevel.java +++ b/src/main/java/com/connorcode/sigmautils/modules/rendering/LightLevel.java @@ -10,12 +10,14 @@ import net.minecraft.block.Blocks; import net.minecraft.client.font.TextRenderer; import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.nbt.NbtCompound; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.RotationAxis; import net.minecraft.util.math.Vec3d; import net.minecraft.world.LightType; +import java.util.HashSet; import java.util.Optional; import static com.connorcode.sigmautils.SigmaUtils.client; @@ -23,47 +25,69 @@ @ModuleInfo(description = "Shows the light level the top of blocks") public class LightLevel extends Module { - EnumSetting source = new EnumSetting(LightLevel.class, "Light Source", LightSource.class).value(LightSource.All).build(); - EnumSetting coloring = new EnumSetting(LightLevel.class, "Coloring", Coloring.class).value(Coloring.Hue).build(); - NumberSetting scale = new NumberSetting(LightLevel.class, "Scale", 0.5, 5).value(2).build(); + EnumSetting source = new EnumSetting<>(LightLevel.class, "Light Source", LightSource.class).value(LightSource.All).build(); + EnumSetting coloring = new EnumSetting<>(LightLevel.class, "Coloring", Coloring.class).value(Coloring.Hue).build(); + NumberSetting scale = new NumberSetting(LightLevel.class, "Scale", 0.5, 3).value(2).build(); NumberSetting rangeH = new NumberSetting(LightLevel.class, "Horizontal Range", 5, 25).value(5).precision(0).build(); NumberSetting rangeV = new NumberSetting(LightLevel.class, "Vertical Range", 0, 10).value(5).precision(0).build(); + BlockPos centerPos; + HashSet positions = new HashSet<>(); + @EventHandler void onRender(WorldRender.PostWorldRenderEvent event) { if (!enabled || client.player == null || client.world == null) return; var profiler = client.getProfiler(); profiler.push("SigmaUtils::LightLevel::Render"); - - var blockPos = client.player.getBlockPos(); - var pos = blockPos.toCenterPos().subtract(new Vec3d(0, .5 - .01, 0)); + updatePositions(); var immediate = client.getBufferBuilders().getEntityVertexConsumers(); RenderSystem.enableBlend(); RenderSystem.defaultBlendFunc(); + for (var pos : positions) { + var light = lightLevel(pos); + var color = getColor(light); + if (color.isEmpty()) continue; + + var width = client.textRenderer.getWidth(Integer.toString(light)); + var text = Integer.toString(light); + var matrix = getFloorMatrices(pos.toCenterPos().subtract(0, .5 - 0.01, 0)); + client.textRenderer.draw(text, + width / -2f, + client.textRenderer.fontHeight / -2f, + color.get(), + false, + matrix.peek().getPositionMatrix(), + immediate, + TextRenderer.TextLayerType.NORMAL, + 0, + 255, + false); + } + + immediate.draw(); + RenderSystem.disableBlend(); + profiler.pop(); + } + + void updatePositions() { + assert client.player != null; + var currentPos = client.player.getBlockPos(); + if (centerPos.equals(currentPos)) return; + centerPos = currentPos; + positions.clear(); + for (var x = -rangeH.intValue(); x <= rangeH.intValue(); x++) { for (var z = -rangeH.intValue(); z <= rangeH.intValue(); z++) { for (var y = -rangeV.intValue(); y <= rangeV.intValue(); y++) { - var thisPos = blockPos.add(x, y, z); + var thisPos = currentPos.add(x, y, z); if (isTransparent(thisPos.down()) || !isTransparent(thisPos)) continue; - - var light = lightLevel(thisPos); - var color = getColor(light); - if (color.isEmpty()) continue; - - var width = client.textRenderer.getWidth(Integer.toString(light)); - var text = Integer.toString(light); - var matrix = getFloorMatrices(pos.add(x, y, z)); - client.textRenderer.draw(text, width / -2f, 0f, color.get(), false, matrix.peek().getPositionMatrix(), immediate, TextRenderer.TextLayerType.NORMAL, 0, 255, false); + positions.add(thisPos); } } } - - immediate.draw(); - RenderSystem.disableBlend(); - profiler.pop(); } boolean isTransparent(BlockPos pos) { @@ -74,9 +98,10 @@ boolean isTransparent(BlockPos pos) { MatrixStack getFloorMatrices(Vec3d pos) { var _scale = (float) scale.value(); - var matrix = getMatrices(pos); // TODO: Remove this + var camYaw = client.gameRenderer.getCamera().getYaw() + 45; + var matrix = getMatrices(pos); matrix.multiply(RotationAxis.POSITIVE_X.rotationDegrees(90)); - matrix.multiply(RotationAxis.POSITIVE_Z.rotationDegrees((int) (client.gameRenderer.getCamera().getYaw() / 90) * 90)); + matrix.multiply(RotationAxis.POSITIVE_Z.rotationDegrees(Math.round(camYaw / 90f) * 90)); matrix.scale(-0.025f * _scale, -0.025f * _scale, 1); return matrix; } @@ -95,10 +120,16 @@ Optional getColor(int light) { var lightf = MathHelper.clamp(light / 15f, 0, 1); return switch (coloring.value()) { case Hue -> Optional.of(MathHelper.hsvToRgb(lightf, 1, 1)); - case HostileSpawn -> Optional.ofNullable(light == 1 ? 0xff0000 : null); + case HostileSpawn -> Optional.ofNullable(light <= 1 ? 0xff0000 : null); }; } + @Override + public NbtCompound saveConfig() { + centerPos = null; + return super.saveConfig(); + } + enum Coloring { Hue, HostileSpawn }