diff --git a/Example/src/main/java/net/hypixel/example/GameCountsExample.java b/Example/src/main/java/net/hypixel/example/GameCountsExample.java deleted file mode 100644 index b588dfa3..00000000 --- a/Example/src/main/java/net/hypixel/example/GameCountsExample.java +++ /dev/null @@ -1,8 +0,0 @@ -package net.hypixel.example; - -public class GameCountsExample { - public static void main(String[] args) { - ExampleUtil.API.getGameCounts().whenComplete(ExampleUtil.getTestConsumer()); - ExampleUtil.await(); - } -} diff --git a/Example/src/main/java/net/hypixel/example/WatchdogStatsExample.java b/Example/src/main/java/net/hypixel/example/WatchdogStatsExample.java deleted file mode 100644 index 00814e0f..00000000 --- a/Example/src/main/java/net/hypixel/example/WatchdogStatsExample.java +++ /dev/null @@ -1,8 +0,0 @@ -package net.hypixel.example; - -public class WatchdogStatsExample { - public static void main(String[] args) { - ExampleUtil.API.getWatchdogStats().whenComplete(ExampleUtil.getTestConsumer()); - ExampleUtil.await(); - } -} diff --git a/Example/src/main/java/net/hypixel/example/skyblock/GetBazaarExample.java b/Example/src/main/java/net/hypixel/example/skyblock/GetBazaarExample.java deleted file mode 100644 index 37b7b70b..00000000 --- a/Example/src/main/java/net/hypixel/example/skyblock/GetBazaarExample.java +++ /dev/null @@ -1,10 +0,0 @@ -package net.hypixel.example.skyblock; - -import net.hypixel.example.ExampleUtil; - -public class GetBazaarExample { - public static void main(String[] args) { - ExampleUtil.API.getBazaar().whenComplete(ExampleUtil.getTestConsumer()); - ExampleUtil.await(); - } -} diff --git a/Java/pom.xml b/Java/pom.xml deleted file mode 100644 index 241c1991..00000000 --- a/Java/pom.xml +++ /dev/null @@ -1,68 +0,0 @@ - - - 4.0.0 - - net.hypixel - HypixelAPI - 3.0.0 - - - - com.google.code.gson - gson - 2.3 - - - org.apache.httpcomponents - httpclient - 4.4 - - - - - - Hypixel - https://repo.hypixel.net/repository/Hypixel/ - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 2.3.2 - - UTF-8 - 1.8 - 1.8 - - - - org.apache.maven.plugins - maven-source-plugin - 3.0.1 - - - attach-sources - - jar - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 3.0.1 - - none - true - - - - - - diff --git a/Java/src/main/java/net/hypixel/api/HypixelAPI.java b/Java/src/main/java/net/hypixel/api/HypixelAPI.java deleted file mode 100644 index bb5835a9..00000000 --- a/Java/src/main/java/net/hypixel/api/HypixelAPI.java +++ /dev/null @@ -1,312 +0,0 @@ -package net.hypixel.api; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonObject; -import net.hypixel.api.adapters.BoostersTypeAdapterFactory; -import net.hypixel.api.adapters.DateTimeTypeAdapter; -import net.hypixel.api.adapters.GameTypeTypeAdapter; -import net.hypixel.api.adapters.UUIDTypeAdapter; -import net.hypixel.api.exceptions.APIThrottleException; -import net.hypixel.api.exceptions.HypixelAPIException; -import net.hypixel.api.reply.*; -import net.hypixel.api.reply.skyblock.*; -import net.hypixel.api.util.GameType; -import net.hypixel.api.util.ResourceType; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.HttpClientBuilder; -import org.apache.http.util.EntityUtils; - -import java.time.ZonedDateTime; -import java.util.UUID; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -public class HypixelAPI { - - private static final Gson GSON = new GsonBuilder() - .registerTypeAdapter(UUID.class, new UUIDTypeAdapter()) - .registerTypeAdapter(GameType.class, new GameTypeTypeAdapter()) - .registerTypeAdapter(ZonedDateTime.class, new DateTimeTypeAdapter()) - - .registerTypeAdapterFactory(new BoostersTypeAdapterFactory<>(BoostersReply.Booster.class)) - - .create(); - private static final String BASE_URL = "https://api.hypixel.net/"; - - private final UUID apiKey; - - private final ExecutorService executorService; - private final HttpClient httpClient; - - public HypixelAPI(UUID apiKey) { - this.apiKey = apiKey; - - this.executorService = Executors.newCachedThreadPool(); - this.httpClient = HttpClientBuilder.create().build(); - } - - /** - * Shuts down the internal executor service - */ - public void shutdown() { - executorService.shutdown(); - } - - /** - * @return currently set API key - */ - public UUID getApiKey() { - return apiKey; - } - - public CompletableFuture getBoosters() { - return get(BoostersReply.class, "boosters"); - } - - public CompletableFuture getLeaderboards() { - return get(LeaderboardsReply.class, "leaderboards"); - } - - public CompletableFuture getWatchdogStats() { - return get(WatchdogStatsReply.class, "watchdogStats"); - } - - /** - * This is now included inside {@link HypixelAPI#getGameCounts()} - */ - @Deprecated - public CompletableFuture getPlayerCount() { - return get(PlayerCountReply.class, "playerCount"); - } - - public CompletableFuture getPlayerByUuid(UUID player) { - return get(PlayerReply.class, "player", "uuid", player); - } - - /** - * @param player uuid of a player in string format, can be both dashed or undashed. - * @return the future - */ - public CompletableFuture getPlayerByUuid(String player) { - return get(PlayerReply.class, "player", "uuid", player); - } - - @Deprecated - public CompletableFuture getPlayerByName(String player) { - return get(PlayerReply.class, "player", "name", player); - } - - public CompletableFuture getFriends(UUID player) { - return get(FriendsReply.class, "friends", "uuid", player); - } - - /** - * @param player uuid of a player in string format, can be both dashed or undashed. - * @return the future - */ - public CompletableFuture getFriends(String player) { - return get(FriendsReply.class, "friends", "uuid", player); - } - - public CompletableFuture getGuildByPlayer(UUID player) { - return get(GuildReply.class, "guild", "player", player); - } - - /** - * @param player uuid of a player in string format, can be both dashed or undashed. - * @return the future - */ - public CompletableFuture getGuildByPlayer(String player) { - return get(GuildReply.class, "guild", "player", player); - } - - public CompletableFuture getGuildByName(String name) { - return get(GuildReply.class, "guild", "name", name); - } - - /** - * @param id mongo id hex string - * @return the future - */ - public CompletableFuture getGuildById(String id) { - return get(GuildReply.class, "guild", "id", id); - } - - /** - * You can directly get the guild using {@link HypixelAPI#getGuildByPlayer(UUID)} - */ - @Deprecated - public CompletableFuture findGuildByPlayer(UUID player) { - return get(FindGuildReply.class, "findGuild", "byUuid", player); - } - - /** - * You can directly get the guild using {@link HypixelAPI#getGuildByPlayer(String)} - */ - @Deprecated - public CompletableFuture findGuildByPlayer(String player) { - return get(FindGuildReply.class, "findGuild", "byUuid", player); - } - - /** - * You can directly get the guild using {@link HypixelAPI#getGuildByName(String)})} - */ - @Deprecated - public CompletableFuture findGuildByName(String name) { - return get(GuildReply.class, "findGuild", "byName", name); - } - - public CompletableFuture getKey() { - return get(KeyReply.class, "key"); - } - - public CompletableFuture getGameCounts() { - return get(GameCountsReply.class, "gameCounts"); - } - - public CompletableFuture getSkyBlockProfile(String profile) { - return get(SkyBlockProfileReply.class, "skyblock/profile", "profile", profile); - } - - public CompletableFuture getSkyBlockNews() { - return get(SkyBlockNewsReply.class, "skyblock/news"); - } - - public CompletableFuture getSkyBlockAuctions(int page) { - return get(SkyBlockAuctionsReply.class, "skyblock/auctions", "page", page); - } - - /** - * Gets the current status of the player with information about the server they are in - * at that moment. - * In case the person is in limbo, result will be the last known server - * - * @param uuid of player - * @return CompletableFuture with status reply - */ - public CompletableFuture getStatus(UUID uuid) { - return get(StatusReply.class, "status", "uuid", uuid); - } - - /** - * Gets up to 100 of the player's most recently played games. Games are removed from this list after 3 days. - * @param uuid of player - * @return CompletableFuture with recentGames reply - */ - public CompletableFuture getRecentGames(UUID uuid) { - return get(RecentGamesReply.class, "recentGames", "uuid", uuid); - } - - /** - * Retrieve resources which don't change often. - * - * @param resource to be requested - * @return CompletableFuture with resource reply - */ - public CompletableFuture getResource(ResourceType resource) { - return getResource(resource.getPath()); - } - - /** - * Requests information about products in bazaar. - * - * @return CompletableFuture with BazaarReply - */ - public CompletableFuture getBazaar() { - return get(BazaarReply.class, "skyblock/bazaar"); - } - - public CompletableFuture getResource(String resource) { - return requestResource(resource); - } - - /** - * Execute Request asynchronously, executes Callback when finished - * - * @param request Request to get - */ - // TODO use a map of string to object? - private CompletableFuture get(Class clazz, String request, Object... params) { - CompletableFuture future = new CompletableFuture<>(); - try { - if (params.length % 2 != 0) - throw new IllegalArgumentException("Need both key and value for parameters"); - - StringBuilder url = new StringBuilder(BASE_URL); - - url.append(request); - url.append("?key=").append(apiKey); - - for (int i = 0; i < params.length - 1; i += 2) { - url.append("&").append(params[i]).append("=").append(params[i + 1]); - } - - executorService.submit(() -> { - try { - R response = httpClient.execute(new HttpGet(url.toString()), obj -> { - String content = EntityUtils.toString(obj.getEntity(), "UTF-8"); - if (clazz == ResourceReply.class) { - return (R) new ResourceReply(GSON.fromJson(content, JsonObject.class)); - } else { - return GSON.fromJson(content, clazz); - } - }); - - checkReply(response); - - future.complete(response); - } catch (Throwable t) { - future.completeExceptionally(t); - } - }); - } catch (Throwable throwable) { - future.completeExceptionally(throwable); - } - return future; - } - - private CompletableFuture requestResource(String resource) { - CompletableFuture future = new CompletableFuture<>(); - try { - StringBuilder url = new StringBuilder(BASE_URL); - url.append("resources/").append(resource); - - executorService.submit(() -> { - try { - ResourceReply response = httpClient.execute(new HttpGet(url.toString()), obj -> { - String content = EntityUtils.toString(obj.getEntity(), "UTF-8"); - return new ResourceReply(GSON.fromJson(content, JsonObject.class)); - }); - - checkReply(response); - - future.complete(response); - } catch (Throwable t) { - future.completeExceptionally(t); - } - }); - } catch (Throwable throwable) { - future.completeExceptionally(throwable); - } - return future; - } - - /** - * Checks reply and throws appropriate exceptions based on it's content - * - * @param reply The reply to check - * @param The class of the reply - */ - private void checkReply(T reply) { - if (reply != null) { - if (reply.isThrottle()) { - throw new APIThrottleException(); - } else if (!reply.isSuccess()) { - throw new HypixelAPIException(reply.getCause()); - } - } - } -} diff --git a/Java/src/main/java/net/hypixel/api/exceptions/APIThrottleException.java b/Java/src/main/java/net/hypixel/api/exceptions/APIThrottleException.java deleted file mode 100644 index d07c3828..00000000 --- a/Java/src/main/java/net/hypixel/api/exceptions/APIThrottleException.java +++ /dev/null @@ -1,7 +0,0 @@ -package net.hypixel.api.exceptions; - -public class APIThrottleException extends HypixelAPIException { - public APIThrottleException() { - super("You have passed the API throttle limit!"); - } -} diff --git a/Java/src/main/java/net/hypixel/api/exceptions/HypixelAPIException.java b/Java/src/main/java/net/hypixel/api/exceptions/HypixelAPIException.java deleted file mode 100644 index 7507fb02..00000000 --- a/Java/src/main/java/net/hypixel/api/exceptions/HypixelAPIException.java +++ /dev/null @@ -1,24 +0,0 @@ -package net.hypixel.api.exceptions; - -@SuppressWarnings("unused") -public class HypixelAPIException extends RuntimeException { - - public HypixelAPIException() { - } - - public HypixelAPIException(String message) { - super(message); - } - - public HypixelAPIException(String message, Throwable cause) { - super(message, cause); - } - - public HypixelAPIException(Throwable cause) { - super(cause); - } - - public HypixelAPIException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { - super(message, cause, enableSuppression, writableStackTrace); - } -} diff --git a/Java/src/main/java/net/hypixel/api/reply/PlayerCountReply.java b/Java/src/main/java/net/hypixel/api/reply/PlayerCountReply.java deleted file mode 100644 index fa086db2..00000000 --- a/Java/src/main/java/net/hypixel/api/reply/PlayerCountReply.java +++ /dev/null @@ -1,20 +0,0 @@ -package net.hypixel.api.reply; - -/** - * This is now included inside {@link GameCountsReply} - */ -@Deprecated -public class PlayerCountReply extends AbstractReply { - private int playerCount; - - public int getPlayerCount() { - return playerCount; - } - - @Override - public String toString() { - return "PlayerCountReply{" + - "playerCount=" + playerCount + - "} " + super.toString(); - } -} diff --git a/LICENSE b/LICENSE index cc788c28..84355340 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2019 Hypixel Inc. +Copyright (c) 2021 Hypixel Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 54828b0b..c5b4486e 100644 --- a/README.md +++ b/README.md @@ -10,13 +10,16 @@ documentation can be found in the code. ### GitHub Issues -Github issues should only be used to report bugs. Everything else should either be in Github discussions or use the +GitHub issues should only be used to report bugs. Everything else should either be in GitHub discussions or use the Hypixel [Code Creations](https://hypixel.net/forums/code-creations.65/) forum. ### Usage -You can use this API as a dependency via the public Hypixel maven repo. +You can use this API as a dependency via the public Hypixel maven repo. You can also use +the [Example Code](https://github.com/HypixelDev/PublicAPI/tree/master/hypixel-api-example) as a good starting point. + +#### Hypixel Maven Repo ```xml Hypixel @@ -24,47 +27,29 @@ You can use this API as a dependency via the public Hypixel maven repo. ``` -```xml - - net.hypixel - HypixelAPI - 3.0.0 - -``` - -This repo can also be used with Gradle in the following form. - +This repo can also be used with Gradle. ```gradle repositories { maven { url 'https://repo.hypixel.net/repository/Hypixel/' } } ``` -```gradle -dependencies { - implementation 'net.hypixel:HypixelAPI:3.0.0' -} -``` +#### Transports -### Dependencies +We include two built in options communicating with the Hypixel API, you can include either of these or even include the +core API directly and create your own instance of HypixelHTTPClient. -The Hypixel PublicAPI has the following dependencies: +* [Apache HttpClient Transport](hypixel-api-transport-apache/README.md) +* [Unirest Java Transport](hypixel-api-transport-unirest/README.md) -* Google Gson library -* Apache HttpClient - -### Query Limitations +### Dependencies -The API server has a request limit of 120 queries per minute. Any abuse of the API or intentions to bypass this limit ( -such as with multiple API keys) will lead to your API key being reset or banned. +The Hypixel API Core implementation has the following dependencies: -If you require a higher limit than the above you can open a support ticket at https://support.hypixel.net and provide -your use case and why you require a higher limit. +* [Google Gson library - 2.8.6](https://mvnrepository.com/artifact/com.google.code.gson/gson) -### Obtaining an API Key +Transports will also have dependencies where required. -You can obtain an API key by joining ```mc.hypixel.net``` with a valid Minecraft account and running the /api command. -You will then be assigned a unique key that is to remain **private**. ### Contributing diff --git a/hypixel-api-core/pom.xml b/hypixel-api-core/pom.xml new file mode 100644 index 00000000..300cd7d6 --- /dev/null +++ b/hypixel-api-core/pom.xml @@ -0,0 +1,24 @@ + + + 4.0.0 + + net.hypixel + hypixel-api-core + 4.0.0-SNAPSHOT + + + 1.8 + 1.8 + + + + + com.google.code.gson + gson + 2.8.6 + + + + diff --git a/hypixel-api-core/src/main/java/net/hypixel/api/HypixelAPI.java b/hypixel-api-core/src/main/java/net/hypixel/api/HypixelAPI.java new file mode 100644 index 00000000..8483187a --- /dev/null +++ b/hypixel-api-core/src/main/java/net/hypixel/api/HypixelAPI.java @@ -0,0 +1,296 @@ +package net.hypixel.api; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; +import com.google.gson.JsonSyntaxException; +import net.hypixel.api.adapters.*; +import net.hypixel.api.data.type.GameType; +import net.hypixel.api.data.type.ServerType; +import net.hypixel.api.exceptions.BadResponseException; +import net.hypixel.api.exceptions.BadStatusCodeException; +import net.hypixel.api.http.HTTPQueryParams; +import net.hypixel.api.http.HypixelHttpClient; +import net.hypixel.api.http.HypixelHttpResponse; +import net.hypixel.api.reply.*; +import net.hypixel.api.reply.skyblock.*; +import net.hypixel.api.util.ResourceType; + +import java.time.ZonedDateTime; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; + +public class HypixelAPI { + + private static final Gson GSON = new GsonBuilder() + .registerTypeAdapter(UUID.class, new UUIDTypeAdapter()) + .registerTypeAdapter(GameType.class, new GameTypeTypeAdapter()) + .registerTypeAdapter(ServerType.class, new ServerTypeTypeAdapter()) + .registerTypeAdapter(ZonedDateTime.class, new DateTimeTypeAdapter()) + .registerTypeAdapterFactory(new BoostersTypeAdapterFactory<>(BoostersReply.Booster.class)) + .create(); + private static final String BASE_URL = "https://api.hypixel.net/"; + + private final HypixelHttpClient httpClient; + + /** + * @param httpClient a {@link HypixelHttpClient} that implements the HTTP behaviour for communicating with the API + */ + public HypixelAPI(HypixelHttpClient httpClient) { + this.httpClient = httpClient; + } + + /** + * Shuts down the {@link HypixelHttpClient} + */ + public void shutdown() { + httpClient.shutdown(); + } + + public CompletableFuture getBoosters() { + return get(BoostersReply.class, "boosters"); + } + + public CompletableFuture getLeaderboards() { + return get(LeaderboardsReply.class, "leaderboards"); + } + + public CompletableFuture getPunishmentStats() { + return get(PunishmentStatsReply.class, "punishmentstats"); + } + + /** + * @param player uuid of a player + * @return {@link CompletableFuture} containing {@link PlayerReply} + */ + public CompletableFuture getPlayerByUuid(UUID player) { + return get(PlayerReply.class, "player", + HTTPQueryParams.create() + .add("uuid", player) + ); + } + + /** + * @param player uuid of a player in string format, can be both dashed or undashed. + * @return {@link CompletableFuture} containing {@link PlayerReply} + */ + public CompletableFuture getPlayerByUuid(String player) { + return get(PlayerReply.class, "player", + HTTPQueryParams.create() + .add("uuid", player) + ); + } + + /** + * @param player the minecraft username of the player. + * @return {@link CompletableFuture} containing {@link PlayerReply} + * @deprecated While this method should continue functioning we recommend using the Mojang API for requesting UUID's by username. + * See issue #249 + * This endpoint is also subject to limiting requests of the same username in a short period of time. + */ + @Deprecated + public CompletableFuture getPlayerByName(String player) { + return get(PlayerReply.class, "player", + HTTPQueryParams.create() + .add("name", player) + ); + } + + public CompletableFuture getFriends(UUID player) { + return get(FriendsReply.class, "friends", + HTTPQueryParams.create() + .add("uuid", player) + ); + } + + /** + * @param player uuid of a player in string format, can be both dashed or undashed + * @return {@link CompletableFuture} containing {@link FriendsReply} + */ + public CompletableFuture getFriends(String player) { + return get(FriendsReply.class, "friends", + HTTPQueryParams.create() + .add("uuid", player) + ); + } + + /** + * @param player uuid of a player + * @return {@link CompletableFuture} containing {@link GuildReply} + */ + public CompletableFuture getGuildByPlayer(UUID player) { + return get(GuildReply.class, "guild", + HTTPQueryParams.create() + .add("player", player) + ); + } + + /** + * @param player uuid of a player in string format, can be both dashed or undashed + * @return {@link CompletableFuture} containing {@link GuildReply} + */ + public CompletableFuture getGuildByPlayer(String player) { + return get(GuildReply.class, "guild", + HTTPQueryParams.create() + .add("player", player) + ); + } + + /** + * @param name the name of the guild + * @return {@link CompletableFuture} containing {@link GuildReply} + */ + public CompletableFuture getGuildByName(String name) { + return get(GuildReply.class, "guild", + HTTPQueryParams.create() + .add("name", name) + ); + } + + /** + * @param id mongo id hex string + * @return {@link CompletableFuture} containing {@link GuildReply} + */ + public CompletableFuture getGuildById(String id) { + return get(GuildReply.class, "guild", + HTTPQueryParams.create() + .add("id", id) + ); + } + + public CompletableFuture getKey() { + return get(KeyReply.class, "key"); + } + + public CompletableFuture getCounts() { + return get(CountsReply.class, "counts"); + } + + /** + * Gets the current status of the player with information about the server they are in + * at that moment. + * In case the person is in limbo, result will be the last known server + * + * @param uuid of player + * @return {@link CompletableFuture} containing {@link StatusReply} + */ + public CompletableFuture getStatus(UUID uuid) { + return get(StatusReply.class, "status", + HTTPQueryParams.create() + .add("uuid", uuid) + ); + } + + /** + * Gets up to 100 of the player's most recently played games. Games are removed from this list after 3 days. + * + * @param uuid of player + * @return {@link CompletableFuture} containing {@link RecentGamesReply} + */ + public CompletableFuture getRecentGames(UUID uuid) { + return get(RecentGamesReply.class, "recentGames", + HTTPQueryParams.create() + .add("uuid", uuid) + ); + } + + /** + * Retrieve resources which don't change often. + * + * @param resource to be requested + * @return {@link CompletableFuture} containing {@link ResourceReply} + */ + public CompletableFuture getResource(ResourceType resource) { + return getResource(resource.getPath()); + } + + public CompletableFuture getResource(String resource) { + return requestResource(resource); + } + + public CompletableFuture getSkyBlockProfile(String profile) { + return get(SkyBlockProfileReply.class, "skyblock/profile", + HTTPQueryParams.create() + .add("profile", profile) + ); + } + + public CompletableFuture getSkyBlockNews() { + return get(SkyBlockNewsReply.class, "skyblock/news"); + } + + public CompletableFuture getSkyBlockAuctions(int page) { + return get(SkyBlockAuctionsReply.class, "skyblock/auctions", + HTTPQueryParams.create() + .add("page", page) + ); + } + + /** + * Requests information about products in bazaar. + * + * @return {@link CompletableFuture} containing {@link SkyBlockBazaarReply} + */ + public CompletableFuture getSkyBlockBazaar() { + return get(SkyBlockBazaarReply.class, "skyblock/bazaar"); + } + + private CompletableFuture get(Class clazz, String request) { + return get(clazz, request, null); + } + + private CompletableFuture get(Class clazz, String request, HTTPQueryParams params) { + String url = BASE_URL + request; + if (params != null) { + url = params.getAsQueryString(url); + } + return httpClient.makeAuthenticatedRequest(url) + .thenApply(this::checkResponse) + .thenApply(response -> { + if (clazz == ResourceReply.class) { + return checkReply((R) new ResourceReply(GSON.fromJson(response.getBody(), JsonObject.class))); + } + return checkReply(GSON.fromJson(response.getBody(), clazz)); + }); + } + + private CompletableFuture requestResource(String resource) { + return httpClient.makeRequest(BASE_URL + "resources/" + resource) + .thenApply(this::checkResponse) + .thenApply(response -> checkReply(new ResourceReply(GSON.fromJson(response.getBody(), JsonObject.class)))); + } + + /** + * Checks the status of the response and throws an exception if needed + */ + private HypixelHttpResponse checkResponse(HypixelHttpResponse response) { + if (response.getStatusCode() == 200) { + return response; + } + + String cause; + try { + cause = GSON.fromJson(response.getBody(), JsonObject.class).get("cause").getAsString(); + } catch (JsonSyntaxException ignored) { + cause = "Unknown (body is not json)"; + } + + throw new BadStatusCodeException(response.getStatusCode(), cause); + } + + /** + * Checks reply and throws appropriate exceptions based on it's content + * + * @param reply The reply to check + * @param The class of the reply + * @return the same object that was provided for cleaner usage + */ + private T checkReply(T reply) { + if (reply != null) { + if (!reply.isSuccess()) { + throw new BadResponseException(reply.getCause()); + } + } + return reply; + } +} diff --git a/Java/src/main/java/net/hypixel/api/adapters/BoostersTypeAdapterFactory.java b/hypixel-api-core/src/main/java/net/hypixel/api/adapters/BoostersTypeAdapterFactory.java similarity index 100% rename from Java/src/main/java/net/hypixel/api/adapters/BoostersTypeAdapterFactory.java rename to hypixel-api-core/src/main/java/net/hypixel/api/adapters/BoostersTypeAdapterFactory.java diff --git a/Java/src/main/java/net/hypixel/api/adapters/CustomizedTypeAdapterFactory.java b/hypixel-api-core/src/main/java/net/hypixel/api/adapters/CustomizedTypeAdapterFactory.java similarity index 100% rename from Java/src/main/java/net/hypixel/api/adapters/CustomizedTypeAdapterFactory.java rename to hypixel-api-core/src/main/java/net/hypixel/api/adapters/CustomizedTypeAdapterFactory.java diff --git a/Java/src/main/java/net/hypixel/api/adapters/DateTimeTypeAdapter.java b/hypixel-api-core/src/main/java/net/hypixel/api/adapters/DateTimeTypeAdapter.java similarity index 100% rename from Java/src/main/java/net/hypixel/api/adapters/DateTimeTypeAdapter.java rename to hypixel-api-core/src/main/java/net/hypixel/api/adapters/DateTimeTypeAdapter.java diff --git a/Java/src/main/java/net/hypixel/api/adapters/GameTypeTypeAdapter.java b/hypixel-api-core/src/main/java/net/hypixel/api/adapters/GameTypeTypeAdapter.java similarity index 95% rename from Java/src/main/java/net/hypixel/api/adapters/GameTypeTypeAdapter.java rename to hypixel-api-core/src/main/java/net/hypixel/api/adapters/GameTypeTypeAdapter.java index 4a12ef04..bcd36941 100644 --- a/Java/src/main/java/net/hypixel/api/adapters/GameTypeTypeAdapter.java +++ b/hypixel-api-core/src/main/java/net/hypixel/api/adapters/GameTypeTypeAdapter.java @@ -1,7 +1,7 @@ package net.hypixel.api.adapters; import com.google.gson.*; -import net.hypixel.api.util.GameType; +import net.hypixel.api.data.type.GameType; import java.lang.reflect.Type; diff --git a/hypixel-api-core/src/main/java/net/hypixel/api/adapters/ServerTypeTypeAdapter.java b/hypixel-api-core/src/main/java/net/hypixel/api/adapters/ServerTypeTypeAdapter.java new file mode 100644 index 00000000..ea89251e --- /dev/null +++ b/hypixel-api-core/src/main/java/net/hypixel/api/adapters/ServerTypeTypeAdapter.java @@ -0,0 +1,26 @@ +package net.hypixel.api.adapters; + +import com.google.gson.*; +import net.hypixel.api.data.type.GameType; +import net.hypixel.api.data.type.ServerType; + +import java.lang.reflect.Type; + +public class ServerTypeTypeAdapter implements JsonDeserializer, JsonSerializer { + + @Override + public JsonElement serialize(ServerType src, Type typeOfSrc, JsonSerializationContext context) { + return new JsonPrimitive(src.name()); + } + + @Override + public ServerType deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + String raw = json.getAsString(); + try { + return GameType.fromId(Integer.parseInt(raw)); + } catch (NumberFormatException ignored) { + } + return ServerType.valueOf(raw); + } + +} diff --git a/Java/src/main/java/net/hypixel/api/adapters/UUIDTypeAdapter.java b/hypixel-api-core/src/main/java/net/hypixel/api/adapters/UUIDTypeAdapter.java similarity index 100% rename from Java/src/main/java/net/hypixel/api/adapters/UUIDTypeAdapter.java rename to hypixel-api-core/src/main/java/net/hypixel/api/adapters/UUIDTypeAdapter.java diff --git a/Java/src/main/java/net/hypixel/api/util/GameType.java b/hypixel-api-core/src/main/java/net/hypixel/api/data/type/GameType.java similarity index 93% rename from Java/src/main/java/net/hypixel/api/util/GameType.java rename to hypixel-api-core/src/main/java/net/hypixel/api/data/type/GameType.java index 51e37bd9..c76ecceb 100644 --- a/Java/src/main/java/net/hypixel/api/util/GameType.java +++ b/hypixel-api-core/src/main/java/net/hypixel/api/data/type/GameType.java @@ -1,6 +1,6 @@ -package net.hypixel.api.util; +package net.hypixel.api.data.type; -public enum GameType { +public enum GameType implements ServerType { QUAKECRAFT("Quakecraft", "Quake", 2), WALLS("Walls", "Walls", 3), PAINTBALL("Paintball", "Paintball", 4), @@ -27,7 +27,10 @@ public enum GameType { BUILD_BATTLE("Build Battle", "BuildBattle", 60), DUELS("Duels", "Duels", 61), SKYBLOCK("SkyBlock", "SkyBlock", 63), - PIT("Pit", "Pit", 64); + PIT("Pit", "Pit", 64), + REPLAY("Replay", "Replay", 65), + SMP("SMP", "SMP", 67), + ; private static final GameType[] VALUES = values(); @@ -77,6 +80,7 @@ public static GameType[] getValues() { /** * @return The official name of the GameType */ + @Override public String getName() { return name; } diff --git a/hypixel-api-core/src/main/java/net/hypixel/api/data/type/LobbyType.java b/hypixel-api-core/src/main/java/net/hypixel/api/data/type/LobbyType.java new file mode 100644 index 00000000..1e69336d --- /dev/null +++ b/hypixel-api-core/src/main/java/net/hypixel/api/data/type/LobbyType.java @@ -0,0 +1,31 @@ +package net.hypixel.api.data.type; + +/** + * A LobbyType is used for lobbies which do not have a gametype linked. + */ +public enum LobbyType implements ServerType { + MAIN("Main Lobby"), + TOURNAMENT("Tournament Hall"), + ; + + private static final LobbyType[] VALUES = values(); + + private final String name; + + LobbyType(String name) { + this.name = name; + } + + /** + * Exposing this method allows people to use the array without cloning. + * Slightly faster but not as safe since the array could be modified. + */ + public static LobbyType[] getValues() { + return VALUES; + } + + @Override + public String getName() { + return name; + } +} diff --git a/hypixel-api-core/src/main/java/net/hypixel/api/data/type/ServerType.java b/hypixel-api-core/src/main/java/net/hypixel/api/data/type/ServerType.java new file mode 100644 index 00000000..aa73aa21 --- /dev/null +++ b/hypixel-api-core/src/main/java/net/hypixel/api/data/type/ServerType.java @@ -0,0 +1,25 @@ +package net.hypixel.api.data.type; + +public interface ServerType { + + String name(); + + String getName(); + + static ServerType valueOf(String value) { + try { + return GameType.valueOf(value); + } catch (IllegalArgumentException e) { + // ignored + } + + try { + return LobbyType.valueOf(value); + } catch (IllegalArgumentException e) { + // ignored + } + + return null; + } + +} diff --git a/hypixel-api-core/src/main/java/net/hypixel/api/exceptions/BadResponseException.java b/hypixel-api-core/src/main/java/net/hypixel/api/exceptions/BadResponseException.java new file mode 100644 index 00000000..064f1cc0 --- /dev/null +++ b/hypixel-api-core/src/main/java/net/hypixel/api/exceptions/BadResponseException.java @@ -0,0 +1,9 @@ +package net.hypixel.api.exceptions; + +public class BadResponseException extends HypixelAPIException { + + public BadResponseException(String responseCause) { + super(responseCause); + } + +} diff --git a/hypixel-api-core/src/main/java/net/hypixel/api/exceptions/BadStatusCodeException.java b/hypixel-api-core/src/main/java/net/hypixel/api/exceptions/BadStatusCodeException.java new file mode 100644 index 00000000..2435b67a --- /dev/null +++ b/hypixel-api-core/src/main/java/net/hypixel/api/exceptions/BadStatusCodeException.java @@ -0,0 +1,20 @@ +package net.hypixel.api.exceptions; + +public class BadStatusCodeException extends HypixelAPIException { + private final int statusCode; + private final String responseCause; + + public BadStatusCodeException(int statusCode, String responseCause) { + super("Got a bad status code " + statusCode + ", cause \"" + responseCause + "\""); + this.statusCode = statusCode; + this.responseCause = responseCause; + } + + public int getStatusCode() { + return statusCode; + } + + public String getResponseCause() { + return responseCause; + } +} diff --git a/hypixel-api-core/src/main/java/net/hypixel/api/exceptions/HypixelAPIException.java b/hypixel-api-core/src/main/java/net/hypixel/api/exceptions/HypixelAPIException.java new file mode 100644 index 00000000..ee65f999 --- /dev/null +++ b/hypixel-api-core/src/main/java/net/hypixel/api/exceptions/HypixelAPIException.java @@ -0,0 +1,9 @@ +package net.hypixel.api.exceptions; + +public abstract class HypixelAPIException extends RuntimeException { + + protected HypixelAPIException(String message) { + super(message); + } + +} diff --git a/hypixel-api-core/src/main/java/net/hypixel/api/http/HTTPQueryParams.java b/hypixel-api-core/src/main/java/net/hypixel/api/http/HTTPQueryParams.java new file mode 100644 index 00000000..3f64ad2e --- /dev/null +++ b/hypixel-api-core/src/main/java/net/hypixel/api/http/HTTPQueryParams.java @@ -0,0 +1,40 @@ +package net.hypixel.api.http; + +import java.util.HashMap; +import java.util.Map; + +public class HTTPQueryParams { + + public static HTTPQueryParams create() { + return new HTTPQueryParams(); + } + + private final Map params = new HashMap<>(); + + private HTTPQueryParams() { + + } + + public HTTPQueryParams add(String key, Object value) { + this.params.put(key, value); + return this; + } + + public String getAsQueryString(String base) { + StringBuilder url = new StringBuilder(base); + boolean startedQuery = false; + + for (Map.Entry entry : params.entrySet()) { + if (!startedQuery) { + startedQuery = true; + url.append("?"); + } else { + url.append("&"); + } + + url.append(entry.getKey()).append("=").append(entry.getValue()); + } + + return url.toString(); + } +} diff --git a/hypixel-api-core/src/main/java/net/hypixel/api/http/HypixelHttpClient.java b/hypixel-api-core/src/main/java/net/hypixel/api/http/HypixelHttpClient.java new file mode 100644 index 00000000..0ae4995e --- /dev/null +++ b/hypixel-api-core/src/main/java/net/hypixel/api/http/HypixelHttpClient.java @@ -0,0 +1,15 @@ +package net.hypixel.api.http; + +import java.util.concurrent.CompletableFuture; + +public interface HypixelHttpClient { + + String DEFAULT_USER_AGENT = "Hypixel PublicAPI/4.0.0"; + + CompletableFuture makeRequest(String url); + + CompletableFuture makeAuthenticatedRequest(String url); + + void shutdown(); + +} diff --git a/hypixel-api-core/src/main/java/net/hypixel/api/http/HypixelHttpResponse.java b/hypixel-api-core/src/main/java/net/hypixel/api/http/HypixelHttpResponse.java new file mode 100644 index 00000000..dc198540 --- /dev/null +++ b/hypixel-api-core/src/main/java/net/hypixel/api/http/HypixelHttpResponse.java @@ -0,0 +1,20 @@ +package net.hypixel.api.http; + +public class HypixelHttpResponse { + + private final int statusCode; + private final String body; + + public HypixelHttpResponse(int statusCode, String body) { + this.statusCode = statusCode; + this.body = body; + } + + public int getStatusCode() { + return statusCode; + } + + public String getBody() { + return body; + } +} diff --git a/Java/src/main/java/net/hypixel/api/pets/Pet.java b/hypixel-api-core/src/main/java/net/hypixel/api/pets/Pet.java similarity index 100% rename from Java/src/main/java/net/hypixel/api/pets/Pet.java rename to hypixel-api-core/src/main/java/net/hypixel/api/pets/Pet.java diff --git a/Java/src/main/java/net/hypixel/api/pets/PetAttribute.java b/hypixel-api-core/src/main/java/net/hypixel/api/pets/PetAttribute.java similarity index 100% rename from Java/src/main/java/net/hypixel/api/pets/PetAttribute.java rename to hypixel-api-core/src/main/java/net/hypixel/api/pets/PetAttribute.java diff --git a/Java/src/main/java/net/hypixel/api/pets/PetStats.java b/hypixel-api-core/src/main/java/net/hypixel/api/pets/PetStats.java similarity index 100% rename from Java/src/main/java/net/hypixel/api/pets/PetStats.java rename to hypixel-api-core/src/main/java/net/hypixel/api/pets/PetStats.java diff --git a/Java/src/main/java/net/hypixel/api/pets/PetType.java b/hypixel-api-core/src/main/java/net/hypixel/api/pets/PetType.java similarity index 100% rename from Java/src/main/java/net/hypixel/api/pets/PetType.java rename to hypixel-api-core/src/main/java/net/hypixel/api/pets/PetType.java diff --git a/Java/src/main/java/net/hypixel/api/reply/AbstractReply.java b/hypixel-api-core/src/main/java/net/hypixel/api/reply/AbstractReply.java similarity index 69% rename from Java/src/main/java/net/hypixel/api/reply/AbstractReply.java rename to hypixel-api-core/src/main/java/net/hypixel/api/reply/AbstractReply.java index d0bd15ff..f366112a 100644 --- a/Java/src/main/java/net/hypixel/api/reply/AbstractReply.java +++ b/hypixel-api-core/src/main/java/net/hypixel/api/reply/AbstractReply.java @@ -2,14 +2,9 @@ public abstract class AbstractReply { - protected boolean throttle; protected boolean success; protected String cause; - public boolean isThrottle() { - return throttle; - } - public boolean isSuccess() { return success; } @@ -21,8 +16,7 @@ public String getCause() { @Override public String toString() { return "AbstractReply{" + - "throttle=" + throttle + - ", success=" + success + + "success=" + success + ", cause='" + cause + '\'' + '}'; } diff --git a/Java/src/main/java/net/hypixel/api/reply/BoostersReply.java b/hypixel-api-core/src/main/java/net/hypixel/api/reply/BoostersReply.java similarity index 98% rename from Java/src/main/java/net/hypixel/api/reply/BoostersReply.java rename to hypixel-api-core/src/main/java/net/hypixel/api/reply/BoostersReply.java index a45145f6..e9c24c14 100644 --- a/Java/src/main/java/net/hypixel/api/reply/BoostersReply.java +++ b/hypixel-api-core/src/main/java/net/hypixel/api/reply/BoostersReply.java @@ -1,6 +1,6 @@ package net.hypixel.api.reply; -import net.hypixel.api.util.GameType; +import net.hypixel.api.data.type.GameType; import java.time.ZonedDateTime; import java.util.List; diff --git a/Java/src/main/java/net/hypixel/api/reply/GameCountsReply.java b/hypixel-api-core/src/main/java/net/hypixel/api/reply/CountsReply.java similarity index 94% rename from Java/src/main/java/net/hypixel/api/reply/GameCountsReply.java rename to hypixel-api-core/src/main/java/net/hypixel/api/reply/CountsReply.java index b5da6d05..31c7a7cd 100644 --- a/Java/src/main/java/net/hypixel/api/reply/GameCountsReply.java +++ b/hypixel-api-core/src/main/java/net/hypixel/api/reply/CountsReply.java @@ -2,7 +2,7 @@ import java.util.Map; -public class GameCountsReply extends AbstractReply { +public class CountsReply extends AbstractReply { private Map games; private int playerCount; diff --git a/Java/src/main/java/net/hypixel/api/reply/FindGuildReply.java b/hypixel-api-core/src/main/java/net/hypixel/api/reply/FindGuildReply.java similarity index 100% rename from Java/src/main/java/net/hypixel/api/reply/FindGuildReply.java rename to hypixel-api-core/src/main/java/net/hypixel/api/reply/FindGuildReply.java diff --git a/Java/src/main/java/net/hypixel/api/reply/FriendsReply.java b/hypixel-api-core/src/main/java/net/hypixel/api/reply/FriendsReply.java similarity index 100% rename from Java/src/main/java/net/hypixel/api/reply/FriendsReply.java rename to hypixel-api-core/src/main/java/net/hypixel/api/reply/FriendsReply.java diff --git a/Java/src/main/java/net/hypixel/api/reply/GuildReply.java b/hypixel-api-core/src/main/java/net/hypixel/api/reply/GuildReply.java similarity index 98% rename from Java/src/main/java/net/hypixel/api/reply/GuildReply.java rename to hypixel-api-core/src/main/java/net/hypixel/api/reply/GuildReply.java index 3eef7a21..862079f4 100644 --- a/Java/src/main/java/net/hypixel/api/reply/GuildReply.java +++ b/hypixel-api-core/src/main/java/net/hypixel/api/reply/GuildReply.java @@ -113,7 +113,7 @@ public String toString() { '}'; } - public class Member { + public static class Member { private UUID uuid; private String rank; private ZonedDateTime joined; diff --git a/Java/src/main/java/net/hypixel/api/reply/KeyReply.java b/hypixel-api-core/src/main/java/net/hypixel/api/reply/KeyReply.java similarity index 99% rename from Java/src/main/java/net/hypixel/api/reply/KeyReply.java rename to hypixel-api-core/src/main/java/net/hypixel/api/reply/KeyReply.java index be90dea2..26654ab8 100644 --- a/Java/src/main/java/net/hypixel/api/reply/KeyReply.java +++ b/hypixel-api-core/src/main/java/net/hypixel/api/reply/KeyReply.java @@ -1,6 +1,7 @@ package net.hypixel.api.reply; import com.google.gson.annotations.SerializedName; + import java.util.UUID; public class KeyReply extends AbstractReply { diff --git a/Java/src/main/java/net/hypixel/api/reply/LeaderboardsReply.java b/hypixel-api-core/src/main/java/net/hypixel/api/reply/LeaderboardsReply.java similarity index 97% rename from Java/src/main/java/net/hypixel/api/reply/LeaderboardsReply.java rename to hypixel-api-core/src/main/java/net/hypixel/api/reply/LeaderboardsReply.java index fe2e6b8b..74e7d45e 100644 --- a/Java/src/main/java/net/hypixel/api/reply/LeaderboardsReply.java +++ b/hypixel-api-core/src/main/java/net/hypixel/api/reply/LeaderboardsReply.java @@ -1,6 +1,6 @@ package net.hypixel.api.reply; -import net.hypixel.api.util.GameType; +import net.hypixel.api.data.type.GameType; import java.util.List; import java.util.Map; diff --git a/Java/src/main/java/net/hypixel/api/reply/PlayerReply.java b/hypixel-api-core/src/main/java/net/hypixel/api/reply/PlayerReply.java similarity index 100% rename from Java/src/main/java/net/hypixel/api/reply/PlayerReply.java rename to hypixel-api-core/src/main/java/net/hypixel/api/reply/PlayerReply.java diff --git a/Java/src/main/java/net/hypixel/api/reply/WatchdogStatsReply.java b/hypixel-api-core/src/main/java/net/hypixel/api/reply/PunishmentStatsReply.java similarity index 95% rename from Java/src/main/java/net/hypixel/api/reply/WatchdogStatsReply.java rename to hypixel-api-core/src/main/java/net/hypixel/api/reply/PunishmentStatsReply.java index 5b0bc5bd..cbe7567e 100644 --- a/Java/src/main/java/net/hypixel/api/reply/WatchdogStatsReply.java +++ b/hypixel-api-core/src/main/java/net/hypixel/api/reply/PunishmentStatsReply.java @@ -2,7 +2,7 @@ import com.google.gson.annotations.SerializedName; -public class WatchdogStatsReply extends AbstractReply { +public class PunishmentStatsReply extends AbstractReply { @SerializedName("staff_rollingDaily") private int staffRollingDaily; @SerializedName("staff_total") diff --git a/Java/src/main/java/net/hypixel/api/reply/RecentGamesReply.java b/hypixel-api-core/src/main/java/net/hypixel/api/reply/RecentGamesReply.java similarity index 95% rename from Java/src/main/java/net/hypixel/api/reply/RecentGamesReply.java rename to hypixel-api-core/src/main/java/net/hypixel/api/reply/RecentGamesReply.java index 5c4a82ee..ecac2e54 100644 --- a/Java/src/main/java/net/hypixel/api/reply/RecentGamesReply.java +++ b/hypixel-api-core/src/main/java/net/hypixel/api/reply/RecentGamesReply.java @@ -1,8 +1,9 @@ package net.hypixel.api.reply; +import net.hypixel.api.data.type.GameType; + import java.time.ZonedDateTime; import java.util.List; -import net.hypixel.api.util.GameType; public class RecentGamesReply extends AbstractReply { @@ -40,7 +41,7 @@ public ZonedDateTime getStartDate() { /** * @return Game played during this session - * @see net.hypixel.api.util.GameType + * @see GameType */ public GameType getGameType() { return gameType; diff --git a/Java/src/main/java/net/hypixel/api/reply/StatusReply.java b/hypixel-api-core/src/main/java/net/hypixel/api/reply/StatusReply.java similarity index 76% rename from Java/src/main/java/net/hypixel/api/reply/StatusReply.java rename to hypixel-api-core/src/main/java/net/hypixel/api/reply/StatusReply.java index e8f48245..5aaee3f7 100644 --- a/Java/src/main/java/net/hypixel/api/reply/StatusReply.java +++ b/hypixel-api-core/src/main/java/net/hypixel/api/reply/StatusReply.java @@ -1,8 +1,7 @@ package net.hypixel.api.reply; -import net.hypixel.api.util.GameType; - -import java.util.Optional; +import com.google.gson.annotations.SerializedName; +import net.hypixel.api.data.type.ServerType; public class StatusReply extends AbstractReply { @@ -23,7 +22,7 @@ public String toString() { "} " + super.toString(); } - public class Session { + public static class Session { /** * Boolean if player is online. @@ -32,12 +31,12 @@ public class Session { private boolean online; /** - * GameType could be null if a new game has been released - * and GameType is not yet added to {@link GameType}. + * ServerType could be null if a new game/lobby has been released and type is not yet added. *

