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);
}
}
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();
}
}
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();
}
}
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;
}
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;
}
Aggregations