Skip to content

Commit 5475304

Browse files
committed
Minimap
1 parent a03520b commit 5475304

File tree

6 files changed

+806
-252
lines changed

6 files changed

+806
-252
lines changed

src/main/java/dev/xpple/seedmapper/SeedMapper.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import dev.xpple.seedmapper.command.commands.DiscordCommand;
1212
import dev.xpple.seedmapper.command.commands.HighlightCommand;
1313
import dev.xpple.seedmapper.command.commands.LocateCommand;
14+
import dev.xpple.seedmapper.command.commands.MinimapCommand;
1415
import dev.xpple.seedmapper.command.commands.SampleCommand;
1516
import dev.xpple.seedmapper.command.commands.SeedMapCommand;
1617
import dev.xpple.seedmapper.command.commands.SourceCommand;
@@ -20,18 +21,21 @@
2021
import dev.xpple.seedmapper.config.SeedResolutionAdapter;
2122
import dev.xpple.seedmapper.render.RenderManager;
2223
import dev.xpple.seedmapper.seedmap.MapFeature;
24+
import dev.xpple.seedmapper.seedmap.SeedMapMinimapManager;
2325
import dev.xpple.seedmapper.util.SeedDatabaseHelper;
2426
import dev.xpple.simplewaypoints.api.SimpleWaypointsAPI;
2527
import net.fabricmc.api.ClientModInitializer;
2628
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
2729
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
2830
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
31+
import dev.xpple.seedmapper.command.CustomClientCommandSource;
2932
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
3033
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
3134
import net.fabricmc.loader.api.FabricLoader;
3235
import net.fabricmc.loader.api.ModContainer;
3336
import net.minecraft.client.KeyMapping;
3437
import net.minecraft.commands.CommandBuildContext;
38+
import net.fabricmc.loader.api.FabricLoader;
3539

