Search in sources :

Example 1 with APIResponse

use of gs.mclo.java.APIResponse in project mclogs-fabric by aternosorg.

the class MclogsFabricLoader method share.

public static int share(ServerCommandSource source, String filename) {
    MclogsAPI.mcversion = source.getMinecraftServer().getVersion();
    logger.log(Level.INFO, "Sharing " + filename);
    source.sendFeedback(new LiteralText("Sharing " + filename), false);
    try {
        Path logs = source.getMinecraftServer().getFile("logs/").toPath();
        Path log = logs.resolve(filename);
        if (!log.getParent().equals(logs)) {
            throw new FileNotFoundException();
        }
        APIResponse response = MclogsAPI.share(log);
        if (response.success) {
            LiteralText feedback = new LiteralText("Your log has been uploaded: ");
            feedback.setStyle(Style.EMPTY.withColor(Formatting.GREEN));
            LiteralText link = new LiteralText(response.url);
            Style linkStyle = Style.EMPTY.withColor(Formatting.BLUE);
            linkStyle = linkStyle.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, response.url));
            link.setStyle(linkStyle);
            source.sendFeedback(feedback.append(link), true);
            return 1;
        } else {
            logger.error("An error occurred when uploading your log: ");
            logger.error(response.error);
            LiteralText error = new LiteralText("An error occurred. Check your log for more details");
            source.sendError(error);
            return 0;
        }
    } catch (FileNotFoundException | IllegalArgumentException e) {
        LiteralText error = new LiteralText("The log file " + filename + " doesn't exist. Use '/mclogs list' to list all logs.");
        source.sendError(error);
        return -1;
    } catch (IOException e) {
        source.sendError(new LiteralText("An error occurred. Check your log for more details"));
        logger.error("Could not get log file!");
        logger.error(e);
        return 0;
    }
}
Also used : Path(java.nio.file.Path) APIResponse(gs.mclo.java.APIResponse) ClickEvent(net.minecraft.text.ClickEvent) FileNotFoundException(java.io.FileNotFoundException) Style(net.minecraft.text.Style) IOException(java.io.IOException) LiteralText(net.minecraft.text.LiteralText)

Aggregations

APIResponse (gs.mclo.java.APIResponse)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 ClickEvent (net.minecraft.text.ClickEvent)1 LiteralText (net.minecraft.text.LiteralText)1 Style (net.minecraft.text.Style)1