Search in sources :

Example 1 with Autotip

use of me.semx11.autotip.Autotip in project Hyperium by HyperiumClient.

the class Autotip method setup.

private void setup() {
    try {
        fileUtil = new FileUtil(this);
        gson = new GsonBuilder().registerTypeAdapter(Config.class, new ConfigCreator(this)).registerTypeAdapter(StatsDaily.class, new StatsDailyCreator(this)).setExclusionStrategies(new AnnotationExclusionStrategy()).setPrettyPrinting().create();
        config = new Config(this);
        reloadGlobalSettings();
        reloadLocale();
        sessionManager = new SessionManager(this);
        statsManager = new StatsManager(this);
        migrationManager = new MigrationManager(this);
        fileUtil.createDirectories();
        config.load();
        taskManager.getExecutor().execute(() -> migrationManager.migrateLegacyFiles());
        registerEvents(new EventClientConnection(this), new EventChatReceived(this));
        registerCommands(new CommandAutotip(this), new CommandLimbo(this));
        Runtime.getRuntime().addShutdownHook(new Thread(sessionManager::logout));
        initialized = true;
    } catch (IOException e) {
        messageUtil.send("Autotip is disabled because it couldn't create the required files.");
        ErrorReport.reportException(e);
    } catch (IllegalStateException e) {
        messageUtil.send("Autotip is disabled because it couldn't connect to the API.");
        ErrorReport.reportException(e);
    }
}
Also used : ConfigCreator(me.semx11.autotip.gson.creator.ConfigCreator) CommandAutotip(me.semx11.autotip.command.impl.CommandAutotip) GsonBuilder(com.google.gson.GsonBuilder) EventClientConnection(me.semx11.autotip.event.impl.EventClientConnection) Config(me.semx11.autotip.config.Config) SessionManager(me.semx11.autotip.core.SessionManager) CommandLimbo(me.semx11.autotip.command.impl.CommandLimbo) StatsDailyCreator(me.semx11.autotip.gson.creator.StatsDailyCreator) IOException(java.io.IOException) MigrationManager(me.semx11.autotip.core.MigrationManager) AnnotationExclusionStrategy(me.semx11.autotip.gson.exclusion.AnnotationExclusionStrategy) StatsManager(me.semx11.autotip.core.StatsManager) EventChatReceived(me.semx11.autotip.event.impl.EventChatReceived) FileUtil(me.semx11.autotip.util.FileUtil)

Example 2 with Autotip

use of me.semx11.autotip.Autotip in project Hyperium by HyperiumClient.

the class RequestHandler method getReply.

