T2CodeLib/src/main/java/net/t2code/lib/Bungee/Lib/update/BUpdateAPI.java

116 lines
5.2 KiB
Java

package net.t2code.lib.Bungee.Lib.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.lib.Bungee.Lib.messages.Bsend;
import net.t2code.lib.Spigot.Lib.update.UpdateObject;
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 BUpdateAPI {
public static HashMap<String, UpdateObject> bungeePluginVersionen = new HashMap<String, UpdateObject>();
public static void sendUpdateMsg(String Prefix, String Spigot, String Discord, String foundVersion, String update_version) {
Bsend.console("§4=========== " + Prefix + " §4===========");
Bsend.console("§6A new version was found!");
Bsend.console("§6Your version: §c" + foundVersion + " §7- §6Current version: §a" + update_version);
Bsend.console("§6You can download it here: §e" + Spigot);
Bsend.console("§6You can find more information on Discord: §e" + Discord);
Bsend.console("§4=========== " + Prefix + " §4===========");
}
public static void sendUpdateMsg(String Prefix, String Spigot, String Discord, String foundVersion, String update_version, CommandSender sender) {
Bsend.sender(sender,"§4=========== " + Prefix + " §4===========");
Bsend.sender(sender,"§6A new version was found!");
Bsend.sender(sender,"§6Your version: §c" + foundVersion + " §7- §6Current version: §a" + update_version);
Bsend.sender(sender,"§6You can download it here: §e" + Spigot);
Bsend.sender(sender,"§6You can find more information on Discord: §e" + Discord);
Bsend.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 BUpdateAPI(plugin, SpigotID)).getVersion((update_version) -> {
pluginVersion = plugin.getDescription().getVersion();
UpdateObject update = new UpdateObject(
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) {
Bsend.console(Prefix + " §2No update found.");
noUpdate = false;
}
}
},Prefix, pluginVersion);
}
}, 0, 20 * 60 * 60L, TimeUnit.SECONDS);
}
private Plugin plugin;
private int resourceId;
public BUpdateAPI(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) {
UpdateObject update = new UpdateObject(
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());
}
});
}
}