Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be out of scope - you can decide yourself. But i think we should set a user agent for all api calls. Also for the other where it is not required - if we do that the option can stay at the Main Config

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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> getAddressFromCoordinatesAsync(
@NotNull GeographicalCoordinate coordinates
) {
CompletableFuture<String> future = new CompletableFuture<>();

String url = BASE_URL + "reverse?format=jsonv2"
+ "&lat=" + coordinates.latitude()
+ "&lon=" + coordinates.longitude()
+ "&addressdetails=1"
+ "&accept-language=en";

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making that optional would be helpful. I would suggest that by default it uses local language and only with a flag uses en. If you have another suggestion/Opinion lmk

ChatHelper.getStandardComponent(false, url);
String userAgent = BuildTeamTools.getInstance().getMainConfig().userAgent();
API.getAsync(url, userAgent, new API.ApiResponseCallback() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requests are sent without shared rate limiting. Concurrent or repeated address lookups can exceed Nominatim’s public API limit of one request per second, leading to rejected requests or blocked access. Add server-wide throttling before sending requests; caching repeated lookups could also help

@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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)";

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should provide a default E-Mail which is also reachable.
Also I'm not sure if we should really let everything change, i think the version should reflect the plugin version and the name should be consistent. It might make sense to provide an option to override it but that should be a property or hidden by default option - I would not include that for now.
So only e-mail should be configurable and a comment should point out that it should be changed

}

Comment on lines +22 to +26

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in the wrong config and should be added/moved to the NavigationConfig or also be used by/for the other api's.

It should also only allow to set a different e-mail/comment (i think the part in () is called that). The BuildTeamTools/ part should be hardcoded and the version part should be the same version as on releases/plugin version.

@Comments("The main world using the custom BTE Dynmaxion projection")
default String earthWorld() {
return "world";
Expand Down
Loading