1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
package de.cowtipper.cowlection.util;
import com.google.gson.JsonSyntaxException;
import com.mojang.util.UUIDTypeAdapter;
import de.cowtipper.cowlection.Cowlection;
import de.cowtipper.cowlection.chesttracker.data.HyBazaarData;
import de.cowtipper.cowlection.chesttracker.data.HyItemsData;
import de.cowtipper.cowlection.chesttracker.data.LowestBinsCache;
import de.cowtipper.cowlection.command.exception.ThrowingConsumer;
import de.cowtipper.cowlection.config.CredentialStorage;
import de.cowtipper.cowlection.data.Friend;
import de.cowtipper.cowlection.data.HyPlayerData;
import de.cowtipper.cowlection.data.HySkyBlockStats;
import de.cowtipper.cowlection.data.HyStalkingData;
import de.cowtipper.cowlection.error.ApiHttpErrorEvent;
import de.cowtipper.cowlection.error.ApiHttpErrorException;
import net.minecraft.util.EnumChatFormatting;
import net.minecraftforge.common.MinecraftForge;
import org.apache.http.HttpStatus;
import sun.security.provider.certpath.SunCertPathBuilderException;
import sun.security.validator.ValidatorException;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLHandshakeException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ApiUtils {
private static final String NAME_TO_UUID_URL = "https://api.mojang.com/users/profiles/minecraft/";
private static final String ONLINE_STATUS_URL = "https://api.hypixel.net/status?uuid=%s";
private static final String SKYBLOCK_STATS_URL = "https://api.hypixel.net/skyblock/profiles?uuid=%s";
private static final String BAZAAR_URL = "https://api.hypixel.net/skyblock/bazaar";
public static final String LOWEST_BINS = "https://moulberry.codes/lowestbin.json";
private static final String ITEMS_URL = "https://api.hypixel.net/resources/skyblock/items";
private static final String PLAYER_URL = "https://api.hypixel.net/player?uuid=%s";
private static final ExecutorService pool = Executors.newCachedThreadPool();
private ApiUtils() {
}
public static void fetchFriendData(String name, ThrowingConsumer<Friend> action) {
pool.execute(() -> action.accept(getFriend(name)));
}
private static Friend getFriend(String name) {
try (BufferedReader reader = makeApiCall(NAME_TO_UUID_URL + name, false)) {
if (reader == null) {
return Friend.FRIEND_NOT_FOUND;
} else {
return GsonUtils.fromJson(reader, Friend.class);
}
} catch (IOException | JsonSyntaxException e) {
handleApiException(e);
}
return null;
}
public static void fetchPlayerStatus(Friend friend, ThrowingConsumer<HyStalkingData> action) {
pool.execute(() -> action.accept(stalkPlayer(friend)));
}
private static HyStalkingData stalkPlayer(Friend friend) {
try (BufferedReader reader = makeApiCall(String.format(ONLINE_STATUS_URL, UUIDTypeAdapter.fromUUID(friend.getUuid())), true)) {
if (reader != null) {
return GsonUtils.fromJson(reader, HyStalkingData.class);
}
} catch (IOException | JsonSyntaxException e) {
handleApiException(e);
}
return null;
}
public static void fetchSkyBlockStats(Friend friend, ThrowingConsumer<HySkyBlockStats> action) {
pool.execute(() -> action.accept(stalkSkyBlockStats(friend)));
}
private static HySkyBlockStats stalkSkyBlockStats(Friend friend) {
try (BufferedReader reader = makeApiCall(String.format(SKYBLOCK_STATS_URL, UUIDTypeAdapter.fromUUID(friend.getUuid())), true)) {
if (reader != null) {
return GsonUtils.fromJson(reader, HySkyBlockStats.class);
}
} catch (IOException | JsonSyntaxException e) {
handleApiException(e);
}
return null;
}
public static void fetchBazaarData(ThrowingConsumer<HyBazaarData> action) {
pool.execute(() -> action.accept(getBazaarData()));
}
private static HyBazaarData getBazaarData() {
try (BufferedReader reader = makeApiCall(BAZAAR_URL, false)) {
if (reader != null) {
return GsonUtils.fromJson(reader, HyBazaarData.class);
}
} catch (IOException | JsonSyntaxException e) {
handleApiException(e);
}
return null;
}
public static void fetchLowestBins(ThrowingConsumer<LowestBinsCache> action) {
pool.execute(() -> action.accept(getLowestBins()));
}
private static LowestBinsCache getLowestBins() {
try (BufferedReader reader = makeApiCall(LOWEST_BINS, false)) {
if (reader != null) {
return GsonUtils.fromJson(reader, LowestBinsCache.class);
}
} catch (IOException | JsonSyntaxException e) {
handleApiException(e);
}
return new LowestBinsCache();
}
public static void fetchItemsData(ThrowingConsumer<HyItemsData> action) {
pool.execute(() -> action.accept(getItemsData()));
}
private static HyItemsData getItemsData() {
try (BufferedReader reader = makeApiCall(ITEMS_URL, false)) {
if (reader != null) {
return GsonUtils.fromJson(reader, HyItemsData.class);
}
} catch (IOException | JsonSyntaxException e) {
handleApiException(e);
}
return null;
}
public static void fetchHyPlayerDetails(Friend stalkedPlayer, ThrowingConsumer<HyPlayerData> action) {
pool.execute(() -> action.accept(stalkHyPlayer(stalkedPlayer)));
}
private static HyPlayerData stalkHyPlayer(Friend stalkedPlayer) {
try (BufferedReader reader = makeApiCall(String.format(PLAYER_URL, UUIDTypeAdapter.fromUUID(stalkedPlayer.getUuid())), true)) {
if (reader != null) {
return GsonUtils.fromJson(reader, HyPlayerData.class);
}
} catch (IOException | JsonSyntaxException e) {
handleApiException(e);
}
return null;
}
private static void handleApiException(Exception e) {
e.printStackTrace();
if (e instanceof ApiHttpErrorException) {
MinecraftForge.EVENT_BUS.post(new ApiHttpErrorEvent(e.getMessage(), ((ApiHttpErrorException) e).getUrl(), ((ApiHttpErrorException) e).wasUsingApiKey()));
}
}
private static BufferedReader makeApiCall(String url, boolean sendApiKey) throws IOException {
return makeApiCall(url, sendApiKey, CredentialStorage.moo);
}
private static BufferedReader makeApiCall(String url, boolean sendApiKey, String key) throws IOException {
if (sendApiKey && !CredentialStorage.isMooValid) {
throw new ApiHttpErrorException("Your current Hypixel API key is invalid. Use /moo apikey to manually set your existing API key.", url);
}
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
if (CredentialStorage.sslContext != null && connection instanceof HttpsURLConnection) {
((HttpsURLConnection) connection).setSSLSocketFactory(CredentialStorage.sslContext.getSocketFactory());
}
if (sendApiKey) {
connection.addRequestProperty("API-Key", key);
}
connection.setConnectTimeout(5000);
connection.setReadTimeout(8000);
connection.addRequestProperty("User-Agent", "Forge Mod " + Cowlection.MODNAME + "/" + Cowlection.VERSION + " (" + Cowlection.GITURL + ")");
connection.getResponseCode();
if (connection.getResponseCode() == HttpStatus.SC_NO_CONTENT) { // http status 204
return null;
} else if (connection.getResponseCode() == HttpStatus.SC_BAD_GATEWAY && url.startsWith("https://api.hypixel.net/")) { // http status 502 (cloudflare)
throw new ApiHttpErrorException("Couldn't contact Hypixel API (502 Bad Gateway). API might be down, check https://status.hypixel.net for info.", "https://status.hypixel.net");
} else if (connection.getResponseCode() == HttpStatus.SC_FORBIDDEN && sendApiKey && url.startsWith("https://api.hypixel.net/")) { // http status 403 Forbidden
Cowlection.getInstance().getMoo().setMooValidity(false);
throw new ApiHttpErrorException("Your current Hypixel API key seems to be invalid. Use /moo apikey to manually set your existing API key.", url);
} else if (connection.getResponseCode() == HttpStatus.SC_SERVICE_UNAVAILABLE) { // http status 503 Service Unavailable
throw new ApiHttpErrorException("Couldn't contact the API (503 Service unavailable). API might be down, or you might be blocked by Cloudflare, check if you can reach: " + url, url, sendApiKey);
} else if (connection.getResponseCode() == HttpStatus.SC_BAD_GATEWAY && url.startsWith("https://moulberry.codes/")) { // http status 502 (cloudflare)
throw new ApiHttpErrorException("Couldn't contact Moulberry's API (502 Bad Gateway). API might be down, check if " + LOWEST_BINS + " is reachable.", LOWEST_BINS);
} else {
BufferedReader reader;
InputStream errorStream = connection.getErrorStream();
if (errorStream != null) {
reader = new BufferedReader(new InputStreamReader(errorStream));
} else {
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
}
return reader;
}
} catch (SSLHandshakeException e) {
Throwable cause = e.getCause();
if (cause instanceof ValidatorException && cause.getCause() instanceof SunCertPathBuilderException) {
throw new ApiHttpErrorException("" + EnumChatFormatting.DARK_RED + EnumChatFormatting.BOLD + " ! "
+ EnumChatFormatting.RED + "Java is outdated and doesn't support Let's Encrypt certificates (out of the box). A game restart might fix this issue. If the problem persists, open a ticket on the Cowshed discord server.", Cowlection.INVITE_URL);
} else {
// not a newer https related issue, thus rethrow exception:
throw e;
}
}
}
}
|