Skip to content

Commit

Permalink
Finished updater
Browse files Browse the repository at this point in the history
  • Loading branch information
bramhaag committed Jan 11, 2017
1 parent 1047ae5 commit 21c27ee
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 70 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.bramhaag</groupId>
<artifactId>Guilds</artifactId>
<version>1.2.1-RELEASE</version>
<version>1.2.2-RELEASE</version>

<repositories>
<repository>
Expand Down
16 changes: 2 additions & 14 deletions src/main/java/me/bramhaag/guilds/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,8 @@ public void onEnable() {

if(getConfig().getBoolean("updater.check")) {
Updater.checkForUpdates((result, exception) -> {
if (result) {
getLogger().log(Level.INFO, "A new update for Guilds has been found!");

if(getConfig().getBoolean("updater.download")) {
getLogger().log(Level.INFO, "Downloading new update...");
Updater.downloadUpdate((downloadResult, exception1) -> {
if(downloadResult) {
getLogger().log(Level.INFO, "Downloaded new update!");
}
else {
getLogger().log(Level.SEVERE, "Something went wrong while downloading a new update!");
}
});
}
if (result != null) {
getLogger().log(Level.INFO, "A new update for Guilds has been found! Go to " + result + " to download it!");
}
else {
getLogger().log(Level.INFO, "No updates found!");
Expand Down
17 changes: 5 additions & 12 deletions src/main/java/me/bramhaag/guilds/commands/CommandUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,11 @@ public CommandUpdate() {

public void execute(CommandSender sender, String[] args) {
Updater.checkForUpdates((result, exception) -> {
if (result) {
Message.sendMessage(sender, Message.COMMAND_UPDATE_FOUND);

Updater.downloadUpdate((downloadResult, downloadException) -> {
if(downloadResult) {
Message.sendMessage(sender, Message.COMMAND_UPDATE_SUCCESSFUL);
return;
}
else {
Message.sendMessage(sender, Message.COMMAND_UPDATE_ERROR);
}
});
if (result != null) {
Message.sendMessage(sender, Message.COMMAND_UPDATE_FOUND.replace("{url}", result));
}
else {
Message.sendMessage(sender, Message.COMMAND_UPDATE_NOT_FOUND);
}
});
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/me/bramhaag/guilds/message/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ public enum Message {
COMMAND_ADMIN_DELETE_CANCELLED,

COMMAND_UPDATE_FOUND,
COMMAND_UPDATE_SUCCESSFUL,
COMMAND_UPDATE_ERROR,
COMMAND_UPDATE_NOT_FOUND,

EVENT_JOIN_PENDING_INVITES;

Expand Down
39 changes: 2 additions & 37 deletions src/main/java/me/bramhaag/guilds/updater/Updater.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
import com.google.gson.Gson;
import me.bramhaag.guilds.Main;
import me.bramhaag.guilds.database.Callback;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;

public class Updater {
Expand All @@ -21,59 +19,26 @@ public class Updater {
private static final String API_URL = "https://api.spiget.org/v2/resources";
private static final String SPIGOT_URL = "https://www.spigotmc.org/resources";

private static UpdateResponse response = null;

private static Gson gson = new Gson();

public static void checkForUpdates(Callback<Boolean, Exception> callback) {
public static void checkForUpdates(Callback<String, Exception> callback) {
Main.newChain()
.asyncFirst(Updater::getLatestVersion)
.syncLast((latestVersion) -> {
response = latestVersion;
long creationTime = Main.getCreationTime();
System.out.println("CREATED: " + creationTime);
System.out.println("Last version: " + latestVersion.getReleaseDate());
System.out.println("Last version > created: " + (latestVersion.getReleaseDate() > creationTime));
callback.call(creationTime != 0 && latestVersion.getReleaseDate() > creationTime, null);
callback.call((creationTime != 0 && latestVersion.getReleaseDate() > creationTime) ? String.format("%s/%s/download?version=%s", SPIGOT_URL, RESOURCE_ID, latestVersion.getId()) : null, null);
})
.execute();
}

public static void downloadUpdate(Callback<Boolean, Exception> callback) {
if(response == null) {
return;
}

Main.newChain()
.asyncFirst(() -> {
try {
//FileUtils.copyURLToFile(new URL();

URL url = new URL(String.format("%s/%s/download?version=%s", SPIGOT_URL, RESOURCE_ID, response.getId()));

URLConnection connection = url.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36");
connection.connect();
FileUtils.copyInputStreamToFile(connection.getInputStream(), Main.getInstance().getDataFolder().getParentFile());

return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
})
.syncLast((result) -> callback.call(result, null))
.execute();
}

private static UpdateResponse getLatestVersion() {
try {
URL url = new URL(String.format("%s/%s/versions?size=1&sort=-releaseDate", API_URL, RESOURCE_ID));
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.addRequestProperty("User-Agent", USER_AGENT);

InputStream stream = connection.getInputStream();
//System.out.println(IOUtils.toString(stream, Charset.defaultCharset()));

UpdateResponse[] result = gson.fromJson(IOUtils.toString(stream, Charset.defaultCharset()), UpdateResponse[].class);
return result[0];
Expand Down
6 changes: 2 additions & 4 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ plugin-prefix: "&a[Guilds]"

updater:
check: true
download: true

name:
# Minimum length of guild names
Expand Down Expand Up @@ -183,9 +182,8 @@ messages:
reload:
reloaded: "&aConfiguration file reloaded!"
update:
found: "&aUpdate found! Downloading..."
successful: "&aUpdate downloaded! Restart your server to run the latest version of Guilds!"
error: "&cSomething went wrong while downloading the update"
found: "&aFound an update! Go to {url} to download it!"
not-found: "&cNo update found!"
event:
join:
pending-invites: "&aYou have {number} pending invite(s) from the guild(s): &e{guilds}"

0 comments on commit 21c27ee

Please sign in to comment.