T2CodeLib/src/main/java/net/t2code/t2codelib/SPIGOT/api/update/T2CupdateAPI.java

244 lines
13 KiB
Java

package net.t2code.t2codelib.SPIGOT.api.update;
import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend;
import net.t2code.t2codelib.SPIGOT.system.T2CodeLibMain;
import net.t2code.t2codelib.SPIGOT.system.UpdateType;
import net.t2code.t2codelib.SPIGOT.system.config.config.SelectLibConfig;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.HashMap;
public class T2CupdateAPI {
public static HashMap<String, T2CupdateObject> pluginVersions = new HashMap<>();
public static void join(Plugin plugin, String prefix, String perm, Player player, Integer spigotID, String discord) {
if (!SelectLibConfig.getUpdateCheckOnJoin()) {
return;
}
if (!player.hasPermission(perm) && !player.isOp()) {
return;
}
if (pluginVersions.get(plugin.getName()) == null) {
new BukkitRunnable() {
@Override
public void run() {
join(plugin, prefix, perm, player, spigotID, discord);
}
}.runTaskLaterAsynchronously(plugin, 20L);
return;
}
T2CupdateWebData webData = pluginVersions.get(plugin.getName()).webData;
String pluginVersion = plugin.getDescription().getVersion();
if (!pluginVersions.get(plugin.getName()).updateAvailable) return;
new BukkitRunnable() {
@Override
public void run() {
sendUpdateMsg(prefix, webData, discord, plugin, player);
}
}.runTaskLaterAsynchronously(T2CodeLibMain.getPlugin(), 200L);
}
public static void sendUpdateMsg(String prefix, T2CupdateWebData webData, String discord, Plugin plugin, Player player) {
String publicVersion = webData.getVersion();
String pluginVersion = plugin.getDescription().getVersion();
if (!pluginVersions.get(plugin.getName()).updateAvailable) {
return;
}
String st = "[prefix]<br>" +
"<click:open_url:'[link]'><hover:show_text:'<gold>You can download it here: <yellow>[link]</yellow></gold>'>[prefix] <gold>A new</gold> [value] <gold>version was" +
" found!</gold></hover></click><br>" +
"<click:open_url:'[link]'><hover:show_text:'<gold>You can download it here: <yellow>[link]</yellow></gold>'>[prefix] <red>[plv]</red> <gray>-></gray> " +
"<green>[puv]</green></hover></click><br>" +
"<click:open_url:'[dc]'><hover:show_text:'<yellow>[dc]</yellow>'>[prefix] <gold>You can find more information on Discord.</gold></hover></click><br>" +
"[prefix] <blue><hover:show_text:'<red>Click for more information</red>'><click:run_command:'/t2c updateinfo " + plugin.getName() + "'>Update information</click></hover></blue><br>" +
"[prefix]";
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;
T2Csend.player(player, st.replace("[prefix]", prefix).replace("[value]", value).replace("[link]", webData.getUpdateUrl())
.replace("[plv]", pluginVersion).replace("[puv]", publicVersion).replace("[dc]", discord));
}
public static void sendUpdateMsg(String prefix, String discord, T2CupdateWebData webData, Plugin plugin) {
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 = "§4=========== " + prefix + " §4===========";
String s1 = "§6A new [value] version was found!§r".replace("[value]", value);
String s2 = "§6Your version: §c" + pluginVersion + " §7- §6Current version: §a" + publicVersion;
String s3 = "§6You can download it here: §e" + webData.getUpdateUrl();
String s4 = "§6You can find more information on Discord: §e" + discord;
String text = "<br>" + h + "<br>" + s1 + "<br>" + s2 + "<br>" + s3 + "<br>" + s4 + "<br>" + h;
T2Csend.console(text);
}
public static String updateInfo(CommandSender sender, String[] args, Boolean player) {
T2CupdateObject object;
try {
object = T2CupdateAPI.pluginVersions.get(args[1]);
} catch (Exception e) {
return "Das Plugin " + args[1] + " gibts net"; // todo
}
T2CupdateWebData webData = object.webData;
String pluginName = T2CupdateAPI.pluginVersions.get(args[1]).pluginName;
String pluginVersion = T2CupdateAPI.pluginVersions.get(args[1]).pluginVersion;
String updateTitle = webData.getUpdateTitle();
String updateVersion = webData.getVersion();
String updateType;
if (webData.isPreRelease()) {
updateType = UpdateType.PRERELEASE.text;
if (updateVersion.toLowerCase().contains("dev")) {
updateType = UpdateType.DEVELOPMENT.text;
}
if (updateVersion.toLowerCase().contains("beta")) {
updateType = UpdateType.BETA.text;
}
if (updateVersion.toLowerCase().contains("snapshot")) {
updateType = UpdateType.SNAPSHOT.text;
}
} else updateType = UpdateType.STABLE.text;
String updateAt = webData.getPublishedAt();
String updateUpdate = webData.getUpdateUrl();
String updateDescription = webData.getUpdateDescription();
String pluginNameString = "<br><dark_red>║</dark_red> <yellow>Plugin:</yellow> <color:#5eff89>" + pluginName + "</color>";
String pluginVersionString = "<br><dark_red>║</dark_red> <yellow>Your version:</yellow> <color:#5eff89>" + pluginVersion + "</color>";
String updateInfoString = "<br><dark_red>║</dark_red> <color:#308aff>[value]:</color>";
String updateTitleString = "<br><dark_red>║</dark_red> <yellow>Title:</yellow> <color:#5eff89>" + updateTitle + "</color>";
String updateVersionString = "<br><dark_red>║</dark_red> <yellow>Version:</yellow> <color:#5eff89>" + updateVersion + "</color>";
String updateAtString = "<br><dark_red>║</dark_red> <yellow>Update at:</yellow> <color:#5eff89>" + updateAt + "</color>";
String updateTypeString = "<br><dark_red>║</dark_red> <yellow>Update type:</yellow> <color:#5eff89>" + updateType + "</color>";
String updateButton;
if (player) {
// updateButton = "<br><dark_red>║</dark_red> <color:#5eff89><hover:show_text:'<gold>" + updateUpdate + "</gold>'><click:open_url:'" + updateUpdate +"'>Download</click></hover></color>";
updateButton = "<br><dark_red>║</dark_red> <color:#5eff89><hover:show_text:'<gold>" + updateUpdate + "</gold>'><click:open_url:'" + updateUpdate + "'>Download</click></hover></color> <dark_gray>|</dark_gray> <blue><hover:show_text:'" + updateDescription + "'>Update Description</hover></blue>";
} else updateButton = "<br><dark_red>║</dark_red> <color:#5eff89>Download:</color> <gold>" + updateUpdate + "</gold>";
String text;
text = "<dark_red>╔══════════════════════</dark_red>";
text = text + pluginNameString;
text = text + pluginVersionString;
text = text + updateInfoString.replace("[value]", object.updateAvailable ? "Update available" : "Info about your version");
if (!updateTitle.equals("OLD")) text = text + updateTitleString;
if (object.updateAvailable) text = text + updateVersionString;
if (!updateTitle.equals("OLD")) text = text + updateAtString;
if (!updateTitle.equals("OLD")) text = text + updateTypeString;
if (object.updateAvailable)
text = text + updateButton;
if (updateTitle.equals("OLD")) text = text + "<br><dark_gray>This plugin does not have the new update checker yet, so there is no exact update / " +
"version information available!</dark_gray>";
text = text + "<br><dark_red>╚══════════════════════</dark_red>";
return text;
}
public static void onUpdateCheck(Plugin plugin, String prefix, int spigotID, String discord) {
new BukkitRunnable() {
@Override
public void run() {
(new T2CupdateChecker((JavaPlugin) plugin, spigotID)).getVersion((update_version) -> {
T2CupdateObject update = new T2CupdateObject(
plugin.getName(),
plugin.getDescription().getVersion(),
new T2CupdateWebData("OLD", update_version, "", "https://www.spigotmc.org/resources/" + spigotID, "",
"https://www.spigotmc.org/resources/" + spigotID, false),
false, !plugin.getDescription().getVersion().equals(update_version)
);
pluginVersions.put(plugin.getName(), update);
if (!pluginVersions.get(plugin.getName()).updateAvailable) {
if (!update.load) {
new BukkitRunnable() {
@Override
public void run() {
update.load = true;
sendUpdateMsg(prefix, discord, update.webData, plugin);
}
}.runTaskLaterAsynchronously(plugin, 600L);
} else sendUpdateMsg(prefix, discord, update.webData, plugin);
} else {
if (!update.load) {
T2Csend.console(prefix + " §2No update found.");
update.load = true;
}
}
}, prefix, plugin.getDescription().getVersion());
}
}.runTaskTimerAsynchronously(plugin, 0L, SelectLibConfig.getUpdateCheckTimeInterval() * 60 * 20L);
}
public static void onUpdateCheck(Plugin plugin, String prefix, String RepoURL, Integer spigotID, String discord) {
if (!RepoURL.contains("?limit=1")) {
RepoURL = RepoURL + "?limit=1";
}
String finalRepoURL = RepoURL;
new BukkitRunnable() {
@Override
public void run() {
(new T2CupdateCheckerGit((JavaPlugin) plugin)).getVersion((webData) -> {
T2CupdateObject update = new T2CupdateObject(
plugin.getName(),
plugin.getDescription().getVersion(),
webData,
false,
!plugin.getDescription().getVersion().equals(webData.getVersion())
);
pluginVersions.put(plugin.getName(), update);
if (!pluginVersions.get(plugin.getName()).updateAvailable) {
if (!update.load) {
new BukkitRunnable() {
@Override
public void run() {
update.load = true;
sendUpdateMsg(prefix, discord, webData, plugin);
}
}.runTaskLaterAsynchronously(plugin, 600L);
} else sendUpdateMsg(prefix, discord, webData, plugin);
} else {
if (!update.load) {
T2Csend.console(prefix + " §2No update found.");
update.load = true;
}
}
}, plugin.getDescription().getVersion(), spigotID, finalRepoURL);
}
}.runTaskTimerAsynchronously(plugin, 0L, SelectLibConfig.getUpdateCheckTimeInterval() * 60 * 20L);
}
}