Skip to content

Commit f029914

Browse files
Throw MojangApiException and IOException in HeadTexture#of and SkullBuilder constructor
Implements #54
1 parent 659e959 commit f029914

File tree

2 files changed

+49
-22
lines changed

2 files changed

+49
-22
lines changed

invui-core/src/main/java/xyz/xenondevs/invui/item/builder/SkullBuilder.java

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
import xyz.xenondevs.inventoryaccess.util.ReflectionRegistry;
1818
import xyz.xenondevs.inventoryaccess.util.ReflectionUtils;
1919
import xyz.xenondevs.invui.util.MojangApiUtils;
20+
import xyz.xenondevs.invui.util.MojangApiUtils.MojangApiException;
2021

22+
import java.io.IOException;
2123
import java.io.Serializable;
2224
import java.util.UUID;
2325
import java.util.concurrent.ExecutionException;
@@ -31,17 +33,21 @@ public final class SkullBuilder extends AbstractItemBuilder<SkullBuilder> {
3133
* Create a {@link SkullBuilder} of a {@link Player Player's} {@link UUID}.
3234
*
3335
* @param uuid The {@link UUID} of the skull owner.
36+
* @throws MojangApiException If the Mojang API returns an error.
37+
* @throws IOException If an I/O error occurs.
3438
*/
35-
public SkullBuilder(@NotNull UUID uuid) {
39+
public SkullBuilder(@NotNull UUID uuid) throws MojangApiException, IOException {
3640
this(HeadTexture.of(uuid));
3741
}
3842

3943
/**
4044
* Create a {@link SkullBuilder} with the {@link Player Player's} username.
4145
*
4246
* @param username The username of the skull owner.
47+
* @throws MojangApiException If the Mojang API returns an error.
48+
* @throws IOException If an I/O error occurs.
4349
*/
44-
public SkullBuilder(@NotNull String username) {
50+
public SkullBuilder(@NotNull String username) throws MojangApiException, IOException {
4551
this(HeadTexture.of(username));
4652
}
4753

@@ -128,9 +134,11 @@ public HeadTexture(@NotNull String textureValue) {
128134
*
129135
* @param offlinePlayer The skull owner.
130136
* @return The {@link HeadTexture} of that player.
137+
* @throws MojangApiException If the Mojang API returns an error.
138+
* @throws IOException If an I/O error occurs.
131139
* @see HeadTexture#of(UUID)
132140
*/
133-
public static HeadTexture of(@NotNull OfflinePlayer offlinePlayer) {
141+
public static @NotNull HeadTexture of(@NotNull OfflinePlayer offlinePlayer) throws MojangApiException, IOException {
134142
return of(offlinePlayer.getUniqueId());
135143
}
136144

@@ -144,20 +152,28 @@ public static HeadTexture of(@NotNull OfflinePlayer offlinePlayer) {
144152
*
145153
* @param playerName The username of the player.
146154
* @return The {@link HeadTexture} of that player.
155+
* @throws MojangApiException If the Mojang API returns an error.
156+
* @throws IOException If an I/O error occurs.
147157
* @see HeadTexture#of(UUID)
148158
*/
149159
@SuppressWarnings("deprecation")
150-
public static HeadTexture of(@NotNull String playerName) {
160+
public static @NotNull HeadTexture of(@NotNull String playerName) throws MojangApiException, IOException {
151161
if (Bukkit.getServer().getOnlineMode()) {
152162
// if the server is in online mode, the Minecraft UUID cache (usercache.json) can be used
153163
return of(Bukkit.getOfflinePlayer(playerName).getUniqueId());
154164
} else {
155165
// the server isn't in online mode - the UUID has to be retrieved from the Mojang API
156166
try {
157-
return of(uuidCache.get(playerName, () -> MojangApiUtils.getCurrentUUID(playerName)));
167+
return of(uuidCache.get(playerName, () -> MojangApiUtils.getCurrentUuid(playerName)));
158168
} catch (ExecutionException e) {
159-
e.printStackTrace();
160-
return null;
169+
Throwable cause = e.getCause();
170+
if (cause instanceof MojangApiException) {
171+
throw (MojangApiException) cause;
172+
} else if (cause instanceof IOException) {
173+
throw (IOException) cause;
174+
} else {
175+
throw new RuntimeException(cause);
176+
}
161177
}
162178
}
163179
}
@@ -169,13 +185,21 @@ public static HeadTexture of(@NotNull String playerName) {
169185
*
170186
* @param uuid The {@link UUID} of the skull owner.
171187
* @return The {@link HeadTexture} of that player.
188+
* @throws MojangApiException If the Mojang API returns an error.
189+
* @throws IOException If an I/O error occurs.
172190
*/
173-
public static HeadTexture of(@NotNull UUID uuid) {
191+
public static @NotNull HeadTexture of(@NotNull UUID uuid) throws MojangApiException, IOException {
174192
try {
175193
return new HeadTexture(textureCache.get(uuid, () -> MojangApiUtils.getSkinData(uuid, false)[0]));
176194
} catch (ExecutionException e) {
177-
e.printStackTrace();
178-
return null;
195+
Throwable cause = e.getCause();
196+
if (cause instanceof MojangApiException) {
197+
throw (MojangApiException) cause;
198+
} else if (cause instanceof IOException) {
199+
throw (IOException) cause;
200+
} else {
201+
throw new RuntimeException(cause);
202+
}
179203
}
180204
}
181205

invui-core/src/main/java/xyz/xenondevs/invui/util/MojangApiUtils.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@
1010
import java.net.URL;
1111
import java.util.UUID;
1212

13-
@SuppressWarnings("deprecation")
1413
public class MojangApiUtils {
1514

1615
private static final String SKIN_DATA_URL = "https://sessionserver.mojang.com/session/minecraft/profile/%s?unsigned=%s";
1716
private static final String NAME_AT_TIME_URL = "https://api.mojang.com/users/profiles/minecraft/%s?at=%s";
1817

19-
public static String[] getSkinData(UUID uuid, boolean requestSignature) throws IOException {
18+
public static String[] getSkinData(UUID uuid, boolean requestSignature) throws MojangApiException, IOException {
2019
String url = String.format(SKIN_DATA_URL, uuid, !requestSignature);
2120
Reader reader = new InputStreamReader(new URL(url).openConnection().getInputStream());
2221
JsonObject jsonObject = new JsonParser().parse(reader).getAsJsonObject();
@@ -33,11 +32,11 @@ public static String[] getSkinData(UUID uuid, boolean requestSignature) throws I
3332
return null;
3433
}
3534

36-
public static UUID getCurrentUUID(String name) throws IOException {
35+
public static UUID getCurrentUuid(String name) throws MojangApiException, IOException {
3736
return getUuidAtTime(name, System.currentTimeMillis() / 1000);
3837
}
3938

40-
public static UUID getUuidAtTime(String name, long timestamp) throws IOException {
39+
public static UUID getUuidAtTime(String name, long timestamp) throws MojangApiException, IOException {
4140
String url = String.format(NAME_AT_TIME_URL, name, timestamp);
4241
Reader reader = new InputStreamReader(new URL(url).openConnection().getInputStream());
4342
JsonObject jsonObject = new JsonParser().parse(reader).getAsJsonObject();
@@ -56,17 +55,21 @@ private static UUID fromUndashed(String undashed) {
5655
}
5756

5857
private static void checkForError(JsonObject jsonObject) throws MojangApiException {
59-
if (jsonObject.has("error") && jsonObject.has("errorMessage")) {
60-
if (jsonObject.has("errorMessage"))
61-
throw new MojangApiException(jsonObject.get("errorMessage").getAsString());
62-
else throw new MojangApiException("");
63-
}
58+
if (jsonObject.has("error"))
59+
throw new MojangApiException(jsonObject);
6460
}
6561

66-
public static class MojangApiException extends IOException {
62+
public static class MojangApiException extends Exception {
63+
64+
private final JsonObject response;
65+
66+
public MojangApiException(JsonObject response) {
67+
super(response.has("errorMessage") ? response.get("errorMessage").getAsString() : "");
68+
this.response = response;
69+
}
6770

68-
public MojangApiException(String message) {
69-
super(message);
71+
public JsonObject getResponse() {
72+
return response;
7073
}
7174

7275
}

0 commit comments

Comments
 (0)