Skip to content

Commit

Permalink
Add automatic update checker (for stable GH releases)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmshal committed Jan 31, 2016
1 parent 2477dbe commit 5f18ac0
Show file tree
Hide file tree
Showing 8 changed files with 310 additions and 4 deletions.
2 changes: 2 additions & 0 deletions MelooonCensor.iml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@
<orderEntry type="library" scope="PROVIDED" name="Maven: net.md-5:bungeecord-chat:1.8-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.bukkit:bukkit:1.8.8-R0.1-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: org.mcstats.bukkit:metrics:R8-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: com.bugsnag:bugsnag:1.3.0" level="project" />
<orderEntry type="library" name="Maven: org.json:json:20151123" level="project" />
</component>
</module>
14 changes: 12 additions & 2 deletions dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.jacobmarshall.meloooncensor</groupId>
<artifactId>MelooonCensor</artifactId>
<artifactId>meloooncensor</artifactId>
<version>3.0.0-SNAPSHOT</version>
<build>
<plugins>
Expand Down Expand Up @@ -34,12 +34,22 @@
<artifactSet>
<includes>
<include>org.mcstats.*:*</include>
<include>com.bugsnag:*</include>
<include>org.json:*</include>
</includes>
</artifactSet>
<relocations>
<relocation>
<pattern>org.mcstats</pattern>
<shadedPattern>io.github.jacobmarshall.meloooncensor.metrics</shadedPattern>
<shadedPattern>io.github.jacobmarshall.meloooncensor.lib.org.mcstats</shadedPattern>
</relocation>
<relocation>
<pattern>com.bugsnag</pattern>
<shadedPattern>io.github.jacobmarshall.meloooncensor.lib.com.bugsnag</shadedPattern>
</relocation>
<relocation>
<pattern>org.json</pattern>
<shadedPattern>io.github.jacobmarshall.meloooncensor.lib.org.json</shadedPattern>
</relocation>
</relocations>
</configuration>
Expand Down
24 changes: 22 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>

<groupId>io.github.jacobmarshall.meloooncensor</groupId>
<artifactId>MelooonCensor</artifactId>
<artifactId>meloooncensor</artifactId>
<version>3.0.0-SNAPSHOT</version>

<build>
Expand All @@ -32,12 +32,22 @@
<artifactSet>
<includes>
<include>org.mcstats.*:*</include>
<include>com.bugsnag:*</include>
<include>org.json:*</include>
</includes>
</artifactSet>
<relocations>
<relocation>
<pattern>org.mcstats</pattern>
<shadedPattern>io.github.jacobmarshall.meloooncensor.metrics</shadedPattern>
<shadedPattern>io.github.jacobmarshall.meloooncensor.lib.org.mcstats</shadedPattern>
</relocation>
<relocation>
<pattern>com.bugsnag</pattern>
<shadedPattern>io.github.jacobmarshall.meloooncensor.lib.com.bugsnag</shadedPattern>
</relocation>
<relocation>
<pattern>org.json</pattern>
<shadedPattern>io.github.jacobmarshall.meloooncensor.lib.org.json</shadedPattern>
</relocation>
</relocations>
</configuration>
Expand Down Expand Up @@ -91,6 +101,16 @@
<version>R8-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.bugsnag</groupId>
<artifactId>bugsnag</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20151123</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
package io.github.jacobmarshall.meloooncensor;

import com.bugsnag.Client;
import io.github.jacobmarshall.meloooncensor.config.Configuration;
import io.github.jacobmarshall.meloooncensor.listener.ChatEventListener;
import io.github.jacobmarshall.meloooncensor.command.CensorCommandExecutor;
import io.github.jacobmarshall.meloooncensor.listener.PlayerJoinEventListener;
import io.github.jacobmarshall.meloooncensor.updater.CheckForUpdatesTask;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitScheduler;
import org.mcstats.Metrics;

import java.io.IOException;

public class MelooonCensor extends JavaPlugin {

private Configuration config;
private CheckForUpdatesTask updater;
private Client bugsnag;

protected void startBugsnag () {
bugsnag = new Client("b5347687fe92ee7494d20cdf5a725fad");
bugsnag.setProjectPackages("io.github.jacobmarshall.meloooncensor");
}

protected void startMetrics () {
try {
Expand All @@ -22,10 +33,18 @@ protected void startMetrics () {
}

protected void registerEvents () {
getServer().getScheduler().runTaskTimerAsynchronously(
this, updater = new CheckForUpdatesTask(this, bugsnag), 0L, 36000L
);

getServer().getPluginManager().registerEvents(
new ChatEventListener(this.config), this
);

getServer().getPluginManager().registerEvents(
new PlayerJoinEventListener(this.updater), this
);

getCommand("censor").setExecutor(
new CensorCommandExecutor(this.config)
);
Expand All @@ -37,6 +56,7 @@ protected void setupConfig () {

@Override
public void onEnable () {
startBugsnag();
setupConfig();
startMetrics();
registerEvents();
Expand Down
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() + ".");
}
}

}
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;
}

}
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;
}

}
Loading

0 comments on commit 5f18ac0

Please sign in to comment.