aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java
diff options
context:
space:
mode:
authorRoman / Nea <roman.graef@gmail.com>2021-12-30 14:37:02 +0100
committerGitHub <noreply@github.com>2021-12-30 14:37:02 +0100
commitb0b0b7567ec0656c60f2f3e4730c0edace353fb7 (patch)
tree0e2b23fe4c52b0242cefc8caee9ed887797542b2 /src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java
parentfc642887639d1918d68f4706e27ea59605a16fcb (diff)
downloadnotenoughupdates-b0b0b7567ec0656c60f2f3e4730c0edace353fb7.tar.gz
notenoughupdates-b0b0b7567ec0656c60f2f3e4730c0edace353fb7.tar.bz2
notenoughupdates-b0b0b7567ec0656c60f2f3e4730c0edace353fb7.zip
Adding support for more recipe types and forge recipes (#40)
* Foundations for support of different crafting recipe types. NeuRecipe is now a base class for a recipe which provides common concepts, such as inputs, outputs and a rendering task. GuiItemRecipe has been reworked to work with this new NeuRecipe. NeuManager now parses said recipes. This should be reworked to be a two step process (first register items, then register recipes). To keep compatibility with older repo versions, NeuRecipes are parse lenient and default to a crafting recipe. New recipes should be added in the `recipes` json field which is an array of json dictionaries, which have a `type` and other fields depending on the `type` of that recipe. This also adds support for having multiple recipes for a single item (e.g. uncrafting storage blocks). * Remove references in existing code * Recipe Generation * ring recipes * recipe generator v2 * quick forge * bugfixes and performance improvements * fix raw craft cost * reload hotm if you open the hotm tree inv * add myself to the changelog * replace quickforge formular with lookup table * do not crash anymore when opening recipes outside of skyblock * format coins differently * remove debug logs * change recipe generator so that it doesnt crash old versions
Diffstat (limited to 'src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java')
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java36
1 files changed, 31 insertions, 5 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java b/src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java
index 923b962a..3d313f25 100644
--- a/src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java
+++ b/src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java
@@ -6,12 +6,15 @@ import io.github.moulberry.notenoughupdates.NotEnoughUpdates;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
+import java.io.UnsupportedEncodingException;
import java.net.ConnectException;
import java.net.URL;
import java.net.URLConnection;
+import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
+import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
@@ -27,12 +30,17 @@ public class HypixelApi {
private final String[] myApiURLs = {"https://moulberry.codes/"};//, "http://moulberry.codes/", "http://51.79.51.21/"};//, "http://51.75.78.252/" };
private final Integer[] myApiSuccesses = {0, 0, 0, 0};
+ public CompletableFuture<JsonObject> getHypixelApiAsync(String apiKey, String method, HashMap<String, String> args) {
+ return getApiAsync(generateApiUrl(apiKey, method, args));
+ }
+
public void getHypixelApiAsync(String apiKey, String method, HashMap<String, String> args, Consumer<JsonObject> consumer) {
- getHypixelApiAsync(apiKey, method, args, consumer, () -> {});
+ getHypixelApiAsync(apiKey, method, args, consumer, () -> {
+ });
}
public void getHypixelApiAsync(String apiKey, String method, HashMap<String, String> args, Consumer<JsonObject> consumer, Runnable error) {
- getApiAsync(generateApiUrl(apiKey != null ? apiKey.trim() : null, method, args), consumer, error);
+ getApiAsync(generateApiUrl(apiKey, method, args), consumer, error);
}
private String getMyApiURL() {
@@ -61,6 +69,18 @@ public class HypixelApi {
}
}
+ public CompletableFuture<JsonObject> getApiAsync(String urlS) {
+ CompletableFuture<JsonObject> result = new CompletableFuture<>();
+ es.submit(() -> {
+ try {
+ result.complete(getApiSync(urlS));
+ } catch (Exception e) {
+ result.completeExceptionally(e);
+ }
+ });
+ return result;
+ }
+
public void getApiAsync(String urlS, Consumer<JsonObject> consumer, Runnable error) {
es.submit(() -> {
try {
@@ -134,16 +154,22 @@ public class HypixelApi {
}
public String generateApiUrl(String apiKey, String method, HashMap<String, String> args) {
- StringBuilder url = new StringBuilder("https://api.hypixel.net/" + method + (apiKey != null ? ("?key=" + apiKey.replace(" ", "")) : ""));
+ if (apiKey != null)
+ args.put("key", apiKey.trim().replace("-", ""));
+ StringBuilder url = new StringBuilder("https://api.hypixel.net/" + method);
boolean first = true;
for (Map.Entry<String, String> entry : args.entrySet()) {
- if (first && apiKey == null) {
+ if (first) {
url.append("?");
first = false;
} else {
url.append("&");
}
- url.append(entry.getKey()).append("=").append(entry.getValue());
+ try {
+ url.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8.name())).append("=")
+ .append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.name()));
+ } catch (UnsupportedEncodingException e) {
+ }
}
return url.toString();
}