From a97406c2be7a7c6de2e45b5b952ac818db7b5081 Mon Sep 17 00:00:00 2001 From: AmyMialee Date: Mon, 5 Oct 2026 19:45:53 +0100 Subject: [PATCH 1/2] Project Restructure --- .gitattributes | 1 - .gitignore | 1 + LICENSE => LICENSE.md | 0 build.gradle | 63 +---- gradle.properties | 10 +- gradle/wrapper/gradle-wrapper.properties | 4 +- .../bettercompass/BetterCompassClient.java | 251 ------------------ .../madu59/bettercompass/BetterCompass.java | 251 +++++++++++++++++- .../bettercompass/config/ClientCommands.java | 0 .../madu59/bettercompass/config/Option.java | 0 .../bettercompass/config/SettingsManager.java | 0 .../BetterCompassConfigScreen.java | 0 .../config/configScreen/ModMenuApiImpl.java | 0 .../configScreen/MyConfigListWidget.java | 0 .../mixin/client/FovMultiplierAccessor.java | 0 .../better-compass.client.mixins.json | 0 src/main/resources/fabric.mod.json | 26 +- 17 files changed, 264 insertions(+), 343 deletions(-) rename LICENSE => LICENSE.md (100%) delete mode 100644 src/client/java/fr/madu59/bettercompass/BetterCompassClient.java rename src/{client => main}/java/fr/madu59/bettercompass/config/ClientCommands.java (100%) rename src/{client => main}/java/fr/madu59/bettercompass/config/Option.java (100%) rename src/{client => main}/java/fr/madu59/bettercompass/config/SettingsManager.java (100%) rename src/{client => main}/java/fr/madu59/bettercompass/config/configScreen/BetterCompassConfigScreen.java (100%) rename src/{client => main}/java/fr/madu59/bettercompass/config/configScreen/ModMenuApiImpl.java (100%) rename src/{client => main}/java/fr/madu59/bettercompass/config/configScreen/MyConfigListWidget.java (100%) rename src/{client => main}/java/fr/madu59/bettercompass/mixin/client/FovMultiplierAccessor.java (100%) rename src/{client => main}/resources/better-compass.client.mixins.json (100%) diff --git a/.gitattributes b/.gitattributes index 097f9f9..afd59d8 100644 --- a/.gitattributes +++ b/.gitattributes @@ -6,4 +6,3 @@ # These are Windows script files and should use crlf *.bat text eol=crlf - diff --git a/.gitignore b/.gitignore index c476faf..8feea40 100644 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,4 @@ hs_err_*.log replay_*.log *.hprof *.jfr +/remappedSrc/ diff --git a/LICENSE b/LICENSE.md similarity index 100% rename from LICENSE rename to LICENSE.md diff --git a/build.gradle b/build.gradle index 62600cf..2b7205b 100644 --- a/build.gradle +++ b/build.gradle @@ -1,90 +1,39 @@ plugins { id 'fabric-loom' version "${loom_version}" - id 'maven-publish' } version = project.mod_version group = project.maven_group - -base { - archivesName = project.archives_base_name -} +base { archivesName = project.mod_id } repositories { - maven { - name = "Terraformers" - url = "https://maven.terraformersmc.com/" - } -} - -loom { - splitEnvironmentSourceSets() - - mods { - "better-compass" { - sourceSet sourceSets.main - sourceSet sourceSets.client - } - } - + maven { url "https://maven.terraformersmc.com/releases/" } } dependencies { - // To change the versions see the gradle.properties file minecraft "com.mojang:minecraft:${project.minecraft_version}" mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" - // Fabric API. This is technically optional, but you probably want it anyway. modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" modCompileOnly("com.terraformersmc:modmenu:${project.modmenu_version}") } processResources { inputs.property "version", project.version - - filesMatching("fabric.mod.json") { - expand "version": inputs.properties.version - } + filteringCharset "UTF-8" + filesMatching("fabric.mod.json") { expand "version": project.version } } -tasks.withType(JavaCompile).configureEach { - it.options.release = 21 -} +tasks.withType(JavaCompile).configureEach { it.options.release = 21 } java { - // Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task - // if it is present. - // If you remove this line, sources will not be generated. withSourcesJar() - sourceCompatibility = JavaVersion.VERSION_21 targetCompatibility = JavaVersion.VERSION_21 } - jar { inputs.property "archivesName", project.base.archivesName - - from("LICENSE") { - rename { "${it}_${inputs.properties.archivesName}"} - } -} - -// configure the maven publication -publishing { - publications { - create("mavenJava", MavenPublication) { - artifactId = project.archives_base_name - from components.java - } - } - - // See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing. - repositories { - // Add repositories to publish to here. - // Notice: This block does NOT have the same function as the block in the top level. - // The repositories here will be used for publishing your artifact, not for - // retrieving dependencies. - } + from("LICENSE.md") { rename { "${it}_${inputs.properties.archivesName}"} } } \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index ae42937..d305f11 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,9 +1,9 @@ -# Done to increase the memory available to gradle. -org.gradle.jvmargs=-Xmx1G +# Done to increase the memory available to Gradle. +org.gradle.jvmargs=-Xmx2G org.gradle.parallel=true +org.gradle.configuration-cache=false -# Fabric Properties -# check these on https://fabricmc.net/develop +# Fabric Properties on https://modmuss50.me/fabric.html minecraft_version=1.21.11 yarn_mappings=1.21.11+build.1 loader_version=0.18.1 @@ -12,7 +12,7 @@ loom_version=1.13-SNAPSHOT # Mod Properties mod_version=1.0.8 maven_group=fr.madu59 -archives_base_name=better-compass +mod_id=better-compass # Dependencies fabric_version=0.139.4+1.21.11 diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index ff23a68..b14ad76 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,7 +1,7 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.2-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-9.8.0-bin.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME -zipStorePath=wrapper/dists +zipStorePath=wrapper/dists \ No newline at end of file diff --git a/src/client/java/fr/madu59/bettercompass/BetterCompassClient.java b/src/client/java/fr/madu59/bettercompass/BetterCompassClient.java deleted file mode 100644 index 4b59cfd..0000000 --- a/src/client/java/fr/madu59/bettercompass/BetterCompassClient.java +++ /dev/null @@ -1,251 +0,0 @@ -package fr.madu59.bettercompass; - - -import java.lang.reflect.Type; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Set; -import java.lang.Math; -import java.io.*; -import java.nio.file.*; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.reflect.TypeToken; -import fr.madu59.bettercompass.config.ClientCommands; -import fr.madu59.bettercompass.config.SettingsManager; -import fr.madu59.bettercompass.mixin.client.FovMultiplierAccessor; - -import net.fabricmc.api.ClientModInitializer; -import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; -import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents; -import net.fabricmc.fabric.api.client.rendering.v1.hud.HudElementRegistry; -import net.fabricmc.fabric.api.client.rendering.v1.hud.VanillaHudElements; -import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.util.Identifier; -import net.minecraft.util.WorldSavePath; -import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.MathHelper; -import net.minecraft.util.math.Vec3d; -import net.minecraft.world.World; -import net.minecraft.client.render.Camera; -import net.minecraft.client.render.RenderTickCounter; -import net.minecraft.text.Text; -import net.minecraft.client.gui.DrawContext; -import net.minecraft.client.network.ServerInfo; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.font.TextRenderer; -import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.item.Items; -import net.minecraft.registry.RegistryKey; -import net.minecraft.registry.RegistryKeys; - - -public class BetterCompassClient implements ClientModInitializer { - - private static final MinecraftClient CLIENT = MinecraftClient.getInstance(); - private static final float GUI_WIDTH = 0.5F; - - private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); - private static Map valueMap = new LinkedHashMap<>(); - - - private static RegistryKey lastDimension = null; - - public static BlockPos deathPointBlockPos = null; - public static RegistryKey deathDimension = null; - public static BlockPos netherPortalBlockPos = null; - public static String serverId; - - @Override - public void onInitializeClient() { - ClientCommands.register(); - // Attach our rendering code to before the chat hud layer. Our layer will render right before the chat. The API will take care of z spacing. - HudElementRegistry.attachElementBefore(VanillaHudElements.CHAT, Identifier.of(BetterCompass.MOD_ID, "before_chat"), BetterCompassClient::render); - - ClientTickEvents.END_CLIENT_TICK.register(client -> { - if (client.player != null && client.player.isDead()) { - deathPointBlockPos = client.player.getBlockPos(); - deathDimension = client.world.getRegistryKey(); - valueMap.put("deathPointBlockPos", blockPosToString(deathPointBlockPos)); - valueMap.put("deathDimension", registryKeyToString(deathDimension)); - saveValues(); - } - if (client.world != null) { - RegistryKey current = client.world.getRegistryKey(); - if (lastDimension != null && !lastDimension.equals(current)) { - if (current == World.NETHER) { - netherPortalBlockPos = client.player.getBlockPos(); - valueMap.put("netherPortalBlockPos", blockPosToString(netherPortalBlockPos)); - saveValues(); - } - else{ - netherPortalBlockPos = null; - } - } - lastDimension = current; - } - }); - ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> { - // This runs when the client enters a world - - if (CLIENT.getServer() == null) { - // Multiplayer - ServerInfo info = CLIENT.getCurrentServerEntry(); - serverId = info != null ? info.address.replace(":", "_") : "unknown_server"; - } else { - // Singleplayer - serverId = CLIENT.getServer().getSavePath(WorldSavePath.ROOT) - .getParent().getFileName().toString(); - } - loadData(); - }); - } - - private static void render(DrawContext context, RenderTickCounter tickCounter) { - float compassPosition = 0.05F; - if(SettingsManager.COMPASS_POSITION.getValueAsString().equals("Bottom")){compassPosition = 1-0.18F;} - float deltaY = 0F; - float size = 1.5F; - int color = 0xFFFFFFFF; - PlayerEntity player = CLIENT.player; - int optionValueIndex = SettingsManager.SHOW_COMPASS_HUD.getValueAsIndex(); - if(optionValueIndex == 3 - || (optionValueIndex == 2 && !player.getMainHandStack().getItem().getTranslationKey().equals("item.minecraft.compass")) - || (optionValueIndex == 1 && !player.getInventory().containsAny(Set.of(Items.COMPASS))) - ){ - return; - } - TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer; - Camera camera = CLIENT.gameRenderer.getCamera(); - if (camera == null) return; - - float fov = ((FovMultiplierAccessor)(Object)CLIENT.gameRenderer).getFovMultiplier() * CLIENT.options.getFov().getValue(); - float camDirection = camera.getYaw(); - - if(!SettingsManager.CARDINALS_DIRECTION_POSITION.getValue().equals("Disabled")){ - color = SettingsManager.getRGBColorFromSetting(SettingsManager.CARDINALS_DIRECTION_COLOR.getValueAsString()); - if(SettingsManager.CARDINALS_DIRECTION_POSITION.getValue().equals("Under")){deltaY = 0.03F; size = 0.8F;} - if(SettingsManager.CARDINALS_DIRECTION_POSITION.getValue().equals("Above")){deltaY = -0.03F; size = 0.8F;} - drawCompassSymbol(context, textRenderer, fov, "N", 180, camDirection, compassPosition + deltaY, color, size); - drawCompassSymbol(context, textRenderer, fov, "E", 270, camDirection, compassPosition + deltaY, color, size); - drawCompassSymbol(context, textRenderer, fov, "S", 0, camDirection, compassPosition + deltaY, color, size); - drawCompassSymbol(context, textRenderer, fov, "W", 90, camDirection, compassPosition + deltaY, color, size); - } - - for (int i = 0; i < 36; i++){ - if(i % 9 != 0 || !SettingsManager.CARDINALS_DIRECTION_POSITION.getValue().equals("Aligned")){ - drawCompassSymbol(context, textRenderer, fov, "|", i * 10, camDirection, compassPosition, 0xFFFFFFFF, 1); - } - } - - //Add death position to the compass HUD - if(deathPointBlockPos != null && CLIENT.world.getRegistryKey() == deathDimension && !SettingsManager.LAST_DEATH_DIRECTION_POSITION.getValue().equals("Disabled")){ - deltaY = 0; - size = 1.5F; - color = SettingsManager.getRGBColorFromSetting(SettingsManager.LAST_DEATH_DIRECTION_COLOR.getValueAsString()); - if(SettingsManager.LAST_DEATH_DIRECTION_POSITION.getValue().equals("Under")){deltaY = 0.03F; size = 0.8F;} - if(SettingsManager.LAST_DEATH_DIRECTION_POSITION.getValue().equals("Above")){deltaY = -0.03F; size = 0.8F;} - Vec3d deathPos = new Vec3d(deathPointBlockPos.getX(), 0, deathPointBlockPos.getZ()); - double dx = deathPos.x - player.getX(); - double dz = deathPos.z - player.getZ(); - drawCompassSymbol(context, textRenderer, fov, "๐Ÿ’€", (float)(MathHelper.atan2(dz, dx) * (180 / Math.PI)) - 90, camDirection, compassPosition + deltaY, color, size); - } - - //Add nether portal position to the compass HUD - if(netherPortalBlockPos != null && CLIENT.world.getRegistryKey() == World.NETHER && !SettingsManager.NETHER_PORTAL_DIRECTION_POSITION.getValue().equals("Disabled")){ - deltaY = 0; - size = 1.5F; - color = SettingsManager.getRGBColorFromSetting(SettingsManager.NETHER_PORTAL_DIRECTION_COLOR.getValueAsString()); - if(SettingsManager.NETHER_PORTAL_DIRECTION_POSITION.getValue().equals("Under")){deltaY = 0.03F; size = 0.8F;} - if(SettingsManager.NETHER_PORTAL_DIRECTION_POSITION.getValue().equals("Above")){deltaY = -0.03F; size = 0.8F;} - Vec3d netherPortalPos = new Vec3d(netherPortalBlockPos.getX(), 0, netherPortalBlockPos.getZ()); - double dx = netherPortalPos.x - player.getX(); - double dz = netherPortalPos.z - player.getZ(); - drawCompassSymbol(context, textRenderer, fov, "๐ŸŒ", (float)(MathHelper.atan2(dz, dx) * (180 / Math.PI)) - 90, camDirection, compassPosition + deltaY, color, size); - } - } - - public static void drawCompassSymbol(DrawContext context, TextRenderer textRenderer, float fov, String symbol, float targetDirection, float camDirection, float y, int color, float scale){ - int screenWidth = context.getScaledWindowWidth(); - int screenHeight = context.getScaledWindowHeight(); - - //Determine the position of the symbol depending on the targetDirection and the camDirection - int x = - (int) (screenWidth * 0.5 * GUI_WIDTH * MathHelper.wrapDegrees(camDirection-targetDirection)/fov); - if(Math.abs(MathHelper.wrapDegrees(camDirection-targetDirection)/fov)>1.0){ - return; - } - - //Adjust symbol transparency - color = color % 16777216; - int alpha = (int) (255 * (1-Math.abs(Math.sin(Math.toRadians(camDirection - targetDirection))))); - color += 16777216 * alpha; - - //Scale the GUI - var matrices = context.getMatrices(); - var before = new org.joml.Matrix3x2f(matrices); - matrices.scale(scale, scale); - - if(SettingsManager.COMPASS_STYLE.getValueAsString().equals("Shadows")){ - context.drawCenteredTextWithShadow(textRenderer, Text.literal(symbol),(int) ((screenWidth/2 + x)/scale),(int) ((screenHeight*y)/scale - textRenderer.fontHeight/2f),color); - } - else{ - context.drawText(textRenderer, symbol,(int) ((screenWidth/2 + x)/scale - textRenderer.getWidth(symbol)/2f),(int) ((screenHeight*y)/scale - textRenderer.fontHeight/2f),color, false); - } - - matrices.set(before); - } - - - - - public static void saveValues() { - Path configPath = FabricLoader.getInstance().getConfigDir().resolve(serverId).resolve(BetterCompass.MOD_ID + ".json"); - try { - Files.createDirectories(configPath.getParent()); - try (Writer writer = Files.newBufferedWriter(configPath)) { - GSON.toJson(valueMap, writer); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - - private static void loadData() { - Path configPath = FabricLoader.getInstance().getConfigDir().resolve(serverId).resolve(BetterCompass.MOD_ID + ".json"); - try (Reader reader = Files.newBufferedReader(configPath)) { - Type type = new TypeToken>() {}.getType(); - valueMap = GSON.fromJson(reader, type); - } catch (IOException e) { - e.printStackTrace(); - return; - } - - if(valueMap.containsKey("deathPointBlockPos")){ - deathPointBlockPos = stringToBlockPos(valueMap.get("deathPointBlockPos")); - } - if(valueMap.containsKey("deathDimension")){ - deathDimension = RegistryKey.of(RegistryKeys.WORLD, Identifier.tryParse(valueMap.get("deathDimension"))); - } - if(valueMap.containsKey("netherPortalBlockPos")){ - netherPortalBlockPos = stringToBlockPos(valueMap.get("netherPortalBlockPos")); - } - } - - private static String blockPosToString(BlockPos pos){ - return pos.getX() + "," + pos.getY() + "," + pos.getZ(); - } - - private static String registryKeyToString(RegistryKey key){ - return key.getValue().toString(); - } - - private static BlockPos stringToBlockPos(String str){ - String[] coords = str.split(","); - return new BlockPos( - Integer.parseInt(coords[0]), - Integer.parseInt(coords[1]), - Integer.parseInt(coords[2]) - ); - } -} \ No newline at end of file diff --git a/src/main/java/fr/madu59/bettercompass/BetterCompass.java b/src/main/java/fr/madu59/bettercompass/BetterCompass.java index 0de4d8e..0c4d1a0 100644 --- a/src/main/java/fr/madu59/bettercompass/BetterCompass.java +++ b/src/main/java/fr/madu59/bettercompass/BetterCompass.java @@ -1,24 +1,253 @@ package fr.madu59.bettercompass; -import net.fabricmc.api.ModInitializer; +import java.lang.reflect.Type; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; +import java.lang.Math; +import java.io.*; +import java.nio.file.*; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; +import fr.madu59.bettercompass.config.ClientCommands; +import fr.madu59.bettercompass.config.SettingsManager; +import fr.madu59.bettercompass.mixin.client.FovMultiplierAccessor; + +import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents; +import net.fabricmc.fabric.api.client.rendering.v1.hud.HudElementRegistry; +import net.fabricmc.fabric.api.client.rendering.v1.hud.VanillaHudElements; +import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.util.Identifier; +import net.minecraft.util.WorldSavePath; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.MathHelper; +import net.minecraft.util.math.Vec3d; +import net.minecraft.world.World; +import net.minecraft.client.render.Camera; +import net.minecraft.client.render.RenderTickCounter; +import net.minecraft.text.Text; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.network.ServerInfo; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.font.TextRenderer; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.Items; +import net.minecraft.registry.RegistryKey; +import net.minecraft.registry.RegistryKeys; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class BetterCompass implements ModInitializer { +public class BetterCompass implements ClientModInitializer { public static final String MOD_ID = "better-compass"; - - // This logger is used to write text to the console and the log file. - // It is considered best practice to use your mod id as the logger's name. - // That way, it's clear which mod wrote info, warnings, and errors. public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); + private static final MinecraftClient CLIENT = MinecraftClient.getInstance(); + private static final float GUI_WIDTH = 0.5F; + + private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); + private static Map valueMap = new LinkedHashMap<>(); + + + private static RegistryKey lastDimension = null; + + public static BlockPos deathPointBlockPos = null; + public static RegistryKey deathDimension = null; + public static BlockPos netherPortalBlockPos = null; + public static String serverId; + @Override - public void onInitialize() { - // This code runs as soon as Minecraft is in a mod-load-ready state. - // However, some things (like resources) may still be uninitialized. - // Proceed with mild caution. + public void onInitializeClient() { + ClientCommands.register(); + // Attach our rendering code to before the chat hud layer. Our layer will render right before the chat. The API will take care of z spacing. + HudElementRegistry.attachElementBefore(VanillaHudElements.CHAT, Identifier.of(MOD_ID, "before_chat"), BetterCompass::render); + + ClientTickEvents.END_CLIENT_TICK.register(client -> { + if (client.player != null && client.player.isDead()) { + deathPointBlockPos = client.player.getBlockPos(); + deathDimension = client.world.getRegistryKey(); + valueMap.put("deathPointBlockPos", blockPosToString(deathPointBlockPos)); + valueMap.put("deathDimension", registryKeyToString(deathDimension)); + saveValues(); + } + if (client.world != null) { + RegistryKey current = client.world.getRegistryKey(); + if (lastDimension != null && !lastDimension.equals(current)) { + if (current == World.NETHER) { + netherPortalBlockPos = client.player.getBlockPos(); + valueMap.put("netherPortalBlockPos", blockPosToString(netherPortalBlockPos)); + saveValues(); + } + else{ + netherPortalBlockPos = null; + } + } + lastDimension = current; + } + }); + ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> { + // This runs when the client enters a world + + if (CLIENT.getServer() == null) { + // Multiplayer + ServerInfo info = CLIENT.getCurrentServerEntry(); + serverId = info != null ? info.address.replace(":", "_") : "unknown_server"; + } else { + // Singleplayer + serverId = CLIENT.getServer().getSavePath(WorldSavePath.ROOT) + .getParent().getFileName().toString(); + } + loadData(); + }); + } + + private static void render(DrawContext context, RenderTickCounter tickCounter) { + float compassPosition = 0.05F; + if(SettingsManager.COMPASS_POSITION.getValueAsString().equals("Bottom")){compassPosition = 1-0.18F;} + float deltaY = 0F; + float size = 1.5F; + int color = 0xFFFFFFFF; + PlayerEntity player = CLIENT.player; + int optionValueIndex = SettingsManager.SHOW_COMPASS_HUD.getValueAsIndex(); + if(optionValueIndex == 3 + || (optionValueIndex == 2 && !player.getMainHandStack().getItem().getTranslationKey().equals("item.minecraft.compass")) + || (optionValueIndex == 1 && !player.getInventory().containsAny(Set.of(Items.COMPASS))) + ){ + return; + } + TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer; + Camera camera = CLIENT.gameRenderer.getCamera(); + if (camera == null) return; + + float fov = ((FovMultiplierAccessor)(Object)CLIENT.gameRenderer).getFovMultiplier() * CLIENT.options.getFov().getValue(); + float camDirection = camera.getYaw(); + + if(!SettingsManager.CARDINALS_DIRECTION_POSITION.getValue().equals("Disabled")){ + color = SettingsManager.getRGBColorFromSetting(SettingsManager.CARDINALS_DIRECTION_COLOR.getValueAsString()); + if(SettingsManager.CARDINALS_DIRECTION_POSITION.getValue().equals("Under")){deltaY = 0.03F; size = 0.8F;} + if(SettingsManager.CARDINALS_DIRECTION_POSITION.getValue().equals("Above")){deltaY = -0.03F; size = 0.8F;} + drawCompassSymbol(context, textRenderer, fov, "N", 180, camDirection, compassPosition + deltaY, color, size); + drawCompassSymbol(context, textRenderer, fov, "E", 270, camDirection, compassPosition + deltaY, color, size); + drawCompassSymbol(context, textRenderer, fov, "S", 0, camDirection, compassPosition + deltaY, color, size); + drawCompassSymbol(context, textRenderer, fov, "W", 90, camDirection, compassPosition + deltaY, color, size); + } + + for (int i = 0; i < 36; i++){ + if(i % 9 != 0 || !SettingsManager.CARDINALS_DIRECTION_POSITION.getValue().equals("Aligned")){ + drawCompassSymbol(context, textRenderer, fov, "|", i * 10, camDirection, compassPosition, 0xFFFFFFFF, 1); + } + } + + //Add death position to the compass HUD + if(deathPointBlockPos != null && CLIENT.world.getRegistryKey() == deathDimension && !SettingsManager.LAST_DEATH_DIRECTION_POSITION.getValue().equals("Disabled")){ + deltaY = 0; + size = 1.5F; + color = SettingsManager.getRGBColorFromSetting(SettingsManager.LAST_DEATH_DIRECTION_COLOR.getValueAsString()); + if(SettingsManager.LAST_DEATH_DIRECTION_POSITION.getValue().equals("Under")){deltaY = 0.03F; size = 0.8F;} + if(SettingsManager.LAST_DEATH_DIRECTION_POSITION.getValue().equals("Above")){deltaY = -0.03F; size = 0.8F;} + Vec3d deathPos = new Vec3d(deathPointBlockPos.getX(), 0, deathPointBlockPos.getZ()); + double dx = deathPos.x - player.getX(); + double dz = deathPos.z - player.getZ(); + drawCompassSymbol(context, textRenderer, fov, "๐Ÿ’€", (float)(MathHelper.atan2(dz, dx) * (180 / Math.PI)) - 90, camDirection, compassPosition + deltaY, color, size); + } + + //Add nether portal position to the compass HUD + if(netherPortalBlockPos != null && CLIENT.world.getRegistryKey() == World.NETHER && !SettingsManager.NETHER_PORTAL_DIRECTION_POSITION.getValue().equals("Disabled")){ + deltaY = 0; + size = 1.5F; + color = SettingsManager.getRGBColorFromSetting(SettingsManager.NETHER_PORTAL_DIRECTION_COLOR.getValueAsString()); + if(SettingsManager.NETHER_PORTAL_DIRECTION_POSITION.getValue().equals("Under")){deltaY = 0.03F; size = 0.8F;} + if(SettingsManager.NETHER_PORTAL_DIRECTION_POSITION.getValue().equals("Above")){deltaY = -0.03F; size = 0.8F;} + Vec3d netherPortalPos = new Vec3d(netherPortalBlockPos.getX(), 0, netherPortalBlockPos.getZ()); + double dx = netherPortalPos.x - player.getX(); + double dz = netherPortalPos.z - player.getZ(); + drawCompassSymbol(context, textRenderer, fov, "๐ŸŒ", (float)(MathHelper.atan2(dz, dx) * (180 / Math.PI)) - 90, camDirection, compassPosition + deltaY, color, size); + } + } + + public static void drawCompassSymbol(DrawContext context, TextRenderer textRenderer, float fov, String symbol, float targetDirection, float camDirection, float y, int color, float scale){ + int screenWidth = context.getScaledWindowWidth(); + int screenHeight = context.getScaledWindowHeight(); + + //Determine the position of the symbol depending on the targetDirection and the camDirection + int x = - (int) (screenWidth * 0.5 * GUI_WIDTH * MathHelper.wrapDegrees(camDirection-targetDirection)/fov); + if(Math.abs(MathHelper.wrapDegrees(camDirection-targetDirection)/fov)>1.0){ + return; + } + + //Adjust symbol transparency + color = color % 16777216; + int alpha = (int) (255 * (1-Math.abs(Math.sin(Math.toRadians(camDirection - targetDirection))))); + color += 16777216 * alpha; + + //Scale the GUI + var matrices = context.getMatrices(); + var before = new org.joml.Matrix3x2f(matrices); + matrices.scale(scale, scale); + + if(SettingsManager.COMPASS_STYLE.getValueAsString().equals("Shadows")){ + context.drawCenteredTextWithShadow(textRenderer, Text.literal(symbol),(int) ((screenWidth/2 + x)/scale),(int) ((screenHeight*y)/scale - textRenderer.fontHeight/2f),color); + } + else{ + context.drawText(textRenderer, symbol,(int) ((screenWidth/2 + x)/scale - textRenderer.getWidth(symbol)/2f),(int) ((screenHeight*y)/scale - textRenderer.fontHeight/2f),color, false); + } + + matrices.set(before); + } + + + + + public static void saveValues() { + Path configPath = FabricLoader.getInstance().getConfigDir().resolve(serverId).resolve(MOD_ID + ".json"); + try { + Files.createDirectories(configPath.getParent()); + try (Writer writer = Files.newBufferedWriter(configPath)) { + GSON.toJson(valueMap, writer); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static void loadData() { + Path configPath = FabricLoader.getInstance().getConfigDir().resolve(serverId).resolve(MOD_ID + ".json"); + try (Reader reader = Files.newBufferedReader(configPath)) { + Type type = new TypeToken>() {}.getType(); + valueMap = GSON.fromJson(reader, type); + } catch (IOException e) { + e.printStackTrace(); + return; + } + + if(valueMap.containsKey("deathPointBlockPos")){ + deathPointBlockPos = stringToBlockPos(valueMap.get("deathPointBlockPos")); + } + if(valueMap.containsKey("deathDimension")){ + deathDimension = RegistryKey.of(RegistryKeys.WORLD, Identifier.tryParse(valueMap.get("deathDimension"))); + } + if(valueMap.containsKey("netherPortalBlockPos")){ + netherPortalBlockPos = stringToBlockPos(valueMap.get("netherPortalBlockPos")); + } + } + + private static String blockPosToString(BlockPos pos){ + return pos.getX() + "," + pos.getY() + "," + pos.getZ(); + } + + private static String registryKeyToString(RegistryKey key){ + return key.getValue().toString(); + } - LOGGER.info("Hello Fabric world!"); + private static BlockPos stringToBlockPos(String str){ + String[] coords = str.split(","); + return new BlockPos( + Integer.parseInt(coords[0]), + Integer.parseInt(coords[1]), + Integer.parseInt(coords[2]) + ); } } \ No newline at end of file diff --git a/src/client/java/fr/madu59/bettercompass/config/ClientCommands.java b/src/main/java/fr/madu59/bettercompass/config/ClientCommands.java similarity index 100% rename from src/client/java/fr/madu59/bettercompass/config/ClientCommands.java rename to src/main/java/fr/madu59/bettercompass/config/ClientCommands.java diff --git a/src/client/java/fr/madu59/bettercompass/config/Option.java b/src/main/java/fr/madu59/bettercompass/config/Option.java similarity index 100% rename from src/client/java/fr/madu59/bettercompass/config/Option.java rename to src/main/java/fr/madu59/bettercompass/config/Option.java diff --git a/src/client/java/fr/madu59/bettercompass/config/SettingsManager.java b/src/main/java/fr/madu59/bettercompass/config/SettingsManager.java similarity index 100% rename from src/client/java/fr/madu59/bettercompass/config/SettingsManager.java rename to src/main/java/fr/madu59/bettercompass/config/SettingsManager.java diff --git a/src/client/java/fr/madu59/bettercompass/config/configScreen/BetterCompassConfigScreen.java b/src/main/java/fr/madu59/bettercompass/config/configScreen/BetterCompassConfigScreen.java similarity index 100% rename from src/client/java/fr/madu59/bettercompass/config/configScreen/BetterCompassConfigScreen.java rename to src/main/java/fr/madu59/bettercompass/config/configScreen/BetterCompassConfigScreen.java diff --git a/src/client/java/fr/madu59/bettercompass/config/configScreen/ModMenuApiImpl.java b/src/main/java/fr/madu59/bettercompass/config/configScreen/ModMenuApiImpl.java similarity index 100% rename from src/client/java/fr/madu59/bettercompass/config/configScreen/ModMenuApiImpl.java rename to src/main/java/fr/madu59/bettercompass/config/configScreen/ModMenuApiImpl.java diff --git a/src/client/java/fr/madu59/bettercompass/config/configScreen/MyConfigListWidget.java b/src/main/java/fr/madu59/bettercompass/config/configScreen/MyConfigListWidget.java similarity index 100% rename from src/client/java/fr/madu59/bettercompass/config/configScreen/MyConfigListWidget.java rename to src/main/java/fr/madu59/bettercompass/config/configScreen/MyConfigListWidget.java diff --git a/src/client/java/fr/madu59/bettercompass/mixin/client/FovMultiplierAccessor.java b/src/main/java/fr/madu59/bettercompass/mixin/client/FovMultiplierAccessor.java similarity index 100% rename from src/client/java/fr/madu59/bettercompass/mixin/client/FovMultiplierAccessor.java rename to src/main/java/fr/madu59/bettercompass/mixin/client/FovMultiplierAccessor.java diff --git a/src/client/resources/better-compass.client.mixins.json b/src/main/resources/better-compass.client.mixins.json similarity index 100% rename from src/client/resources/better-compass.client.mixins.json rename to src/main/resources/better-compass.client.mixins.json diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 30f68da..e9c2eb8 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -3,25 +3,25 @@ "id": "better-compass", "version": "${version}", "name": "Better Compass", - "description": "This is an example description! Tell everyone what your mod is about!", + "description": "Display a clean and comprehensive compass HUD instead of having to use the compass item or F3", "authors": [ - "Me!" + "maDU59_" + ], + "contributors": [ + "AmyMialee" ], "contact": { - "homepage": "https://fabricmc.net/", - "sources": "https://github.com/FabricMC/fabric-example-mod" + "homepage": "https://discord.gg/ATTZQQeVw4", + "sources": "https://github.com/maDU59/BetterCompass/" }, "license": "MIT", "icon": "assets/better-compass/icon.png", - "environment": "*", + "environment": "client", "entrypoints": { - "main": [ - "fr.madu59.bettercompass.BetterCompass" - ], "client": [ - "fr.madu59.bettercompass.BetterCompassClient" + "fr.madu59.bettercompass.BetterCompass" ], - "modmenu": [ + "modmenu": [ "fr.madu59.bettercompass.config.configScreen.ModMenuApiImpl" ] }, @@ -29,12 +29,6 @@ "better-compass.client.mixins.json" ], "depends": { - "fabricloader": ">=0.17.2", - "minecraft": "~1.21.9", - "java": ">=21", "fabric-api": "*" - }, - "suggests": { - "another-mod": "*" } } \ No newline at end of file From 8ea1183b3bd8ad5f65fb89a0d6915f28db28f437 Mon Sep 17 00:00:00 2001 From: AmyMialee Date: Mon, 5 Oct 2026 20:02:49 +0100 Subject: [PATCH 2/2] 26.3 Port and Cleanup --- build.gradle | 16 +- gradle.properties | 12 +- .../madu59/bettercompass/BetterCompass.java | 176 ++++++++++-------- .../bettercompass/config/ClientCommands.java | 37 ---- .../madu59/bettercompass/config/Option.java | 12 +- .../bettercompass/config/SettingsManager.java | 159 +++++++--------- .../BetterCompassConfigScreen.java | 80 -------- .../configScreen/MyConfigListWidget.java | 132 ------------- .../mixin/client/FovMultiplierAccessor.java | 12 -- .../modmenu/BetterCompassConfigScreen.java | 59 ++++++ .../BetterCompassModMenu.java} | 6 +- .../modmenu/MyConfigListWidget.java | 126 +++++++++++++ .../better-compass.client.mixins.json | 6 +- src/main/resources/fabric.mod.json | 8 +- 14 files changed, 377 insertions(+), 464 deletions(-) delete mode 100644 src/main/java/fr/madu59/bettercompass/config/ClientCommands.java delete mode 100644 src/main/java/fr/madu59/bettercompass/config/configScreen/BetterCompassConfigScreen.java delete mode 100644 src/main/java/fr/madu59/bettercompass/config/configScreen/MyConfigListWidget.java delete mode 100644 src/main/java/fr/madu59/bettercompass/mixin/client/FovMultiplierAccessor.java create mode 100644 src/main/java/fr/madu59/bettercompass/modmenu/BetterCompassConfigScreen.java rename src/main/java/fr/madu59/bettercompass/{config/configScreen/ModMenuApiImpl.java => modmenu/BetterCompassModMenu.java} (71%) create mode 100644 src/main/java/fr/madu59/bettercompass/modmenu/MyConfigListWidget.java diff --git a/build.gradle b/build.gradle index 2b7205b..a76e836 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ plugins { - id 'fabric-loom' version "${loom_version}" + id 'net.fabricmc.fabric-loom' version "${loom_version}" } version = project.mod_version @@ -12,11 +12,9 @@ repositories { dependencies { minecraft "com.mojang:minecraft:${project.minecraft_version}" - mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" - modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" - - modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" - modCompileOnly("com.terraformersmc:modmenu:${project.modmenu_version}") + implementation "net.fabricmc:fabric-loader:${project.loader_version}" + implementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_api_version}" + implementation "com.terraformersmc:modmenu:${project.modmenu_version}" } processResources { @@ -25,12 +23,12 @@ processResources { filesMatching("fabric.mod.json") { expand "version": project.version } } -tasks.withType(JavaCompile).configureEach { it.options.release = 21 } +tasks.withType(JavaCompile).configureEach { it.options.release = 25 } java { withSourcesJar() - sourceCompatibility = JavaVersion.VERSION_21 - targetCompatibility = JavaVersion.VERSION_21 + sourceCompatibility = JavaVersion.VERSION_25 + targetCompatibility = JavaVersion.VERSION_25 } jar { diff --git a/gradle.properties b/gradle.properties index d305f11..4bb23ea 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,10 +4,10 @@ org.gradle.parallel=true org.gradle.configuration-cache=false # Fabric Properties on https://modmuss50.me/fabric.html -minecraft_version=1.21.11 -yarn_mappings=1.21.11+build.1 -loader_version=0.18.1 -loom_version=1.13-SNAPSHOT +minecraft_version=26.3 +loom_version=1.18-SNAPSHOT +loader_version=0.19.5 +fabric_api_version=0.161.0+26.3 # Mod Properties mod_version=1.0.8 @@ -15,5 +15,5 @@ maven_group=fr.madu59 mod_id=better-compass # Dependencies -fabric_version=0.139.4+1.21.11 -modmenu_version = 15.0.0 \ No newline at end of file +# Mod Menu from https://modrinth.com/mod/modmenu/versions +modmenu_version=21.0.0 \ No newline at end of file diff --git a/src/main/java/fr/madu59/bettercompass/BetterCompass.java b/src/main/java/fr/madu59/bettercompass/BetterCompass.java index 0c4d1a0..08bc633 100644 --- a/src/main/java/fr/madu59/bettercompass/BetterCompass.java +++ b/src/main/java/fr/madu59/bettercompass/BetterCompass.java @@ -1,83 +1,100 @@ package fr.madu59.bettercompass; -import java.lang.reflect.Type; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Set; -import java.lang.Math; -import java.io.*; -import java.nio.file.*; - import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; -import fr.madu59.bettercompass.config.ClientCommands; +import com.mojang.brigadier.arguments.StringArgumentType; import fr.madu59.bettercompass.config.SettingsManager; -import fr.madu59.bettercompass.mixin.client.FovMultiplierAccessor; - import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; +import net.fabricmc.fabric.api.client.command.v2.ClientCommands; import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents; import net.fabricmc.fabric.api.client.rendering.v1.hud.HudElementRegistry; import net.fabricmc.fabric.api.client.rendering.v1.hud.VanillaHudElements; import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.util.Identifier; -import net.minecraft.util.WorldSavePath; -import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.MathHelper; -import net.minecraft.util.math.Vec3d; -import net.minecraft.world.World; -import net.minecraft.client.render.Camera; -import net.minecraft.client.render.RenderTickCounter; -import net.minecraft.text.Text; -import net.minecraft.client.gui.DrawContext; -import net.minecraft.client.network.ServerInfo; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.font.TextRenderer; -import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.item.Items; -import net.minecraft.registry.RegistryKey; -import net.minecraft.registry.RegistryKeys; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import net.minecraft.client.Camera; +import net.minecraft.client.DeltaTracker; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.Font; +import net.minecraft.client.gui.GuiGraphicsExtractor; +import net.minecraft.client.multiplayer.ServerData; +import net.minecraft.commands.SharedSuggestionProvider; +import net.minecraft.core.BlockPos; +import net.minecraft.core.registries.Registries; +import net.minecraft.network.chat.Component; +import net.minecraft.resources.Identifier; +import net.minecraft.resources.ResourceKey; +import net.minecraft.util.Mth; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.Items; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.storage.LevelResource; +import net.minecraft.world.phys.Vec3; +import org.joml.Matrix3x2f; + +import java.io.IOException; +import java.io.Reader; +import java.io.Writer; +import java.lang.reflect.Type; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; public class BetterCompass implements ClientModInitializer { public static final String MOD_ID = "better-compass"; - public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); - private static final MinecraftClient CLIENT = MinecraftClient.getInstance(); + private static final Minecraft CLIENT = Minecraft.getInstance(); private static final float GUI_WIDTH = 0.5F; private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); private static Map valueMap = new LinkedHashMap<>(); - - private static RegistryKey lastDimension = null; + private static ResourceKey lastDimension = null; public static BlockPos deathPointBlockPos = null; - public static RegistryKey deathDimension = null; + public static ResourceKey deathDimension = null; public static BlockPos netherPortalBlockPos = null; public static String serverId; @Override public void onInitializeClient() { - ClientCommands.register(); + ClientCommandRegistrationCallback.EVENT.register((dispatcher, _) -> dispatcher.register(ClientCommands.literal("bettercompass") + .then(ClientCommands.argument("option", StringArgumentType.string()).suggests((_, builder) -> SharedSuggestionProvider.suggest(SettingsManager.getAllOptionsId(), builder)) + .then(ClientCommands.argument("value", StringArgumentType.string()).suggests((context, builder) -> SharedSuggestionProvider.suggest(SettingsManager.getOptionPossibleValues(StringArgumentType.getString(context, "option")), builder)) + .executes(context -> { + String option = StringArgumentType.getString(context, "option"); + String value = StringArgumentType.getString(context, "value"); + + boolean success = SettingsManager.setOptionValue(option, value); + context.getSource().sendFeedback(Component.literal(success ? "Updated " + option + " to " + value : "Failed to update setting.")); + if(success){ + SettingsManager.saveSettings(SettingsManager.ALL_OPTIONS); + } + return success ? 1 : 0; + }) + ) + ) + )); + // Attach our rendering code to before the chat hud layer. Our layer will render right before the chat. The API will take care of z spacing. - HudElementRegistry.attachElementBefore(VanillaHudElements.CHAT, Identifier.of(MOD_ID, "before_chat"), BetterCompass::render); + HudElementRegistry.attachElementBefore(VanillaHudElements.CHAT, Identifier.fromNamespaceAndPath(MOD_ID, "before_chat"), BetterCompass::render); ClientTickEvents.END_CLIENT_TICK.register(client -> { - if (client.player != null && client.player.isDead()) { - deathPointBlockPos = client.player.getBlockPos(); - deathDimension = client.world.getRegistryKey(); + if (client.player != null && client.player.isDeadOrDying()) { + deathPointBlockPos = client.player.blockPosition(); + deathDimension = client.level.dimension(); valueMap.put("deathPointBlockPos", blockPosToString(deathPointBlockPos)); valueMap.put("deathDimension", registryKeyToString(deathDimension)); saveValues(); } - if (client.world != null) { - RegistryKey current = client.world.getRegistryKey(); + if (client.level != null) { + ResourceKey current = client.level.dimension(); if (lastDimension != null && !lastDimension.equals(current)) { - if (current == World.NETHER) { - netherPortalBlockPos = client.player.getBlockPos(); + if (current == Level.NETHER) { + netherPortalBlockPos = client.player.blockPosition(); valueMap.put("netherPortalBlockPos", blockPosToString(netherPortalBlockPos)); saveValues(); } @@ -91,39 +108,38 @@ public void onInitializeClient() { ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> { // This runs when the client enters a world - if (CLIENT.getServer() == null) { + if (CLIENT.getSingleplayerServer() == null) { // Multiplayer - ServerInfo info = CLIENT.getCurrentServerEntry(); - serverId = info != null ? info.address.replace(":", "_") : "unknown_server"; + ServerData info = CLIENT.getCurrentServer(); + serverId = info != null ? info.ip.replace(":", "_") : "unknown_server"; } else { // Singleplayer - serverId = CLIENT.getServer().getSavePath(WorldSavePath.ROOT) + serverId = CLIENT.getSingleplayerServer().getWorldPath(LevelResource.ROOT) .getParent().getFileName().toString(); } loadData(); }); } - private static void render(DrawContext context, RenderTickCounter tickCounter) { + private static void render(GuiGraphicsExtractor context, DeltaTracker tickCounter) { float compassPosition = 0.05F; if(SettingsManager.COMPASS_POSITION.getValueAsString().equals("Bottom")){compassPosition = 1-0.18F;} float deltaY = 0F; float size = 1.5F; - int color = 0xFFFFFFFF; - PlayerEntity player = CLIENT.player; + int color; + Player player = CLIENT.player; int optionValueIndex = SettingsManager.SHOW_COMPASS_HUD.getValueAsIndex(); if(optionValueIndex == 3 - || (optionValueIndex == 2 && !player.getMainHandStack().getItem().getTranslationKey().equals("item.minecraft.compass")) - || (optionValueIndex == 1 && !player.getInventory().containsAny(Set.of(Items.COMPASS))) + || (optionValueIndex == 2 && !player.getMainHandItem().getItem().getDescriptionId().equals("item.minecraft.compass")) + || (optionValueIndex == 1 && !player.getInventory().hasAnyOf(Set.of(Items.COMPASS))) ){ return; } - TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer; - Camera camera = CLIENT.gameRenderer.getCamera(); - if (camera == null) return; + Font textRenderer = Minecraft.getInstance().font; + Camera camera = CLIENT.gameRenderer.mainCamera(); - float fov = ((FovMultiplierAccessor)(Object)CLIENT.gameRenderer).getFovMultiplier() * CLIENT.options.getFov().getValue(); - float camDirection = camera.getYaw(); + float fov = camera.getFov(); + float camDirection = camera.yRot(); if(!SettingsManager.CARDINALS_DIRECTION_POSITION.getValue().equals("Disabled")){ color = SettingsManager.getRGBColorFromSetting(SettingsManager.CARDINALS_DIRECTION_COLOR.getValueAsString()); @@ -142,39 +158,39 @@ private static void render(DrawContext context, RenderTickCounter tickCounter) { } //Add death position to the compass HUD - if(deathPointBlockPos != null && CLIENT.world.getRegistryKey() == deathDimension && !SettingsManager.LAST_DEATH_DIRECTION_POSITION.getValue().equals("Disabled")){ + if(deathPointBlockPos != null && CLIENT.level.dimension() == deathDimension && !SettingsManager.LAST_DEATH_DIRECTION_POSITION.getValue().equals("Disabled")){ deltaY = 0; size = 1.5F; color = SettingsManager.getRGBColorFromSetting(SettingsManager.LAST_DEATH_DIRECTION_COLOR.getValueAsString()); if(SettingsManager.LAST_DEATH_DIRECTION_POSITION.getValue().equals("Under")){deltaY = 0.03F; size = 0.8F;} if(SettingsManager.LAST_DEATH_DIRECTION_POSITION.getValue().equals("Above")){deltaY = -0.03F; size = 0.8F;} - Vec3d deathPos = new Vec3d(deathPointBlockPos.getX(), 0, deathPointBlockPos.getZ()); + Vec3 deathPos = new Vec3(deathPointBlockPos.getX(), 0, deathPointBlockPos.getZ()); double dx = deathPos.x - player.getX(); double dz = deathPos.z - player.getZ(); - drawCompassSymbol(context, textRenderer, fov, "๐Ÿ’€", (float)(MathHelper.atan2(dz, dx) * (180 / Math.PI)) - 90, camDirection, compassPosition + deltaY, color, size); + drawCompassSymbol(context, textRenderer, fov, "๐Ÿ’€", (float)(Mth.atan2(dz, dx) * (180 / Math.PI)) - 90, camDirection, compassPosition + deltaY, color, size); } //Add nether portal position to the compass HUD - if(netherPortalBlockPos != null && CLIENT.world.getRegistryKey() == World.NETHER && !SettingsManager.NETHER_PORTAL_DIRECTION_POSITION.getValue().equals("Disabled")){ + if(netherPortalBlockPos != null && CLIENT.level.dimension() == Level.NETHER && !SettingsManager.NETHER_PORTAL_DIRECTION_POSITION.getValue().equals("Disabled")){ deltaY = 0; size = 1.5F; color = SettingsManager.getRGBColorFromSetting(SettingsManager.NETHER_PORTAL_DIRECTION_COLOR.getValueAsString()); if(SettingsManager.NETHER_PORTAL_DIRECTION_POSITION.getValue().equals("Under")){deltaY = 0.03F; size = 0.8F;} if(SettingsManager.NETHER_PORTAL_DIRECTION_POSITION.getValue().equals("Above")){deltaY = -0.03F; size = 0.8F;} - Vec3d netherPortalPos = new Vec3d(netherPortalBlockPos.getX(), 0, netherPortalBlockPos.getZ()); + Vec3 netherPortalPos = new Vec3(netherPortalBlockPos.getX(), 0, netherPortalBlockPos.getZ()); double dx = netherPortalPos.x - player.getX(); double dz = netherPortalPos.z - player.getZ(); - drawCompassSymbol(context, textRenderer, fov, "๐ŸŒ", (float)(MathHelper.atan2(dz, dx) * (180 / Math.PI)) - 90, camDirection, compassPosition + deltaY, color, size); + drawCompassSymbol(context, textRenderer, fov, "๐ŸŒ", (float)(Mth.atan2(dz, dx) * (180 / Math.PI)) - 90, camDirection, compassPosition + deltaY, color, size); } } - public static void drawCompassSymbol(DrawContext context, TextRenderer textRenderer, float fov, String symbol, float targetDirection, float camDirection, float y, int color, float scale){ - int screenWidth = context.getScaledWindowWidth(); - int screenHeight = context.getScaledWindowHeight(); + public static void drawCompassSymbol(GuiGraphicsExtractor context, Font textRenderer, float fov, String symbol, float targetDirection, float camDirection, float y, int color, float scale){ + int screenWidth = context.guiWidth(); + int screenHeight = context.guiHeight(); //Determine the position of the symbol depending on the targetDirection and the camDirection - int x = - (int) (screenWidth * 0.5 * GUI_WIDTH * MathHelper.wrapDegrees(camDirection-targetDirection)/fov); - if(Math.abs(MathHelper.wrapDegrees(camDirection-targetDirection)/fov)>1.0){ + int x = - (int) (screenWidth * 0.5 * GUI_WIDTH * Mth.wrapDegrees(camDirection - targetDirection) / fov); + if (Math.abs(Mth.wrapDegrees(camDirection - targetDirection) / fov)>1.0){ return; } @@ -184,23 +200,19 @@ public static void drawCompassSymbol(DrawContext context, TextRenderer textRende color += 16777216 * alpha; //Scale the GUI - var matrices = context.getMatrices(); - var before = new org.joml.Matrix3x2f(matrices); + var matrices = context.pose(); + var before = new Matrix3x2f(matrices); matrices.scale(scale, scale); - if(SettingsManager.COMPASS_STYLE.getValueAsString().equals("Shadows")){ - context.drawCenteredTextWithShadow(textRenderer, Text.literal(symbol),(int) ((screenWidth/2 + x)/scale),(int) ((screenHeight*y)/scale - textRenderer.fontHeight/2f),color); - } - else{ - context.drawText(textRenderer, symbol,(int) ((screenWidth/2 + x)/scale - textRenderer.getWidth(symbol)/2f),(int) ((screenHeight*y)/scale - textRenderer.fontHeight/2f),color, false); + if (SettingsManager.COMPASS_STYLE.getValueAsString().equals("Shadows")) { + context.centeredText(textRenderer, Component.literal(symbol), (int) ((screenWidth / 2f + x) / scale), (int) ((screenHeight * y) / scale - textRenderer.lineHeight / 2f), color); + } else { + context.text(textRenderer, symbol,(int) ((screenWidth / 2f + x) / scale - textRenderer.width(symbol) / 2f), (int) ((screenHeight * y)/scale - textRenderer.lineHeight / 2f), color, false); } matrices.set(before); } - - - public static void saveValues() { Path configPath = FabricLoader.getInstance().getConfigDir().resolve(serverId).resolve(MOD_ID + ".json"); try { @@ -227,7 +239,7 @@ private static void loadData() { deathPointBlockPos = stringToBlockPos(valueMap.get("deathPointBlockPos")); } if(valueMap.containsKey("deathDimension")){ - deathDimension = RegistryKey.of(RegistryKeys.WORLD, Identifier.tryParse(valueMap.get("deathDimension"))); + deathDimension = ResourceKey.create(Registries.DIMENSION, Identifier.tryParse(valueMap.get("deathDimension"))); } if(valueMap.containsKey("netherPortalBlockPos")){ netherPortalBlockPos = stringToBlockPos(valueMap.get("netherPortalBlockPos")); @@ -238,8 +250,8 @@ private static String blockPosToString(BlockPos pos){ return pos.getX() + "," + pos.getY() + "," + pos.getZ(); } - private static String registryKeyToString(RegistryKey key){ - return key.getValue().toString(); + private static String registryKeyToString(ResourceKey key){ + return key.identifier().toString(); } private static BlockPos stringToBlockPos(String str){ diff --git a/src/main/java/fr/madu59/bettercompass/config/ClientCommands.java b/src/main/java/fr/madu59/bettercompass/config/ClientCommands.java deleted file mode 100644 index 3413991..0000000 --- a/src/main/java/fr/madu59/bettercompass/config/ClientCommands.java +++ /dev/null @@ -1,37 +0,0 @@ -package fr.madu59.bettercompass.config; - -import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.*; -import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; -import net.minecraft.client.MinecraftClient; -import net.minecraft.command.CommandSource; -import net.minecraft.text.Text; -import com.mojang.brigadier.arguments.StringArgumentType; - -public class ClientCommands { - public static void register() { - ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> { - dispatcher.register( - literal("BetterCompassConfig") - .then(argument("option", StringArgumentType.string()).suggests((context, builder) -> { return CommandSource.suggestMatching(SettingsManager.getAllOptionsId(), builder);}) - .then(argument("value", StringArgumentType.string()).suggests((context, builder) -> { return CommandSource.suggestMatching(SettingsManager.getOptionPossibleValues(StringArgumentType.getString(context, "option")), builder);}) - .executes(context -> { - String option = StringArgumentType.getString(context, "option"); - String value = StringArgumentType.getString(context, "value"); - - boolean success = SettingsManager.setOptionValue(option, (Object) value); - MinecraftClient.getInstance().player.sendMessage( - Text.literal(success ? "Updated " + option + " to " + value : "Failed to update setting."), - false - ); - if(success){ - SettingsManager.saveSettings(SettingsManager.ALL_OPTIONS); - } - return success ? 1 : 0; - }) - ) - ) - ); - }); - } -} - diff --git a/src/main/java/fr/madu59/bettercompass/config/Option.java b/src/main/java/fr/madu59/bettercompass/config/Option.java index a905b75..52b2d72 100644 --- a/src/main/java/fr/madu59/bettercompass/config/Option.java +++ b/src/main/java/fr/madu59/bettercompass/config/Option.java @@ -1,8 +1,8 @@ package fr.madu59.bettercompass.config; -import java.util.List; +import net.minecraft.client.resources.language.I18n; -import net.minecraft.client.resource.language.I18n; +import java.util.List; public class Option { public String id; @@ -39,11 +39,11 @@ public String getId() { } public String getName() { - return I18n.translate(this.name); + return I18n.get(this.name); } public String getDescription() { - return I18n.translate(this.description); + return I18n.get(this.description); } public List getPossibleValues(){ @@ -52,9 +52,9 @@ public List getPossibleValues(){ public String getValueAsString() { if( value instanceof Boolean boolValue) { - return boolValue ? I18n.translate("better-compass.config.enabled") : I18n.translate("better-compass.config.disabled"); + return boolValue ? I18n.get("better-compass.config.enabled") : I18n.get("better-compass.config.disabled"); } - return I18n.translate(this.value.toString()); + return I18n.get(this.value.toString()); } public int getValueAsIndex(){ diff --git a/src/main/java/fr/madu59/bettercompass/config/SettingsManager.java b/src/main/java/fr/madu59/bettercompass/config/SettingsManager.java index 810e157..fae9f1e 100644 --- a/src/main/java/fr/madu59/bettercompass/config/SettingsManager.java +++ b/src/main/java/fr/madu59/bettercompass/config/SettingsManager.java @@ -3,18 +3,19 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; - import fr.madu59.bettercompass.BetterCompass; import net.fabricmc.loader.api.FabricLoader; -import java.io.*; +import java.io.IOException; +import java.io.Reader; +import java.io.Writer; import java.lang.reflect.Type; -import java.nio.file.*; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.*; import java.util.stream.Collectors; public class SettingsManager { - public static List