public static Optional<Reply> getReply(Request request, URI uri) {
    String json = null;
    HttpURLConnection conn = null;
    try {
        conn = (HttpURLConnection) uri.toURL().openConnection();
        conn.setRequestProperty("User-Agent", "Autotip v" + autotip.getVersion());
        InputStream input = conn.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST ? conn.getInputStream() : conn.getErrorStream();
        json = IOUtils.toString(input, StandardCharsets.UTF_8);
        Autotip.LOGGER.info(request.getType() + " JSON: " + json);
        Reply reply = GSON.fromJson(json, (Type) request.getType().getReplyClass());
        return Optional.ofNullable(reply);
    } catch (IOException | JsonParseException e) {
        ErrorReport.reportException(e);
        Autotip.LOGGER.debug(request.getType() + " JSON: " + json);
        return Optional.empty();
    } finally {
        if (conn != null)
            conn.disconnect();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) Reply(me.semx11.autotip.api.reply.Reply) IOException(java.io.IOException) JsonParseException(com.google.gson.JsonParseException)

Example 3 with Autotip

use of me.semx11.autotip.Autotip in project Hyperium by HyperiumClient.

the class ErrorReport method reportException.

public static void reportException(Throwable t) {
    Autotip.LOGGER.error(t.getMessage(), t);
    HttpURLConnection conn = null;
    try {
        URL url = new URL("https://api.autotip.pro/error_report.php");
        conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        JsonObjectBuilder builder = JsonObjectBuilder.newBuilder().addString("username", autotip.getGameProfile().getName()).addString("uuid", autotip.getGameProfile().getId()).addString("v", autotip.getVersion()).addString("mc", autotip.getMcVersion()).addString("os", System.getProperty("os.name")).addString("forge", "hyperium").addString("stackTrace", ExceptionUtils.getStackTrace(t)).addNumber("time", System.currentTimeMillis());
        if (autotip.isInitialized()) {
            EventClientConnection event = autotip.getEvent(EventClientConnection.class);
            builder.addString("sessionKey", autotip.getSessionManager().getKey()).addString("serverIp", event.getServerIp());
        }
        byte[] jsonBytes = builder.build().toString().getBytes(StandardCharsets.UTF_8);
        conn.setFixedLengthStreamingMode(jsonBytes.length);
        conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        conn.setRequestProperty("User-Agent", "Autotip v" + autotip.getVersion());
        conn.connect();
        try (OutputStream out = conn.getOutputStream()) {
            out.write(jsonBytes);
        }
        InputStream input = conn.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST ? conn.getInputStream() : conn.getErrorStream();
        String json = IOUtils.toString(input, StandardCharsets.UTF_8);
        Autotip.LOGGER.info("Error JSON: " + json);
        input.close();
        conn.disconnect();
    } catch (IOException e) {
        // Hmm... what would happen if I were to report this one?
        e.printStackTrace();
    } finally {
        if (conn != null)
            conn.disconnect();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) EventClientConnection(me.semx11.autotip.event.impl.EventClientConnection) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) URL(java.net.URL)

Example 4 with Autotip

use of me.semx11.autotip.Autotip in project Hyperium by HyperiumClient.

the class StatsManager method get.

/**
 * Get the {@link StatsDaily} for the specified date. This method uses a cache to reduce the
 * amount of read/write cycles.
 *
 * @param date The {@link LocalDate} of the StatsDaily you want to get
 * @return {@link StatsDaily} for the specified date
 */
public StatsDaily get(LocalDate date) {
    if (cache.containsKey(date))
        return cache.get(date);
    StatsDaily stats = load(new StatsDaily(autotip, date));
    cache.put(date, stats);
    return stats;
}
Also used : StatsDaily(me.semx11.autotip.stats.StatsDaily)

Example 5 with Autotip

use of me.semx11.autotip.Autotip in project Hyperium by HyperiumClient.

the class StatsManager method getRange.

/**
 * Get the {@link StatsRange} for the specified date range. This method uses {@link
 * #get(LocalDate)} to get all the {@link StatsDaily} that are contained within this range.
 *
 * @param start The starting {@link LocalDate}
 * @param end   The ending {@link LocalDate}
 * @return {@link StatsRange} for the specified date range
 */
public StatsRange getRange(LocalDate start, LocalDate end) {
    if (start.isBefore(fileUtil.getFirstDate()))
        start = fileUtil.getFirstDate();
    if (end.isAfter(LocalDate.now()))
        end = LocalDate.now();
    StatsRange range = new StatsRange(autotip, start, end);
    Stream.iterate(start, date -> date.plusDays(1)).limit(ChronoUnit.DAYS.between(start, end) + 1).forEach(date -> range.merge(get(date)));
    return range;
}
Also used : StatsRange(me.semx11.autotip.stats.StatsRange)

Aggregations

IOException (java.io.IOException)4 EventClientConnection (me.semx11.autotip.event.impl.EventClientConnection)3 InputStream (java.io.InputStream)2 HttpURLConnection (java.net.HttpURLConnection)2 StatsRange (me.semx11.autotip.stats.StatsRange)2 FileUtil (me.semx11.autotip.util.FileUtil)2 GsonBuilder (com.google.gson.GsonBuilder)1 JsonParseException (com.google.gson.JsonParseException)1 GameProfile (com.mojang.authlib.GameProfile)1 File (java.io.File)1 OutputStream (java.io.OutputStream)1 URL (java.net.URL)1 Files (java.nio.file.Files)1 LocalDate (java.time.LocalDate)1 List (java.util.List)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 Autotip (me.semx11.autotip.Autotip)1 Reply (me.semx11.autotip.api.reply.Reply)1