Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
MX233 committed Oct 7, 2021
1 parent e1c6d70 commit 92b4be0
Show file tree
Hide file tree
Showing 18 changed files with 484 additions and 368 deletions.
46 changes: 42 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,56 @@ 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` 反查`用户名`
- 通过`UUID` 获取`皮肤``披风`
- 通过`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<String> players = new ArrayList<>();
players.add("CuteStarX");
Map<String,String> 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());
}
8 changes: 0 additions & 8 deletions src/tax/cute/minecraftinfoapi/ApiUrl.java

This file was deleted.

7 changes: 7 additions & 0 deletions src/tax/cute/minecraftinfoapi/CommonException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package tax.cute.minecraftinfoapi;

public class CommonException extends Exception{
public CommonException(String msg) {
super(msg);
}
}
85 changes: 0 additions & 85 deletions src/tax/cute/minecraftinfoapi/Main.java

This file was deleted.

32 changes: 32 additions & 0 deletions src/tax/cute/minecraftinfoapi/MultiplePlayers.java
Original file line number Diff line number Diff line change
@@ -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<String,String> getPlayers(List<String> 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<String,String> data = new HashMap<>();
array.forEach(object -> {
JSONObject jsonObject = (JSONObject)object;
data.put(jsonObject.getString("id"),jsonObject.getString("name"));
});

return data;
}
}
61 changes: 61 additions & 0 deletions src/tax/cute/minecraftinfoapi/NameHistory.java
Original file line number Diff line number Diff line change
@@ -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> nameHistoryData;
private final String uuid;

public NameHistory(
List<NameHistoryData> 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> 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<NameHistoryData> 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;
}
}
22 changes: 22 additions & 0 deletions src/tax/cute/minecraftinfoapi/NameHistoryData.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
40 changes: 40 additions & 0 deletions src/tax/cute/minecraftinfoapi/Player.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading

0 comments on commit 92b4be0

Please sign in to comment.