-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add automatic update checker (for stable GH releases)
- Loading branch information
Showing
8 changed files
with
310 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/java/io/github/jacobmarshall/meloooncensor/listener/PlayerJoinEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,30 @@ | ||
package io.github.jacobmarshall.meloooncensor.listener; | ||
|
||
import io.github.jacobmarshall.meloooncensor.config.Configuration; | ||
import io.github.jacobmarshall.meloooncensor.updater.CheckForUpdatesTask; | ||
import io.github.jacobmarshall.meloooncensor.updater.Release; | ||
import org.bukkit.ChatColor; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.player.PlayerJoinEvent; | ||
|
||
public class PlayerJoinEventListener implements Listener { | ||
|
||
CheckForUpdatesTask updater; | ||
|
||
public PlayerJoinEventListener (CheckForUpdatesTask updater) { | ||
this.updater = updater; | ||
} | ||
|
||
@EventHandler | ||
public void onPlayerJoin (PlayerJoinEvent event) { | ||
Player player = event.getPlayer(); | ||
|
||
if (player.isOp() && updater.isUpdateAvailable()) { | ||
Release latestRelease = updater.getLatestRelease(); | ||
player.sendMessage(ChatColor.AQUA + "A new version of MelooonCensor is available, please visit " + latestRelease.getReleaseUrl() + "."); | ||
} | ||
} | ||
|
||
} |
120 changes: 120 additions & 0 deletions
120
src/main/java/io/github/jacobmarshall/meloooncensor/updater/CheckForUpdatesTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package io.github.jacobmarshall.meloooncensor.updater; | ||
|
||
import com.bugsnag.Client; | ||
import io.github.jacobmarshall.meloooncensor.MelooonCensor; | ||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.util.Iterator; | ||
|
||
public class CheckForUpdatesTask implements Runnable { | ||
|
||
public static final String API_URL = "https://api.github.com"; | ||
public static final String API_ACCEPT = "application/vnd.github.v3+json"; | ||
public static final String REPO = "jacobmarshall/meloooncensor"; | ||
|
||
private MelooonCensor plugin; | ||
private Version version; | ||
private Client bugsnag; | ||
|
||
private boolean isOutdated; | ||
private Release latestRelease; | ||
|
||
public CheckForUpdatesTask (MelooonCensor plugin, Client bugsnag) { | ||
this.plugin = plugin; | ||
this.bugsnag = bugsnag; | ||
this.version = new Version(plugin.getDescription().getVersion()); | ||
} | ||
|
||
@Override | ||
public void run () { | ||
boolean isOutdated = false; | ||
Release latestRelease = null; | ||
|
||
try { | ||
String releasesText = sendRequest("/repos/" + REPO + "/releases"); | ||
if (releasesText != null) { | ||
JSONArray releases = new JSONArray(releasesText); | ||
|
||
if (releases.length() > 0) { | ||
for (int index = 0; index < releases.length(); index++) { | ||
Release release = Release.from(releases.getJSONObject(index)); | ||
|
||
if ( ! release.isPreRelease()) { | ||
Version releaseVersion = new Version(release.getVersion()); | ||
|
||
if (releaseVersion.compareTo(version) > 0) { | ||
isOutdated = true; | ||
latestRelease = release; | ||
} | ||
|
||
break; | ||
} | ||
} | ||
} | ||
} | ||
} catch (Exception err) { | ||
bugsnag.notify(err); | ||
} finally { | ||
this.isOutdated = isOutdated; | ||
this.latestRelease = latestRelease; | ||
} | ||
} | ||
|
||
private String sendRequest (String api) { | ||
HttpURLConnection connection = null; | ||
|
||
try { | ||
URL url = new URL(API_URL + "/" + api); | ||
connection = (HttpURLConnection) url.openConnection(); | ||
connection.setRequestMethod("GET"); | ||
connection.setRequestProperty("Accept", API_ACCEPT); | ||
|
||
int statusCode = connection.getResponseCode(); | ||
|
||
if (statusCode == HttpURLConnection.HTTP_OK) { | ||
BufferedReader reader = null; | ||
|
||
try { | ||
reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); | ||
|
||
String line; | ||
StringBuilder response = new StringBuilder(); | ||
|
||
while ((line = reader.readLine()) != null) { | ||
response.append(line); | ||
} | ||
|
||
return response.toString(); | ||
} catch (IOException err) { | ||
bugsnag.notify(err); | ||
} finally { | ||
if (reader != null) { | ||
reader.close(); | ||
} | ||
} | ||
} | ||
} catch (Exception err) { | ||
bugsnag.notify(err); | ||
} finally { | ||
if (connection != null) { | ||
connection.disconnect(); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public boolean isUpdateAvailable () { | ||
return isOutdated; | ||
} | ||
|
||
public Release getLatestRelease () { | ||
return latestRelease; | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/io/github/jacobmarshall/meloooncensor/updater/Release.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.github.jacobmarshall.meloooncensor.updater; | ||
|
||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
|
||
public class Release { | ||
|
||
String version; | ||
String description; | ||
String downloadUrl; | ||
String releaseUrl; | ||
boolean isPreRelease; | ||
|
||
private Release () {} | ||
|
||
public String getVersion () { | ||
return version; | ||
} | ||
|
||
public String getDescription () { | ||
return description; | ||
} | ||
|
||
public String getDownloadUrl () { | ||
return downloadUrl; | ||
} | ||
|
||
public String getReleaseUrl () { | ||
return releaseUrl; | ||
} | ||
|
||
public boolean isPreRelease () { | ||
return isPreRelease; | ||
} | ||
|
||
public static Release from (JSONObject json) { | ||
Release release = new Release(); | ||
release.version = json.getString("tag_name"); | ||
release.description = json.getString("body"); | ||
{ | ||
JSONArray assets = json.getJSONArray("assets"); | ||
JSONObject jar = assets.getJSONObject(0); | ||
release.downloadUrl = jar.getString("browser_download_url"); | ||
} | ||
release.releaseUrl = json.getString("html_url"); | ||
release.isPreRelease = json.getBoolean("prerelease"); | ||
return release; | ||
} | ||
|
||
} |
Oops, something went wrong.