Search in sources :

Example 1 with PGZIPOutputStream

use of cn.nukkit.nbt.stream.PGZIPOutputStream in project Nukkit by Nukkit.

the class TimingsExport method run.

@Override
public void run() {
    this.sender.sendMessage(new TranslationContainer("nukkit.command.timings.uploadStart"));
    this.out.add("data", JsonUtil.mapToArray(this.history, TimingsHistory::export));
    String response = null;
    try {
        HttpURLConnection con = (HttpURLConnection) new URL("http://timings.aikar.co/post").openConnection();
        con.setDoOutput(true);
        con.setRequestProperty("User-Agent", "Nukkit/" + Server.getInstance().getName() + "/" + InetAddress.getLocalHost().getHostName());
        con.setRequestMethod("POST");
        con.setInstanceFollowRedirects(false);
        PGZIPOutputStream request = new PGZIPOutputStream(con.getOutputStream());
        request.setLevel(Deflater.BEST_COMPRESSION);
        request.write(new Gson().toJson(this.out).getBytes("UTF-8"));
        request.close();
        response = getResponse(con);
        if (con.getResponseCode() != 302) {
            this.sender.sendMessage(new TranslationContainer("nukkit.command.timings.uploadError", new String[] { String.valueOf(con.getResponseCode()), con.getResponseMessage() }));
            if (response != null) {
                Server.getInstance().getLogger().alert(response);
            }
            return;
        }
        String location = con.getHeaderField("Location");
        this.sender.sendMessage(new TranslationContainer("nukkit.command.timings.timingsLocation", location));
        if (!(this.sender instanceof ConsoleCommandSender)) {
            Server.getInstance().getLogger().info(Server.getInstance().getLanguage().translateString("nukkit.command.timings.timingsLocation", location));
        }
        if (response != null && !response.isEmpty()) {
            Server.getInstance().getLogger().info(Server.getInstance().getLanguage().translateString("nukkit.command.timings.timingsResponse", response));
        }
        File timingFolder = new File(Server.getInstance().getDataPath() + File.separator + "timings");
        timingFolder.mkdirs();
        String fileName = timingFolder + File.separator + new SimpleDateFormat("'timings-'yyyy-MM-dd-hh-mm'.txt'").format(new Date());
        FileWriter writer = new FileWriter(fileName);
        writer.write(Server.getInstance().getLanguage().translateString("nukkit.command.timings.timingsLocation", location) + "\n\n");
        writer.write(new GsonBuilder().setPrettyPrinting().create().toJson(this.out));
        writer.close();
        Server.getInstance().getLogger().info(Server.getInstance().getLanguage().translateString("nukkit.command.timings.timingsWrite", fileName));
    } catch (IOException exception) {
        this.sender.sendMessage(TextFormat.RED + "" + new TranslationContainer("nukkit.command.timings.reportError"));
        if (response != null) {
            Server.getInstance().getLogger().alert(response);
        }
        Server.getInstance().getLogger().logException(exception);
    }
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) PGZIPOutputStream(cn.nukkit.nbt.stream.PGZIPOutputStream) Gson(com.google.gson.Gson) ConsoleCommandSender(cn.nukkit.command.ConsoleCommandSender) RemoteConsoleCommandSender(cn.nukkit.command.RemoteConsoleCommandSender) URL(java.net.URL) Date(java.util.Date) HttpURLConnection(java.net.HttpURLConnection) TranslationContainer(cn.nukkit.lang.TranslationContainer) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

ConsoleCommandSender (cn.nukkit.command.ConsoleCommandSender)1 RemoteConsoleCommandSender (cn.nukkit.command.RemoteConsoleCommandSender)1 TranslationContainer (cn.nukkit.lang.TranslationContainer)1 PGZIPOutputStream (cn.nukkit.nbt.stream.PGZIPOutputStream)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1