diff --git a/gradlew.bat b/gradlew.bat index e509b2d..c4bdd3a 100755 --- a/gradlew.bat +++ b/gradlew.bat @@ -1,93 +1,93 @@ -@rem -@rem Copyright 2015 the original author or authors. -@rem -@rem Licensed under the Apache License, Version 2.0 (the "License"); -@rem you may not use this file except in compliance with the License. -@rem You may obtain a copy of the License at -@rem -@rem https://www.apache.org/licenses/LICENSE-2.0 -@rem -@rem Unless required by applicable law or agreed to in writing, software -@rem distributed under the License is distributed on an "AS IS" BASIS, -@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -@rem See the License for the specific language governing permissions and -@rem limitations under the License. -@rem -@rem SPDX-License-Identifier: Apache-2.0 -@rem - -@if "%DEBUG%"=="" @echo off -@rem ########################################################################## -@rem -@rem Gradle startup script for Windows -@rem -@rem ########################################################################## - -@rem Set local scope for the variables with windows NT shell -if "%OS%"=="Windows_NT" setlocal - -set DIRNAME=%~dp0 -if "%DIRNAME%"=="" set DIRNAME=. -@rem This is normally unused -set APP_BASE_NAME=%~n0 -set APP_HOME=%DIRNAME% - -@rem Resolve any "." and ".." in APP_HOME to make it shorter. -for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi - -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" - -@rem Find java.exe -if defined JAVA_HOME goto findJavaFromJavaHome - -set JAVA_EXE=java.exe -%JAVA_EXE% -version >NUL 2>&1 -if %ERRORLEVEL% equ 0 goto execute - -echo. 1>&2 -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 -echo. 1>&2 -echo Please set the JAVA_HOME variable in your environment to match the 1>&2 -echo location of your Java installation. 1>&2 - -goto fail - -:findJavaFromJavaHome -set JAVA_HOME=%JAVA_HOME:"=% -set JAVA_EXE=%JAVA_HOME%/bin/java.exe - -if exist "%JAVA_EXE%" goto execute - -echo. 1>&2 -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 -echo. 1>&2 -echo Please set the JAVA_HOME variable in your environment to match the 1>&2 -echo location of your Java installation. 1>&2 - -goto fail - -:execute -@rem Setup the command line - - - -@rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* - -:end -@rem End local scope for the variables with windows NT shell -if %ERRORLEVEL% equ 0 goto mainEnd - -:fail -rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of -rem the _cmd.exe /c_ return code! -set EXIT_CODE=%ERRORLEVEL% -if %EXIT_CODE% equ 0 set EXIT_CODE=1 -if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% -exit /b %EXIT_CODE% - -:mainEnd -if "%OS%"=="Windows_NT" endlocal - -:omega +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/src/client/java/fr/sukikui/playercoordsapi/PlayerCoordsAPIClient.java b/src/client/java/fr/sukikui/playercoordsapi/PlayerCoordsAPIClient.java index f580bc7..ddcf9d5 100644 --- a/src/client/java/fr/sukikui/playercoordsapi/PlayerCoordsAPIClient.java +++ b/src/client/java/fr/sukikui/playercoordsapi/PlayerCoordsAPIClient.java @@ -3,7 +3,10 @@ import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpServer; import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents; import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientWorldEvents; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents; import net.minecraft.client.MinecraftClient; import net.minecraft.client.world.ClientWorld; import net.minecraft.entity.player.PlayerEntity; @@ -19,6 +22,7 @@ public class PlayerCoordsAPIClient implements ClientModInitializer { private HttpServer server; private boolean serverStarted = false; + private volatile PlayerSnapshot latestSnapshot; // Hardcoded port value - no longer in config private static final int PORT = 25565; @@ -31,6 +35,7 @@ public void onInitializeClient() { // Register tick event to constantly check config status ClientTickEvents.END_CLIENT_TICK.register(client -> { + updateSnapshot(client); boolean configEnabled = PlayerCoordsAPI.getConfig().enabled; // If enabled and server not started, start server @@ -44,9 +49,47 @@ public void onInitializeClient() { } }); + ClientWorldEvents.AFTER_CLIENT_WORLD_CHANGE.register((client, world) -> updateSnapshot(client)); + ClientPlayConnectionEvents.DISCONNECT.register((handler, client) -> clearSnapshot()); + ClientLifecycleEvents.CLIENT_STOPPING.register(client -> { + clearSnapshot(); + stopServer(); + }); + PlayerCoordsAPI.LOGGER.info("Registered config monitor"); } + private void updateSnapshot(MinecraftClient client) { + PlayerEntity player = client.player; + ClientWorld worldObj = client.world; + + if (player == null || worldObj == null) { + latestSnapshot = null; + return; + } + + RegistryEntry biomeEntry = worldObj.getBiome(player.getBlockPos()); + String biome = biomeEntry.getKey() + .map(key -> key.getValue().toString()) + .orElse("unknown"); + + latestSnapshot = new PlayerSnapshot( + player.getX(), + player.getY(), + player.getZ(), + player.getYaw(), + player.getPitch(), + worldObj.getRegistryKey().getValue().toString(), + biome, + player.getUuid().toString(), + player.getName().getString() + ); + } + + private void clearSnapshot() { + latestSnapshot = null; + } + private void startServer() { if (serverStarted) return; @@ -96,36 +139,9 @@ private void handleCoordsRequest(HttpExchange exchange) throws IOException { return; } - // Get player coordinates - MinecraftClient client = MinecraftClient.getInstance(); - PlayerEntity player = client.player; - ClientWorld worldObj = client.world; - - String responseText; - if (player != null && worldObj != null) { - double x = player.getX(); - double y = player.getY(); - double z = player.getZ(); - String world = worldObj.getRegistryKey().getValue().toString(); - - // Get biome information - RegistryEntry biomeEntry = worldObj.getBiome(player.getBlockPos()); - String biome = biomeEntry.getKey().orElseThrow().getValue().toString(); - - // Get player UUID and username - String uuid = player.getUuid().toString(); - String username = player.getName().getString(); - - // Fetch pitch/yaw, note they are floats internally - float yaw = player.getYaw(); - float pitch = player.getPitch(); - - // Format as JSON using US locale to ensure dots instead of commas - responseText = String.format(Locale.US, - "{\"x\": %.2f, \"y\": %.2f, \"z\": %.2f, \"yaw\": %.2f, \"pitch\": %.2f, \"world\": \"%s\", \"biome\": \"%s\", \"uuid\": \"%s\", \"username\": \"%s\"}", - x, y, z, yaw, pitch, world, biome, uuid, username - ); - sendResponse(exchange, 200, responseText); + PlayerSnapshot snapshot = latestSnapshot; + if (snapshot != null) { + sendResponse(exchange, 200, snapshot.toJson()); } else { sendResponse(exchange, 404, "{\"error\": \"Player not in world\"}"); } @@ -148,4 +164,23 @@ private void sendResponse(HttpExchange exchange, int statusCode, String response exchange.sendResponseHeaders(statusCode, -1); // No response body } } + + private record PlayerSnapshot( + double x, + double y, + double z, + float yaw, + float pitch, + String world, + String biome, + String uuid, + String username + ) { + private String toJson() { + return String.format(Locale.US, + "{\"x\": %.2f, \"y\": %.2f, \"z\": %.2f, \"yaw\": %.2f, \"pitch\": %.2f, \"world\": \"%s\", \"biome\": \"%s\", \"uuid\": \"%s\", \"username\": \"%s\"}", + x, y, z, yaw, pitch, world, biome, uuid, username + ); + } + } }