From a4e36d4d9382d2595ec1e20f00c85383788c654b Mon Sep 17 00:00:00 2001 From: redstoner507 Date: Sat, 17 Oct 2026 10:57:50 +0200 Subject: [PATCH] feat(address): Switch to NominatimAPI for reverse geocoding and add User-Agent override to API function --- .../address/commands/AddressCommand.java | 3 +- .../modules/network/api/API.java | 15 ++++- .../modules/network/api/NominatimAPI.java | 66 +++++++++++++++++++ .../buildteamtools/utils/io/MainConfig.java | 5 ++ 4 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 src/main/java/net/buildtheearth/buildteamtools/modules/network/api/NominatimAPI.java diff --git a/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/address/commands/AddressCommand.java b/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/address/commands/AddressCommand.java index 4fcdd13a..e0cf8d8b 100644 --- a/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/address/commands/AddressCommand.java +++ b/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/address/commands/AddressCommand.java @@ -5,6 +5,7 @@ import net.buildtheearth.buildteamtools.BuildTeamTools; import net.buildtheearth.buildteamtools.modules.Module; import net.buildtheearth.buildteamtools.modules.common.commands.BttCommandManager; +import net.buildtheearth.buildteamtools.modules.network.api.NominatimAPI; import net.buildtheearth.buildteamtools.modules.network.api.PhotonAPI; import net.buildtheearth.buildteamtools.modules.network.model.Permissions; import net.buildtheearth.model.GeographicalCoordinate; @@ -90,7 +91,7 @@ private void handleGetCommand(@NonNull Player player) { loc.getZ() ); - PhotonAPI.getAddressFromCoordinatesAsync(geoCoord) + NominatimAPI.getAddressFromCoordinatesAsync(geoCoord) .thenAccept(address -> { if (address.isBlank()) { player.sendMessage( diff --git a/src/main/java/net/buildtheearth/buildteamtools/modules/network/api/API.java b/src/main/java/net/buildtheearth/buildteamtools/modules/network/api/API.java index 336d2671..db8be360 100644 --- a/src/main/java/net/buildtheearth/buildteamtools/modules/network/api/API.java +++ b/src/main/java/net/buildtheearth/buildteamtools/modules/network/api/API.java @@ -56,9 +56,18 @@ public static String get(String url) throws IOException { * @param url The URL to perform a get request on */ public static void getAsync(String url, ApiResponseCallback callback) { - Request request = new Request.Builder() - .url(url) - .build(); + getAsync(url, null, callback); + } + + public static void getAsync(String url, String userAgent, ApiResponseCallback callback) { + Request.Builder requestBuilder = new Request.Builder() + .url(url); + + if (userAgent != null && !userAgent.isBlank()) { + requestBuilder.header("User-Agent", userAgent); + } + + Request request = requestBuilder.build(); httpClient.newCall(request).enqueue(new Callback() { @Override diff --git a/src/main/java/net/buildtheearth/buildteamtools/modules/network/api/NominatimAPI.java b/src/main/java/net/buildtheearth/buildteamtools/modules/network/api/NominatimAPI.java new file mode 100644 index 00000000..35b45847 --- /dev/null +++ b/src/main/java/net/buildtheearth/buildteamtools/modules/network/api/NominatimAPI.java @@ -0,0 +1,66 @@ +package net.buildtheearth.buildteamtools.modules.network.api; + +import com.alpsbte.alpslib.utils.ChatHelper; +import net.buildtheearth.buildteamtools.BuildTeamTools; +import net.buildtheearth.model.GeographicalCoordinate; +import org.jetbrains.annotations.NotNull; +import org.json.simple.JSONObject; + +import java.io.IOException; +import java.util.concurrent.CompletableFuture; + +public class NominatimAPI { + private static final String BASE_URL = "https://nominatim.openstreetmap.org/"; + + public static @NotNull CompletableFuture getAddressFromCoordinatesAsync( + @NotNull GeographicalCoordinate coordinates + ) { + CompletableFuture future = new CompletableFuture<>(); + + String url = BASE_URL + "reverse?format=jsonv2" + + "&lat=" + coordinates.latitude() + + "&lon=" + coordinates.longitude() + + "&addressdetails=1" + + "&accept-language=en"; + ChatHelper.getStandardComponent(false, url); + String userAgent = BuildTeamTools.getInstance().getMainConfig().userAgent(); + API.getAsync(url, userAgent, new API.ApiResponseCallback() { + @Override + public void onResponse(String response) { + try { + String address = formatAddressFromResponse(response); + future.complete(address); + } catch (Exception e) { + future.completeExceptionally(e); + } + } + + @Override + public void onFailure(IOException e) { + future.completeExceptionally(e); + } + }); + + return future; + } + + private static String formatAddressFromResponse(String response) { + JSONObject jsonObject = API.createJSONObject(response); + + ChatHelper.logDebug( + "Response from Nominatim: %s", + jsonObject + ); + + Object displayName = jsonObject.get("display_name"); + + if (displayName == null || displayName.toString().isBlank()) { + ChatHelper.logError( + "No address data found for these coordinates." + ); + return ""; + } + + return displayName.toString(); + } +} diff --git a/src/main/java/net/buildtheearth/buildteamtools/utils/io/MainConfig.java b/src/main/java/net/buildtheearth/buildteamtools/utils/io/MainConfig.java index 88f798fc..c05ba515 100644 --- a/src/main/java/net/buildtheearth/buildteamtools/utils/io/MainConfig.java +++ b/src/main/java/net/buildtheearth/buildteamtools/utils/io/MainConfig.java @@ -19,6 +19,11 @@ default String apiKey() { return "00000000-0000-0000-0000-000000000000"; } + @Comments("User Agent for NominatimAPI") + default String userAgent() { + return "BuildTeamTools/1.0 (contact: email@example.com)"; + } + @Comments("The main world using the custom BTE Dynmaxion projection") default String earthWorld() { return "world";