* This will NOT throw an exception. */ - private GameType gameType; + @SerializedName("gameType") + private ServerType serverType; /** * Mode of game being played @@ -55,8 +54,8 @@ public boolean isOnline() { return online; } - public GameType getGameType() { - return gameType; + public ServerType getServerType() { + return serverType; } public String getMode() { @@ -71,7 +70,7 @@ public String getMap() { public String toString() { return "Session{" + "online=" + online + - ", gameType=" + gameType + + ", serverType=" + serverType + ", mode=" + mode + ", map=" + map + "}"; diff --git a/Java/src/main/java/net/hypixel/api/reply/skyblock/ResourceReply.java b/hypixel-api-core/src/main/java/net/hypixel/api/reply/skyblock/ResourceReply.java similarity index 93% rename from Java/src/main/java/net/hypixel/api/reply/skyblock/ResourceReply.java rename to hypixel-api-core/src/main/java/net/hypixel/api/reply/skyblock/ResourceReply.java index 438c7265..ff2f0b29 100644 --- a/Java/src/main/java/net/hypixel/api/reply/skyblock/ResourceReply.java +++ b/hypixel-api-core/src/main/java/net/hypixel/api/reply/skyblock/ResourceReply.java @@ -5,8 +5,8 @@ public class ResourceReply extends AbstractReply { - private long lastUpdated; - private JsonObject response; + private final long lastUpdated; + private final JsonObject response; public ResourceReply(JsonObject response) { this.response = response; @@ -26,6 +26,7 @@ public JsonObject getResponse() { /** * Gets unix time when the resource was updated. * Will return -1 if last updated was not included in response + * * @return long unix time */ public long getLastUpdated() { diff --git a/Java/src/main/java/net/hypixel/api/reply/skyblock/SkyBlockAuctionsReply.java b/hypixel-api-core/src/main/java/net/hypixel/api/reply/skyblock/SkyBlockAuctionsReply.java similarity index 100% rename from Java/src/main/java/net/hypixel/api/reply/skyblock/SkyBlockAuctionsReply.java rename to hypixel-api-core/src/main/java/net/hypixel/api/reply/skyblock/SkyBlockAuctionsReply.java diff --git a/Java/src/main/java/net/hypixel/api/reply/skyblock/BazaarReply.java b/hypixel-api-core/src/main/java/net/hypixel/api/reply/skyblock/SkyBlockBazaarReply.java similarity index 84% rename from Java/src/main/java/net/hypixel/api/reply/skyblock/BazaarReply.java rename to hypixel-api-core/src/main/java/net/hypixel/api/reply/skyblock/SkyBlockBazaarReply.java index ae9e2f36..6e18d051 100644 --- a/Java/src/main/java/net/hypixel/api/reply/skyblock/BazaarReply.java +++ b/hypixel-api-core/src/main/java/net/hypixel/api/reply/skyblock/SkyBlockBazaarReply.java @@ -6,7 +6,7 @@ import java.util.List; import java.util.Map; -public class BazaarReply extends AbstractReply { +public class SkyBlockBazaarReply extends AbstractReply { private long lastUpdated; private Map products; @@ -76,11 +76,11 @@ public String toString() { public class Summary { - private int amount; + private long amount; private double pricePerUnit; - private int orders; + private long orders; - public int getAmount() { + public long getAmount() { return amount; } @@ -88,7 +88,7 @@ public double getPricePerUnit() { return pricePerUnit; } - public int getOrders() { + public long getOrders() { return orders; } @@ -105,22 +105,14 @@ public String toString() { public class Status { private String productId; - private double sellPrice; - - private int sellVolume; - - private int sellMovingWeek; - - private int sellOrders; - + private long sellVolume; + private long sellMovingWeek; + private long sellOrders; private double buyPrice; - - private int buyVolume; - - private int buyMovingWeek; - - private int buyOrders; + private long buyVolume; + private long buyMovingWeek; + private long buyOrders; public String getProductId() { return productId; @@ -130,15 +122,15 @@ public double getSellPrice() { return sellPrice; } - public int getSellVolume() { + public long getSellVolume() { return sellVolume; } - public int getSellMovingWeek() { + public long getSellMovingWeek() { return sellMovingWeek; } - public int getSellOrders() { + public long getSellOrders() { return sellOrders; } @@ -146,15 +138,15 @@ public double getBuyPrice() { return buyPrice; } - public int getBuyVolume() { + public long getBuyVolume() { return buyVolume; } - public int getBuyMovingWeek() { + public long getBuyMovingWeek() { return buyMovingWeek; } - public int getBuyOrders() { + public long getBuyOrders() { return buyOrders; } diff --git a/Java/src/main/java/net/hypixel/api/reply/skyblock/SkyBlockNewsReply.java b/hypixel-api-core/src/main/java/net/hypixel/api/reply/skyblock/SkyBlockNewsReply.java similarity index 100% rename from Java/src/main/java/net/hypixel/api/reply/skyblock/SkyBlockNewsReply.java rename to hypixel-api-core/src/main/java/net/hypixel/api/reply/skyblock/SkyBlockNewsReply.java index 6bfd7bad..d1942496 100644 --- a/Java/src/main/java/net/hypixel/api/reply/skyblock/SkyBlockNewsReply.java +++ b/hypixel-api-core/src/main/java/net/hypixel/api/reply/skyblock/SkyBlockNewsReply.java @@ -1,7 +1,7 @@ package net.hypixel.api.reply.skyblock; -import com.google.gson.JsonElement; import com.google.gson.JsonArray; +import com.google.gson.JsonElement; import net.hypixel.api.reply.AbstractReply; public class SkyBlockNewsReply extends AbstractReply { diff --git a/Java/src/main/java/net/hypixel/api/reply/skyblock/SkyBlockProfileReply.java b/hypixel-api-core/src/main/java/net/hypixel/api/reply/skyblock/SkyBlockProfileReply.java similarity index 100% rename from Java/src/main/java/net/hypixel/api/reply/skyblock/SkyBlockProfileReply.java rename to hypixel-api-core/src/main/java/net/hypixel/api/reply/skyblock/SkyBlockProfileReply.java diff --git a/Java/src/main/java/net/hypixel/api/util/Banner.java b/hypixel-api-core/src/main/java/net/hypixel/api/util/Banner.java similarity index 100% rename from Java/src/main/java/net/hypixel/api/util/Banner.java rename to hypixel-api-core/src/main/java/net/hypixel/api/util/Banner.java diff --git a/Java/src/main/java/net/hypixel/api/util/ILeveling.java b/hypixel-api-core/src/main/java/net/hypixel/api/util/ILeveling.java similarity index 100% rename from Java/src/main/java/net/hypixel/api/util/ILeveling.java rename to hypixel-api-core/src/main/java/net/hypixel/api/util/ILeveling.java diff --git a/Java/src/main/java/net/hypixel/api/util/Rarity.java b/hypixel-api-core/src/main/java/net/hypixel/api/util/Rarity.java similarity index 100% rename from Java/src/main/java/net/hypixel/api/util/Rarity.java rename to hypixel-api-core/src/main/java/net/hypixel/api/util/Rarity.java diff --git a/Java/src/main/java/net/hypixel/api/util/ResourceType.java b/hypixel-api-core/src/main/java/net/hypixel/api/util/ResourceType.java similarity index 100% rename from Java/src/main/java/net/hypixel/api/util/ResourceType.java rename to hypixel-api-core/src/main/java/net/hypixel/api/util/ResourceType.java diff --git a/Java/src/main/java/net/hypixel/api/util/Utilities.java b/hypixel-api-core/src/main/java/net/hypixel/api/util/Utilities.java similarity index 100% rename from Java/src/main/java/net/hypixel/api/util/Utilities.java rename to hypixel-api-core/src/main/java/net/hypixel/api/util/Utilities.java diff --git a/hypixel-api-example/README.md b/hypixel-api-example/README.md new file mode 100644 index 00000000..417f1b68 --- /dev/null +++ b/hypixel-api-example/README.md @@ -0,0 +1,3 @@ +# HypixelAPI Java Examples + +This codebase serves as examples for how to integrate the HypixelAPI into your project. \ No newline at end of file diff --git a/Example/pom.xml b/hypixel-api-example/pom.xml similarity index 56% rename from Example/pom.xml rename to hypixel-api-example/pom.xml index 7982fc48..8de6e66f 100644 --- a/Example/pom.xml +++ b/hypixel-api-example/pom.xml @@ -2,11 +2,14 @@ + + hypixel-api + net.hypixel + 4.0.0-SNAPSHOT + 4.0.0 - Example - net.hypixel - 2.0.0 + hypixel-api-example true @@ -25,18 +28,22 @@ - - - Hypixel - https://repo.hypixel.net/repository/Hypixel/ - - - net.hypixel - HypixelAPI - 3.0.0 + hypixel-api-transport-apache + 4.0.0-SNAPSHOT + + + com.konghq + unirest-java + 3.11.09 + + + org.apache.httpcomponents + httpclient + 4.5.13 - + + \ No newline at end of file diff --git a/Example/src/main/java/net/hypixel/example/ExampleUtil.java b/hypixel-api-example/src/main/java/net/hypixel/api/example/ExampleUtil.java similarity index 71% rename from Example/src/main/java/net/hypixel/example/ExampleUtil.java rename to hypixel-api-example/src/main/java/net/hypixel/api/example/ExampleUtil.java index cd7f810d..94190af6 100644 --- a/Example/src/main/java/net/hypixel/example/ExampleUtil.java +++ b/hypixel-api-example/src/main/java/net/hypixel/api/example/ExampleUtil.java @@ -1,6 +1,7 @@ -package net.hypixel.example; +package net.hypixel.api.example; import net.hypixel.api.HypixelAPI; +import net.hypixel.api.apache.ApacheHttpClient; import net.hypixel.api.reply.AbstractReply; import java.util.UUID; @@ -8,7 +9,12 @@ public class ExampleUtil { - public static final HypixelAPI API = new HypixelAPI(UUID.fromString("64bd424e-ccb0-42ed-8b66-6e42a135afb4")); // arbitrary key, replace with your own to test + public static final HypixelAPI API; + + static { + String key = System.getProperty("apiKey", "64bd424e-ccb0-42ed-8b66-6e42a135afb4"); // arbitrary key, replace with your own to test or use the property + API = new HypixelAPI(new ApacheHttpClient(UUID.fromString(key))); + } public static final UUID HYPIXEL = UUID.fromString("f7c77d99-9f15-4a66-a87d-c4a51ef30d19"); diff --git a/Example/src/main/java/net/hypixel/example/GetBoostersExample.java b/hypixel-api-example/src/main/java/net/hypixel/api/example/GetBoostersExample.java similarity index 85% rename from Example/src/main/java/net/hypixel/example/GetBoostersExample.java rename to hypixel-api-example/src/main/java/net/hypixel/api/example/GetBoostersExample.java index 46b59b8a..190ccd78 100644 --- a/Example/src/main/java/net/hypixel/example/GetBoostersExample.java +++ b/hypixel-api-example/src/main/java/net/hypixel/api/example/GetBoostersExample.java @@ -1,4 +1,4 @@ -package net.hypixel.example; +package net.hypixel.api.example; public class GetBoostersExample { public static void main(String[] args) { diff --git a/hypixel-api-example/src/main/java/net/hypixel/api/example/GetCountsExample.java b/hypixel-api-example/src/main/java/net/hypixel/api/example/GetCountsExample.java new file mode 100644 index 00000000..9f7f32c3 --- /dev/null +++ b/hypixel-api-example/src/main/java/net/hypixel/api/example/GetCountsExample.java @@ -0,0 +1,8 @@ +package net.hypixel.api.example; + +public class GetCountsExample { + public static void main(String[] args) { + ExampleUtil.API.getCounts().whenComplete(ExampleUtil.getTestConsumer()); + ExampleUtil.await(); + } +} diff --git a/Example/src/main/java/net/hypixel/example/GetFriendsExample.java b/hypixel-api-example/src/main/java/net/hypixel/api/example/GetFriendsExample.java similarity index 86% rename from Example/src/main/java/net/hypixel/example/GetFriendsExample.java rename to hypixel-api-example/src/main/java/net/hypixel/api/example/GetFriendsExample.java index 5d6c3375..470ffac0 100644 --- a/Example/src/main/java/net/hypixel/example/GetFriendsExample.java +++ b/hypixel-api-example/src/main/java/net/hypixel/api/example/GetFriendsExample.java @@ -1,4 +1,4 @@ -package net.hypixel.example; +package net.hypixel.api.example; public class GetFriendsExample { public static void main(String[] args) { diff --git a/Example/src/main/java/net/hypixel/example/GetGuildExample.java b/hypixel-api-example/src/main/java/net/hypixel/api/example/GetGuildExample.java similarity index 87% rename from Example/src/main/java/net/hypixel/example/GetGuildExample.java rename to hypixel-api-example/src/main/java/net/hypixel/api/example/GetGuildExample.java index 394e1ebd..91997b7c 100644 --- a/Example/src/main/java/net/hypixel/example/GetGuildExample.java +++ b/hypixel-api-example/src/main/java/net/hypixel/api/example/GetGuildExample.java @@ -1,4 +1,4 @@ -package net.hypixel.example; +package net.hypixel.api.example; public class GetGuildExample { public static void main(String[] args) { diff --git a/Example/src/main/java/net/hypixel/example/GetLeaderboardsExample.java b/hypixel-api-example/src/main/java/net/hypixel/api/example/GetLeaderboardsExample.java similarity index 86% rename from Example/src/main/java/net/hypixel/example/GetLeaderboardsExample.java rename to hypixel-api-example/src/main/java/net/hypixel/api/example/GetLeaderboardsExample.java index 5ee203e6..cd7e5096 100644 --- a/Example/src/main/java/net/hypixel/example/GetLeaderboardsExample.java +++ b/hypixel-api-example/src/main/java/net/hypixel/api/example/GetLeaderboardsExample.java @@ -1,4 +1,4 @@ -package net.hypixel.example; +package net.hypixel.api.example; public class GetLeaderboardsExample { public static void main(String[] args) { diff --git a/Example/src/main/java/net/hypixel/example/GetPlayerExample.java b/hypixel-api-example/src/main/java/net/hypixel/api/example/GetPlayerExample.java similarity index 87% rename from Example/src/main/java/net/hypixel/example/GetPlayerExample.java rename to hypixel-api-example/src/main/java/net/hypixel/api/example/GetPlayerExample.java index bb45390e..22ba58af 100644 --- a/Example/src/main/java/net/hypixel/example/GetPlayerExample.java +++ b/hypixel-api-example/src/main/java/net/hypixel/api/example/GetPlayerExample.java @@ -1,4 +1,4 @@ -package net.hypixel.example; +package net.hypixel.api.example; public class GetPlayerExample { public static void main(String[] args) { diff --git a/hypixel-api-example/src/main/java/net/hypixel/api/example/GetPunishmentStatsExample.java b/hypixel-api-example/src/main/java/net/hypixel/api/example/GetPunishmentStatsExample.java new file mode 100644 index 00000000..34574991 --- /dev/null +++ b/hypixel-api-example/src/main/java/net/hypixel/api/example/GetPunishmentStatsExample.java @@ -0,0 +1,8 @@ +package net.hypixel.api.example; + +public class GetPunishmentStatsExample { + public static void main(String[] args) { + ExampleUtil.API.getPunishmentStats().whenComplete(ExampleUtil.getTestConsumer()); + ExampleUtil.await(); + } +} diff --git a/Example/src/main/java/net/hypixel/example/GetRecentGamesExample.java b/hypixel-api-example/src/main/java/net/hypixel/api/example/GetRecentGamesExample.java similarity index 87% rename from Example/src/main/java/net/hypixel/example/GetRecentGamesExample.java rename to hypixel-api-example/src/main/java/net/hypixel/api/example/GetRecentGamesExample.java index 62fbcc07..9dd5ed75 100644 --- a/Example/src/main/java/net/hypixel/example/GetRecentGamesExample.java +++ b/hypixel-api-example/src/main/java/net/hypixel/api/example/GetRecentGamesExample.java @@ -1,4 +1,4 @@ -package net.hypixel.example; +package net.hypixel.api.example; public class GetRecentGamesExample { diff --git a/Example/src/main/java/net/hypixel/example/GetResourceExample.java b/hypixel-api-example/src/main/java/net/hypixel/api/example/GetResourceExample.java similarity index 88% rename from Example/src/main/java/net/hypixel/example/GetResourceExample.java rename to hypixel-api-example/src/main/java/net/hypixel/api/example/GetResourceExample.java index a6108ed7..a5c58f43 100644 --- a/Example/src/main/java/net/hypixel/example/GetResourceExample.java +++ b/hypixel-api-example/src/main/java/net/hypixel/api/example/GetResourceExample.java @@ -1,4 +1,4 @@ -package net.hypixel.example; +package net.hypixel.api.example; import net.hypixel.api.util.ResourceType; diff --git a/Example/src/main/java/net/hypixel/example/GetStatusExample.java b/hypixel-api-example/src/main/java/net/hypixel/api/example/GetStatusExample.java similarity index 91% rename from Example/src/main/java/net/hypixel/example/GetStatusExample.java rename to hypixel-api-example/src/main/java/net/hypixel/api/example/GetStatusExample.java index 96ab615d..39f98226 100644 --- a/Example/src/main/java/net/hypixel/example/GetStatusExample.java +++ b/hypixel-api-example/src/main/java/net/hypixel/api/example/GetStatusExample.java @@ -1,4 +1,4 @@ -package net.hypixel.example; +package net.hypixel.api.example; public class GetStatusExample { public static void main(String[] args) { diff --git a/Example/src/main/java/net/hypixel/example/KeyInfoExample.java b/hypixel-api-example/src/main/java/net/hypixel/api/example/KeyInfoExample.java similarity index 85% rename from Example/src/main/java/net/hypixel/example/KeyInfoExample.java rename to hypixel-api-example/src/main/java/net/hypixel/api/example/KeyInfoExample.java index 5e9014f8..79b9951b 100644 --- a/Example/src/main/java/net/hypixel/example/KeyInfoExample.java +++ b/hypixel-api-example/src/main/java/net/hypixel/api/example/KeyInfoExample.java @@ -1,4 +1,4 @@ -package net.hypixel.example; +package net.hypixel.api.example; public class KeyInfoExample { public static void main(String[] args) { diff --git a/hypixel-api-example/src/main/java/net/hypixel/api/example/skyblock/GetBazaarExample.java b/hypixel-api-example/src/main/java/net/hypixel/api/example/skyblock/GetBazaarExample.java new file mode 100644 index 00000000..2424ba7a --- /dev/null +++ b/hypixel-api-example/src/main/java/net/hypixel/api/example/skyblock/GetBazaarExample.java @@ -0,0 +1,10 @@ +package net.hypixel.api.example.skyblock; + +import net.hypixel.api.example.ExampleUtil; + +public class GetBazaarExample { + public static void main(String[] args) { + ExampleUtil.API.getSkyBlockBazaar().whenComplete(ExampleUtil.getTestConsumer()); + ExampleUtil.await(); + } +} diff --git a/Example/src/main/java/net/hypixel/example/skyblock/GetSkyBlockAuctionsExample.java b/hypixel-api-example/src/main/java/net/hypixel/api/example/skyblock/GetSkyBlockAuctionsExample.java similarity index 88% rename from Example/src/main/java/net/hypixel/example/skyblock/GetSkyBlockAuctionsExample.java rename to hypixel-api-example/src/main/java/net/hypixel/api/example/skyblock/GetSkyBlockAuctionsExample.java index 9881958a..789af16c 100644 --- a/Example/src/main/java/net/hypixel/example/skyblock/GetSkyBlockAuctionsExample.java +++ b/hypixel-api-example/src/main/java/net/hypixel/api/example/skyblock/GetSkyBlockAuctionsExample.java @@ -1,6 +1,6 @@ -package net.hypixel.example.skyblock; +package net.hypixel.api.example.skyblock; -import net.hypixel.example.ExampleUtil; +import net.hypixel.api.example.ExampleUtil; public class GetSkyBlockAuctionsExample { public static void main(String[] args) { diff --git a/Example/src/main/java/net/hypixel/example/skyblock/GetSkyBlockNewsExample.java b/hypixel-api-example/src/main/java/net/hypixel/api/example/skyblock/GetSkyBlockNewsExample.java similarity index 70% rename from Example/src/main/java/net/hypixel/example/skyblock/GetSkyBlockNewsExample.java rename to hypixel-api-example/src/main/java/net/hypixel/api/example/skyblock/GetSkyBlockNewsExample.java index 5a80d321..11c77b99 100644 --- a/Example/src/main/java/net/hypixel/example/skyblock/GetSkyBlockNewsExample.java +++ b/hypixel-api-example/src/main/java/net/hypixel/api/example/skyblock/GetSkyBlockNewsExample.java @@ -1,6 +1,6 @@ -package net.hypixel.example.skyblock; +package net.hypixel.api.example.skyblock; -import net.hypixel.example.ExampleUtil; +import net.hypixel.api.example.ExampleUtil; public class GetSkyBlockNewsExample { public static void main(String[] args) { diff --git a/Example/src/main/java/net/hypixel/example/skyblock/GetSkyBlockProfileExample.java b/hypixel-api-example/src/main/java/net/hypixel/api/example/skyblock/GetSkyBlockProfileExample.java similarity index 90% rename from Example/src/main/java/net/hypixel/example/skyblock/GetSkyBlockProfileExample.java rename to hypixel-api-example/src/main/java/net/hypixel/api/example/skyblock/GetSkyBlockProfileExample.java index 977e4ffd..d96b0eb7 100644 --- a/Example/src/main/java/net/hypixel/example/skyblock/GetSkyBlockProfileExample.java +++ b/hypixel-api-example/src/main/java/net/hypixel/api/example/skyblock/GetSkyBlockProfileExample.java @@ -1,7 +1,7 @@ -package net.hypixel.example.skyblock; +package net.hypixel.api.example.skyblock; import com.google.gson.JsonElement; -import net.hypixel.example.ExampleUtil; +import net.hypixel.api.example.ExampleUtil; import java.util.Map; diff --git a/hypixel-api-transport-apache/README.md b/hypixel-api-transport-apache/README.md new file mode 100644 index 00000000..1061a8fb --- /dev/null +++ b/hypixel-api-transport-apache/README.md @@ -0,0 +1,45 @@ +Hypixel Public API - Apache Transport +====== + +### Usage + +```xml + + net.hypixel + hypixel-api-transport-apache + 4.0.0 + +``` + +Can also be included with Gradle. + +```gradle +dependencies { + implementation 'net.hypixel:hypixel-api-transport-apache:4.0.0' +} +``` + +### Example code + +```java +public class Main { + public static void main(String[] args) { + HypixelHttpClient client = new ApacheHttpClient(UUID.fromString("your-api-key-here")); + HypixelAPI hypixelAPI = new HypixelAPI(client); + hypixelAPI.getPlayerByName("Hypixel") + .exceptionally(throwable -> { + // Handle exceptions here + throwable.printStackTrace(); + return null; + }) + .thenAccept(System.out::println); + } +} +``` + +### Dependencies + +This transport depends on the following: + +* [Google Gson library - 2.8.6](https://mvnrepository.com/artifact/com.google.code.gson/gson) (for hypixel-api-core) +* [Apache HttpClient - 4.5.13](https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient) \ No newline at end of file diff --git a/hypixel-api-transport-apache/pom.xml b/hypixel-api-transport-apache/pom.xml new file mode 100644 index 00000000..5262d978 --- /dev/null +++ b/hypixel-api-transport-apache/pom.xml @@ -0,0 +1,40 @@ + + + + hypixel-api + net.hypixel + 4.0.0-SNAPSHOT + + 4.0.0 + + hypixel-api-transport-apache + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + + net.hypixel + hypixel-api-core + 4.0.0-SNAPSHOT + + + org.apache.httpcomponents + httpclient + 4.5.13 + + + + \ No newline at end of file diff --git a/hypixel-api-transport-apache/src/main/java/net/hypixel/api/apache/ApacheHttpClient.java b/hypixel-api-transport-apache/src/main/java/net/hypixel/api/apache/ApacheHttpClient.java new file mode 100644 index 00000000..c4600bb8 --- /dev/null +++ b/hypixel-api-transport-apache/src/main/java/net/hypixel/api/apache/ApacheHttpClient.java @@ -0,0 +1,60 @@ +package net.hypixel.api.apache; + +import net.hypixel.api.http.HypixelHttpClient; +import net.hypixel.api.http.HypixelHttpResponse; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.util.EntityUtils; + +import java.io.IOException; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +public class ApacheHttpClient implements HypixelHttpClient { + + private final UUID apiKey; + private final ExecutorService executorService; + private final HttpClient httpClient; + + public ApacheHttpClient(UUID apiKey) { + this.apiKey = apiKey; + this.executorService = Executors.newCachedThreadPool(); + this.httpClient = HttpClientBuilder.create().setUserAgent(DEFAULT_USER_AGENT).build(); + } + + @Override + public CompletableFuture makeRequest(String url) { + return CompletableFuture.supplyAsync(() -> { + try { + HttpResponse response = this.httpClient.execute(new HttpGet(url)); + return new HypixelHttpResponse(response.getStatusLine().getStatusCode(), EntityUtils.toString(response.getEntity(), "UTF-8")); + } catch (IOException e) { + throw new RuntimeException(e); + } + }, this.executorService); + } + + @Override + public CompletableFuture makeAuthenticatedRequest(String url) { + return CompletableFuture.supplyAsync(() -> { + HttpGet request = new HttpGet(url); + request.addHeader("API-Key", this.apiKey.toString()); + try { + HttpResponse response = this.httpClient.execute(request); + return new HypixelHttpResponse(response.getStatusLine().getStatusCode(), EntityUtils.toString(response.getEntity(), "UTF-8")); + } catch (IOException e) { + throw new RuntimeException(e); + } + }, this.executorService); + } + + @Override + public void shutdown() { + this.executorService.shutdown(); + } + +} diff --git a/hypixel-api-transport-unirest/README.md b/hypixel-api-transport-unirest/README.md new file mode 100644 index 00000000..7f7f8598 --- /dev/null +++ b/hypixel-api-transport-unirest/README.md @@ -0,0 +1,45 @@ +Hypixel Public API - Unirest Transport +====== + +### Usage + +```xml + + net.hypixel + hypixel-api-transport-unirest + 4.0.0 + +``` + +Can also be included with Gradle. + +```gradle +dependencies { + implementation 'net.hypixel:hypixel-api-transport-unirest:4.0.0' +} +``` + +### Example code + +```java +public class Main { + public static void main(String[] args) { + HypixelHttpClient client = new UnirestHttpClient(UUID.fromString("your-api-key-here")); + HypixelAPI hypixelAPI = new HypixelAPI(client); + hypixelAPI.getPlayerByName("Hypixel") + .exceptionally(throwable -> { + // Handle exceptions here + throwable.printStackTrace(); + return null; + }) + .thenAccept(System.out::println); + } +} +``` + +### Dependencies + +This transport depends on the following: + +* [Google Gson library - 2.8.6](https://mvnrepository.com/artifact/com.google.code.gson/gson) (for hypixel-api-core) +* [Unirest Java - 3.11.11](https://mvnrepository.com/artifact/com.konghq/unirest-java) \ No newline at end of file diff --git a/hypixel-api-transport-unirest/pom.xml b/hypixel-api-transport-unirest/pom.xml new file mode 100644 index 00000000..a9c8dcee --- /dev/null +++ b/hypixel-api-transport-unirest/pom.xml @@ -0,0 +1,40 @@ + + + + hypixel-api + net.hypixel + 4.0.0-SNAPSHOT + + 4.0.0 + + hypixel-api-transport-unirest + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + + net.hypixel + hypixel-api-core + 4.0.0-SNAPSHOT + + + com.konghq + unirest-java + 3.11.11 + + + + \ No newline at end of file diff --git a/hypixel-api-transport-unirest/src/main/java/net/hypixel/api/unirest/UnirestHttpClient.java b/hypixel-api-transport-unirest/src/main/java/net/hypixel/api/unirest/UnirestHttpClient.java new file mode 100644 index 00000000..48690211 --- /dev/null +++ b/hypixel-api-transport-unirest/src/main/java/net/hypixel/api/unirest/UnirestHttpClient.java @@ -0,0 +1,33 @@ +package net.hypixel.api.unirest; + +import kong.unirest.Unirest; +import net.hypixel.api.http.HypixelHttpClient; +import net.hypixel.api.http.HypixelHttpResponse; + +import java.util.UUID; +import java.util.concurrent.CompletableFuture; + +public class UnirestHttpClient implements HypixelHttpClient { + + private final UUID apiKey; + + public UnirestHttpClient(UUID apiKey) { + this.apiKey = apiKey; + } + + @Override + public CompletableFuture makeRequest(String url) { + return Unirest.get(url).header("User-Agent", DEFAULT_USER_AGENT).asStringAsync().thenApply(res -> new HypixelHttpResponse(res.getStatus(), res.getBody())); + } + + @Override + public CompletableFuture makeAuthenticatedRequest(String url) { + return Unirest.get(url).header("User-Agent", DEFAULT_USER_AGENT).header("API-Key", this.apiKey.toString()).asStringAsync().thenApply(res -> new HypixelHttpResponse(res.getStatus(), res.getBody())); + } + + @Override + public void shutdown() { + Unirest.shutDown(); + } + +} diff --git a/pom.xml b/pom.xml index 0df9c032..facb75d4 100644 --- a/pom.xml +++ b/pom.xml @@ -5,13 +5,59 @@ 4.0.0 net.hypixel - PublicAPI + hypixel-api pom - 2.0-SNAPSHOT + 4.0.0-SNAPSHOT - Example - Java + hypixel-api-core + hypixel-api-example + hypixel-api-transport-apache + hypixel-api-transport-unirest + + + Hypixel + https://repo.hypixel.net/repository/Hypixel/ + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + UTF-8 + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-source-plugin + 3.0.1 + + + attach-sources + + jar + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.0.1 + + none + true + + + + + \ No newline at end of file