001/*
002 * PlotSquared, a land and world management plugin for Minecraft.
003 * Copyright (C) IntellectualSites <https://intellectualsites.com>
004 * Copyright (C) IntellectualSites team and contributors
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
018 */
019package com.plotsquared.bukkit.util;
020
021import com.google.gson.JsonObject;
022import com.google.gson.JsonParser;
023import com.google.gson.stream.JsonReader;
024import com.google.inject.Inject;
025import com.plotsquared.core.PlotSquared;
026import com.plotsquared.core.PlotVersion;
027import com.plotsquared.core.configuration.Settings;
028import org.apache.logging.log4j.LogManager;
029import org.apache.logging.log4j.Logger;
030import org.bukkit.Bukkit;
031import org.bukkit.event.Listener;
032import org.bukkit.plugin.java.JavaPlugin;
033import org.bukkit.scheduler.BukkitTask;
034
035import javax.net.ssl.HttpsURLConnection;
036import java.io.IOException;
037import java.io.InputStreamReader;
038import java.net.URI;
039
040public class UpdateUtility implements Listener {
041
042    private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + UpdateUtility.class.getSimpleName());
043
044    public static PlotVersion internalVersion;
045    public static String spigotVersion;
046    public static boolean hasUpdate;
047    private static BukkitTask task;
048    public final JavaPlugin javaPlugin;
049    private boolean notify = true;
050
051    @Inject
052    public UpdateUtility(final JavaPlugin javaPlugin) {
053        this.javaPlugin = javaPlugin;
054        internalVersion = PlotSquared.get().getVersion();
055    }
056
057    @SuppressWarnings({"deprecation", "DefaultCharset"})
058    // Suppress Json deprecation, we can't use features from gson 2.8.1 and newer yet
059    public void updateChecker() {
060        task = Bukkit.getScheduler().runTaskTimerAsynchronously(this.javaPlugin, () -> {
061            try {
062                HttpsURLConnection connection = (HttpsURLConnection) URI.create(
063                        "https://api.spigotmc.org/simple/0.2/index.php?action=getResource&id=77506")
064                        .toURL()
065                        .openConnection();
066                connection.setRequestMethod("GET");
067                JsonObject result = new JsonParser()
068                        .parse(new JsonReader(new InputStreamReader(connection.getInputStream())))
069                        .getAsJsonObject();
070                spigotVersion = result.get("current_version").getAsString();
071            } catch (IOException e) {
072                LOGGER.error("Unable to check for updates. Error: {}", e.getMessage());
073                return;
074            }
075
076            if (internalVersion.isLaterVersion(spigotVersion)) {
077                LOGGER.info("There appears to be a PlotSquared update available!");
078                LOGGER.info("You are running version {}, the latest version is {}",
079                        internalVersion.versionString(), spigotVersion
080                );
081                LOGGER.info("https://www.spigotmc.org/resources/77506/updates");
082                hasUpdate = true;
083                if (Settings.UpdateChecker.NOTIFY_ONCE) {
084                    cancelTask();
085                }
086            } else if (notify) {
087                notify = false;
088                LOGGER.info("Congratulations! You are running the latest PlotSquared version");
089            }
090        }, 0L, (long) Settings.UpdateChecker.POLL_RATE * 60 * 20);
091    }
092
093    private void cancelTask() {
094        Bukkit.getScheduler().runTaskLater(javaPlugin, () -> task.cancel(), 20L);
095    }
096
097}