Skip to content

Commit

Permalink
Auto check for updates once a day
Browse files Browse the repository at this point in the history
  • Loading branch information
heychazza committed Feb 12, 2024
1 parent 5a33b1d commit ad24e54
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 39 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
defaultTasks("clean", "shadowJar")

group = "io.tebex.analytics"
version = "2.1.0"
version = "2.2.0"

subprojects {
plugins.apply("java")
Expand Down
78 changes: 43 additions & 35 deletions bukkit/src/main/java/io/tebex/analytics/AnalyticsPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
Expand Down Expand Up @@ -145,7 +146,7 @@ public void onEnable() {
});
} else {
log(Level.WARNING, "Welcome to Tebex Analytics! It seems like this is a new setup.");
log(Level.WARNING, "To get started, please use the 'analytics setup <key>' command in the console.");
log(Level.WARNING, "To get started, please use the 'analytics setup <secret-key>' command in the console.");
}

// Register events.
Expand All @@ -154,27 +155,6 @@ public void onEnable() {

debug("Debug mode enabled. Type 'analytics debug' to disable.");

sdk.getPluginVersion(getType()).thenAccept(pluginInformation -> {
if (VersionUtil.isNewerVersion(getVersion(), pluginInformation.getVersionName())) {
log(Level.WARNING, String.format("New version available (v%s). You are currently running v%s.", pluginInformation.getVersionName(), getDescription().getVersion()));
log(Level.WARNING, "Download the latest version at: " + pluginInformation.getDownloadUrl());
log(Level.WARNING, "View the changelog at: https://analy.se/plugin/releases/tag/" + pluginInformation.getVersionName());
} else {
log("You are running the latest version of Analytics.");
}
}).exceptionally(ex -> {
Throwable cause = ex.getCause();
log(Level.WARNING, "Failed to get plugin version: " + cause.getMessage());

if(cause instanceof ServerNotFoundException) {
this.halt();
} else {
cause.printStackTrace();
}

return null;
});

proxyMessageListener = new ProxyMessageListener(this);
proxyModeEnabled = config.hasProxyModeEnabled();

Expand All @@ -196,13 +176,39 @@ public void onEnable() {
} catch (final ClassNotFoundException ignored) {
getScheduler().globalRegionalScheduler().runDelayed(this::loadModules, 1);
}

getScheduler().asyncScheduler().runAtFixedRate(this::sendTelemetry, Duration.ZERO, Duration.ofDays(1));
getScheduler().asyncScheduler().runAtFixedRate(this::checkForUpdates, Duration.ZERO, Duration.ofDays(1));
}

@Override
public void onDisable() {
unloadModules();
}

private void checkForUpdates() {
sdk.getPluginVersion(getType()).thenAccept(pluginInformation -> {
if (!VersionUtil.isNewerVersion(getVersion(), pluginInformation.getVersionName())) {
return;
}

log(Level.WARNING, String.format("New version available (v%s). You are currently running v%s.", pluginInformation.getVersionName(), getDescription().getVersion()));
log(Level.WARNING, "Download the latest version at: " + pluginInformation.getDownloadUrl());
log(Level.WARNING, "View the changelog at: https://analy.se/plugin/releases/tag/" + pluginInformation.getVersionName());
}).exceptionally(ex -> {
Throwable cause = ex.getCause();
log(Level.WARNING, "Failed to get plugin version: " + cause.getMessage());

if(cause instanceof ServerNotFoundException) {
this.halt();
} else {
cause.printStackTrace();
}

return null;
});
}

@Override
public void loadModules() {
try {
Expand Down Expand Up @@ -306,24 +312,26 @@ public boolean isSetup() {
public void configure() {
setup = true;
heartbeatManager.start();
}

if(isSetup()) {
sdk.sendTelemetry().thenAccept(telemetry -> {
debug("Sent telemetry data.");
}).exceptionally(ex -> {
Throwable cause = ex.getCause();
private void sendTelemetry() {
if(! isSetup()) return;

log(Level.WARNING, "Failed to send telemetry: " + cause.getMessage());
sdk.sendTelemetry().thenAccept(telemetry -> {
debug("Sent telemetry data.");
}).exceptionally(ex -> {
Throwable cause = ex.getCause();

if(cause instanceof RateLimitException) {
log(Level.WARNING, "Please wait a few minutes and try again.");
} else {
cause.printStackTrace();
}
log(Level.WARNING, "Failed to send telemetry: " + cause.getMessage());

if(cause instanceof RateLimitException) {
log(Level.WARNING, "Please wait a few minutes and try again.");
return null;
});
}
}

cause.printStackTrace();
return null;
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public class DebugCommand extends SubCommand {
public DebugCommand(AnalyticsPlugin platform) {
super(platform, "debug", "analytics.admin");
super(platform, "debug", "analytics.debug");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public class ReloadCommand extends SubCommand {
public ReloadCommand(AnalyticsPlugin platform) {
super(platform, "reload", "analytics.admin");
super(platform, "reload", "analytics.reload");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

public class SetupCommand extends SubCommand {
public SetupCommand(AnalyticsPlugin platform) {
super(platform, "setup", "analytics.admin");
super(platform, "setup", "analytics.setup");
}

@Override
Expand Down
30 changes: 30 additions & 0 deletions bukkit/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,36 @@ softdepend:
commands:
analytics:
description: The main command
permission: analytics.admin
aliases:
- analyse
- tebexanalytics
permissions:
analytics.admin:
description: Allows debug command
default: op
analytics.debug:
description: Allows debug command
default: op
analytics.reload:
description: Allows reload command
default: op
analytics.setup:
description: Allows setup command
default: op
analytics.stats:
description: Allows stats command
default: op
analytics.track:
description: Allows track command
default: op
analytics.*:
description: Wildcard permission
default: op
children:
analytics.admin: true
analytics.debug: true
analytics.reload: true
analytics.setup: true
analytics.stats: true
analytics.track: true

0 comments on commit ad24e54

Please sign in to comment.