· 6 years ago · Jan 14, 2020, 06:56 PM
1import com.kbrewster.*;
2import com.kbrewster.hypixelapi.player.*;
3import java.io.*;
4import com.kbrewster.hypixelapi.session.*;
5import com.kbrewster.mojangapi.*;
6import com.kbrewster.hypixelapi.friends.*;
7import java.util.*;
8import com.kbrewster.hypixelapi.boosters.*;
9import com.kbrewster.hypixelapi.leaderboards.*;
10import com.kbrewster.hypixelapi.guild.*;
11import com.kbrewster.exceptions.*;
12import com.kbrewster.hypixelapi.key.*;
13import com.google.gson.*;
14
15@Reference(apiName = "Hypixel API", apiVersion = "1.1.2")
16public class HypixelAPI extends API
17{
18 private final String BASE_URL = "https://api.hypixel.net";
19 private final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36";
20 private String key;
21
22 public HypixelAPI(final String key) {
23 this.key = key;
24 }
25
26 public HypixelPlayer getPlayer(final String name) throws APIException, InvalidPlayerException, IOException {
27 final Gson gson = new Gson();
28 final String url = String.format("https://api.hypixel.net/player?name=%s&key=%s", name, this.key);
29 final JsonObject json = this.readJsonUrl(url);
30 if (!json.get("success").getAsBoolean()) {
31 throw new APIException(json.get("cause").getAsString());
32 }
33 final JsonElement player = json.get("player");
34 if (player.isJsonNull()) {
35 throw new InvalidPlayerException();
36 }
37 return gson.fromJson(player, HypixelPlayer.class);
38 }
39
40 public HypixelPlayer getPlayerByUUID(final String uuid) throws APIException, InvalidPlayerException, IOException {
41 final Gson gson = new Gson();
42 final String url = String.format("https://api.hypixel.net/player?uuid=%s&key=%s", uuid, this.key);
43 final JsonObject json = this.readJsonUrl(url);
44 if (!json.get("success").getAsBoolean()) {
45 throw new APIException(json.get("cause").getAsString());
46 }
47 final JsonElement player = json.get("player");
48 if (player.isJsonNull()) {
49 throw new InvalidPlayerException();
50 }
51 return gson.fromJson(player, HypixelPlayer.class);
52 }
53
54 public Session getSession(final String name) throws APIException, IOException {
55 final String uuid = MojangAPI.getUUID(name);
56 return this.getSessionByUUID(uuid);
57 }
58
59 public Session getSessionByUUID(final String uuid) throws APIException, IOException {
60 final Gson gson = new Gson();
61 final String url = String.format("https://api.hypixel.net/session?uuid=%s&key=%s", uuid, this.key);
62 final JsonObject json = this.readJsonUrl(url);
63 if (!json.get("success").getAsBoolean()) {
64 throw new APIException(json.get("cause").getAsString());
65 }
66 final JsonElement session = json.get("session");
67 if (session.isJsonNull()) {
68 throw new APIException("No session found!");
69 }
70 return gson.fromJson(session, Session.class);
71 }
72
73 public List<Friend> getFriends(final String name) throws APIException, InvalidPlayerException, IOException {
74 final String uuid = MojangAPI.getUUID(name);
75 return this.getFriendsByUUID(uuid);
76 }
77
78 public List<Friend> getFriendsByUUID(final String uuid) throws APIException, InvalidPlayerException, IOException {
79 final Gson gson = new Gson();
80 final ArrayList<Friend> friends = new ArrayList<Friend>();
81 final String url = String.format("https://api.hypixel.net/friends?uuid=%s&key=%s", uuid, this.key);
82 final JsonObject json = this.readJsonUrl(url);
83 if (!json.get("success").getAsBoolean()) {
84 throw new APIException(json.get("cause").getAsString());
85 }
86 final JsonElement records = json.get("records");
87 if (records.isJsonNull()) {
88 throw new InvalidPlayerException();
89 }
90 records.getAsJsonArray().forEach(jsonElement -> friends.add(gson.fromJson(jsonElement, Friend.class)));
91 return friends;
92 }
93
94 public List<Booster> getBoosters() throws APIException, IOException {
95 final Gson gson = new Gson();
96 final ArrayList<Booster> boosters = new ArrayList<Booster>();
97 final String url = String.format("https://api.hypixel.net/boosters?key=%s", this.key);
98 final JsonObject json = this.readJsonUrl(url);
99 if (!json.get("success").getAsBoolean()) {
100 throw new APIException(json.get("cause").getAsString());
101 }
102 final JsonElement records = json.get("boosters");
103 if (records.isJsonNull()) {
104 throw new APIException("No boosters found!");
105 }
106 records.getAsJsonArray().forEach(jsonElement -> boosters.add(gson.fromJson(jsonElement, Booster.class)));
107 return boosters;
108 }
109
110 public List<Leaderboard> getLeaderboard(final Leaderboards leaderboard) throws APIException, IOException {
111 final Gson gson = new Gson();
112 final ArrayList<Leaderboard> leaderboards = new ArrayList<Leaderboard>();
113 final String url = String.format("https://api.hypixel.net/leaderboards?key=%s", this.key);
114 final JsonObject json = this.readJsonUrl(url);
115 if (!json.get("success").getAsBoolean()) {
116 throw new APIException(json.get("cause").getAsString());
117 }
118 final JsonObject lb = json.getAsJsonObject("leaderboards");
119 System.out.println(leaderboard.name());
120 lb.getAsJsonArray(leaderboard.name()).forEach(element -> leaderboards.add(gson.fromJson(element, Leaderboard.class)));
121 return leaderboards;
122 }
123
124 public String getGuildID(final String name) throws IOException, APIException {
125 final String uuid = MojangAPI.getUUID(name);
126 return this.getGuildIDByUUID(uuid);
127 }
128
129 public Guild getGuild(final String guildID) throws IOException, APIException {
130 final Gson gson = new Gson();
131 final String url = String.format("https://api.hypixel.net/guild?key=%s&id=%s", this.key, guildID);
132 final JsonObject json = this.readJsonUrl(url);
133 if (!json.get("success").getAsBoolean()) {
134 throw new APIException(json.get("cause").getAsString());
135 }
136 final JsonElement guild = json.get("guild");
137 if (guild.isJsonNull()) {
138 throw new InvalidGuildException();
139 }
140 return gson.fromJson(guild, Guild.class);
141 }
142
143 public String getGuildIDByUUID(final String uuid) throws IOException, APIException {
144 final String url = String.format("https://api.hypixel.net/findGuild?byUuid=%s&key=%s", uuid, this.key);
145 final JsonObject json = this.readJsonUrl(url);
146 if (!json.get("success").getAsBoolean()) {
147 throw new APIException(json.get("cause").getAsString());
148 }
149 return json.get("guild").getAsString();
150 }
151
152 public APIKey getKeyInfo() throws IOException, APIException {
153 final Gson gson = new Gson();
154 final String url = String.format("https://api.hypixel.net/key?key=%s", this.key);
155 final JsonObject json = this.readJsonUrl(url);
156 if (!json.get("success").getAsBoolean()) {
157 throw new APIException(json.get("cause").getAsString());
158 }
159 final JsonElement record = json.get("record");
160 if (record.isJsonNull()) {
161 throw new APIException("No record found!");
162 }
163 return gson.fromJson(record, APIKey.class);
164 }
165
166 private JsonObject readJsonUrl(final String url) throws IOException {
167 final JsonElement jElement = new JsonParser().parse(API.sendGet(url, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"));
168 return jElement.getAsJsonObject();
169 }