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

193 lines
11 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.config.config.SelectLibConfig;
import net.t2code.t2codelib.UpdateType;
import net.t2code.t2codelib.T2CupdateObject;
import net.t2code.t2codelib.T2CupdateWebData;
import net.t2code.t2codelib.Util;
import org.bukkit.Bukkit;
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 (pluginVersions.get(plugin.getName()) == null) {
Bukkit.getScheduler().runTaskLaterAsynchronously(T2CodeLibMain.getPlugin(), new Runnable() {
@Override
public void run() {
join(plugin, prefix, perm, player, spigotID, discord);
}
}, 2 * 20L);
return;
}
if (!pluginVersions.get(plugin.getName()).updateCheckOnJoin) {
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;
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:'<color:#6e90ff>You can download it here: <yellow>[link]</yellow></color>'>[prefix] <color:#6e90ff>A new</color> [value] <color:#6e90ff>version was" +
" found!</color></hover></click><br>" +
"<click:open_url:'[link]'><hover:show_text:'<color:#6e90ff>You can download it here: <yellow>[link]</yellow></color>'>[prefix] <red>[plv]</red> <gray>-></gray> " +
"<green>[puv]</green></hover></click><br>" +
"<click:open_url:'[dc]'><hover:show_text:'<yellow>[dc]</yellow>'>[prefix] <color:#6e90ff>You can find more information on Discord.</color></hover></click><br>" +
"[prefix] <color:#ff9499><hover:show_text:'<red>Click for more information</red>'><click:run_command:'/t2c updateinfo " + plugin.getName() + "'>Update information</click></hover></color><br>" +
"[prefix]";
String updateStatusVersion;
if (webData.isPreRelease()) {
//todo if (!SelectLibConfig.getSeePreReleaseUpdates()) return;
updateStatusVersion = UpdateType.PRERELEASE.text;
if (publicVersion.toLowerCase().contains("dev")) {
updateStatusVersion = UpdateType.DEVELOPMENT.text;
}
if (publicVersion.toLowerCase().contains("beta")) {
updateStatusVersion = UpdateType.BETA.text;
}
if (publicVersion.toLowerCase().contains("snapshot")) {
updateStatusVersion = UpdateType.SNAPSHOT.text;
}
} else updateStatusVersion = UpdateType.STABLE.text;
T2Csend.player(player, st.replace("[prefix]", prefix).replace("[value]", updateStatusVersion).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 updateStatusVersion;
if (webData.isPreRelease()) {
updateStatusVersion = UpdateType.PRERELEASE.text;
if (publicVersion.toLowerCase().contains("dev")) {
updateStatusVersion = UpdateType.DEVELOPMENT.text;
}
if (publicVersion.toLowerCase().contains("beta")) {
updateStatusVersion = UpdateType.BETA.text;
}
if (publicVersion.toLowerCase().contains("snapshot")) {
updateStatusVersion = UpdateType.SNAPSHOT.text;
}
} else updateStatusVersion = UpdateType.STABLE.text;
String h = "<br><dark_red>╔══════════════</dark_red>" + prefix + "<dark_red>══════════════</dark_red>";
String s1 = "<br><dark_red>║</dark_red> <color:#6e90ff>A new [value] version was found!</color>".replace("[value]", updateStatusVersion);
String s2 = "<br><dark_red>║</dark_red> <color:#6e90ff>Your version: <red>" + pluginVersion + "</red> <gray>-</gray> Current version:</color> <green>" + webData.getVersion() + "</green>";
String s3 = "<br><dark_red>║</dark_red> <color:#6e90ff>You can download it here:</color> <yellow>" + webData.getUpdateUrl() + "</yellow>";
String s4 = "<br><dark_red>║</dark_red> <color:#6e90ff>You can find more information on Discord:</color> <yellow>" + discord + "</yellow>";
String f = "<br><dark_red>╚══════════════</dark_red>" + prefix + "<dark_red>══════════════</dark_red>";
String text = h + s1 + s2 + s3 + s4 + f;
T2Csend.console(text);
}
public static String updateInfo(String[] args, Boolean player) {
T2CupdateObject object;
try {
object = T2CupdateAPI.pluginVersions.get(args[1]);
} catch (Exception e) {
return Util.getPrefix() + " <red>The plugin <yellow>" + args[1] + "</yellow> is not registered in the update checker of T2Code!</red>";
}
if (object == null) return Util.getPrefix() + " <red>The plugin <yellow>" + args[1] + "</yellow> is not registered in the update checker of T2Code!</red>";
T2CupdateWebData webData = object.webData;
if (webData == null) {
return Util.getPrefix() + " <b><dark_red>It could not be checked for updates with the plugin <yellow>" + args[1] + "</yellow>!</dark_red></b>";
}
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> <color:#6e90ff>Plugin:</color> <color:#5eff89>" + pluginName + "</color>";
String pluginVersionString = "<br><dark_red>║</dark_red> <color:#6e90ff>Your version:</color> <color:#5eff89>" + pluginVersion + "</color>";
String updateInfoString = "<br><dark_red>║</dark_red> <color:#09ff00>[value]:</color>";
String updateTitleString = "<br><dark_red>║</dark_red> <color:#6e90ff>Title:</color> <color:#5eff89>" + updateTitle + "</color>";
String updateVersionString = "<br><dark_red>║</dark_red> <color:#6e90ff>Version:</color> <color:#5eff89>" + updateVersion + "</color>";
String updateAtString = "<br><dark_red>║</dark_red> <color:#6e90ff>Published on:</color> <color:#5eff89>" + updateAt + "</color>";
String updateTypeString = "<br><dark_red>║</dark_red> <color:#6e90ff>Version type:</color> <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> <dark_gray>|</dark_gray> <color:#ff9499><hover:show_text:'" + updateDescription + "'><click:open_url:'" + webData.getGitURL() + "'>Update Description</click></hover></color>";
} else updateButton = "<br><dark_red>║</dark_red> <color:#5eff89>Download:</color> <gold>" + updateUpdate + "</gold>";
String text;
text = "<br><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;
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, String gitKey, Integer spigotID, String discord, Boolean updateCheckOnJoin, Boolean seePreReleaseUpdates, Integer timeInterval) {
if (SelectLibConfig.getUpdateCheckFullDisable()) return;
new T2CupdateCheckerGit((JavaPlugin) plugin, prefix, gitKey, spigotID, discord, updateCheckOnJoin, seePreReleaseUpdates, timeInterval);
}
}