2020-04-16 04:52:39 +02:00
|
|
|
/*
|
|
|
|
* _____ _ _ _____ _
|
|
|
|
* | __ \| | | | / ____| | |
|
|
|
|
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
|
|
|
|
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
|
|
|
|
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
|
|
|
|
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
|
|
|
|
* | |
|
|
|
|
* |_|
|
|
|
|
* PlotSquared plot management system for Minecraft
|
|
|
|
* Copyright (C) 2020 IntellectualSites
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-04-11 02:19:18 +02:00
|
|
|
package com.plotsquared.bukkit.util;
|
2020-02-16 01:33:43 +01:00
|
|
|
|
2020-04-21 09:56:23 +02:00
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import com.google.gson.JsonParser;
|
|
|
|
import com.google.gson.stream.JsonReader;
|
2020-07-10 22:12:37 +02:00
|
|
|
import com.google.inject.Inject;
|
2020-04-15 21:26:54 +02:00
|
|
|
import com.plotsquared.core.PlotSquared;
|
2020-04-30 11:17:19 +02:00
|
|
|
import com.plotsquared.core.PlotVersion;
|
|
|
|
import com.plotsquared.core.configuration.Settings;
|
2020-02-16 01:33:43 +01:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.event.Listener;
|
|
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
2020-04-30 11:17:19 +02:00
|
|
|
import org.bukkit.scheduler.BukkitTask;
|
2020-07-06 17:17:46 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2020-02-16 01:33:43 +01:00
|
|
|
|
|
|
|
import javax.net.ssl.HttpsURLConnection;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.net.URL;
|
|
|
|
|
|
|
|
public class UpdateUtility implements Listener {
|
|
|
|
|
2020-07-13 22:51:57 +02:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger("P2/" + UpdateUtility.class.getSimpleName());
|
2020-07-06 17:17:46 +02:00
|
|
|
|
2020-04-30 11:17:19 +02:00
|
|
|
public static PlotVersion internalVersion;
|
2020-02-16 02:34:51 +01:00
|
|
|
public static String spigotVersion;
|
2020-04-22 13:09:00 +02:00
|
|
|
public static boolean hasUpdate;
|
2020-04-30 11:17:19 +02:00
|
|
|
private static BukkitTask task;
|
2020-02-16 02:34:51 +01:00
|
|
|
public final JavaPlugin javaPlugin;
|
2020-04-22 13:09:00 +02:00
|
|
|
private boolean notify = true;
|
2020-02-16 01:33:43 +01:00
|
|
|
|
2020-07-10 22:12:37 +02:00
|
|
|
@Inject public UpdateUtility(final JavaPlugin javaPlugin) {
|
2020-02-16 01:33:43 +01:00
|
|
|
this.javaPlugin = javaPlugin;
|
2020-04-30 11:17:19 +02:00
|
|
|
internalVersion = PlotSquared.get().getVersion();
|
2020-02-16 01:33:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void updateChecker() {
|
2020-04-30 11:17:19 +02:00
|
|
|
task = Bukkit.getScheduler().runTaskTimerAsynchronously(this.javaPlugin, () -> {
|
2020-04-21 09:56:23 +02:00
|
|
|
try {
|
|
|
|
HttpsURLConnection connection = (HttpsURLConnection) new URL(
|
|
|
|
"https://api.spigotmc.org/simple/0.1/index.php?action=getResource&id=77506")
|
|
|
|
.openConnection();
|
|
|
|
connection.setRequestMethod("GET");
|
|
|
|
JsonObject result = (new JsonParser())
|
|
|
|
.parse(new JsonReader(new InputStreamReader(connection.getInputStream())))
|
|
|
|
.getAsJsonObject();
|
2020-04-22 10:57:46 +02:00
|
|
|
spigotVersion = result.get("current_version").getAsString();
|
2020-04-21 09:56:23 +02:00
|
|
|
} catch (IOException e) {
|
2020-07-13 23:04:27 +02:00
|
|
|
logger.error("[P2] Unable to check for updates. Error: {}", e.getMessage());
|
2020-04-21 09:56:23 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-02-16 01:33:43 +01:00
|
|
|
|
2020-04-30 11:17:19 +02:00
|
|
|
if (internalVersion.isLaterVersion(spigotVersion)) {
|
2020-07-13 23:04:27 +02:00
|
|
|
logger.info("[P2] There appears to be a PlotSquared update available!");
|
|
|
|
logger.info("[P2] You are running version {}, the latest version is {}",
|
2020-07-06 17:17:46 +02:00
|
|
|
internalVersion.versionString(), spigotVersion);
|
2020-07-13 23:04:27 +02:00
|
|
|
logger.info("[P2] https://www.spigotmc.org/resources/77506/updates");
|
2020-04-22 13:09:00 +02:00
|
|
|
hasUpdate = true;
|
2020-04-30 11:17:19 +02:00
|
|
|
if (Settings.UpdateChecker.NOTIFY_ONCE) {
|
|
|
|
cancelTask();
|
|
|
|
}
|
2020-04-22 13:09:00 +02:00
|
|
|
} else if (notify) {
|
|
|
|
notify = false;
|
2020-07-13 23:04:27 +02:00
|
|
|
logger.info("[P2] Congratulations! You are running the latest PlotSquared version");
|
2020-02-16 01:33:43 +01:00
|
|
|
}
|
2020-04-30 11:17:19 +02:00
|
|
|
}, 0L, Settings.UpdateChecker.POLL_RATE * 60 * 20);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void cancelTask() {
|
|
|
|
Bukkit.getScheduler().runTaskLater(javaPlugin, () -> task.cancel(), 20L);
|
2020-02-16 01:33:43 +01:00
|
|
|
}
|
2020-04-11 01:26:07 +02:00
|
|
|
}
|