From 92b4be07f29a5f3e5af5e00a39b9b831ec1f4d68 Mon Sep 17 00:00:00 2001 From: MX233 <3225971360@qq.com> Date: Thu, 7 Oct 2021 15:16:49 +0800 Subject: [PATCH] Update --- README.md | 46 ++++++++- src/tax/cute/minecraftinfoapi/ApiUrl.java | 8 -- .../minecraftinfoapi/CommonException.java | 7 ++ src/tax/cute/minecraftinfoapi/Main.java | 85 ---------------- .../minecraftinfoapi/MultiplePlayers.java | 32 +++++++ .../cute/minecraftinfoapi/NameHistory.java | 61 ++++++++++++ .../minecraftinfoapi/NameHistoryData.java | 22 +++++ src/tax/cute/minecraftinfoapi/Player.java | 40 ++++++++ src/tax/cute/minecraftinfoapi/PlayerInfo.java | 96 ------------------- src/tax/cute/minecraftinfoapi/Profile.java | 89 +++++++++++++++++ .../minecraftinfoapi/SalesOfMinecraft.java | 60 ++++++++++++ src/tax/cute/minecraftinfoapi/Skin.java | 89 ----------------- src/tax/cute/minecraftinfoapi/Status.java | 21 ++-- src/tax/cute/minecraftinfoapi/UUID.java | 35 ------- src/tax/cute/minecraftinfoapi/Util.java | 41 -------- .../cute/minecraftinfoapi/utils/ApiUrl.java | 10 ++ src/tax/cute/minecraftinfoapi/utils/Http.java | 46 +++++++++ src/tax/cute/minecraftinfoapi/utils/Util.java | 64 +++++++++++++ 18 files changed, 484 insertions(+), 368 deletions(-) delete mode 100644 src/tax/cute/minecraftinfoapi/ApiUrl.java create mode 100644 src/tax/cute/minecraftinfoapi/CommonException.java delete mode 100644 src/tax/cute/minecraftinfoapi/Main.java create mode 100644 src/tax/cute/minecraftinfoapi/MultiplePlayers.java create mode 100644 src/tax/cute/minecraftinfoapi/NameHistory.java create mode 100644 src/tax/cute/minecraftinfoapi/NameHistoryData.java create mode 100644 src/tax/cute/minecraftinfoapi/Player.java delete mode 100644 src/tax/cute/minecraftinfoapi/PlayerInfo.java create mode 100644 src/tax/cute/minecraftinfoapi/Profile.java create mode 100644 src/tax/cute/minecraftinfoapi/SalesOfMinecraft.java delete mode 100644 src/tax/cute/minecraftinfoapi/Skin.java delete mode 100644 src/tax/cute/minecraftinfoapi/UUID.java delete mode 100644 src/tax/cute/minecraftinfoapi/Util.java create mode 100644 src/tax/cute/minecraftinfoapi/utils/ApiUrl.java create mode 100644 src/tax/cute/minecraftinfoapi/utils/Http.java create mode 100644 src/tax/cute/minecraftinfoapi/utils/Util.java diff --git a/README.md b/README.md index bc3e879..e1f6dcc 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,9 @@ A convenient api to get Minecraft uuid and skin If you cannot read Chinese, please use Google Translate == -`MinecraftInfoAPI` 是一个可以让你轻松获取`Minecraft` 玩家信息的api +`MinecraftInfoAPI` 是一个可以让你轻松获取`Minecraft` 玩家信息的库 + +没什么技术力,只是方便点 目前,`MinecraftInfoAPI` 有以下功能: - 通过`用户名` 获取`UUID` 或通过`UUID` 反查`用户名` @@ -12,10 +14,46 @@ If you cannot read Chinese, please use Google Translate - 通过`UUID` 获得 `名称历史` 以及名称的修改时间 - 获取`Minecraft` 相关网站的状态(例如`Minecraft.net` `mojang.com`) -`MinecraftInfoAPI` 需要[`Fastjson`](https://github.com/alibaba/fastjson) 才能使用 +本项目语法和编译版本为`Java8` -到[Releases](https://github.com/MX233/MinecraftInfoAPI/releases) 下载依赖并导入到你的项目中即可使用 +用到的库 : [`Fastjson`](https://github.com/alibaba/fastjson) -使用示例:[Main.java](https://github.com/MX233/MinecraftInfoAPI/blob/main/src/tax/cute/minecraftinfoapi/Main.java) +到[Releases](https://github.com/MX233/MinecraftInfoAPI/releases) 下载并导入到你的项目中即可使用 我是一个新手,可能做的不好,你可以clone下来自行修改 + +用法: + +- 用户名查询UUID + + Player player = Player.getPlayer("CuteStarX"); + System.out.println(player.getName() + " 的UUID是: " + player.getUuid()); + +- 查询10个以内的玩家UUID + + List players = new ArrayList<>(); + players.add("CuteStarX"); + + Map map = new MultiplePlayers().getPlayers(players); + map.keySet().forEach(uuid -> System.out.println(map.get(uuid) + " 的UUID是: " + uuid)); + +- 查询玩家曾用名 + + NameHistory nameHistory = NameHistory.getNameHistory("e3beb716afa1451b96c6ddfebd1ce1fb"); + System.out.println("这个UUID现在的玩家名称为: " + nameHistory.getCurrentName()); + System.out.println("这个UUID初始玩家名称为: " + nameHistory.getInitName()); + + System.out.println("这个UUID使用过的玩家名称:"); + nameHistory.getNameHistoryData().forEach(data -> System.out.println("名称: " + data.getName() + " 修改时间: " + Util.toTime(data.getChangedToAt()))); + +- 查询玩家皮肤 + + Profile profile = Profile.getProfile(Player.getPlayer("CuteStarX").getUuid()); + System.out.println("皮肤模型是: " + profile.getModel()); + System.out.println("皮肤链接是: " + profile.getSkinUrl()); + System.out.println("披风链接是: " + profile.getCapeUrl()); + + //将皮肤保存到本地 + try (OutputStream out = new FileOutputStream("skin.png")) { + out.write(profile.getSkinBytes()); + } \ No newline at end of file diff --git a/src/tax/cute/minecraftinfoapi/ApiUrl.java b/src/tax/cute/minecraftinfoapi/ApiUrl.java deleted file mode 100644 index d84f4f3..0000000 --- a/src/tax/cute/minecraftinfoapi/ApiUrl.java +++ /dev/null @@ -1,8 +0,0 @@ -package tax.cute.minecraftinfoapi; - -public class ApiUrl { - public static final String STATUS_API = "https://status.mojang.com/check"; - public static final String USER_NAME_TO_UUID_API = "https://api.mojang.com/users/profiles/minecraft/"; - public static final String UUID_TO_NAME_HISTORY = "https://api.mojang.com/user/profiles/%id/names"; - public static final String SKIN_API = "https://sessionserver.mojang.com/session/minecraft/profile/"; -} \ No newline at end of file diff --git a/src/tax/cute/minecraftinfoapi/CommonException.java b/src/tax/cute/minecraftinfoapi/CommonException.java new file mode 100644 index 0000000..51a6b73 --- /dev/null +++ b/src/tax/cute/minecraftinfoapi/CommonException.java @@ -0,0 +1,7 @@ +package tax.cute.minecraftinfoapi; + +public class CommonException extends Exception{ + public CommonException(String msg) { + super(msg); + } +} diff --git a/src/tax/cute/minecraftinfoapi/Main.java b/src/tax/cute/minecraftinfoapi/Main.java deleted file mode 100644 index 0901133..0000000 --- a/src/tax/cute/minecraftinfoapi/Main.java +++ /dev/null @@ -1,85 +0,0 @@ -package tax.cute.minecraftinfoapi; - -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStream; - -public class Main { - public static void main(String[] args)throws IOException { - //No data will return a null pointer - //It is recommended to pay attention in actual use - getUUid("CuteStarX"); - System.out.println(); - getSkin("e3beb716-afa1-451b-96c6-ddfebd1ce1fb"); - System.out.println(); - getPlayer("e3beb716-afa1-451b-96c6-ddfebd1ce1fb"); - } - - static void getUUid(String userName) throws IOException{ - UUID uuid = UUID.getId(userName); - if (uuid == null) { - System.out.println("Failed to get uuid"); - return; - } - System.out.println( - "uuid of " + uuid.getName() + " is:" + uuid.getId() - ); - } - - static void getStatus() throws IOException{ - Status status = Status.getStatus(); - System.out.println("Mojang.com status is:" + status.getMojang_com()); - System.out.println("Minecraft.com status is:" + status.getMinecraft_net()); - //... - } - - static void getSkin(String uuid) throws IOException{ - Skin skin = Skin.getSkin(uuid); - if (skin == null) { - System.out.println("Failed to get skin"); - return; - } - System.out.println("The link to this skin is:" + skin.getSkinUrl()); -// System.out.println("The link to this skin is:" + skin.getCapeUrl()); - boolean isDownloadSkin = false; - boolean isDownloadCape = false; - - if (isDownloadSkin) { - OutputStream out = new FileOutputStream("\\Skin.PNG"); - out.write(skin.getSkinBytes()); - System.out.println("Skin download is complete"); - out.flush(); - out.close(); - } - -// if (isDownloadCape) { -// OutputStream out = new FileOutputStream("\\Cape.PNG"); -// out.write(skin.getCapeBytes()); -// System.out.println("Cape download is complete"); -// out.flush(); -// out.close(); -// } - } - - static void getPlayer(String uuid){ - PlayerInfo info = PlayerInfo.getPlayerInfo(uuid); - if (info == null) { - System.out.println("Failed to get player information"); - return; - } - System.out.println("Now name:" + info.getNowName()); - System.out.println("Init name:" + info.getInitName()); - System.out.println(); - for (int i = 0; i < info.getData().size(); i++) { - String time; - if(info.getData().get(i).getTimestamp() != -1) - time = info.getData().get(i).getTime(); - else - time = "Init Name"; - System.out.println("Name:" + info.getData().get(i).getName()); - System.out.println("Change time:" + time); - System.out.println(); - } - } - -} \ No newline at end of file diff --git a/src/tax/cute/minecraftinfoapi/MultiplePlayers.java b/src/tax/cute/minecraftinfoapi/MultiplePlayers.java new file mode 100644 index 0000000..1f4611d --- /dev/null +++ b/src/tax/cute/minecraftinfoapi/MultiplePlayers.java @@ -0,0 +1,32 @@ +package tax.cute.minecraftinfoapi; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import tax.cute.minecraftinfoapi.utils.ApiUrl; +import tax.cute.minecraftinfoapi.utils.Util; + +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class MultiplePlayers { + public Map getPlayers(List players) throws IOException,CommonException { + if(players.isEmpty()) + throw new CommonException("Players is empty"); + if(players.size() > 10) + throw new CommonException("Players too big,It cannot be greater than 10"); + JSONArray array = new JSONArray(); + array.addAll(players); + + array = JSONArray.parseArray(Util.sendPost(ApiUrl.PLAYERS,"Content-Type","application/json",array.toJSONString().getBytes())); + + Map data = new HashMap<>(); + array.forEach(object -> { + JSONObject jsonObject = (JSONObject)object; + data.put(jsonObject.getString("id"),jsonObject.getString("name")); + }); + + return data; + } +} diff --git a/src/tax/cute/minecraftinfoapi/NameHistory.java b/src/tax/cute/minecraftinfoapi/NameHistory.java new file mode 100644 index 0000000..88956f4 --- /dev/null +++ b/src/tax/cute/minecraftinfoapi/NameHistory.java @@ -0,0 +1,61 @@ +package tax.cute.minecraftinfoapi; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import tax.cute.minecraftinfoapi.utils.ApiUrl; +import tax.cute.minecraftinfoapi.utils.Http; +import tax.cute.minecraftinfoapi.utils.Util; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.util.ArrayList; +import java.util.List; + +public class NameHistory { + private final List nameHistoryData; + private final String uuid; + + public NameHistory( + List nameHistoryData, + String uuid + ) { + this.nameHistoryData = nameHistoryData; + this.uuid = uuid; + } + + public static NameHistory getNameHistory(String uuid) throws IOException,CommonException { + if (uuid == null || uuid.isEmpty() || !Util.isUuid(uuid)) + throw new CommonException("Input error"); + Http http = Http.getHttp(ApiUrl.NAME_HISTORY + .replace("%uuid", uuid) + ); + if (http.getCode() != HttpURLConnection.HTTP_OK) + throw new CommonException("no this player"); + + List nameHistoryData = new ArrayList<>(); + + JSONArray array = JSONArray.parseArray(http.getTextString()); + array.forEach(object -> { + JSONObject json = (JSONObject) object; + nameHistoryData.add(new NameHistoryData(json.getString("name"), Util.notNullLong(json.getLong("changedToAt")))); + }); + + return new NameHistory(nameHistoryData,uuid); + } + + public List getNameHistoryData() { + return nameHistoryData; + } + + public String getInitName() { + return nameHistoryData.get(0).getName(); + } + + public String getCurrentName() { + return nameHistoryData.get(nameHistoryData.size() - 1).getName(); + } + + public String getUuid() { + return uuid; + } +} \ No newline at end of file diff --git a/src/tax/cute/minecraftinfoapi/NameHistoryData.java b/src/tax/cute/minecraftinfoapi/NameHistoryData.java new file mode 100644 index 0000000..04311b1 --- /dev/null +++ b/src/tax/cute/minecraftinfoapi/NameHistoryData.java @@ -0,0 +1,22 @@ +package tax.cute.minecraftinfoapi; + +public class NameHistoryData { + private final String name; + private final long changedToAt; + + public NameHistoryData( + String name, + long changedToAt + ) { + this.name = name; + this.changedToAt = changedToAt; + } + + public String getName() { + return name; + } + + public long getChangedToAt() { + return changedToAt; + } +} diff --git a/src/tax/cute/minecraftinfoapi/Player.java b/src/tax/cute/minecraftinfoapi/Player.java new file mode 100644 index 0000000..93ccb70 --- /dev/null +++ b/src/tax/cute/minecraftinfoapi/Player.java @@ -0,0 +1,40 @@ +package tax.cute.minecraftinfoapi; + +import com.alibaba.fastjson.JSONObject; +import tax.cute.minecraftinfoapi.utils.*; + +import java.io.IOException; +import java.net.HttpURLConnection; + +public class Player { + private final String name; + private final String uuid; + + public Player( + String name, + String uuid + ) { + this.name = name; + this.uuid = uuid; + } + + public static Player getPlayer(String name) throws IOException,CommonException { + if (name == null || name.isEmpty() || !Util.isLegalUsername(name))throw new CommonException("Illegal username"); + Http http = Http.getHttp(ApiUrl.PLAYER + .replace("%name",name) + ); + + if (http.getCode() != HttpURLConnection.HTTP_OK)throw new CommonException("No this player"); + + JSONObject json = JSONObject.parseObject(http.getTextString()); + return new Player(json.getString("name"),json.getString("id")); + } + + public String getName() { + return name; + } + + public String getUuid() { + return uuid; + } +} \ No newline at end of file diff --git a/src/tax/cute/minecraftinfoapi/PlayerInfo.java b/src/tax/cute/minecraftinfoapi/PlayerInfo.java deleted file mode 100644 index fd50d15..0000000 --- a/src/tax/cute/minecraftinfoapi/PlayerInfo.java +++ /dev/null @@ -1,96 +0,0 @@ -package tax.cute.minecraftinfoapi; - -import com.alibaba.fastjson.JSONArray; -import com.alibaba.fastjson.JSONObject; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -public class PlayerInfo { - private List data; - private List nameList; - public PlayerInfo(List data, List nameList) { - this.data = data; - this.nameList = nameList; - } - - public static PlayerInfo getPlayerInfo(String uuid){ - String jsonText; - try { - jsonText = Util.getWebText(ApiUrl.UUID_TO_NAME_HISTORY.replace("%id", uuid)); - } catch (IOException e) { - return null; - } - - JSONArray array = JSONArray.parseArray(jsonText); - - if (array == null) { - System.out.println("Failed to get player information"); - return null; - } - - List data = new ArrayList<>(); - List nameList = new ArrayList<>(); - - for (int i = 0; i < array.size(); i++) { - JSONObject jsonObject = array.getJSONObject(i); - String name; - if(jsonObject.containsKey("name")) - name = jsonObject.getString("name"); - else - name = null; - - long timestamp; - if(jsonObject.containsKey("changedToAt")) - timestamp = jsonObject.getLong("changedToAt"); - else - timestamp = -1; - - nameList.add(name); - - NameHistoryList list = new NameHistoryList(name,timestamp); - - data.add(list); - } - return new PlayerInfo(data,nameList); - } - - public List getData() { - return this.data; - } - - public List getNameList() { - return this.nameList; - } - - public String getInitName() { - return this.nameList.get(0); - } - - public String getNowName() { - return nameList.get(nameList.size() - 1); - } - - public static class NameHistoryList { - private String name; - private long timestamp; - - public NameHistoryList(String name, long timestamp) { - this.name = name; - this.timestamp = timestamp; - } - - public String getName() { - return this.name; - } - - public long getTimestamp() { - return this.timestamp; - } - - public String getTime() { - return Util.timestampToTime(timestamp); - } - } -} \ No newline at end of file diff --git a/src/tax/cute/minecraftinfoapi/Profile.java b/src/tax/cute/minecraftinfoapi/Profile.java new file mode 100644 index 0000000..ddf86be --- /dev/null +++ b/src/tax/cute/minecraftinfoapi/Profile.java @@ -0,0 +1,89 @@ +package tax.cute.minecraftinfoapi; + +import com.alibaba.fastjson.JSONObject; +import tax.cute.minecraftinfoapi.utils.ApiUrl; +import tax.cute.minecraftinfoapi.utils.Http; +import tax.cute.minecraftinfoapi.utils.Util; + +import java.io.IOException; +import java.net.HttpURLConnection; + +public class Profile { + private final String uuid; + private final String name; + private final String skinUrl; + private final String capeUrl; + private final String model; + + public Profile( + String uuid, + String name, + String skinUrl, + String capeUrl, + String model + ) { + this.uuid = uuid; + this.name = name; + this.skinUrl = skinUrl; + this.capeUrl = capeUrl; + this.model = model; + } + + public static Profile getProfile(String uuid) throws IOException,CommonException { + if (uuid == null || uuid.isEmpty() || !Util.isUuid(uuid)) + throw new CommonException("Input error"); + Http http = Http.getHttp(ApiUrl.PROFILE + .replace("%uuid", uuid) + ); + if (http.getCode() != HttpURLConnection.HTTP_OK) + throw new CommonException("no this player"); + + JSONObject json = JSONObject.parseObject(http.getTextString()); + + String base64Str = json.getJSONArray("properties").getJSONObject(0).getString("value"); + json = JSONObject.parseObject(Util.toString(base64Str)); + + uuid = json.getString("profileId"); + String name = json.getString("profileName"); + + json = json.getJSONObject("textures"); + JSONObject data = json.getJSONObject("SKIN"); + String skinUrl = (data != null) ? data.getString("url") : null; + + data = (data != null) ? data.getJSONObject("metadata") : null; + String model = (data != null) ? data.getString("model") : null; + + data = json.getJSONObject("CAPE"); + String capeUrl = (data == null) ? null : data.getString("url"); + + return new Profile(uuid,name,skinUrl,capeUrl,model); + } + + public String getUuid() { + return uuid; + } + + public String getName() { + return name; + } + + public String getCapeUrl() { + return capeUrl; + } + + public String getSkinUrl() { + return skinUrl; + } + + public String getModel() { + return model; + } + + public byte[] getSkinBytes() throws IOException{ + return (skinUrl == null) ? null : Http.getHttp(skinUrl).getBytes(); + } + + public byte[] getCapeBytes() throws IOException{ + return (capeUrl == null) ? null : Http.getHttp(capeUrl).getBytes(); + } +} \ No newline at end of file diff --git a/src/tax/cute/minecraftinfoapi/SalesOfMinecraft.java b/src/tax/cute/minecraftinfoapi/SalesOfMinecraft.java new file mode 100644 index 0000000..17eee1c --- /dev/null +++ b/src/tax/cute/minecraftinfoapi/SalesOfMinecraft.java @@ -0,0 +1,60 @@ +package tax.cute.minecraftinfoapi; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import tax.cute.minecraftinfoapi.utils.ApiUrl; +import tax.cute.minecraftinfoapi.utils.Util; + +import java.io.IOException; +import java.util.List; + +public class SalesOfMinecraft { + public static final String ITEM_SOLD_MINECRAFT = "item_sold_minecraft"; + public static final String PREPAID_CARD_REDEEMED_MINECRAFT = "prepaid_card_redeemed_minecraft"; + public static final String ITEM_SOLD_COBALT = "item_sold_cobalt"; + public static final String ITEM_SOLD_SCROLLS = "item_sold_scrolls"; + public static final String PREPAID_CARD_REDEEMED_COBALT = "prepaid_card_redeemed_cobalt"; + public static final String ITEM_SOLD_DUNGEONS = "item_sold_dungeons"; + + private final long total; + private final int last24h; + private final double saleVelocityPerSeconds; + + public SalesOfMinecraft( + long total, + int last24h, + double saleVelocityPerSeconds + ) { + this.total = total; + this.last24h = last24h; + this.saleVelocityPerSeconds = saleVelocityPerSeconds; + } + + public static SalesOfMinecraft getSales(List options) throws IOException { + JSONArray array = new JSONArray(); + array.addAll(options); + + JSONObject json = new JSONObject(); + json.put("metricKeys",array); + + json = JSONObject.parseObject(Util.sendPost(ApiUrl.SALES,Util.COMMON_KEY,Util.COMMON_VALUE,json.toJSONString().getBytes())); + + long total = Util.notNullLong(json.getLong("total")); + int last24h = Util.notNull(json.getInteger("last24h")); + double saleVelocityPerSeconds = Util.notNullDouble(json.getDouble("saleVelocityPerSeconds")); + + return new SalesOfMinecraft(total,last24h,saleVelocityPerSeconds); + } + + public double getSaleVelocityPerSeconds() { + return saleVelocityPerSeconds; + } + + public int getLast24h() { + return last24h; + } + + public long getTotal() { + return total; + } +} \ No newline at end of file diff --git a/src/tax/cute/minecraftinfoapi/Skin.java b/src/tax/cute/minecraftinfoapi/Skin.java deleted file mode 100644 index 0e94585..0000000 --- a/src/tax/cute/minecraftinfoapi/Skin.java +++ /dev/null @@ -1,89 +0,0 @@ -package tax.cute.minecraftinfoapi; - -import com.alibaba.fastjson.JSONArray; -import com.alibaba.fastjson.JSONObject; - -import java.io.IOException; - -public class Skin { - private String id; - private String name; - private long timestamp; - private String skinUrl; - private String capeUrl; - - public Skin(String id, String name,long timestamp, String skinUrl,String capeUrl) { - this.id = id; - this.name = name; - this.timestamp = timestamp; - this.skinUrl = skinUrl; - this.capeUrl = capeUrl; - } - - public static Skin getSkin(String uuid) throws IOException { - String jsonString = Util.getWebText(ApiUrl.SKIN_API + uuid); - if(jsonString == null) return null; - JSONObject json = JSONObject.parseObject(jsonString); - - if(json == null) - return null; - - JSONArray properties_array = json.getJSONArray("properties"); - JSONObject properties_object = properties_array.getJSONObject(0); - String value = properties_object.getString("value"); - - String properties_text = Util.base64toString(value); - json = JSONObject.parseObject(properties_text); - - long timestamp = json.getLong("timestamp"); - String id = json.getString("profileId"); - String name = json.getString("profileName"); - - JSONObject textures = json.getJSONObject("textures"); - JSONObject textures_skin = textures.getJSONObject("SKIN"); - String skinUrl = textures_skin.getString("url"); - - JSONObject textures_cape = textures.getJSONObject("CAPE"); - - String capeUrl; - if (textures_cape == null) { - capeUrl = null; - } else { - capeUrl = textures_cape.getString("url"); - } - - return new Skin(id,name,timestamp,skinUrl,capeUrl); - } - - public String getId() { - return this.id; - } - - public String getName() { - return this.name; - } - - public long getTimestamp() { - return this.timestamp; - } - - public String getTime() { - return Util.timestampToTime(getTimestamp()); - } - - public String getSkinUrl() { - return this.skinUrl; - } - - public byte[] getSkinBytes() throws IOException{ - return Util.getWebBytes(this.skinUrl); - } - - public String getCapeUrl() { - return this.capeUrl; - } - - public byte[] getCapeBytes() throws IOException{ - return Util.getWebBytes(this.capeUrl); - } -} diff --git a/src/tax/cute/minecraftinfoapi/Status.java b/src/tax/cute/minecraftinfoapi/Status.java index 8c7e16d..6cfb8a3 100644 --- a/src/tax/cute/minecraftinfoapi/Status.java +++ b/src/tax/cute/minecraftinfoapi/Status.java @@ -6,16 +6,18 @@ import java.util.Set; import com.alibaba.fastjson.*; +import tax.cute.minecraftinfoapi.utils.ApiUrl; +import tax.cute.minecraftinfoapi.utils.Http; public class Status { - private String minecraft_net; - private String session_minecraft_net; - private String account_mojang_com; - private String authserver_mojang_com; - private String sessionserver_mojang_com; - private String api_mojang_com; - private String textures_minecraft_net; - private String mojang_com; + private final String minecraft_net; + private final String session_minecraft_net; + private final String account_mojang_com; + private final String authserver_mojang_com; + private final String sessionserver_mojang_com; + private final String api_mojang_com; + private final String textures_minecraft_net; + private final String mojang_com; public Status( String minecraft_net, @@ -38,8 +40,7 @@ public Status( } public static Status getStatus() throws IOException { - //get json string - String jsonText = Util.getWebText(ApiUrl.STATUS_API); + String jsonText = Http.getHttp(ApiUrl.STATUS).getTextString(); if(jsonText == null) return null; JSONArray array = JSON.parseArray(jsonText); if(array == null) return null; diff --git a/src/tax/cute/minecraftinfoapi/UUID.java b/src/tax/cute/minecraftinfoapi/UUID.java deleted file mode 100644 index 285536f..0000000 --- a/src/tax/cute/minecraftinfoapi/UUID.java +++ /dev/null @@ -1,35 +0,0 @@ -package tax.cute.minecraftinfoapi; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; - -import java.io.IOException; - -public class UUID { - private String name; - private String id; - - public UUID(String name,String id) { - this.name = name; - this.id = id; - } - - public static UUID getId(String userName) throws IOException { - String jsonText = Util.getWebText(ApiUrl.USER_NAME_TO_UUID_API + userName); - if(jsonText == null) return null; - JSONObject jsonObject = JSON.parseObject(jsonText); - if (jsonObject == null) return null; - - String name = jsonObject.getString("name"); - String id = jsonObject.getString("id"); - return new UUID(name,id); - } - - public String getName() { - return this.name; - } - - public String getId() { - return this.id; - } -} diff --git a/src/tax/cute/minecraftinfoapi/Util.java b/src/tax/cute/minecraftinfoapi/Util.java deleted file mode 100644 index 080dd52..0000000 --- a/src/tax/cute/minecraftinfoapi/Util.java +++ /dev/null @@ -1,41 +0,0 @@ -package tax.cute.minecraftinfoapi; - -import java.io.IOException; -import java.io.InputStream; -import java.net.HttpURLConnection; -import java.net.URL; -import java.text.SimpleDateFormat; -import java.util.Base64; -import java.util.Date; - -public class Util { - public static byte[] getWebBytes(String url) throws IOException{ - URL u = new URL(url); - HttpURLConnection web = (HttpURLConnection) u.openConnection(); - if(web.getResponseCode() != 200) return null; - InputStream in = web.getInputStream(); - byte[] bytes = new byte[in.available()]; - in.read(bytes); - in.close(); - return bytes; - } - public static String getWebText(String url) throws IOException{ - return getWebText(url,"UTF-8"); - } - - public static String getWebText(String url,String charsets) throws IOException { - byte[] bytes = getWebBytes(url); - if(bytes == null) return null; - return new String(bytes,charsets); - } - - public static String timestampToTime(long timestamp) { - SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd hh:mm"); - return simple.format(new Date(timestamp)); - } - - public static String base64toString(String base64) { - byte[] bytes = Base64.getDecoder().decode(base64); - return new String(bytes); - } -} diff --git a/src/tax/cute/minecraftinfoapi/utils/ApiUrl.java b/src/tax/cute/minecraftinfoapi/utils/ApiUrl.java new file mode 100644 index 0000000..085ad55 --- /dev/null +++ b/src/tax/cute/minecraftinfoapi/utils/ApiUrl.java @@ -0,0 +1,10 @@ +package tax.cute.minecraftinfoapi.utils; + +public class ApiUrl { + public static final String STATUS = "https://status.mojang.com/check"; + public static final String PROFILE = "https://sessionserver.mojang.com/session/minecraft/profile/%uuid"; + public static final String PLAYER = "https://api.mojang.com/users/profiles/minecraft/%name"; + public static final String PLAYERS = "https://api.mojang.com/profiles/minecraft"; + public static final String SALES = "https://api.mojang.com/orders/statistics"; + public static final String NAME_HISTORY = "https://api.mojang.com/user/profiles/%uuid/names"; +} \ No newline at end of file diff --git a/src/tax/cute/minecraftinfoapi/utils/Http.java b/src/tax/cute/minecraftinfoapi/utils/Http.java new file mode 100644 index 0000000..2afbba5 --- /dev/null +++ b/src/tax/cute/minecraftinfoapi/utils/Http.java @@ -0,0 +1,46 @@ +package tax.cute.minecraftinfoapi.utils; + +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; + +public class Http { + private final int code; + private final byte[] bytes; + + public Http( + int code, + byte[] bytes + ) { + this.code = code; + this.bytes = bytes; + } + + public static Http getHttp(String url) throws IOException { + HttpURLConnection http = (HttpURLConnection)new URL(url).openConnection(); + int code = http.getResponseCode(); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + try(InputStream in = (code == HttpURLConnection.HTTP_OK) ? http.getInputStream() : http.getErrorStream()) { + if(in == null)return new Http(code,null); + int i; + while ((i = in.read()) != -1) { + out.write(i); + } + } + + return new Http(code,out.toByteArray()); + } + + public int getCode() { + return code; + } + + public String getTextString(){ + return (bytes != null) ? new String(bytes) : null; + } + + public byte[] getBytes(){ + return bytes; + } +} \ No newline at end of file diff --git a/src/tax/cute/minecraftinfoapi/utils/Util.java b/src/tax/cute/minecraftinfoapi/utils/Util.java new file mode 100644 index 0000000..0f222ee --- /dev/null +++ b/src/tax/cute/minecraftinfoapi/utils/Util.java @@ -0,0 +1,64 @@ +package tax.cute.minecraftinfoapi.utils; + +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; +import java.text.SimpleDateFormat; +import java.util.Base64; +import java.util.Date; +import java.util.regex.Pattern; + +public class Util { + public static final String COMMON_KEY = "Content-Type"; + public static final String COMMON_VALUE = "application/json"; + + public static String toTime(long timestamp) { + return (timestamp != -1) ? new SimpleDateFormat("yyyy-MM-dd hh:mm").format(new Date(timestamp)) : ""; + } + + public static String toString(String base64) { + return (base64 == null || base64.isEmpty()) ? null : new String(Base64.getDecoder().decode(base64.replace("\n",""))); + } + + public static boolean isLegalUsername(String name) { + return Pattern.matches("[a-zA-Z0-9_]{3,16}", name); + } + + public static String sendPost(String url,String key,String value,byte[] data) throws IOException { + HttpURLConnection http = (HttpURLConnection) new URL(url).openConnection(); + http.setDoInput(true); + http.setDoOutput(true); + + http.addRequestProperty(key, value); + + try (OutputStream out = http.getOutputStream()) { + out.write(data); + } + + StringBuilder text = new StringBuilder(); + try (BufferedReader reader = new BufferedReader(new InputStreamReader(http.getInputStream()))) { + String s; + while ((s = reader.readLine()) != null) { + text.append(s); + } + } + + return text.toString(); + } + + public static int notNull(Object object) { + return (object instanceof Integer || object instanceof Long || object instanceof Double) ? (int)object : -1; + } + + public static long notNullLong(Object object) { + return (object instanceof Integer || object instanceof Long || object instanceof Double) ? (long)object : -1; + } + + public static double notNullDouble(Object object) { + return (object instanceof Integer || object instanceof Long || object instanceof Double) ? (double)object : -1; + } + + public static boolean isUuid(String s) { + return s.replace("-","").length() == 32; + } +} \ No newline at end of file