T2CodeLib/src/main/java/net/t2code/t2codelib/BUNGEE/api/update/T2CBupdateAPI.java

114 lines
5.2 KiB
Java

package net.t2code.t2codelib.BUNGEE.api.update;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.plugin.Plugin;
import net.t2code.t2codelib.BUNGEE.api.messages.T2CBsend;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
public class T2CBupdateAPI {
public static HashMap<String, T2CBupdateObject> bungeePluginVersionen = new HashMap<String, T2CBupdateObject>();
public static void sendUpdateMsg(String Prefix, String Spigot, String Discord, String foundVersion, String update_version) {
T2CBsend.console("§4=========== " + Prefix + " §4===========");
T2CBsend.console("§6A new version was found!");
T2CBsend.console("§6Your version: §c" + foundVersion + " §7- §6Current version: §a" + update_version);
T2CBsend.console("§6You can download it here: §e" + Spigot);
T2CBsend.console("§6You can find more information on Discord: §e" + Discord);
T2CBsend.console("§4=========== " + Prefix + " §4===========");
}
public static void sendUpdateMsg(String Prefix, String Spigot, String Discord, String foundVersion, String update_version, CommandSender sender) {
T2CBsend.sender(sender,"§4=========== " + Prefix + " §4===========");
T2CBsend.sender(sender,"§6A new version was found!");
T2CBsend.sender(sender,"§6Your version: §c" + foundVersion + " §7- §6Current version: §a" + update_version);
T2CBsend.sender(sender,"§6You can download it here: §e" + Spigot);
T2CBsend.sender(sender,"§6You can find more information on Discord: §e" + Discord);
T2CBsend.sender(sender,"§4=========== " + Prefix + " §4===========");
}
private static Boolean noUpdate = true;
private static String pluginVersion;
public static void onUpdateCheckTimer(Plugin plugin, String Prefix, String Spigot, String Discord, Integer SpigotID) {
ProxyServer.getInstance().getScheduler().schedule(plugin, new Runnable() {
public void run() {
(new T2CBupdateAPI(plugin, SpigotID)).getVersion((update_version) -> {
pluginVersion = plugin.getDescription().getVersion();
T2CBupdateObject update = new T2CBupdateObject(
plugin.getDescription().getName(),
pluginVersion,
update_version
);
bungeePluginVersionen.put(plugin.getDescription().getName(), update);
if (!pluginVersion.replace("_Bungee", "").equalsIgnoreCase(update_version)) {
sendUpdateMsg(Prefix, Spigot, Discord, pluginVersion, update_version);
noUpdate = true;
} else {
if (noUpdate) {
T2CBsend.console(Prefix + " §2No update found.");
noUpdate = false;
}
}
},Prefix, pluginVersion);
}
}, 0, 20 * 60 * 60L, TimeUnit.SECONDS);
}
private Plugin plugin;
private int resourceId;
public T2CBupdateAPI(Plugin plugin, int resourceId) {
this.plugin = plugin;
this.resourceId = resourceId;
}
public void getVersion(Consumer<String> consumer, String Prefix, String pluginVersion) {
ProxyServer.getInstance().getScheduler().runAsync(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) {
T2CBupdateObject update = new T2CBupdateObject(
plugin.getDescription().getName(),
pluginVersion,
"§4No public version found!"
);
bungeePluginVersionen.put(plugin.getDescription().getName(), update);
this.plugin.getLogger().severe(Prefix + "§4 Cannot look for updates: " + var10.getMessage());
}
});
}
}