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

160 lines
7.6 KiB
Java

package net.t2code.t2codelib.BUNGEE.api.update;
import net.md_5.bungee.BungeeCord;
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 net.t2code.t2codelib.BUNGEE.system.config.T2CBlibConfig;
import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend;
import net.t2code.t2codelib.T2CupdateObject;
import net.t2code.t2codelib.T2CupdateWebData;
import net.t2code.t2codelib.UpdateType;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
public class T2CBupdateAPI {
public static HashMap<String, T2CupdateObject> bungeePluginVersins = new HashMap<>();
public static void sendUpdateMsg(String prefix, String discord, T2CupdateWebData webData, Plugin plugin, CommandSender sender) {
String publicVersion = webData.getVersion();
String pluginVersion = plugin.getDescription().getVersion();
String value;
if (webData.isPreRelease()) {
value = UpdateType.PRERELEASE.text;
if (publicVersion.toLowerCase().contains("dev")) {
value = UpdateType.DEVELOPMENT.text;
}
if (publicVersion.toLowerCase().contains("beta")) {
value = UpdateType.BETA.text;
}
if (publicVersion.toLowerCase().contains("snapshot")) {
value = UpdateType.SNAPSHOT.text;
}
} else value = UpdateType.STABLE.text;
String h = "<br><dark_red>╔══════════════</dark_red>" + prefix + "<dark_red>══════════════</dark_red>";
String s1 = "<br><dark_red>║</dark_red> <gold>A new [value] version was found!</gold>".replace("[value]", value);
String s2 = "<br><dark_red>║</dark_red> <gold>Your version: <red>" + pluginVersion + "</red> <gray>-</gray> Current version:</gold> <green>" + webData.getVersion() + "</green>";
String s3 = "<br><dark_red>║</dark_red> <gold>You can download it here:</gold> <yellow>" + webData.getUpdateUrl() + "</yellow>";
String s4 = "<br><dark_red>║</dark_red> <gold>You can find more information on Discord:</gold> <yellow>" + discord + "</yellow>";
String f = "<br><dark_red>╚══════════════</dark_red>" + prefix + "<dark_red>══════════════</dark_red>";
String text = h + s1 + s2 + s3 + s4 + f;
if (sender == null) {
T2CBsend.console(text);
} else T2CBsend.sender(sender, text);
}
private static Boolean noUpdate = true;
private static String pluginVersion;
public static void onUpdateCheckTimer(Plugin plugin, String prefix, String discord, Integer spigotID, String url) {
ProxyServer.getInstance().getScheduler().schedule(plugin, new Runnable() {
public void run() {
(new T2CBupdateAPI(plugin, spigotID)).getVersion((webData) -> {
pluginVersion = plugin.getDescription().getVersion();
T2CupdateObject update = new T2CupdateObject(
plugin.getDescription().getName(),
plugin.getDescription().getVersion(),
webData,
false,
!plugin.getDescription().getVersion().equals(webData.getVersion())
);
bungeePluginVersins.put(plugin.getDescription().getName(), update);
if (!pluginVersion.replace("_Bungee", "").equalsIgnoreCase(webData.getVersion())) {
sendUpdateMsg(prefix, discord, webData, plugin, null);
noUpdate = true;
} else {
if (noUpdate) {
T2CBsend.console(prefix + " §2No update found.");
noUpdate = false;
}
}
}, pluginVersion, spigotID, url);
}
}, 0, T2CBlibConfig.getUpdateTimer() * 60 * 20L, TimeUnit.SECONDS);
}
private Plugin plugin;
private int resourceId;
public T2CBupdateAPI(Plugin plugin, int resourceId) {
this.plugin = plugin;
this.resourceId = resourceId;
}
public void getVersion(Consumer<T2CupdateWebData> consumer, String pluginVersion, Integer spigotID, String URL) {
BungeeCord.getInstance().getScheduler().runAsync(this.plugin, () -> {
try {
URL url = new URL(URL);
URLConnection yc = url.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
String inputLine;
String data = "";
while ((inputLine = in.readLine()) != null)
data = inputLine;
in.close();
data = data.substring(1, data.length() - 1);
if (data.isEmpty()) {
consumer.accept(null);
return;
}
JSONObject obj = new JSONObject(data);
String UpdateName = obj.getString("name");
String tag_name = obj.getString("tag_name");
String body = obj.getString("body").replace("\n", "<br>").replace("\r", "").replace("'", "''");
String updateurl = obj.getString("html_url");
boolean prerelease = obj.getBoolean("prerelease");
String date = obj.getString("published_at");
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
Date parsedDate = inputFormat.parse(date);
String formattedDate = outputFormat.format(parsedDate);
JSONArray downloadArray = obj.getJSONArray("assets");
String downloadURL;
if (downloadArray.isEmpty()) {
downloadURL = "https://www.spigotmc.org/resources/" + spigotID;
} else {
downloadURL = downloadArray.getJSONObject(0).getString("browser_download_url");
}
if (!prerelease) {
downloadURL = "https://www.spigotmc.org/resources/" + spigotID;
updateurl = "https://www.spigotmc.org/resources/" + spigotID;
}
T2CupdateWebData webData = new T2CupdateWebData(UpdateName, tag_name, body, updateurl, formattedDate, downloadURL, prerelease);
consumer.accept(webData);
} catch (Exception var10) {
Boolean load = false;
if (bungeePluginVersins.containsKey(plugin.getDescription().getName())) {
load = bungeePluginVersins.get(plugin.getDescription().getName()).load;
}
T2CupdateObject update = new T2CupdateObject(
plugin.getDescription().getName(),
pluginVersion,
null,
load,
false
);
bungeePluginVersins.put(plugin.getDescription().getName(), update);
this.plugin.getLogger().severe("§4 Cannot look for updates: " + var10.getMessage());
}
});
}
}