Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
Features:Add new version checking feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
CerealAxis committed Feb 21, 2024
1 parent 2c9839d commit b9050c8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/main/java/xaviermc/top/iseeyou/ConfigData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ data class ConfigData(
var clearOutdatedRecordFile: OutdatedRecordRetentionConfig = OutdatedRecordRetentionConfig(),
var recordSuspiciousPlayer: RecordSuspiciousPlayerConfig = RecordSuspiciousPlayerConfig(),
var enableBstats: Boolean = true,
var enableUpdateChecker: Boolean = true,
// var asyncSave: Boolean = false,
) {
fun isConfigValid(): String? {
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/xaviermc/top/iseeyou/ISeeYou.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import xaviermc.top.iseeyou.anticheat.AntiCheatListener
import xaviermc.top.iseeyou.anticheat.listeners.MatrixListener
import xaviermc.top.iseeyou.anticheat.listeners.ThemisListener
import xaviermc.top.iseeyou.anticheat.suspiciousPhotographers
import xaviermc.top.iseeyou.lib.UpdateChecker
import xaviermc.top.iseeyou.metrics.Metrics
import java.io.IOException
import java.nio.file.*
Expand Down Expand Up @@ -77,7 +78,10 @@ class ISeeYou : JavaPlugin(), CommandExecutor {
Metrics.SimplePie(
"chart_id"
) { "My value" })

}
if (toml!!.data.enableUpdateChecker){
val updateChecker: UpdateChecker = UpdateChecker(this, 115177)
updateChecker.checkForUpdates()
}
}

Expand Down
51 changes: 51 additions & 0 deletions src/main/java/xaviermc/top/iseeyou/lib/UpdateChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package xaviermc.top.iseeyou.lib;

import org.bukkit.Bukkit;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Scanner;
import java.util.function.Consumer;

public class UpdateChecker {

private final JavaPlugin plugin;
private final int resourceId;

public UpdateChecker(JavaPlugin plugin, int resourceId) {
this.plugin = plugin;
this.resourceId = resourceId;
}

public void getVersion(final Consumer<String> consumer) {
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> {
try (InputStream inputStream = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + this.resourceId).openStream(); Scanner scanner = new Scanner(inputStream)) {
if (scanner.hasNext()) {
consumer.accept(scanner.next());
}
} catch (IOException exception) {
plugin.getLogger().info("Failed to check for updates: " + exception.getMessage());
}
});
}

public void checkForUpdates() {
getVersion(version -> {
String currentVersion = "v1.1.6";
if (currentVersion.compareToIgnoreCase(version) > 0) {
plugin.getLogger().info("You are currently using a possible test version. For stability, please use the latest stable version!");
plugin.getLogger().info("The current latest stable version is: " + version);
String updateUrl = "https://www.spigotmc.org/resources/iseeyou-fork.115177/";
plugin.getLogger().info("Update URL: " + updateUrl);
} else if (currentVersion.equals(version)) {
plugin.getLogger().info("No new updates available.");
} else {
plugin.getLogger().info("A new update is available. Version: " + version);
String updateUrl = "https://www.spigotmc.org/resources/iseeyou-fork.115177/";
plugin.getLogger().info("Update URL: " + updateUrl);
}
});
}
}

0 comments on commit b9050c8

Please sign in to comment.