Merge remote-tracking branch 'origin/breaking' into breaking

This commit is contained in:
Sauilitired
2018-12-24 01:50:45 +01:00
4 changed files with 110 additions and 119 deletions

View File

@ -43,34 +43,32 @@ public class Save extends SubCommand {
plot.addRunning();
SchematicHandler.manager.getCompoundTag(plot, new RunnableVal<CompoundTag>() {
@Override public void run(final CompoundTag value) {
TaskManager.runTaskAsync(new Runnable() {
@Override public void run() {
String time = (System.currentTimeMillis() / 1000) + "";
Location[] corners = plot.getCorners();
corners[0].setY(0);
corners[1].setY(255);
int size = (corners[1].getX() - corners[0].getX()) + 1;
PlotId id = plot.getId();
String world = plot.getArea().toString().replaceAll(";", "-")
.replaceAll("[^A-Za-z0-9]", "");
final String file =
time + '_' + world + '_' + id.x + '_' + id.y + '_' + size;
UUID uuid = player.getUUID();
SchematicHandler.manager.upload(value, uuid, file, new RunnableVal<URL>() {
@Override public void run(URL url) {
plot.removeRunning();
if (url == null) {
MainUtil.sendMessage(player, C.SAVE_FAILED);
return;
}
MainUtil.sendMessage(player, C.SAVE_SUCCESS);
List<String> schematics = player.getMeta("plot_schematics");
if (schematics != null) {
schematics.add(file);
}
TaskManager.runTaskAsync(() -> {
String time = (System.currentTimeMillis() / 1000) + "";
Location[] corners = plot.getCorners();
corners[0].setY(0);
corners[1].setY(255);
int size = (corners[1].getX() - corners[0].getX()) + 1;
PlotId id = plot.getId();
String world1 = plot.getArea().toString().replaceAll(";", "-")
.replaceAll("[^A-Za-z0-9]", "");
final String file =
time + '_' + world1 + '_' + id.x + '_' + id.y + '_' + size;
UUID uuid = player.getUUID();
SchematicHandler.manager.upload(value, uuid, file, new RunnableVal<URL>() {
@Override public void run(URL url) {
plot.removeRunning();
if (url == null) {
MainUtil.sendMessage(player, C.SAVE_FAILED);
return;
}
});
}
MainUtil.sendMessage(player, C.SAVE_SUCCESS);
List<String> schematics = player.getMeta("plot_schematics");
if (schematics != null) {
schematics.add(file);
}
}
});
});
}
});

View File

@ -114,68 +114,66 @@ public class MainUtil {
whenDone.run();
return;
}
TaskManager.runTaskAsync(new Runnable() {
@Override public void run() {
try {
String boundary = Long.toHexString(System.currentTimeMillis());
URLConnection con = new URL(website).openConnection();
con.setDoOutput(true);
con.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);
try (OutputStream output = con.getOutputStream();
PrintWriter writer = new PrintWriter(
new OutputStreamWriter(output, StandardCharsets.UTF_8), true)) {
String CRLF = "\r\n";
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"param\"")
.append(CRLF);
writer.append("Content-Type: text/plain; charset=" + StandardCharsets.UTF_8
.displayName()).append(CRLF);
String param = "value";
writer.append(CRLF).append(param).append(CRLF).flush();
writer.append("--" + boundary).append(CRLF);
writer.append(
"Content-Disposition: form-data; name=\"schematicFile\"; filename=\""
+ filename + '"').append(CRLF);
writer.append(
"Content-Type: " + URLConnection.guessContentTypeFromName(filename))
.append(CRLF);
writer.append("Content-Transfer-Encoding: binary").append(CRLF);
writer.append(CRLF).flush();
writeTask.value = new AbstractDelegateOutputStream(output) {
@Override public void close() {
} // Don't close
};
writeTask.run();
output.flush();
writer.append(CRLF).flush();
writer.append("--" + boundary + "--").append(CRLF).flush();
TaskManager.runTaskAsync(() -> {
try {
String boundary = Long.toHexString(System.currentTimeMillis());
URLConnection con = new URL(website).openConnection();
con.setDoOutput(true);
con.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);
try (OutputStream output = con.getOutputStream();
PrintWriter writer = new PrintWriter(
new OutputStreamWriter(output, StandardCharsets.UTF_8), true)) {
String CRLF = "\r\n";
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"param\"")
.append(CRLF);
writer.append("Content-Type: text/plain; charset=" + StandardCharsets.UTF_8
.displayName()).append(CRLF);
String param = "value";
writer.append(CRLF).append(param).append(CRLF).flush();
writer.append("--" + boundary).append(CRLF);
writer.append(
"Content-Disposition: form-data; name=\"schematicFile\"; filename=\""
+ filename + '"').append(CRLF);
writer.append(
"Content-Type: " + URLConnection.guessContentTypeFromName(filename))
.append(CRLF);
writer.append("Content-Transfer-Encoding: binary").append(CRLF);
writer.append(CRLF).flush();
writeTask.value = new AbstractDelegateOutputStream(output) {
@Override public void close() {
} // Don't close
};
writeTask.run();
output.flush();
writer.append(CRLF).flush();
writer.append("--" + boundary + "--").append(CRLF).flush();
}
try (Reader response = new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8)) {
final char[] buffer = new char[256];
final StringBuilder result = new StringBuilder();
while (true) {
final int r = response.read(buffer);
if (r < 0) {
break;
}
result.append(buffer, 0, r);
}
// try (Reader response = new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8)) {
// final char[] buffer = new char[256];
// final StringBuilder result = new StringBuilder();
// while (true) {
// final int r = response.read(buffer);
// if (r < 0) {
// break;
// }
// result.append(buffer, 0, r);
// }
// if (!result.toString().startsWith("Success")) {
// PS.debug(result);
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
int responseCode = ((HttpURLConnection) con).getResponseCode();
if (responseCode == 200) {
whenDone.value = url;
if (!result.toString().startsWith("Success")) {
PlotSquared.debug(result);
}
TaskManager.runTask(whenDone);
} catch (IOException e) {
e.printStackTrace();
TaskManager.runTask(whenDone);
}
int responseCode = ((HttpURLConnection) con).getResponseCode();
if (responseCode == 200) {
whenDone.value = url;
}
TaskManager.runTask(whenDone);
} catch (IOException e) {
e.printStackTrace();
TaskManager.runTask(whenDone);
}
});
}

View File

@ -412,14 +412,11 @@ public abstract class SchematicHandler {
}
MainUtil.upload(uuid, file, "schematic", new RunnableVal<OutputStream>() {
@Override public void run(OutputStream output) {
try {
try (GZIPOutputStream gzip = new GZIPOutputStream(output, true)) {
try (NBTOutputStream nos = new NBTOutputStream(gzip)) {
nos.writeNamedTag("Schematic", tag);
}
}
} catch (IOException e) {
e.printStackTrace();
try (NBTOutputStream nos = new NBTOutputStream(
new GZIPOutputStream(output, true))) {
nos.writeNamedTag("Schematic", tag);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}, whenDone);
@ -476,6 +473,7 @@ public abstract class SchematicHandler {
});
}
public class UnsupportedFormatException extends Exception {
/**
* Throw with a message.