|
1 | 1 | package com.bencodez.simpleapi.player; |
2 | 2 |
|
3 | | -import java.io.BufferedReader; |
4 | | -import java.io.InputStream; |
5 | | -import java.io.InputStreamReader; |
| 3 | +import java.io.IOException; |
6 | 4 | import java.lang.reflect.InvocationTargetException; |
7 | 5 | import java.lang.reflect.Method; |
8 | | -import java.net.HttpURLConnection; |
9 | | -import java.net.URL; |
| 6 | +import java.net.URI; |
| 7 | +import java.net.http.HttpClient; |
| 8 | +import java.net.http.HttpRequest; |
| 9 | +import java.net.http.HttpResponse; |
| 10 | +import java.time.Duration; |
| 11 | +import java.util.UUID; |
10 | 12 |
|
11 | 13 | import org.bukkit.Bukkit; |
12 | 14 | import org.bukkit.Location; |
@@ -63,27 +65,34 @@ public static boolean canInteract(Player p, Block clickedBlock, Action action, I |
63 | 65 | return true; |
64 | 66 | } |
65 | 67 |
|
66 | | - public static java.util.UUID fetchUUID(String playerName) throws Exception { |
67 | | - // Get response from Mojang API |
68 | | - URL url = new URL("https://api.mojang.com/users/profiles/minecraft/" + playerName); |
69 | | - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
70 | | - connection.connect(); |
| 68 | + /** |
| 69 | + * Fetches UUID from Mojang API. |
| 70 | + * |
| 71 | + * @param playerName player name |
| 72 | + * @return UUID or null if not found |
| 73 | + * @throws Exception if request fails |
| 74 | + */ |
| 75 | + public static UUID fetchUUID(String playerName) throws Exception { |
| 76 | + HttpClient client = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(5)).build(); |
71 | 77 |
|
72 | | - if (connection.getResponseCode() == 400) { |
73 | | - // plugin.debug("There is no player with the name \"" + playerName + "\"!"); |
| 78 | + HttpRequest request = HttpRequest.newBuilder() |
| 79 | + .uri(URI.create("https://api.mojang.com/users/profiles/minecraft/" + playerName)).GET() |
| 80 | + .timeout(Duration.ofSeconds(5)).build(); |
| 81 | + |
| 82 | + HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 83 | + |
| 84 | + if (response.statusCode() == 400 || response.statusCode() == 404) { |
74 | 85 | return null; |
75 | 86 | } |
76 | 87 |
|
77 | | - InputStream inputStream = connection.getInputStream(); |
78 | | - BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); |
79 | | - |
80 | | - // Parse JSON response and get UUID |
| 88 | + if (response.statusCode() < 200 || response.statusCode() >= 300) { |
| 89 | + throw new IOException("HTTP " + response.statusCode()); |
| 90 | + } |
81 | 91 |
|
82 | | - JsonElement element = JsonParser.parseReader(bufferedReader); |
| 92 | + JsonElement element = JsonParser.parseString(response.body()); |
83 | 93 | JsonObject object = element.getAsJsonObject(); |
84 | 94 | String uuidAsString = object.get("id").getAsString(); |
85 | 95 |
|
86 | | - // Return UUID |
87 | 96 | return parseUUIDFromString(uuidAsString); |
88 | 97 | } |
89 | 98 |
|
@@ -185,37 +194,38 @@ public static IServerHandle loadHandle() { |
185 | 194 | return serverHandle = new CraftBukkitHandle(); |
186 | 195 | } |
187 | 196 | } |
188 | | - |
189 | | - /** |
190 | | - * Gets the nearest player to the given location within the specified max distance. |
191 | | - * |
192 | | - * @param location The center location to search from. |
193 | | - * @param maxDistance The maximum distance (in blocks). |
194 | | - * @return The nearest Player, or null if none are within range. |
195 | | - */ |
196 | | - public static Player getNearestPlayer(Location location, double maxDistance) { |
197 | | - if (location == null || location.getWorld() == null) { |
198 | | - return null; |
199 | | - } |
200 | | - |
201 | | - Player nearest = null; |
202 | | - double maxDistanceSquared = maxDistance * maxDistance; |
203 | | - double closestSoFar = maxDistanceSquared; |
204 | | - |
205 | | - for (Player player : Bukkit.getOnlinePlayers()) { |
206 | | - if (player.getWorld() != location.getWorld()) { |
207 | | - continue; |
208 | | - } |
209 | | - |
210 | | - double distanceSquared = player.getLocation().distanceSquared(location); |
211 | | - if (distanceSquared <= closestSoFar) { |
212 | | - closestSoFar = distanceSquared; |
213 | | - nearest = player; |
214 | | - } |
215 | | - } |
216 | | - |
217 | | - return nearest; |
218 | | - } |
| 197 | + |
| 198 | + /** |
| 199 | + * Gets the nearest player to the given location within the specified max |
| 200 | + * distance. |
| 201 | + * |
| 202 | + * @param location The center location to search from. |
| 203 | + * @param maxDistance The maximum distance (in blocks). |
| 204 | + * @return The nearest Player, or null if none are within range. |
| 205 | + */ |
| 206 | + public static Player getNearestPlayer(Location location, double maxDistance) { |
| 207 | + if (location == null || location.getWorld() == null) { |
| 208 | + return null; |
| 209 | + } |
| 210 | + |
| 211 | + Player nearest = null; |
| 212 | + double maxDistanceSquared = maxDistance * maxDistance; |
| 213 | + double closestSoFar = maxDistanceSquared; |
| 214 | + |
| 215 | + for (Player player : Bukkit.getOnlinePlayers()) { |
| 216 | + if (player.getWorld() != location.getWorld()) { |
| 217 | + continue; |
| 218 | + } |
| 219 | + |
| 220 | + double distanceSquared = player.getLocation().distanceSquared(location); |
| 221 | + if (distanceSquared <= closestSoFar) { |
| 222 | + closestSoFar = distanceSquared; |
| 223 | + nearest = player; |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + return nearest; |
| 228 | + } |
219 | 229 |
|
220 | 230 | private static java.util.UUID parseUUIDFromString(String uuidAsString) { |
221 | 231 | String[] parts = { "0x" + uuidAsString.substring(0, 8), "0x" + uuidAsString.substring(8, 12), |
|
0 commit comments