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.URL;
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) new URL(
063                        "https://api.spigotmc.org/simple/0.1/index.php?action=getResource&id=77506")
064                        .openConnection();
065                connection.setRequestMethod("GET");
066                JsonObject result = new JsonParser()
067                        .parse(new JsonReader(new InputStreamReader(connection.getInputStream())))
068                        .getAsJsonObject();
069                spigotVersion = result.get("current_version").getAsString();
070            } catch (IOException e) {
071                LOGGER.error("Unable to check for updates. Error: {}", e.getMessage());
072                return;
073            }
074
075            if (internalVersion.isLaterVersion(spigotVersion)) {
076                LOGGER.info("There appears to be a PlotSquared update available!");
077                LOGGER.info("You are running version {}, the latest version is {}",
078                        internalVersion.versionString(), spigotVersion
079                );
080                LOGGER.info("https://www.spigotmc.org/resources/77506/updates");
081                hasUpdate = true;
082                if (Settings.UpdateChecker.NOTIFY_ONCE) {
083                    cancelTask();
084                }
085            } else if (notify) {
086                notify = false;
087                LOGGER.info("Congratulations! You are running the latest PlotSquared version");
088            }
089        }, 0L, (long) Settings.UpdateChecker.POLL_RATE * 60 * 20);
090    }
091
092    private void cancelTask() {
093        Bukkit.getScheduler().runTaskLater(javaPlugin, () -> task.cancel(), 20L);
094    }
095
096}