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