New Bungee Updater

This commit is contained in:
Jkobs 2022-11-14 14:41:45 +01:00
parent 5857e02695
commit f70767b361
12 changed files with 94 additions and 73 deletions

View File

@ -39,7 +39,7 @@ public class T2CBtemplate {
} }
public static void sendInfo(CommandSender sender, Plugin plugin, String prefix, String spigot, String discord, String autor) { public static void sendInfo(CommandSender sender, Plugin plugin, String prefix, String spigot, String discord, String autor) {
String publicVersion = T2CBupdateAPI.bungeePluginVersins.get(plugin.getDescription().getName()).publicVersion; String publicVersion = T2CBupdateAPI.bungeePluginVersins.get(plugin.getDescription().getName()).webData.getVersion();
String pluginVersion = plugin.getDescription().getVersion(); String pluginVersion = plugin.getDescription().getVersion();
T2CBsend.sender(sender, prefix + "§4======= " + prefix + " §4======="); T2CBsend.sender(sender, prefix + "§4======= " + prefix + " §4=======");
T2CBsend.sender(sender, prefix + " §2Autor: §6" + autor); T2CBsend.sender(sender, prefix + " §2Autor: §6" + autor);

View File

@ -1,20 +1,26 @@
package net.t2code.t2codelib.BUNGEE.api.update; 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.CommandSender;
import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.plugin.Plugin; import net.md_5.bungee.api.plugin.Plugin;
import net.t2code.t2codelib.BUNGEE.api.messages.T2CBsend; import net.t2code.t2codelib.BUNGEE.api.messages.T2CBsend;
import net.t2code.t2codelib.T2CupdateObject;
import java.io.IOException; import net.t2code.t2codelib.T2CupdateWebData;
import java.io.InputStream; import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL; import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Scanner;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.function.Consumer; import java.util.function.Consumer;
public class T2CBupdateAPI { public class T2CBupdateAPI {
public static HashMap<String, T2CBupdateObject> bungeePluginVersins = new HashMap<String, T2CBupdateObject>(); public static HashMap<String, T2CupdateObject> bungeePluginVersins = new HashMap<>();
public static void sendUpdateMsg(String Prefix, String Spigot, String Discord, String foundVersion, String update_version) { public static void sendUpdateMsg(String Prefix, String Spigot, String Discord, String foundVersion, String update_version) {
T2CBsend.console("§4=========== " + Prefix + " §4==========="); T2CBsend.console("§4=========== " + Prefix + " §4===========");
@ -24,29 +30,34 @@ public class T2CBupdateAPI {
T2CBsend.console("§6You can find more information on Discord: §e" + Discord); T2CBsend.console("§6You can find more information on Discord: §e" + Discord);
T2CBsend.console("§4=========== " + Prefix + " §4==========="); T2CBsend.console("§4=========== " + Prefix + " §4===========");
} }
public static void sendUpdateMsg(String Prefix, String Spigot, String Discord, String foundVersion, String update_version, CommandSender sender) { 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, "§4=========== " + Prefix + " §4===========");
T2CBsend.sender(sender,"§6A new version was found!"); T2CBsend.sender(sender, "§6A new version was found!");
T2CBsend.sender(sender,"§6Your version: §c" + foundVersion + " §7- §6Current version: §a" + update_version); 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 download it here: §e" + Spigot);
T2CBsend.sender(sender,"§6You can find more information on Discord: §e" + Discord); T2CBsend.sender(sender, "§6You can find more information on Discord: §e" + Discord);
T2CBsend.sender(sender,"§4=========== " + Prefix + " §4==========="); T2CBsend.sender(sender, "§4=========== " + Prefix + " §4===========");
} }
private static Boolean noUpdate = true; private static Boolean noUpdate = true;
private static String pluginVersion; private static String pluginVersion;
public static void onUpdateCheckTimer(Plugin plugin, String Prefix, String Spigot, String Discord, Integer SpigotID) {
public static void onUpdateCheckTimer(Plugin plugin, String Prefix, String Spigot, String Discord, Integer SpigotID, String url) {
ProxyServer.getInstance().getScheduler().schedule(plugin, new Runnable() { ProxyServer.getInstance().getScheduler().schedule(plugin, new Runnable() {
public void run() { public void run() {
(new T2CBupdateAPI(plugin, SpigotID)).getVersion((update_version) -> { (new T2CBupdateAPI(plugin, SpigotID)).getVersion((update_version) -> {
pluginVersion = plugin.getDescription().getVersion(); pluginVersion = plugin.getDescription().getVersion();
T2CBupdateObject update = new T2CBupdateObject( T2CupdateObject update = new T2CupdateObject(
plugin.getDescription().getName(), plugin.getDescription().getName(),
pluginVersion, plugin.getDescription().getVersion(),
update_version update_version,
false,
!plugin.getDescription().getVersion().equals(update_version.getVersion())
); );
bungeePluginVersins.put(plugin.getDescription().getName(), update); bungeePluginVersins.put(plugin.getDescription().getName(), update);
if (!pluginVersion.replace("_Bungee", "").equalsIgnoreCase(update_version)) { if (!pluginVersion.replace("_Bungee", "").equalsIgnoreCase(update_version.getVersion())) {
sendUpdateMsg(Prefix, Spigot, Discord, pluginVersion, update_version); sendUpdateMsg(Prefix, Spigot, Discord, pluginVersion, update_version.getVersion());
noUpdate = true; noUpdate = true;
} else { } else {
if (noUpdate) { if (noUpdate) {
@ -54,10 +65,11 @@ public class T2CBupdateAPI {
noUpdate = false; noUpdate = false;
} }
} }
},Prefix, pluginVersion); }, pluginVersion, SpigotID, url);
} }
}, 0, 20 * 60 * 60L, TimeUnit.SECONDS); }, 0, 20 * 60 * 60L, TimeUnit.SECONDS);
} }
private Plugin plugin; private Plugin plugin;
private int resourceId; private int resourceId;
@ -66,47 +78,66 @@ public class T2CBupdateAPI {
this.resourceId = resourceId; this.resourceId = resourceId;
} }
public void getVersion(Consumer<String> consumer, String Prefix, String pluginVersion) { public void getVersion(Consumer<T2CupdateWebData> consumer, String pluginVersion, Integer spigotID, String URL) {
ProxyServer.getInstance().getScheduler().runAsync(this.plugin, () -> { BungeeCord.getInstance().getScheduler().runAsync(this.plugin, () -> {
try { try {
InputStream inputStream = (new URL("https://api.spigotmc.org/legacy/update.php?resource=" + this.resourceId)).openStream(); URL url = new URL(URL);
try { URLConnection yc = url.openConnection();
Scanner scanner = new Scanner(inputStream); 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");
try { String date = obj.getString("published_at");
if (scanner.hasNext()) { SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
consumer.accept(scanner.next()); 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");
} }
} catch (Throwable var8) {
try { if (!prerelease) {
scanner.close(); downloadURL = "https://www.spigotmc.org/resources/" + spigotID;
} catch (Throwable var7) { updateurl = "https://www.spigotmc.org/resources/" + spigotID;
var8.addSuppressed(var7);
} }
throw var8;
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;
} }
scanner.close(); T2CupdateObject update = new T2CupdateObject(
} 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(), plugin.getDescription().getName(),
pluginVersion, pluginVersion,
"§4No public version found!" null,
load,
false
); );
bungeePluginVersins.put(plugin.getDescription().getName(), update); bungeePluginVersins.put(plugin.getDescription().getName(), update);
this.plugin.getLogger().severe(Prefix + "§4 Cannot look for updates: " + var10.getMessage()); this.plugin.getLogger().severe("§4 Cannot look for updates: " + var10.getMessage());
} }
}); });
} }

