Created
June 1, 2020 23:33
-
-
Save TheNullicorn/f86a7a57ca713033d71062a8e25636c6 to your computer and use it in GitHub Desktop.
Sample Java program for the Hypixel API Wiki
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package <YOUR_PACKAGE_NAME>; | |
| import com.google.gson.JsonElement; | |
| import com.google.gson.JsonObject; | |
| import java.util.Scanner; | |
| import java.util.UUID; | |
| import net.hypixel.api.HypixelAPI; | |
| public class <YOUR_CLASS_NAME> { | |
| public static void main(String[] args) { | |
| // Be sure to replace YOUR_API_KEY with... well, your API key | |
| HypixelAPI api = new HypixelAPI(UUID.fromString("YOUR_API_KEY")); | |
| // Print instruction message | |
| System.out.println("Hello! Type a username to view their stats, or type \"q\" to quit!"); | |
| // Continuously read from the console | |
| Scanner sc = new Scanner(System.in); | |
| while (sc.hasNextLine()) { | |
| // Get the line of text that was typed | |
| String line = sc.nextLine().replaceAll("\n", ""); | |
| // Make sure some text was typed | |
| if (line.length() == 0) { | |
| continue; | |
| } | |
| // Quit if the input is "Q" or "q" | |
| if (line.equalsIgnoreCase("Q")) { | |
| System.out.println("Goodbye!"); | |
| System.exit(0); | |
| } | |
| /* | |
| Make sure the username is valid | |
| The regular expression (RegEx) used here checks for between 3 and 16 characters that are | |
| in the ranges A-Z, a-z, 0-9, or _ (underscores) | |
| */ | |
| if (!line.matches("^\\w{3,16}$")) { | |
| System.err.println("Invalid username. Please try again!"); | |
| continue; | |
| } | |
| /* | |
| NOTE: We are only using getPlayerByName() for the sake of convenience. In real projects, | |
| you should avoid using deprecated methods like this as they could potentially be removed | |
| in the future. Instead, you should get the player's UUID from the Mojang API and pass | |
| that into getPlayerByUuid() | |
| The .whenComplete(...) tells the CompletableFuture what code we want to run when the API | |
| responds. In our case, we want to print out the player's stats, so that's what we put | |
| between the two curly brackets [e.g "(response, error) -> {...}"] | |
| */ | |
| api.getPlayerByName(line).whenComplete((response, error) -> { | |
| // Check if there was an API error | |
| if (error != null) { | |
| error.printStackTrace(); | |
| return; | |
| } | |
| // Get the player object from the response. This object stores any available information about a given player | |
| JsonObject player = response.getPlayer(); | |
| if (player != null) { | |
| /* | |
| The player was found; print some basic info about them | |
| Not every player has all of these fields (for example, staff members don't have | |
| "mostRecentGameType"), so I added a handy little method to check if a field | |
| exists. If it doesn't, it returns "N/A" rather than throwing a | |
| NullPounterException | |
| */ | |
| System.out.println("Name: " + getFieldOrNA("displayname", player)); | |
| System.out.println("UUID: " + getFieldOrNA("uuid", player)); | |
| System.out.println("Network Experience: " + getFieldOrNA("networkExp", player)); | |
| System.out.println("Most Recent Game: " + getFieldOrNA("mostRecentGameType", player)); | |
| } else { | |
| // If we're here, it means that Hypixel has no info on this player | |
| System.err.println("That player was not found"); | |
| } | |
| }); | |
| } | |
| } | |
| private static String getFieldOrNA(String field, JsonObject json) { | |
| JsonElement value = json.get(field); | |
| if (value != null) { | |
| // If the field was found, return its value | |
| return value.getAsString(); | |
| } else { | |
| // Otherwise, return "N/A" | |
| return "N/A"; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment