Skip to content

Commit 92b4be0

Browse files
author
MX233
committed
Update
1 parent e1c6d70 commit 92b4be0

18 files changed

Lines changed: 484 additions & 368 deletions

README.md

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,56 @@ A convenient api to get Minecraft uuid and skin
44
If you cannot read Chinese, please use Google Translate
55
==
66

7-
`MinecraftInfoAPI` 是一个可以让你轻松获取`Minecraft` 玩家信息的api
7+
`MinecraftInfoAPI` 是一个可以让你轻松获取`Minecraft` 玩家信息的库
8+
9+
没什么技术力,只是方便点
810

911
目前,`MinecraftInfoAPI` 有以下功能:
1012
- 通过`用户名` 获取`UUID` 或通过`UUID` 反查`用户名`
1113
- 通过`UUID` 获取`皮肤``披风`
1214
- 通过`UUID` 获得 `名称历史` 以及名称的修改时间
1315
- 获取`Minecraft` 相关网站的状态(例如`Minecraft.net` `mojang.com`)
1416

15-
`MinecraftInfoAPI` 需要[`Fastjson`](https://github.com/alibaba/fastjson) 才能使用
17+
本项目语法和编译版本为`Java8`
1618

17-
[Releases](https://github.com/MX233/MinecraftInfoAPI/releases) 下载依赖并导入到你的项目中即可使用
19+
用到的库 : [`Fastjson`](https://github.com/alibaba/fastjson)
1820

19-
使用示例:[Main.java](https://github.com/MX233/MinecraftInfoAPI/blob/main/src/tax/cute/minecraftinfoapi/Main.java)
21+
[Releases](https://github.com/MX233/MinecraftInfoAPI/releases) 下载并导入到你的项目中即可使用
2022

2123
我是一个新手,可能做的不好,你可以clone下来自行修改
24+
25+
用法:
26+
27+
- 用户名查询UUID
28+
29+
Player player = Player.getPlayer("CuteStarX");
30+
System.out.println(player.getName() + " 的UUID是: " + player.getUuid());
31+
32+
- 查询10个以内的玩家UUID
33+
34+
List<String> players = new ArrayList<>();
35+
players.add("CuteStarX");
36+
37+
Map<String,String> map = new MultiplePlayers().getPlayers(players);
38+
map.keySet().forEach(uuid -> System.out.println(map.get(uuid) + " 的UUID是: " + uuid));
39+
40+
- 查询玩家曾用名
41+
42+
NameHistory nameHistory = NameHistory.getNameHistory("e3beb716afa1451b96c6ddfebd1ce1fb");
43+
System.out.println("这个UUID现在的玩家名称为: " + nameHistory.getCurrentName());
44+
System.out.println("这个UUID初始玩家名称为: " + nameHistory.getInitName());
45+
46+
System.out.println("这个UUID使用过的玩家名称:");
47+
nameHistory.getNameHistoryData().forEach(data -> System.out.println("名称: " + data.getName() + " 修改时间: " + Util.toTime(data.getChangedToAt())));
48+
49+
- 查询玩家皮肤
50+
51+
Profile profile = Profile.getProfile(Player.getPlayer("CuteStarX").getUuid());
52+
System.out.println("皮肤模型是: " + profile.getModel());
53+
System.out.println("皮肤链接是: " + profile.getSkinUrl());
54+
System.out.println("披风链接是: " + profile.getCapeUrl());
55+
56+
//将皮肤保存到本地
57+
try (OutputStream out = new FileOutputStream("skin.png")) {
58+
out.write(profile.getSkinBytes());
59+
}

src/tax/cute/minecraftinfoapi/ApiUrl.java

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package tax.cute.minecraftinfoapi;
2+
3+
public class CommonException extends Exception{
4+
public CommonException(String msg) {
5+
super(msg);
6+
}
7+
}

src/tax/cute/minecraftinfoapi/Main.java

Lines changed: 0 additions & 85 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package tax.cute.minecraftinfoapi;
2+
3+
import com.alibaba.fastjson.JSONArray;
4+
import com.alibaba.fastjson.JSONObject;
5+
import tax.cute.minecraftinfoapi.utils.ApiUrl;
6+
import tax.cute.minecraftinfoapi.utils.Util;
7+
8+
import java.io.IOException;
9+
import java.util.HashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
public class MultiplePlayers {
14+
public Map<String,String> getPlayers(List<String> players) throws IOException,CommonException {
15+
if(players.isEmpty())
16+
throw new CommonException("Players is empty");
17+
if(players.size() > 10)
18+
throw new CommonException("Players too big,It cannot be greater than 10");
19+
JSONArray array = new JSONArray();
20+
array.addAll(players);
21+
22+
array = JSONArray.parseArray(Util.sendPost(ApiUrl.PLAYERS,"Content-Type","application/json",array.toJSONString().getBytes()));
23+
24+
Map<String,String> data = new HashMap<>();
25+
array.forEach(object -> {
26+
JSONObject jsonObject = (JSONObject)object;
27+
data.put(jsonObject.getString("id"),jsonObject.getString("name"));
28+
});
29+
30+
return data;
31+
}
32+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package tax.cute.minecraftinfoapi;
2+
3+
import com.alibaba.fastjson.JSONArray;
4+
import com.alibaba.fastjson.JSONObject;
5+
import tax.cute.minecraftinfoapi.utils.ApiUrl;
6+
import tax.cute.minecraftinfoapi.utils.Http;
7+
import tax.cute.minecraftinfoapi.utils.Util;
8+
9+
import java.io.IOException;
10+
import java.net.HttpURLConnection;
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
14+
public class NameHistory {
15+
private final List<NameHistoryData> nameHistoryData;
16+
private final String uuid;
17+
18+
public NameHistory(
19+
List<NameHistoryData> nameHistoryData,
20+
String uuid
21+
) {
22+
this.nameHistoryData = nameHistoryData;
23+
this.uuid = uuid;
24+
}
25+
26+
public static NameHistory getNameHistory(String uuid) throws IOException,CommonException {
27+
if (uuid == null || uuid.isEmpty() || !Util.isUuid(uuid))
28+
throw new CommonException("Input error");
29+
Http http = Http.getHttp(ApiUrl.NAME_HISTORY
30+
.replace("%uuid", uuid)
31+
);
32+
if (http.getCode() != HttpURLConnection.HTTP_OK)
33+
throw new CommonException("no this player");
34+
35+
List<NameHistoryData> nameHistoryData = new ArrayList<>();
36+
37+
JSONArray array = JSONArray.parseArray(http.getTextString());
38+
array.forEach(object -> {
39+
JSONObject json = (JSONObject) object;
40+
nameHistoryData.add(new NameHistoryData(json.getString("name"), Util.notNullLong(json.getLong("changedToAt"))));
41+
});
42+
43+
return new NameHistory(nameHistoryData,uuid);
44+
}
45+
46+
public List<NameHistoryData> getNameHistoryData() {
47+
return nameHistoryData;
48+
}
49+
50+
public String getInitName() {
51+
return nameHistoryData.get(0).getName();
52+
}
53+
54+
public String getCurrentName() {
55+
return nameHistoryData.get(nameHistoryData.size() - 1).getName();
56+
}
57+
58+
public String getUuid() {
59+
return uuid;
60+
}
61+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package tax.cute.minecraftinfoapi;
2+
3+
public class NameHistoryData {
4+
private final String name;
5+
private final long changedToAt;
6+
7+
public NameHistoryData(
8+
String name,
9+
long changedToAt
10+
) {
11+
this.name = name;
12+
this.changedToAt = changedToAt;
13+
}
14+
15+
public String getName() {
16+
return name;
17+
}
18+
19+
public long getChangedToAt() {
20+
return changedToAt;
21+
}
22+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package tax.cute.minecraftinfoapi;
2+
3+
import com.alibaba.fastjson.JSONObject;
4+
import tax.cute.minecraftinfoapi.utils.*;
5+
6+
import java.io.IOException;
7+
import java.net.HttpURLConnection;
8+
9+
public class Player {
10+
private final String name;
11+
private final String uuid;
12+
13+
public Player(
14+
String name,
15+
String uuid
16+
) {
17+
this.name = name;
18+
this.uuid = uuid;
19+
}
20+
21+
public static Player getPlayer(String name) throws IOException,CommonException {
22+
if (name == null || name.isEmpty() || !Util.isLegalUsername(name))throw new CommonException("Illegal username");
23+
Http http = Http.getHttp(ApiUrl.PLAYER
24+
.replace("%name",name)
25+
);
26+
27+
if (http.getCode() != HttpURLConnection.HTTP_OK)throw new CommonException("No this player");
28+
29+
JSONObject json = JSONObject.parseObject(http.getTextString());
30+
return new Player(json.getString("name"),json.getString("id"));
31+
}
32+
33+
public String getName() {
34+
return name;
35+
}
36+
37+
public String getUuid() {
38+
return uuid;
39+
}
40+
}

0 commit comments

Comments
 (0)