Skip to content

Commit 9addb0d

Browse files
updating version 0.0.5
1 parent 35d8efa commit 9addb0d

File tree

19 files changed

+497
-65
lines changed

19 files changed

+497
-65
lines changed

.classpath

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
10+
<attributes>
11+
<attribute name="maven.pomderived" value="true"/>
12+
</attributes>
13+
</classpathentry>
14+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
15+
<attributes>
16+
<attribute name="optional" value="true"/>
17+
<attribute name="maven.pomderived" value="true"/>
18+
<attribute name="test" value="true"/>
19+
</attributes>
20+
</classpathentry>
21+
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
22+
<attributes>
23+
<attribute name="maven.pomderived" value="true"/>
24+
<attribute name="test" value="true"/>
25+
</attributes>
26+
</classpathentry>
27+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
28+
<attributes>
29+
<attribute name="maven.pomderived" value="true"/>
30+
</attributes>
31+
</classpathentry>
32+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
33+
<attributes>
34+
<attribute name="maven.pomderived" value="true"/>
35+
</attributes>
36+
</classpathentry>
37+
<classpathentry kind="output" path="target/classes"/>
38+
</classpath>

.project

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>mojang-api</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
22+
</natures>
23+
</projectDescription>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
3+
org.eclipse.jdt.core.compiler.compliance=1.8
4+
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
5+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
6+
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
7+
org.eclipse.jdt.core.compiler.release=disabled
8+
org.eclipse.jdt.core.compiler.source=1.8

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ Javadoc: [Click here](https://maxpixel.cn:20443/xiaopangxie732/projects/mojang-a
2424
1.Install Maven.
2525
2.cd to source directory.
2626
3.Run <code>mvn clean install</code>
27-
4.When build complete, files are all in target folder
27+
4.When build complete, files are all in target folder.
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* MojangAPI-in-Java--Mojang Public API Java implementation.
3+
* Copyright (C) 2019 XiaoPangxie732
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
package cn.xiaopangxie732.mojang_api;
19+
20+
import java.awt.image.BufferedImage;
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.File;
23+
import java.io.IOException;
24+
import java.io.InputStream;
25+
import java.io.UnsupportedEncodingException;
26+
import java.net.HttpURLConnection;
27+
import java.net.MalformedURLException;
28+
import java.net.URI;
29+
import java.net.URL;
30+
import java.net.URLEncoder;
31+
import java.util.Properties;
32+
33+
import javax.imageio.ImageIO;
34+
import com.google.gson.Gson;
35+
36+
import cn.xiaopangxie732.mojang_api.util.Auth;
37+
38+
public class Skin {
39+
/**
40+
* To change skin.
41+
* @param access_token The access token of UUID's account.
42+
* @param isSlim Skin is slim or not.
43+
* @param uuid UUID of player. can be get be using {@link UserName#UUIDAtNow(String)}
44+
* @param uri The skin image path. if it is a local file, it needs add a prefix "file:///" and replace "\\" to "/"(Windows).
45+
*/
46+
public static void changeSkin(String access_token, boolean isSlim, String uuid, URI uri) {
47+
try {
48+
String response = Auth.postConnection("https://api.mojang.com/user/profile/" + uuid
49+
+"/skin", "model=" + (isSlim ? "slim" : "") + "&url=" +
50+
URLEncoder.encode(uri.toURL().toString(), "UTF-8"), access_token);
51+
if(response != null)
52+
throw new IllegalArgumentException("Failed to change skin");
53+
} catch (UnsupportedEncodingException | MalformedURLException e) {
54+
e.printStackTrace();
55+
}
56+
}
57+
public static void changeSkinAndUpload(String access_token, boolean isSlim, String uuid, URI uri) {
58+
StringBuffer response = new StringBuffer();
59+
HttpURLConnection connection = null;
60+
try {
61+
connection = (HttpURLConnection)(new URL("https://api.mojang.com/user/profile/" + uuid
62+
+"/skin").openConnection());
63+
connection.setDoOutput(true);
64+
connection.setRequestMethod("PUT");
65+
connection.setRequestProperty("Authorization", "Bearer " + access_token);
66+
/*
67+
* The boundary is Base64-encoded of "MojangAPI-in-Java_datatransfer"
68+
*/
69+
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=TW9qYW5nQVBJLWluLUphdmFfZGF0YXRyYW5zZmVy");
70+
71+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
72+
ImageIO.write(ImageIO.read(new File(uri)), new File(uri).getName().split(".")[1], baos);
73+
74+
connection.getOutputStream().write(new String(
75+
"--TW9qYW5nQVBJLWluLUphdmFfZGF0YXRyYW5zZmVy\r\n" +
76+
"Content-Disposition: form-data; name=\"model\"\r\n" +
77+
"\r\n" +
78+
"\r\n" +
79+
"--TW9qYW5nQVBJLWluLUphdmFfZGF0YXRyYW5zZmVy\r\n" +
80+
"Content-Disposition: form-data; name=\"file\"; filename=\"skin.png\"\r\n" +
81+
"Content-Type: image/png\r\n" +
82+
(isSlim ? "slim" : "") + "\r\n" +
83+
new String(baos.toByteArray()) + "\r\n" +
84+
"--TW9qYW5nQVBJLWluLUphdmFfZGF0YXRyYW5zZmVy--").getBytes("UTF-8"));
85+
connection.connect();
86+
InputStream in = connection.getInputStream();
87+
int i;
88+
while((i = in.read())!= -1) {
89+
response.append((char)i);
90+
}
91+
} catch(IOException ioe) {
92+
InputStream err = connection.getErrorStream();
93+
int i;
94+
try {
95+
while((i = err.read())!= -1) {
96+
response.append((char)i);
97+
}
98+
} catch (IOException e1) {
99+
e1.printStackTrace();
100+
}
101+
throw new IllegalStateException(new Gson().fromJson(response.toString(), Properties.class).getProperty("errorMessage"));
102+
} catch(Exception e) {
103+
e.printStackTrace();
104+
} finally {
105+
connection.disconnect();
106+
}
107+
}
108+
public static void resetSkin(String access_token, String uuid) {
109+
StringBuffer response = new StringBuffer();
110+
HttpURLConnection connection = null;
111+
try {
112+
connection = (HttpURLConnection)(new URL("https://api.mojang.com/user/profile/" + uuid
113+
+"/skin").openConnection());
114+
connection.setDoOutput(true);
115+
connection.setRequestMethod("DELETE");
116+
connection.setRequestProperty("Authorization", "Bearer " + access_token);
117+
connection.connect();
118+
InputStream in = connection.getInputStream();
119+
int i;
120+
while((i = in.read())!= -1) {
121+
response.append((char)i);
122+
}
123+
} catch(IOException ioe) {
124+
InputStream err = connection.getErrorStream();
125+
int i;
126+
try {
127+
while((i = err.read())!= -1) {
128+
response.append((char)i);
129+
}
130+
} catch (IOException e1) {
131+
e1.printStackTrace();
132+
}
133+
throw new IllegalStateException(new Gson().fromJson(response.toString(), Properties.class).getProperty("errorMessage"));
134+
} catch(Exception e) {
135+
e.printStackTrace();
136+
} finally {
137+
connection.disconnect();
138+
}
139+
}
140+
}

src/main/java/cn/xiaopangxie732/mojang_api/Status.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/*
2+
* MojangAPI-in-Java--Mojang Public API Java implementation.
3+
* Copyright (C) 2019 XiaoPangxie732
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
118
package cn.xiaopangxie732.mojang_api;
219

320
import com.google.gson.JsonArray;
@@ -18,7 +35,7 @@
1835
public class Status {
1936

2037
/**
21-
* Lists all the servers that can check the status.
38+
* Lists all the servers.
2239
* @since 0.0.1
2340
* @author XiaoPangxie732
2441
*/
@@ -56,12 +73,6 @@ public enum StatusServer {
5673
*/
5774
MOJANG_COM;
5875

59-
/**
60-
* {@code toString} method.
61-
* @return The lower case and replace "_" to the " " enum name.
62-
* @since 0.0.1
63-
* @author XiaoPangxie732
64-
*/
6576
@Override
6677
public String toString() {
6778
return name().toLowerCase().replace("_", ".");
@@ -87,12 +98,6 @@ public enum StatusType {
8798
*/
8899
RED;
89100

90-
/**
91-
* {@code toString} method.
92-
* @return The lower case of enum name.
93-
* @since 0.0.1
94-
* @author XiaoPangxie732
95-
*/
96101
@Override
97102
public String toString() {
98103
return name().toLowerCase().replace("_", " ");
@@ -142,7 +147,7 @@ public StatusType getStatus(StatusServer server) throws NullPointerException {
142147
}
143148

144149
/**
145-
* To check server status and list them.<br>
150+
* To check server status and list them.
146151
* @param store Whether to store the output.
147152
* @return The all server status.<br>Return as <code>Properties</code> format.
148153
* @since 0.0.4
@@ -183,11 +188,11 @@ public String getAllStatus() {
183188
}
184189

185190
/**
186-
* To fresh Mojang servers status.
191+
* To validate servers status.
187192
* @since 0.0.2
188193
* @author XiaoPangxie732
189194
*/
190-
public void fresh() {
195+
public void validate() {
191196
response = Net.getConnection(url);
192197
}
193198
}
Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,46 @@
1+
/*
2+
* MojangAPI-in-Java--Mojang Public API Java implementation.
3+
* Copyright (C) 2019 XiaoPangxie732
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
118
package cn.xiaopangxie732.mojang_api;
219

320
import cn.xiaopangxie732.mojang_api.util.Net;
4-
import com.google.gson.Gson;
521
import com.google.gson.JsonArray;
622
import com.google.gson.JsonParser;
723

24+
import java.io.FileOutputStream;
25+
import java.io.IOException;
826
import java.text.DateFormat;
27+
import java.util.Base64;
928
import java.util.Date;
1029

30+
import javax.imageio.ImageIO;
31+
1132
/**
1233
* Used for UUID operating.
1334
* @author XiaoPangxie732
1435
*/
1536
public class UUIDName {
1637

17-
private static Gson json = new Gson();
18-
1938
/**
2039
* To get UUID's name history.
21-
* @param uuid The player's UUID, can be get be using {@link UserName#UUIDAtNow(String)}
40+
* @param uuid The player's UUID. can be get be using {@link UserName#UUIDAtNow(String)}
2241
* @return The name history of the UUID.
2342
*/
24-
public static String NameHistory(String uuid) {
43+
public static String nameHistory(String uuid) {
2544
String url = "https://api.mojang.com/user/profiles/" + uuid + "/names";
2645
JsonArray response = new JsonParser().parse(Net.getConnection(url)).getAsJsonArray();
2746
StringBuilder result = new StringBuilder("Original=" + response.get(0).getAsJsonObject().get("name").getAsString());
@@ -31,4 +50,28 @@ public static String NameHistory(String uuid) {
3150
}
3251
return result.toString();
3352
}
53+
/**
54+
* To get this UUID's skin URL.
55+
* @param uuid The UUID of player. can be get be using {@link UserName#UUIDAtNow(String)}
56+
* @return The skin URL of given UUID.
57+
*/
58+
public static String getSkinURL(String uuid) {
59+
return new JsonParser().parse(new String(Base64.getDecoder().decode(new JsonParser()
60+
.parse(Net.getConnection("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid))
61+
.getAsJsonObject().get("properties").getAsJsonArray().get(0).getAsJsonObject().get("value").getAsString()))).getAsJsonObject()
62+
.get("textures").getAsJsonObject().get("SKIN").getAsJsonObject().get("url").getAsString();
63+
}
64+
/**
65+
* Store this UUID's skin png to desktop.
66+
* @param uuid The UUID of player. can be get be using {@link UserName#UUIDAtNow(String)}
67+
*/
68+
public static void storeSkinImageToDesktop(String uuid) {
69+
try {
70+
ImageIO.createImageOutputStream(new FileOutputStream(System.getProperty("user.home") + "\\Desktop\\Skin.png")).writeBytes(Net.getConnection(getSkinURL(uuid)));
71+
System.out.println("File store complete");
72+
} catch (IOException e) {
73+
System.out.println("File stored failed!");
74+
e.printStackTrace();
75+
}
76+
}
3477
}

0 commit comments

Comments
 (0)