Citizens2/main/src/main/java/net/citizensnpcs/util/MojangSkinGenerator.java

138 lines
5.4 KiB
Java
Raw Normal View History

2023-03-28 19:28:08 +02:00
package net.citizensnpcs.util;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import com.google.common.io.CharStreams;
import net.citizensnpcs.api.util.Messaging;
2023-03-28 19:28:08 +02:00
public class MojangSkinGenerator {
public static JSONObject generateFromPNG(final byte[] png, boolean slim)
throws InterruptedException, ExecutionException {
2023-03-28 19:28:08 +02:00
return EXECUTOR.submit(() -> {
DataOutputStream out = null;
2023-10-01 11:27:38 +02:00
InputStreamReader reader = null;
2023-03-28 19:28:08 +02:00
try {
URL target = new URL("https://api.mineskin.org/generate/upload" + (slim ? "?model=slim" : ""));
2023-03-28 19:28:08 +02:00
HttpURLConnection con = (HttpURLConnection) target.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setRequestProperty("User-Agent", "Citizens/2.0");
2023-03-28 19:28:08 +02:00
con.setRequestProperty("Cache-Control", "no-cache");
con.setRequestProperty("Content-Type", "multipart/form-data;boundary=*****");
con.setConnectTimeout(1000);
con.setReadTimeout(30000);
out = new DataOutputStream(con.getOutputStream());
out.writeBytes("--*****\r\n");
out.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"skin.png\"\r\n");
out.writeBytes("Content-Type: image/png\r\n\r\n");
2023-03-28 19:28:08 +02:00
out.write(png);
out.writeBytes("\r\n");
out.writeBytes("--*****\r\n");
out.writeBytes("Content-Disposition: form-data; name=\"name\";\r\n\r\n\r\n");
if (slim) {
out.writeBytes("--*****\r\n");
out.writeBytes("Content-Disposition: form-data; name=\"variant\";\r\n\r\n");
out.writeBytes("slim\r\n");
}
2023-03-28 19:28:08 +02:00
out.writeBytes("--*****--\r\n");
out.flush();
out.close();
2023-10-01 11:27:38 +02:00
reader = new InputStreamReader(con.getInputStream());
String str = CharStreams.toString(reader);
if (Messaging.isDebugging()) {
Messaging.debug(str);
}
2023-10-01 11:27:38 +02:00
if (con.getResponseCode() != 200)
return null;
JSONObject output = (JSONObject) new JSONParser().parse(str);
2023-03-28 19:28:08 +02:00
JSONObject data = (JSONObject) output.get("data");
con.disconnect();
return data;
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
}
}).get();
}
public static JSONObject generateFromURL(final String url, boolean slim)
throws InterruptedException, ExecutionException {
2023-03-28 19:28:08 +02:00
return EXECUTOR.submit(() -> {
DataOutputStream out = null;
2023-10-01 11:27:38 +02:00
InputStreamReader reader = null;
2023-03-28 19:28:08 +02:00
try {
URL target = new URL("https://api.mineskin.org/generate/url");
HttpURLConnection con = (HttpURLConnection) target.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setRequestProperty("User-Agent", "Citizens/2.0");
con.setRequestProperty("Cache-Control", "no-cache");
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("Content-Type", "application/json");
2023-03-28 19:28:08 +02:00
con.setConnectTimeout(1000);
con.setReadTimeout(30000);
out = new DataOutputStream(con.getOutputStream());
JSONObject req = new JSONObject();
req.put("url", url);
req.put("name", "");
if (slim) {
req.put("variant", "slim");
}
out.writeBytes(req.toJSONString().replace("\\", ""));
2023-03-28 19:28:08 +02:00
out.close();
2023-10-01 11:27:38 +02:00
reader = new InputStreamReader(con.getInputStream());
String str = CharStreams.toString(reader);
if (Messaging.isDebugging()) {
Messaging.debug(str);
}
2023-10-01 11:27:38 +02:00
if (con.getResponseCode() != 200)
return null;
JSONObject output = (JSONObject) new JSONParser().parse(str);
2023-03-28 19:28:08 +02:00
JSONObject data = (JSONObject) output.get("data");
con.disconnect();
return data;
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
}
}).get();
}
private static final ExecutorService EXECUTOR = Executors.newSingleThreadExecutor();
}