11package me .nik .combatplus .handlers ;
22
33import me .nik .combatplus .CombatPlus ;
4- import me .nik .combatplus .listeners .UpdateReminder ;
54import me .nik .combatplus .managers .MsgType ;
5+ import org .bukkit .event .EventHandler ;
6+ import org .bukkit .event .Listener ;
7+ import org .bukkit .event .player .PlayerJoinEvent ;
68import org .bukkit .scheduler .BukkitRunnable ;
79
8- import javax .net .ssl .HttpsURLConnection ;
910import java .io .BufferedReader ;
1011import java .io .IOException ;
1112import java .io .InputStreamReader ;
1213import java .net .URL ;
14+ import java .net .URLConnection ;
1315
14- public class UpdateChecker extends BukkitRunnable {
16+ public class UpdateChecker extends BukkitRunnable implements Listener {
1517
1618 private final CombatPlus plugin ;
1719
@@ -24,18 +26,32 @@ public UpdateChecker(CombatPlus plugin) {
2426 @ Override
2527 public void run () {
2628 try {
27- HttpsURLConnection connection = (HttpsURLConnection ) new URL ("https://api.spigotmc.org/legacy/update.php?resource=76788" ).openConnection ();
28- String version = new BufferedReader (new InputStreamReader (connection .getInputStream ())).readLine ();
29-
30- if (!plugin .getDescription ().getVersion ().equalsIgnoreCase (version )) {
31- newVersion = version ;
32- plugin .consoleMessage (MsgType .UPDATE_REMINDER .getMessage ().replaceAll ("%current%" , plugin .getDescription ().getVersion ()).replaceAll ("%new%" , newVersion ));
33- plugin .registerEvent (new UpdateReminder (plugin , this ));
34- } else {
35- plugin .consoleMessage (MsgType .CONSOLE_UPDATE_NOT_FOUND .getMessage ());
36- }
37- } catch (IOException ignored ) {
29+ newVersion = readLines ();
30+ } catch (IOException e ) {
31+ plugin .getLogger ().warning ("Couldn't check for updates, Is the server connected to the internet?" );
3832 }
33+
34+ if (!plugin .getDescription ().getVersion ().equals (newVersion )) {
35+ plugin .consoleMessage (MsgType .UPDATE_REMINDER .getMessage ().replaceAll ("%current%" , plugin .getDescription ().getVersion ()).replaceAll ("%new%" , newVersion ));
36+ plugin .getServer ().getPluginManager ().registerEvents (this , plugin );
37+ } else {
38+ plugin .consoleMessage (MsgType .CONSOLE_UPDATE_NOT_FOUND .getMessage ());
39+ }
40+ }
41+
42+ private String readLines () throws IOException {
43+ URLConnection connection = new URL ("https://raw.githubusercontent.com/NikV2/CombatPlus/master/version.txt" ).openConnection ();
44+ connection .addRequestProperty ("User-Agent" , "Mozilla/4.0" );
45+
46+ BufferedReader reader = new BufferedReader (new InputStreamReader (connection .getInputStream ()));
47+
48+ return reader .readLine ();
49+ }
50+
51+ @ EventHandler
52+ public void onJoin (PlayerJoinEvent e ) {
53+ if (!e .getPlayer ().hasPermission ("cp.admin" )) return ;
54+ e .getPlayer ().sendMessage (MsgType .UPDATE_REMINDER .getMessage ().replaceAll ("%current%" , plugin .getDescription ().getVersion ()).replaceAll ("%new%" , newVersion ));
3955 }
4056
4157 public String getNewVersion () {
0 commit comments