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+ }
0 commit comments