3640
import java.io.IOException;
3741
import java.nio.file.Files;
@@ -59,6 +63,7 @@ public class SeedMapper implements ClientModInitializer {
5963

6064
@Override
6165
public void onInitializeClient() {
66+
6267
new ModConfigBuilder<>(MOD_ID, Configs.class)
6368
.registerType(SeedResolutionArgument.SeedResolution.class, new SeedResolutionAdapter(), SeedResolutionArgument::seedResolution)
6469
.registerTypeHierarchy(MapFeature.class, new MapFeatureAdapter(), MapFeatureArgument::mapFeature)
@@ -77,14 +82,19 @@ public void onInitializeClient() {
7782
SeedDatabaseHelper.fetchSeeds();
7883

7984
KeyMapping keyMapping = KeyBindingHelper.registerKeyBinding(new KeyMapping("key.seedMap", InputConstants.KEY_M, KeyMapping.Category.GAMEPLAY));
85+
KeyMapping minimapKeyMapping = KeyBindingHelper.registerKeyBinding(new KeyMapping("key.seedMapMinimap", InputConstants.KEY_COMMA, KeyMapping.Category.GAMEPLAY));
8086
ClientTickEvents.END_CLIENT_TICK.register(minecraft -> {
8187
while (keyMapping.consumeClick()) {
8288
minecraft.player.connection.sendCommand("sm:seedmap");
8389
}
90+
while (minimapKeyMapping.consumeClick()) {
91+
minecraft.player.connection.sendCommand("sm:minimap");
92+
}
8493
});
8594

8695
ClientCommandRegistrationCallback.EVENT.register(SeedMapper::registerCommands);
8796
RenderManager.registerEvents();
97+
SeedMapMinimapManager.registerHud();
8898
}
8999

90100
private static void registerCommands(CommandDispatcher<FabricClientCommandSource> dispatcher, CommandBuildContext context) {
@@ -96,6 +106,7 @@ private static void registerCommands(CommandDispatcher<FabricClientCommandSource
96106
ClearCommand.register(dispatcher);
97107
StopTaskCommand.register(dispatcher);
98108
SeedMapCommand.register(dispatcher);
109+
MinimapCommand.register(dispatcher);
99110
DiscordCommand.register(dispatcher);
100111
SampleCommand.register(dispatcher);
101112
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package dev.xpple.seedmapper.command.commands;
2+
3+
import com.mojang.brigadier.Command;
4+
import com.mojang.brigadier.CommandDispatcher;
5+
import com.mojang.brigadier.exceptions.CommandSyntaxException;
6+
import dev.xpple.seedmapper.command.CustomClientCommandSource;
7+
import dev.xpple.seedmapper.seedmap.SeedMapMinimapManager;
8+
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
9+
import net.minecraft.core.BlockPos;
10+
11+
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal;
12+
13+
public class MinimapCommand {
14+
15+
public static void register(CommandDispatcher<FabricClientCommandSource> dispatcher) {
16+
dispatcher.register(literal("sm:minimap")
17+
.executes(ctx -> toggle(CustomClientCommandSource.of(ctx.getSource())))
18+
.then(literal("on").executes(ctx -> enable(CustomClientCommandSource.of(ctx.getSource()))))
19+
.then(literal("off").executes(ctx -> disable(CustomClientCommandSource.of(ctx.getSource())))));
20+
}
21+
22+
private static int toggle(CustomClientCommandSource source) throws CommandSyntaxException {
23+
long seed = source.getSeed().getSecond();
24+
int dimension = source.getDimension();
25+
int version = source.getVersion();
26+
BlockPos playerPos = BlockPos.containing(source.getPosition());
27+
source.getClient().schedule(() -> SeedMapMinimapManager.toggle(seed, dimension, version, playerPos));
28+
return Command.SINGLE_SUCCESS;
29+
}
30+
31+
private static int enable(CustomClientCommandSource source) throws CommandSyntaxException {
32+
long seed = source.getSeed().getSecond();
33+
int dimension = source.getDimension();
34+
int version = source.getVersion();
35+
BlockPos playerPos = BlockPos.containing(source.getPosition());
36+
source.getClient().schedule(() -> SeedMapMinimapManager.show(seed, dimension, version, playerPos));
37+
return Command.SINGLE_SUCCESS;
38+
}
39+
40+
private static int disable(CustomClientCommandSource source) {
41+
source.getClient().schedule(SeedMapMinimapManager::hide);
42+
return Command.SINGLE_SUCCESS;
43+
}
44+
}

src/main/java/dev/xpple/seedmapper/config/Configs.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,65 @@ private static void setPixelsPerBiome(int pixelsPerBiome) {
6262
PixelsPerBiome = Math.clamp(pixelsPerBiome, SeedMapScreen.MIN_PIXELS_PER_BIOME, SeedMapScreen.MAX_PIXELS_PER_BIOME);
6363
}
6464

65+
@Config(setter = @Config.Setter("setMinimapOffsetX"))
66+
public static int SeedMapMinimapOffsetX = 4;
67+
68+
private static void setMinimapOffsetX(int offsetX) {
69+
SeedMapMinimapOffsetX = Math.max(0, offsetX);
70+
}
71+
72+
@Config(setter = @Config.Setter("setMinimapOffsetY"))
73+
public static int SeedMapMinimapOffsetY = 4;
74+
75+
private static void setMinimapOffsetY(int offsetY) {
76+
SeedMapMinimapOffsetY = Math.max(0, offsetY);
77+
}
78+
79+
@Config(setter = @Config.Setter("setMinimapWidth"))
80+
public static int SeedMapMinimapWidth = 205;
81+
82+
private static void setMinimapWidth(int width) {
83+
SeedMapMinimapWidth = Math.clamp(width, 64, 512);
84+
}
85+
86+
@Config(setter = @Config.Setter("setMinimapHeight"))
87+
public static int SeedMapMinimapHeight = 205;
88+
89+
private static void setMinimapHeight(int height) {
90+
SeedMapMinimapHeight = Math.clamp(height, 64, 512);
91+
}
92+
93+
@Config
94+
public static boolean SeedMapMinimapRotateWithPlayer = true;
95+
96+
@Config(setter = @Config.Setter("setMinimapPixelsPerBiome"))
97+
public static double SeedMapMinimapPixelsPerBiome = 1.5D;
98+
99+
private static void setMinimapPixelsPerBiome(double pixelsPerBiome) {
100+
SeedMapMinimapPixelsPerBiome = Math.clamp(pixelsPerBiome, SeedMapScreen.MIN_PIXELS_PER_BIOME, SeedMapScreen.MAX_PIXELS_PER_BIOME);
101+
}
102+
103+
@Config(setter = @Config.Setter("setMinimapIconScale"))
104+
public static double SeedMapMinimapIconScale = 0.5D;
105+
106+
private static void setMinimapIconScale(double iconScale) {
107+
SeedMapMinimapIconScale = Math.clamp(iconScale, 0.25D, 4.0D);
108+
}
109+
110+
@Config(setter = @Config.Setter("setMinimapOpacity"))
111+
public static double SeedMapMinimapOpacity = 1.0D;
112+
113+
private static void setMinimapOpacity(double opacity) {
114+
SeedMapMinimapOpacity = Math.clamp(opacity, 0.00D, 1.0D);
115+
}
116+
117+
@Config(comment = "getPlayerDirectionArrowComment")
118+
public static boolean ShowPlayerDirectionArrow = true;
119+
120+
private static Component getPlayerDirectionArrowComment() {
121+
return Component.translatable("config.showPlayerDirectionArrow.comment");
122+
}
123+
65124
@Config(chatRepresentation = "listToggledFeatures")
66125
public static EnumSet<MapFeature> ToggledFeatures = Util.make(() -> {
67126
EnumSet<MapFeature> toggledFeatures = EnumSet.allOf(MapFeature.class);
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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

Comments
 (0)