T2Code-Alias/src/main/java/net/t2code/alias/Spigot/system/UpdateChecker.java

117 lines
5.0 KiB
Java

// This claas was created by JaTiTV
package net.t2code.alias.Spigot.system;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.TextComponent;
import net.t2code.alias.Spigot.Main;
import net.t2code.lib.Spigot.Lib.messages.TextBuilder;
import net.t2code.lib.Spigot.Lib.messages.send;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Scanner;
import java.util.function.Consumer;
public class UpdateChecker {
public static void sendUpdateMsg(String Prefix, String foundVersion, String update_version) {
send.console("§4=========== " + Prefix + " §4===========");
send.console("§6A new version was found!");
send.console("§6Your version: §c" + foundVersion + " §7- §6Current version: §a" + update_version);
send.console("§6You can download it here: §e" + Main.spigot);
send.console("§6You can find more information on Discord: §e" + Main.discord);
send.console("§4=========== " + Prefix + " §4===========");
}
public static void sendUpdateMsg(String Prefix, String foundVersion, String update_version, Player player) {
TextComponent comp = new TextBuilder(Prefix + " §6A new version was found!")
.addHover("§6You can download it here: §e" + Main.spigot).addClickEvent(ClickEvent.Action.OPEN_URL, Main.spigot).build();
player.spigot().sendMessage(comp);
TextComponent comp1 = new TextBuilder(Prefix + " §c" + foundVersion + " §7-> §a" + update_version)
.addHover("§6You can download it here: §e" + Main.spigot).addClickEvent(ClickEvent.Action.OPEN_URL, Main.spigot).build();
player.spigot().sendMessage(comp1);
TextComponent comp2 = new TextBuilder(Prefix + " §6You can find more information on Discord.")
.addHover("§e" + Main.discord).addClickEvent(ClickEvent.Action.OPEN_URL, Main.discord).build();
player.spigot().sendMessage(comp2);
}
private static Boolean noUpdate = true;
public static void onUpdateCheck() {
int taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.plugin, new Runnable() {
public void run() {
(new UpdateChecker(Main.plugin, Main.spigotID)).getVersion((update_version) -> {
String foundVersion = Main.plugin.getDescription().getVersion();
// Main.update_version = update_version;
if (!foundVersion.equalsIgnoreCase(update_version)) {
new BukkitRunnable() {
@Override
public void run() {
sendUpdateMsg(Main.prefix, foundVersion, update_version);
}
}.runTaskLater(Main.plugin, 600L);
}else {
if (noUpdate) {
send.console(Main.prefix + " §2No update found.");
noUpdate = false;
}
}
});
}
}, 0L, 20 * 60 * 60L);
}
private JavaPlugin plugin;
private int resourceId;
public UpdateChecker(JavaPlugin plugin, int resourceId) {
this.plugin = plugin;
this.resourceId = resourceId;
}
public void getVersion(Consumer<String> consumer) {
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> {
try {
InputStream inputStream = (new URL("https://api.spigotmc.org/legacy/update.php?resource=" + this.resourceId)).openStream();
try {
Scanner scanner = new Scanner(inputStream);
try {
if (scanner.hasNext()) {
consumer.accept(scanner.next());
}
} catch (Throwable var8) {
try {
scanner.close();
} catch (Throwable var7) {
var8.addSuppressed(var7);
}
throw var8;
}
scanner.close();
} catch (Throwable var9) {
if (inputStream != null) {
try {
inputStream.close();
} catch (Throwable var6) {
var9.addSuppressed(var6);
}
}
throw var9;
}
if (inputStream != null) {
inputStream.close();
}
} catch (IOException var10) {
// Main.update_version = " §4No public version found!";
this.plugin.getLogger().severe(Main.prefix + "§4 Cannot look for updates: " + var10.getMessage());
}
});
}
}