|
| 1 | +package dev.xpple.seedmapper.seedmap; |
| 2 | + |
| 3 | +import dev.xpple.seedmapper.command.arguments.DimensionArgument; |
| 4 | +import com.mojang.brigadier.StringReader; |
| 5 | +import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback; |
| 6 | +import net.minecraft.client.DeltaTracker; |
| 7 | +import net.minecraft.client.Minecraft; |
| 8 | +import net.minecraft.client.gui.GuiGraphics; |
| 9 | +import net.minecraft.client.player.LocalPlayer; |
| 10 | +import net.minecraft.core.BlockPos; |
| 11 | +import org.jetbrains.annotations.Nullable; |
| 12 | + |
| 13 | +public final class SeedMapMinimapManager { |
| 14 | + private static final SeedMapMinimapManager INSTANCE = new SeedMapMinimapManager(); |
| 15 | + |
| 16 | + private @Nullable SeedMapMinimapScreen minimapScreen; |
| 17 | + private long activeSeed; |
| 18 | + private int activeVersion; |
| 19 | + // no world preset context for minimap; but keep basic context so we can re-open on dimension change |
| 20 | + private boolean hasContext; |
| 21 | + |
| 22 | + private SeedMapMinimapManager() { |
| 23 | + } |
| 24 | + |
| 25 | + public static void registerHud() { |
| 26 | + HudRenderCallback.EVENT.register((guiGraphics, deltaTracker) -> INSTANCE.render(guiGraphics, deltaTracker)); |
| 27 | + } |
| 28 | + |
| 29 | + public static boolean isVisible() { |
| 30 | + return INSTANCE.minimapScreen != null; |
| 31 | + } |
| 32 | + |
| 33 | + public static void show(long seed, int dimension, int version, BlockPos pos) { |
| 34 | + INSTANCE.enable(seed, dimension, version, pos); |
| 35 | + } |
| 36 | + |
| 37 | + public static void hide() { |
| 38 | + INSTANCE.disable(); |
| 39 | + } |
| 40 | + |
| 41 | + public static void toggle(long seed, int dimension, int version, BlockPos pos) { |
| 42 | + if (INSTANCE.minimapScreen != null) { |
| 43 | + INSTANCE.disable(); |
| 44 | + return; |
| 45 | + } |
| 46 | + INSTANCE.enable(seed, dimension, version, pos); |
| 47 | + } |
| 48 | + |
| 49 | + public static void disableMinimap() { |
| 50 | + INSTANCE.disable(); |
| 51 | + } |
| 52 | + |
| 53 | + private void enable(long seed, int dimension, int version, BlockPos pos) { |
| 54 | + this.disable(); |
| 55 | + this.activeSeed = seed; |
| 56 | + this.activeVersion = version; |
| 57 | + this.hasContext = true; |
| 58 | + this.minimapScreen = new SeedMapMinimapScreen(seed, dimension, version, pos); |
| 59 | + } |
| 60 | + |
| 61 | + private void disable() { |
| 62 | + if (this.minimapScreen != null) { |
| 63 | + if (this.minimapScreen.isInitialized()) { |
| 64 | + this.minimapScreen.onClose(); |
| 65 | + } |
| 66 | + this.minimapScreen = null; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private void render(GuiGraphics guiGraphics, DeltaTracker deltaTracker) { |
| 71 | + if (this.minimapScreen == null) { |
| 72 | + return; |
| 73 | + } |
| 74 | + Minecraft minecraft = Minecraft.getInstance(); |
| 75 | + LocalPlayer player = minecraft.player; |
| 76 | + if (player == null || minecraft.level == null) { |
| 77 | + this.disable(); |
| 78 | + return; |
| 79 | + } |
| 80 | + BlockPos playerPos = player.blockPosition(); |
| 81 | + try { |
| 82 | + int currentDimensionId = DimensionArgument.dimension().parse(new StringReader(minecraft.level.dimension().identifier().getPath())); |
| 83 | + if (currentDimensionId != this.minimapScreen.getDimensionId()) { |
| 84 | + // dimension changed: re-create minimap for new dimension if we have context |
| 85 | + if (!this.hasContext) { |
| 86 | + this.disable(); |
| 87 | + return; |
| 88 | + } |
| 89 | + this.enable(this.activeSeed, currentDimensionId, this.activeVersion, playerPos); |
| 90 | + } |
| 91 | + } catch (com.mojang.brigadier.exceptions.CommandSyntaxException e) { |
| 92 | + this.disable(); |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + this.minimapScreen.focusOn(playerPos); |
| 97 | + |
| 98 | + float partialTick = deltaTracker.getGameTimeDeltaPartialTick(false); |
| 99 | + this.minimapScreen.renderToHud(guiGraphics, player, partialTick); |
| 100 | + } |
| 101 | +} |
0 commit comments