Skip to content

Add player object #260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 61 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
c10f78f
Wrap player object in class
TheNullicorn Jun 12, 2020
f27df69
Add utility methods to player
TheNullicorn Jun 12, 2020
ddee992
Add javadocs for player object
TheNullicorn Jun 12, 2020
699548a
Add javadocs for property accessors
TheNullicorn Jun 12, 2020
26804c3
Add get level method to player
TheNullicorn Jun 12, 2020
bc4cb40
Update examples
TheNullicorn Jun 12, 2020
497babd
Fix incorrect network exp
TheNullicorn Jun 13, 2020
efb911a
Use getLongProperty instead of getNumberProperty.longValue
TheNullicorn Jun 13, 2020
7fec2dd
Use fallback when displayname is not present
TheNullicorn Jun 13, 2020
7af2c06
Fix exp calculation
TheNullicorn Jun 13, 2020
affff2a
Add method for recent GameType
TheNullicorn Jun 13, 2020
5e76557
Add method for PetStats
TheNullicorn Jun 13, 2020
0e817bf
Add methods to check player api visibility
TheNullicorn Jun 13, 2020
b383c02
Add rank color & mc version methods to player
TheNullicorn Jun 14, 2020
bcda957
Fix incorrect javadocs
TheNullicorn Jun 18, 2020
64182b6
Fix javadoc typo
TheNullicorn May 10, 2021
3ddf2e2
Remove methods that access player settings
TheNullicorn May 10, 2021
39c6113
Add public constructor to Player
TheNullicorn May 10, 2021
cdb61ba
Check for exception in GetPlayerExample
TheNullicorn May 10, 2021
28a8dc0
Return UUID instead of String from getUuid
TheNullicorn May 11, 2021
d8acbf3
Move GSON into Utilities
TheNullicorn May 12, 2021
a68c6a8
Clarify getUuid javadoc
TheNullicorn May 12, 2021
7f2e9c4
Merge remote-tracking branch 'origin/feature/player-obj' into feature…
TheNullicorn May 12, 2021
7014db4
Change getPlusColor to getSelectedPlusColor
TheNullicorn May 12, 2021
9be634c
Explicitly exit in GetPlayerExample
TheNullicorn May 12, 2021
c015b25
Clean up uuidFromString
TheNullicorn May 12, 2021
b3f66c1
Add hasProperty() to Player
TheNullicorn May 12, 2021
0e12836
Merge remote-tracking branch 'origin/feature/player-obj' into feature…
TheNullicorn May 12, 2021
72dbd7a
Add ComplexHypixelObject for property methods
TheNullicorn May 20, 2021
5b17df2
Remove extra parenthesis
TheNullicorn May 20, 2021
8795df0
Simplify uuidFromString
TheNullicorn May 20, 2021
cde1a1a
Begin property filter
TheNullicorn Jun 4, 2021
ca8fee1
Let keys be removed from filter
TheNullicorn Jun 4, 2021
79c078b
Check for null keys
TheNullicorn Jun 4, 2021
b6a231d
Document PropertyFilter
TheNullicorn Jun 5, 2021
eb0757d
Overload player fetch methods w/ filters
TheNullicorn Jun 5, 2021
7a57ab8
Allow batch filtering
TheNullicorn Jun 5, 2021
58b8757
Detect & fix key collisions
TheNullicorn Jun 6, 2021
8144a69
Respect escapes when applying
TheNullicorn Jun 6, 2021
3ba6380
Respect escapes in #getProperty(...)
TheNullicorn Jun 6, 2021
3eec189
Apply suggestions from code review
TheNullicorn Jun 19, 2021
b97b770
Make raw non-null
TheNullicorn Jun 19, 2021
9147d87
Change "complex" to "unstable"
TheNullicorn Jun 19, 2021
8eb803c
Only compile key splitter once
TheNullicorn Jun 20, 2021
0342aa4
Private constructor & final for Utilities
TheNullicorn Jun 20, 2021
d64e646
Remove redundant getProperty() logic
TheNullicorn Jun 20, 2021
623ea92
More getProperty() simplifying
TheNullicorn Jun 20, 2021
296edf4
Change with() to including()
TheNullicorn Jun 20, 2021
57a7883
Remove "keys" from method names
TheNullicorn Jun 20, 2021
4f83f23
Clarify property javadocs
TheNullicorn Jun 20, 2021
da7da2b
Update exists() check
TheNullicorn Jun 20, 2021
59bb5b0
Formatting & example change
TheNullicorn Jun 20, 2021
23d394e
Fix null players & flip filter/object dependence
TheNullicorn Jun 29, 2021
9978d84
Clarify documentation for players
TheNullicorn Jul 11, 2021
757a680
Use ZonedDateTime instead of Date
TheNullicorn Jul 11, 2021
12654da
Use a more informative example
TheNullicorn Jul 11, 2021
c6a3450
Add a note about getRaw()
TheNullicorn Jul 11, 2021
c91c40f
Suppression on lazily assigned field
TheNullicorn Jul 11, 2021
b964dda
Change toString() for null players
TheNullicorn Jul 11, 2021
bd235f9
Manual merge
ConnorLinfoot Jul 26, 2021
657ea8b
Fix broken import
ConnorLinfoot Jul 26, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ the [Example Code](https://github.com/HypixelDev/PublicAPI/tree/master/hypixel-a
#### Hypixel Maven Repo

```xml

<repository>
<id>Hypixel</id>
<url>https://repo.hypixel.net/repository/Hypixel/</url>
Expand Down
59 changes: 41 additions & 18 deletions hypixel-api-core/src/main/java/net/hypixel/api/HypixelAPI.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
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.PropertyFilter;
import net.hypixel.api.util.ResourceType;
import net.hypixel.api.util.Utilities;

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;
Expand Down Expand Up @@ -81,6 +69,31 @@ public CompletableFuture<PlayerReply> getPlayerByUuid(String player) {
);
}

/**
* Same as {@link #getPlayerByUuid(UUID)}, but the resulting player object will only contain
* properties explicitly included via a {@link PropertyFilter filter}.
*/
public CompletableFuture<PlayerReply> getPlayerByUuid(UUID player, PropertyFilter filter) {
return applyFilterFuture(getPlayerByUuid(player), filter);
}

/**
* Same as {@link #getPlayerByUuid(String)}, but the resulting player object will only contain
* properties explicitly included via a {@link PropertyFilter filter}.
*/
public CompletableFuture<PlayerReply> getPlayerByUuid(String player, PropertyFilter filter) {
return applyFilterFuture(getPlayerByUuid(player), filter);
}

/**
* Same as {@link #getPlayerByName(String)}, but the resulting player object will only contain
* properties explicitly included via a {@link PropertyFilter filter}.
*/
@Deprecated
public CompletableFuture<PlayerReply> getPlayerByName(String player, PropertyFilter filter) {
return applyFilterFuture(getPlayerByName(player), filter);
}

/**
* @param player the minecraft username of the player.
* @return {@link CompletableFuture} containing {@link PlayerReply}
Expand Down Expand Up @@ -257,6 +270,16 @@ public CompletableFuture<SkyBlockBazaarReply> getSkyBlockBazaar() {
return get(SkyBlockBazaarReply.class, "skyblock/bazaar");
}

/**
* Applies a {@code filter} to a player object when it is received in an API response.
*/
private CompletableFuture<PlayerReply> applyFilterFuture(CompletableFuture<PlayerReply> future, PropertyFilter filter) {
return future.thenApply(reply -> {
reply.getPlayer().filter(filter);
return reply;
});
}

private <R extends AbstractReply> CompletableFuture<R> get(Class<R> clazz, String request) {
return get(clazz, request, null);
}
Expand All @@ -270,16 +293,16 @@ private <R extends AbstractReply> CompletableFuture<R> get(Class<R> clazz, Strin
.thenApply(this::checkResponse)
.thenApply(response -> {
if (clazz == ResourceReply.class) {
return checkReply((R) new ResourceReply(GSON.fromJson(response.getBody(), JsonObject.class)));
return checkReply((R) new ResourceReply(Utilities.GSON.fromJson(response.getBody(), JsonObject.class)));
}
return checkReply(GSON.fromJson(response.getBody(), clazz));
return checkReply(Utilities.GSON.fromJson(response.getBody(), clazz));
});
}

private CompletableFuture<ResourceReply> requestResource(String resource) {
return httpClient.makeRequest(BASE_URL + "resources/" + resource)
.thenApply(this::checkResponse)
.thenApply(response -> checkReply(new ResourceReply(GSON.fromJson(response.getBody(), JsonObject.class))));
.thenApply(response -> checkReply(new ResourceReply(Utilities.GSON.fromJson(response.getBody(), JsonObject.class))));
}

/**
Expand All @@ -292,7 +315,7 @@ private HypixelHttpResponse checkResponse(HypixelHttpResponse response) {

String cause;
try {
cause = GSON.fromJson(response.getBody(), JsonObject.class).get("cause").getAsString();
cause = Utilities.GSON.fromJson(response.getBody(), JsonObject.class).get("cause").getAsString();
} catch (JsonSyntaxException ignored) {
cause = "Unknown (body is not json)";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package net.hypixel.api.adapters;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import net.hypixel.api.reply.PlayerReply.Player;

import java.io.IOException;

public class PlayerTypeAdapter extends TypeAdapter<Player> {

private final TypeAdapter<JsonElement> defaultAdapter;

public PlayerTypeAdapter() {
defaultAdapter = new Gson().getAdapter(JsonElement.class);
}

@Override
public void write(JsonWriter out, Player value) throws IOException {
defaultAdapter.write(out, value.getRaw());
}

@Override
public Player read(JsonReader in) throws IOException {
JsonToken type = in.peek();
if (type == JsonToken.NULL) {
in.nextNull();
return new Player(null);
}
return new Player(defaultAdapter.read(in));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.hypixel.api.adapters;

import com.google.gson.*;
import net.hypixel.api.util.Utilities;

import java.lang.reflect.Type;
import java.util.UUID;
Expand All @@ -14,11 +15,6 @@ public JsonElement serialize(UUID src, Type typeOfSrc, JsonSerializationContext

@Override
public UUID deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
String uuid = json.getAsString();
if (uuid.contains("-")) {
return UUID.fromString(uuid);
} else {
return UUID.fromString(uuid.substring(0, 8) + "-" + uuid.substring(8, 12) + "-" + uuid.substring(12, 16) + "-" + uuid.substring(16, 20) + "-" + uuid.substring(20, 32));
}
return Utilities.uuidFromString(json.getAsString());
}
}
Loading