199 lines
9.3 KiB
Java
199 lines
9.3 KiB
Java
package net.t2code.lib.Spigot.Lib.update;
|
|
|
|
|
|
import net.md_5.bungee.api.chat.ClickEvent;
|
|
import net.md_5.bungee.api.chat.TextComponent;
|
|
|
|
import net.t2code.lib.Spigot.Lib.messages.TextBuilder;
|
|
import net.t2code.lib.Spigot.Lib.messages.send;
|
|
import net.t2code.lib.Spigot.system.Main;
|
|
import net.t2code.lib.Spigot.system.config.SelectLibConfig;
|
|
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.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.net.URL;
|
|
import java.util.HashMap;
|
|
import java.util.Scanner;
|
|
import java.util.function.Consumer;
|
|
|
|
public class UpdateAPI {
|
|
public static HashMap<String, UpdateObject> PluginVersionen = new HashMap<>();
|
|
|
|
public static void join(Plugin plugin, String prefix, String perm, Player player, String spigot, String discord) {
|
|
String pluginVersion = plugin.getDescription().getVersion();
|
|
if (!player.hasPermission(perm) || !player.isOp()) {
|
|
return;
|
|
}
|
|
if (UpdateAPI.PluginVersionen.get(plugin.getName()) == null) {
|
|
new BukkitRunnable() {
|
|
@Override
|
|
public void run() {
|
|
join(plugin, prefix, perm, player, spigot, discord);
|
|
}
|
|
}.runTaskLater(plugin, 20L);
|
|
} else use(plugin, prefix, player, pluginVersion, UpdateAPI.PluginVersionen.get(plugin.getName()).publicVersion, spigot, discord);
|
|
}
|
|
|
|
private static void use(Plugin plugin, String prefix, Player player, String pluginVersion, String publicVersion, String spigot, String discord) {
|
|
new BukkitRunnable() {
|
|
@Override
|
|
public void run() {
|
|
if (!pluginVersion.equals(publicVersion)) {
|
|
if (SelectLibConfig.UpdateCheckOnJoin) {
|
|
UpdateAPI.sendUpdateMsg(prefix, spigot, discord, pluginVersion, publicVersion, player);
|
|
}
|
|
}
|
|
}
|
|
}.runTaskLater(plugin, 200L);
|
|
}
|
|
|
|
public static void sendUpdateMsg(String Prefix, String Spigot, String Discord, String pluginVersion, String publicVersion) {
|
|
send.console("§4=========== " + Prefix + " §4===========");
|
|
if (publicVersion.toLowerCase().contains("dev") || publicVersion.toLowerCase().contains("beta") || publicVersion.toLowerCase().contains("snapshot")){
|
|
if (publicVersion.toLowerCase().contains("dev")){
|
|
send.console("§6A new §4DEV§6 version was found!");
|
|
}
|
|
if (publicVersion.toLowerCase().contains("beta")){
|
|
send.console("§6A new §2BETA§6 version was found!");
|
|
}
|
|
if (publicVersion.toLowerCase().contains("snapshot")){
|
|
send.console("§6A new §eSNAPSHOT§6 version was found!");
|
|
}
|
|
} else {
|
|
send.console("§6A new version was found!");
|
|
}
|
|
send.console("§6Your version: §c" + pluginVersion + " §7- §6Current version: §a" + publicVersion);
|
|
send.console("§6You can download it here: §e" + Spigot);
|
|
send.console("§6You can find more information on Discord: §e" + Discord);
|
|
send.console("§4=========== " + Prefix + " §4===========");
|
|
}
|
|
|
|
public static void sendUpdateMsg(String Prefix, String Spigot, String Discord, String pluginVersion, String publicVersion, Player player) {
|
|
if (publicVersion.equals("§4No public version found!")) {
|
|
return;
|
|
}
|
|
send.player(player, Prefix);
|
|
if (publicVersion.toLowerCase().contains("dev") || publicVersion.toLowerCase().contains("beta") || publicVersion.toLowerCase().contains("snapshot")){
|
|
if (publicVersion.toLowerCase().contains("dev")){
|
|
TextComponent comp = new TextBuilder(Prefix + " §6A new §4DEV§6 version was found!")
|
|
.addHover("§6You can download it here: §e" + Spigot).addClickEvent(ClickEvent.Action.OPEN_URL, Spigot).build();
|
|
player.spigot().sendMessage(comp);
|
|
}
|
|
if (publicVersion.toLowerCase().contains("beta")){
|
|
TextComponent comp = new TextBuilder(Prefix + " §6A new §2BETA§6 version was found!")
|
|
.addHover("§6You can download it here: §e" + Spigot).addClickEvent(ClickEvent.Action.OPEN_URL, Spigot).build();
|
|
player.spigot().sendMessage(comp);
|
|
}
|
|
if (publicVersion.toLowerCase().contains("snapshot")){
|
|
TextComponent comp = new TextBuilder(Prefix + " §6A new §eSNAPSHOT§6 version was found!")
|
|
.addHover("§6You can download it here: §e" + Spigot).addClickEvent(ClickEvent.Action.OPEN_URL, Spigot).build();
|
|
player.spigot().sendMessage(comp);
|
|
}
|
|
} else {
|
|
TextComponent comp = new TextBuilder(Prefix + " §6A new version was found!")
|
|
.addHover("§6You can download it here: §e" + Spigot).addClickEvent(ClickEvent.Action.OPEN_URL, Spigot).build();
|
|
player.spigot().sendMessage(comp);
|
|
}
|
|
TextComponent comp1 = new TextBuilder(Prefix + " §c" + pluginVersion + " §7-> §a" + publicVersion)
|
|
.addHover("§6You can download it here: §e" + Spigot).addClickEvent(ClickEvent.Action.OPEN_URL, Spigot).build();
|
|
player.spigot().sendMessage(comp1);
|
|
TextComponent comp2 = new TextBuilder(Prefix + " §6You can find more information on Discord.")
|
|
.addHover("§e" + Discord).addClickEvent(ClickEvent.Action.OPEN_URL, Discord).build();
|
|
player.spigot().sendMessage(comp2);
|
|
send.player(player, Prefix);
|
|
}
|
|
|
|
private static Boolean noUpdate = true;
|
|
|
|
public static void onUpdateCheck(Plugin plugin, String Prefix, String Spigot, int SpigotID, String Discord) {
|
|
onUpdateCheck(plugin, Prefix, Spigot, SpigotID, Discord, 60);
|
|
}
|
|
|
|
public static void onUpdateCheck(Plugin plugin, String Prefix, String Spigot, int SpigotID, String Discord, Integer timeInMin) {
|
|
int taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
|
|
public void run() {
|
|
(new UpdateAPI((JavaPlugin) plugin, SpigotID)).getVersion((update_version) -> {
|
|
UpdateObject update = new UpdateObject(
|
|
plugin.getName(),
|
|
plugin.getDescription().getVersion(),
|
|
update_version
|
|
);
|
|
UpdateAPI.PluginVersionen.put(plugin.getName(), update);
|
|
if (!plugin.getDescription().getVersion().equalsIgnoreCase(update_version)) {
|
|
noUpdate = true;
|
|
new BukkitRunnable() {
|
|
@Override
|
|
public void run() {
|
|
sendUpdateMsg(Prefix, Spigot, Discord, plugin.getDescription().getVersion(), update_version);
|
|
}
|
|
}.runTaskLater(plugin, 600L);
|
|
} else {
|
|
if (noUpdate) {
|
|
send.console(Prefix + " §2No update found.");
|
|
noUpdate = false;
|
|
}
|
|
}
|
|
}, Prefix, plugin.getDescription().getVersion());
|
|
}
|
|
}, 0L, timeInMin * 60 * 20L);
|
|
}
|
|
|
|
private JavaPlugin plugin;
|
|
private int resourceId;
|
|
|
|
public UpdateAPI(JavaPlugin plugin, int resourceId) {
|
|
this.plugin = plugin;
|
|
this.resourceId = resourceId;
|
|
}
|
|
|
|
public void getVersion(Consumer<String> consumer, String Prefix, String pluginVersion) {
|
|
Bukkit.getScheduler().runTaskAsynchronously(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.getName(),
|
|
pluginVersion,
|
|
"§4No public version found!"
|
|
);
|
|
UpdateAPI.PluginVersionen.put(plugin.getName(), update);
|
|
this.plugin.getLogger().severe(Prefix + "§4 Cannot look for updates: " + var10.getMessage());
|
|
}
|
|
});
|
|
}
|
|
}
|