View File

@ -1,12 +0,0 @@
package net.t2code.t2codelib.BUNGEE.api.update;
public class T2CBupdateObject {
public String pluginName;
public String pluginVersion;
public String publicVersion;
public T2CBupdateObject(String pluginName, String pluginVersion, String publicVersion){
this.pluginName = pluginName;
this.pluginVersion = pluginVersion;
this.publicVersion = publicVersion;
}
}

View File

@ -13,7 +13,7 @@ import net.t2code.t2codelib.BUNGEE.system.pluginMessaging.opSecurity.T2CapiOpSec
import java.io.IOException; import java.io.IOException;
public class T2CBload { public class T2CBload {
public static void onLoad(Plugin plugin, String prefix, String autor, String version, String spigot, String discord, Integer spigotID, Integer bstatsID) { public static void onLoad(Plugin plugin, String prefix, String autor, String version, String spigot, String discord, Integer spigotID, Integer bstatsID, String url) {
long long_ = System.currentTimeMillis(); long long_ = System.currentTimeMillis();
T2CBsend.console(prefix + "§4============================= " + prefix + " §4============================="); T2CBsend.console(prefix + "§4============================= " + prefix + " §4=============================");
T2CBsend.console(prefix + " §2Autor: §6" + String.valueOf(autor).replace("[", "").replace("]", "")); T2CBsend.console(prefix + " §2Autor: §6" + String.valueOf(autor).replace("[", "").replace("]", ""));
@ -33,7 +33,7 @@ public class T2CBload {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
T2CBupdateAPI.onUpdateCheckTimer(plugin, prefix, spigot, discord, spigotID); T2CBupdateAPI.onUpdateCheckTimer(plugin, prefix, spigot, discord, spigotID,url);
//API //API
plugin.getProxy().registerChannel("t2c:bcmd"); plugin.getProxy().registerChannel("t2c:bcmd");

View File

@ -23,7 +23,7 @@ public class T2CodeBMain extends Plugin {
plugin = this; plugin = this;
orgVersion = plugin.getDescription().getVersion(); orgVersion = plugin.getDescription().getVersion();
autor = plugin.getDescription().getAuthor(); autor = plugin.getDescription().getAuthor();
T2CBload.onLoad(plugin, Util.getPrefix(), autor, orgVersion, Util.getSpigot(), Util.getDiscord(), Util.getSpigotID(), Util.getBstatsID()); T2CBload.onLoad(plugin, Util.getPrefix(), autor, orgVersion, Util.getSpigot(), Util.getDiscord(), Util.getSpigotID(), Util.getBstatsID(),Util.getGit());
String[] fv = orgVersion.split("_"); String[] fv = orgVersion.split("_");
plugin.getDescription().setVersion(fv[0]); plugin.getDescription().setVersion(fv[0]);
version = plugin.getDescription().getVersion(); version = plugin.getDescription().getVersion();

View File

@ -4,6 +4,8 @@ import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend;
import net.t2code.t2codelib.SPIGOT.system.T2CodeLibMain; import net.t2code.t2codelib.SPIGOT.system.T2CodeLibMain;
import net.t2code.t2codelib.SPIGOT.system.UpdateType; import net.t2code.t2codelib.SPIGOT.system.UpdateType;
import net.t2code.t2codelib.SPIGOT.system.config.config.SelectLibConfig; import net.t2code.t2codelib.SPIGOT.system.config.config.SelectLibConfig;
import net.t2code.t2codelib.T2CupdateObject;
import net.t2code.t2codelib.T2CupdateWebData;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;

View File

@ -1,5 +1,6 @@
package net.t2code.t2codelib.SPIGOT.api.update; package net.t2code.t2codelib.SPIGOT.api.update;
import net.t2code.t2codelib.T2CupdateObject;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;

View File

@ -1,5 +1,7 @@
package net.t2code.t2codelib.SPIGOT.api.update; package net.t2code.t2codelib.SPIGOT.api.update;
import net.t2code.t2codelib.T2CupdateObject;
import net.t2code.t2codelib.T2CupdateWebData;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray; import org.json.JSONArray;

View File

@ -2,7 +2,7 @@ package net.t2code.t2codelib.SPIGOT.system.cmd;
import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend; import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend;
import net.t2code.t2codelib.SPIGOT.api.update.T2CupdateAPI; import net.t2code.t2codelib.SPIGOT.api.update.T2CupdateAPI;
import net.t2code.t2codelib.SPIGOT.api.update.T2CupdateObject; import net.t2code.t2codelib.T2CupdateObject;
import net.t2code.t2codelib.SPIGOT.system.config.config.SelectLibConfig; import net.t2code.t2codelib.SPIGOT.system.config.config.SelectLibConfig;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;

View File

@ -3,10 +3,7 @@ package net.t2code.t2codelib.SPIGOT.system.cmd;
import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend; import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend;
import net.t2code.t2codelib.SPIGOT.api.messages.T2Ctemplate; import net.t2code.t2codelib.SPIGOT.api.messages.T2Ctemplate;
import net.t2code.t2codelib.SPIGOT.api.update.T2CupdateAPI; import net.t2code.t2codelib.SPIGOT.api.update.T2CupdateAPI;
import net.t2code.t2codelib.SPIGOT.api.update.T2CupdateObject;
import net.t2code.t2codelib.SPIGOT.api.update.T2CupdateWebData;
import net.t2code.t2codelib.SPIGOT.system.T2CodeLibMain; import net.t2code.t2codelib.SPIGOT.system.T2CodeLibMain;
import net.t2code.t2codelib.SPIGOT.system.UpdateType;
import net.t2code.t2codelib.Util; import net.t2code.t2codelib.Util;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;

View File

@ -1,4 +1,4 @@
package net.t2code.t2codelib.SPIGOT.api.update; package net.t2code.t2codelib;
public class T2CupdateObject { public class T2CupdateObject {

View File

@ -1,4 +1,4 @@
package net.t2code.t2codelib.SPIGOT.api.update; package net.t2code.t2codelib;
import lombok.Getter; import lombok.Getter;