diff options
Diffstat (limited to 'src/main/java/eu/olli/cowmoonication/util')
3 files changed, 162 insertions, 0 deletions
diff --git a/src/main/java/eu/olli/cowmoonication/util/ApiUtils.java b/src/main/java/eu/olli/cowmoonication/util/ApiUtils.java index 5ff7d5d..d408721 100644 --- a/src/main/java/eu/olli/cowmoonication/util/ApiUtils.java +++ b/src/main/java/eu/olli/cowmoonication/util/ApiUtils.java @@ -5,10 +5,13 @@ import com.google.gson.GsonBuilder; import com.google.gson.JsonArray; import com.google.gson.JsonParser; import com.mojang.util.UUIDTypeAdapter; +import eu.olli.cowmoonication.Cowmoonication; +import eu.olli.cowmoonication.config.MooConfig; import eu.olli.cowmoonication.friends.Friend; import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; @@ -21,6 +24,7 @@ public class ApiUtils { public static final String UUID_NOT_FOUND = "UUID-NOT-FOUND"; private static final String NAME_TO_UUID_URL = "https://api.mojang.com/users/profiles/minecraft/"; private static final String UUID_TO_NAME_URL = "https://api.mojang.com/user/profiles/%s/names"; + private static final String STALKING_URL = "https://api.hypixel.net/status?key=%s&uuid=%s"; private static ExecutorService pool = Executors.newCachedThreadPool(); private static Gson gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).registerTypeAdapter(Friend.class, new Friend.FriendCreator()).create(); @@ -67,4 +71,34 @@ public class ApiUtils { } return null; } + + public static void fetchPlayerStatus(Friend friend, Consumer<HyStalking> action) { + pool.execute(() -> action.accept(stalkPlayer(friend))); + } + + private static HyStalking stalkPlayer(Friend friend) { + try { + HttpURLConnection connection = (HttpURLConnection) new URL(String.format(STALKING_URL, MooConfig.moo, UUIDTypeAdapter.fromUUID(friend.getUuid()))).openConnection(); + connection.setReadTimeout(5000); + connection.addRequestProperty("User-Agent", "Forge Mod " + Cowmoonication.MODNAME + "/" + Cowmoonication.VERSION + " (https://github.com/cow-mc/Cowmoonication/)"); + + connection.getResponseCode(); + if (connection.getResponseCode() == 204) { + return null; + } else { // various possible http status code: 200, 403, 422 + BufferedReader reader; + InputStream errorStream = connection.getErrorStream(); + if (errorStream != null) { + reader = new BufferedReader(new InputStreamReader(errorStream)); + } else { + reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + } + + return gson.fromJson(reader, HyStalking.class); + } + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } } diff --git a/src/main/java/eu/olli/cowmoonication/util/HyStalking.java b/src/main/java/eu/olli/cowmoonication/util/HyStalking.java new file mode 100644 index 0000000..a46e1b5 --- /dev/null +++ b/src/main/java/eu/olli/cowmoonication/util/HyStalking.java @@ -0,0 +1,123 @@ +package eu.olli.cowmoonication.util; + +import org.apache.commons.lang3.text.WordUtils; + +public class HyStalking { + private boolean success; + private String cause; + private HySession session; + + public HyStalking() { + } + + public boolean isSuccess() { + return success; + } + + public String getCause() { + return cause; + } + + public HySession getSession() { + return session; + } + + public static class HySession { + private boolean online; + private String gameType; + private String mode; + private String map; + + public HySession() { + } + + public boolean isOnline() { + return online; + } + + public String getGameType() { + String cleanGameType; + try { + cleanGameType = GameType.valueOf(gameType).getCleanName(); + } catch (IllegalArgumentException e) { + // no matching game type found + cleanGameType = WordUtils.capitalizeFully(gameType.replace('_', ' ')); + } + return cleanGameType; + } + + public String getMode() { + // list partially taken from https://api.hypixel.net/gameCounts?key=MOO + switch (mode) { + // SkyBlock related + case "dynamic": + return "Private Island"; + case "hub": + return "Hub"; + case "combat_1": + return "Spider's Den"; + case "combat_2": + return "Blazing Fortress"; + case "combat_3": + return "The End"; + case "farming_1": + return "The Barn"; + case "farming_2": + return "Mushroom Desert"; + case "foraging_1": + return "The Park"; + case "mining_1": + return "Gold Mine"; + case "mining_2": + return "Deep Caverns"; + default: + return WordUtils.capitalizeFully(mode.replace('_', ' ')); + } + } + + public String getMap() { + return map; + } + + // TODO replace with api request: https://github.com/HypixelDev/PublicAPI/blob/master/Documentation/misc/GameType.md + public enum GameType { + QUAKECRAFT("Quakecraft"), + WALLS("Walls"), + PAINTBALL("Paintball"), + SURVIVAL_GAMES("Blitz Survival Games"), + TNTGAMES("The TNT Games"), + VAMPIREZ("VampireZ"), + WALLS3("Mega Walls"), + ARCADE("Arcade"), + ARENA("Arena Brawl"), + UHC("UHC Champions"), + MCGO("Cops and Crims"), + BATTLEGROUND("Warlords"), + SUPER_SMASH("Smash Heroes"), + GINGERBREAD("Turbo Kart Racers"), + HOUSING("Housing"), + SKYWARS("SkyWars"), + TRUE_COMBAT("Crazy Walls"), + SPEED_UHC("Speed UHC"), + SKYCLASH("SkyClash"), + LEGACY("Classic Games"), + PROTOTYPE("Prototype"), + BEDWARS("Bed Wars"), + MURDER_MYSTERY("Murder Mystery"), + BUILD_BATTLE("Build Battle"), + DUELS("Duels"), + SKYBLOCK("SkyBlock"), + PIT("Pit"); + + private final String cleanName; + + GameType(String cleanName) { + this.cleanName = cleanName; + } + + public String getCleanName() { + return cleanName; + } + } + } +} diff --git a/src/main/java/eu/olli/cowmoonication/util/Utils.java b/src/main/java/eu/olli/cowmoonication/util/Utils.java index 0b890f0..ec96bf1 100644 --- a/src/main/java/eu/olli/cowmoonication/util/Utils.java +++ b/src/main/java/eu/olli/cowmoonication/util/Utils.java @@ -3,11 +3,16 @@ package eu.olli.cowmoonication.util; import java.util.regex.Pattern; public final class Utils { + public static final Pattern VALID_UUID_PATTERN = Pattern.compile("^(\\w{8})-(\\w{4})-(\\w{4})-(\\w{4})-(\\w{12})$"); private static final Pattern VALID_USERNAME = Pattern.compile("^[\\w]{1,16}$"); private Utils() { } + public static boolean isValidUuid(String uuid) { + return VALID_UUID_PATTERN.matcher(uuid).matches(); + } + public static boolean isValidMcName(String username) { return VALID_USERNAME.matcher(username).matches(); } |