34 lines
1.1 KiB
Java
34 lines
1.1 KiB
Java
package net.t2code.lib.Spigot.system;
|
|
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.plugin.Plugin;
|
|
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("Unable to check for updates: " + exception.getMessage());
|
|
}
|
|
});
|
|
}
|
|
}
|