|
| 1 | +package dev.xpple.seedmapper.seedmap; |
| 2 | + |
| 3 | +import dev.xpple.seedmapper.config.Configs; |
| 4 | +import dev.xpple.simplewaypoints.api.SimpleWaypointsAPI; |
| 5 | +import dev.xpple.simplewaypoints.api.Waypoint; |
| 6 | +import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback; |
| 7 | +import net.minecraft.ChatFormatting; |
| 8 | +import net.minecraft.client.Camera; |
| 9 | +import net.minecraft.client.DeltaTracker; |
| 10 | +import net.minecraft.client.Minecraft; |
| 11 | +import net.minecraft.client.gui.GuiGraphics; |
| 12 | +import net.minecraft.client.renderer.GameRenderer; |
| 13 | +import net.minecraft.network.chat.Component; |
| 14 | +import net.minecraft.network.chat.ComponentUtils; |
| 15 | +import net.minecraft.world.entity.Entity; |
| 16 | +import net.minecraft.world.phys.Vec3; |
| 17 | +import org.joml.Vector2d; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Comparator; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +public final class ManualWaypointCompassOverlay { |
| 26 | + private ManualWaypointCompassOverlay() { |
| 27 | + } |
| 28 | + |
| 29 | + public static void registerHud() { |
| 30 | + HudRenderCallback.EVENT.register(ManualWaypointCompassOverlay::render); |
| 31 | + } |
| 32 | + |
| 33 | + private static void render(GuiGraphics guiGraphics, DeltaTracker deltaTracker) { |
| 34 | + Configs.applyWaypointCompassOverlaySetting(); |
| 35 | + if (!Configs.ManualWaypointCompassOverlay) { |
| 36 | + return; |
| 37 | + } |
| 38 | + Minecraft minecraft = Minecraft.getInstance(); |
| 39 | + if (minecraft.level == null) { |
| 40 | + return; |
| 41 | + } |
| 42 | + SimpleWaypointsAPI api = SimpleWaypointsAPI.getInstance(); |
| 43 | + String worldIdentifier = api.getWorldIdentifier(minecraft); |
| 44 | + if (worldIdentifier == null || worldIdentifier.isBlank()) { |
| 45 | + return; |
| 46 | + } |
| 47 | + Set<String> enabled = Configs.getWaypointCompassEnabled(worldIdentifier); |
| 48 | + if (enabled.isEmpty()) { |
| 49 | + return; |
| 50 | + } |
| 51 | + Map<String, Waypoint> worldWaypoints = api.getWorldWaypoints(worldIdentifier); |
| 52 | + if (worldWaypoints == null || worldWaypoints.isEmpty()) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + GameRenderer gameRenderer = minecraft.gameRenderer; |
| 57 | + Camera camera = gameRenderer.getMainCamera(); |
| 58 | + Entity cameraEntity = camera.entity(); |
| 59 | + float partialTicks = deltaTracker.getGameTimeDeltaPartialTick(true); |
| 60 | + double verticalFovRad = Math.toRadians(readFov(gameRenderer, camera, partialTicks)); |
| 61 | + double aspectRatio = (double) minecraft.getWindow().getGuiScaledWidth() / minecraft.getWindow().getGuiScaledHeight(); |
| 62 | + double horizontalFovRad = 2 * Math.atan(Math.tan(verticalFovRad / 2) * aspectRatio); |
| 63 | + |
| 64 | + Vec3 viewVector3 = cameraEntity.getViewVector(1.0f); |
| 65 | + Vector2d viewVector = new Vector2d(viewVector3.x, viewVector3.z); |
| 66 | + Vector2d position = new Vector2d(cameraEntity.getEyePosition().x, cameraEntity.getEyePosition().z); |
| 67 | + |
| 68 | + List<WaypointMarkerLocation> xPositions = new ArrayList<>(); |
| 69 | + worldWaypoints.forEach((waypointName, waypoint) -> { |
| 70 | + if (!enabled.contains(waypointName)) { |
| 71 | + return; |
| 72 | + } |
| 73 | + if (!waypoint.dimension().identifier().equals(minecraft.level.dimension().identifier())) { |
| 74 | + return; |
| 75 | + } |
| 76 | + if (!waypoint.visible()) { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + double distanceSquared = waypoint.location().distToCenterSqr(cameraEntity.position()); |
| 81 | + long distance = Math.round(Math.sqrt(distanceSquared)); |
| 82 | + Component marker = ComponentUtils.wrapInSquareBrackets(Component.literal(waypointName + ' ' + distance).withStyle(ChatFormatting.YELLOW)); |
| 83 | + Vec3 waypointCenter = waypoint.location().getCenter(); |
| 84 | + |
| 85 | + Vector2d waypointLocation = new Vector2d(waypointCenter.x, waypointCenter.z); |
| 86 | + double angleRad = viewVector.angle(waypointLocation.sub(position, new Vector2d())); |
| 87 | + boolean right = angleRad > 0; |
| 88 | + angleRad = Math.abs(angleRad); |
| 89 | + |
| 90 | + int x; |
| 91 | + if (angleRad > horizontalFovRad / 2) { |
| 92 | + int width = minecraft.font.width(marker); |
| 93 | + x = right ? guiGraphics.guiWidth() - width / 2 : width / 2; |
| 94 | + } else { |
| 95 | + double mv = Math.tan(angleRad) * GameRenderer.PROJECTION_Z_NEAR; |
| 96 | + double av = Math.tan(horizontalFovRad / 2) * GameRenderer.PROJECTION_Z_NEAR; |
| 97 | + double ab = 2 * av; |
| 98 | + double am = right ? mv + av : ab - (mv + av); |
| 99 | + double perc = am / ab; |
| 100 | + int guiWidth = guiGraphics.guiWidth(); |
| 101 | + int halfWidth = minecraft.font.width(marker) / 2; |
| 102 | + x = Math.clamp((int) (perc * guiWidth), halfWidth, guiWidth - halfWidth); |
| 103 | + } |
| 104 | + xPositions.add(new WaypointMarkerLocation(marker, x)); |
| 105 | + }); |
| 106 | + |
| 107 | + xPositions.sort(Comparator.comparingInt(WaypointMarkerLocation::location)); |
| 108 | + |
| 109 | + List<List<WaypointMarkerLocation>> positions = new ArrayList<>(); |
| 110 | + positions.add(xPositions); |
| 111 | + |
| 112 | + for (int line = 0; line < positions.size(); line++) { |
| 113 | + List<WaypointMarkerLocation> waypointMarkerLocations = positions.get(line); |
| 114 | + int i = 0; |
| 115 | + while (i < waypointMarkerLocations.size() - 1) { |
| 116 | + WaypointMarkerLocation left = waypointMarkerLocations.get(i); |
| 117 | + WaypointMarkerLocation right = waypointMarkerLocations.get(i + 1); |
| 118 | + int leftX = left.location(); |
| 119 | + int rightX = right.location(); |
| 120 | + int leftWidth = minecraft.font.width(left.marker()); |
| 121 | + int rightWidth = minecraft.font.width(right.marker()); |
| 122 | + if (leftWidth / 2 + rightWidth / 2 > rightX - leftX) { |
| 123 | + if (line + 1 == positions.size()) { |
| 124 | + positions.add(new ArrayList<>()); |
| 125 | + } |
| 126 | + List<WaypointMarkerLocation> nextLevel = positions.get(line + 1); |
| 127 | + WaypointMarkerLocation removed = waypointMarkerLocations.remove(i + 1); |
| 128 | + nextLevel.add(removed); |
| 129 | + } else { |
| 130 | + i++; |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + for (int line = 0; line < positions.size(); line++) { |
| 136 | + List<WaypointMarkerLocation> w = positions.get(line); |
| 137 | + for (WaypointMarkerLocation waypoint : w) { |
| 138 | + guiGraphics.drawCenteredString(minecraft.font, waypoint.marker(), waypoint.location(), 1 + line * minecraft.font.lineHeight, 0xFFFFFFFF); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private record WaypointMarkerLocation(Component marker, int location) { |
| 144 | + } |
| 145 | + |
| 146 | + private static double readFov(GameRenderer gameRenderer, Camera camera, float partialTicks) { |
| 147 | + try { |
| 148 | + java.lang.reflect.Method method = GameRenderer.class.getDeclaredMethod("getFov", Camera.class, float.class, boolean.class); |
| 149 | + method.setAccessible(true); |
| 150 | + Object value = method.invoke(gameRenderer, camera, partialTicks, true); |
| 151 | + if (value instanceof Number number) { |
| 152 | + return number.doubleValue(); |
| 153 | + } |
| 154 | + } catch (Throwable ignored) { |
| 155 | + } |
| 156 | + Minecraft minecraft = Minecraft.getInstance(); |
| 157 | + return minecraft.options.fov().get(); |
| 158 | + } |
| 159 | +} |
0 commit comments