generated from TobiasDeBruijn/BaseBukkitPlugin
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f202a44
commit 4750444
Showing
6 changed files
with
152 additions
and
25 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
24 changes: 24 additions & 0 deletions
24
src/main/java/nl/thedutchmc/SkinFixer/updatechecker/GithubResponse.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,24 @@ | ||
package nl.thedutchmc.SkinFixer.updatechecker; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
|
||
public class GithubResponse { | ||
@SerializedName("html_url") | ||
private String htmlUrl; | ||
|
||
@SerializedName("tag_name") | ||
private String tagName; | ||
|
||
/** | ||
* @return The URL of the new GitHub Release | ||
*/ | ||
public String getUrl() { | ||
return htmlUrl; | ||
} | ||
/** | ||
* @return The name of the Release | ||
*/ | ||
public String getTagName() { | ||
return tagName; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/main/java/nl/thedutchmc/SkinFixer/updatechecker/UpdateChecker.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,73 @@ | ||
package nl.thedutchmc.SkinFixer.updatechecker; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.regex.Pattern; | ||
|
||
import com.google.gson.Gson; | ||
|
||
import nl.thedutchmc.SkinFixer.SkinFixer; | ||
import nl.thedutchmc.httplib.Http; | ||
import nl.thedutchmc.httplib.Http.RequestMethod; | ||
import nl.thedutchmc.httplib.Http.ResponseObject; | ||
|
||
public class UpdateChecker { | ||
|
||
private SkinFixer plugin; | ||
|
||
public UpdateChecker(SkinFixer plugin) { | ||
this.plugin = plugin; | ||
} | ||
|
||
public void checkUpdate() { | ||
String[] currentVersion = this.plugin.getDescription().getVersion().split(Pattern.quote(".")); | ||
int currentMajorVersion = Integer.parseInt(currentVersion[0]); | ||
int currentMinorVersion = Integer.parseInt(currentVersion[1]); | ||
int currentBuild = Integer.parseInt((currentVersion.length > 2) ? currentVersion[2] : "0"); | ||
|
||
HashMap<String, String> headers = new HashMap<>(); | ||
headers.put("User-Agent", "SkinFixer Plugin v" + this.plugin.getDescription().getVersion()); | ||
|
||
ResponseObject response; | ||
try { | ||
response = new Http().makeRequest(RequestMethod.GET, "https://api.github.com/repos/thedutchmc/skinfixer/releases/latest", null, null, null, headers); | ||
} catch(IOException e) { | ||
SkinFixer.logWarn(String.format("An issue occurred while checking what the latest version of SkinFixer is: IOException (%s)", e.getMessage())); | ||
return; | ||
} | ||
|
||
if(response.getResponseCode() != 200) { | ||
SkinFixer.logWarn(String.format("Got a non-200 status code while checking what the latest version of SkinFixer is: HTTP-%d (%s)", response.getResponseCode(), response.getConnectionMessage())); | ||
return; | ||
} | ||
|
||
final Gson gson = new Gson(); | ||
GithubResponse responseDeserialized = gson.fromJson(response.getMessage(), GithubResponse.class); | ||
|
||
String[] latestVersion = responseDeserialized.getTagName().split(Pattern.quote(".")); | ||
int latestMajorVersion = Integer.parseInt(latestVersion[0]); | ||
int latestMinorVersion = Integer.parseInt(latestVersion[1]); | ||
int latestBuild = Integer.parseInt((latestVersion.length > 2) ? latestVersion[2] : "0"); | ||
|
||
if(latestMajorVersion > currentMajorVersion) { | ||
updateAvailable(responseDeserialized.getUrl(), responseDeserialized.getTagName()); | ||
return; | ||
} | ||
|
||
if(latestMinorVersion > currentMinorVersion) { | ||
updateAvailable(responseDeserialized.getUrl(), responseDeserialized.getTagName()); | ||
return; | ||
} | ||
|
||
if(latestBuild > currentBuild) { | ||
updateAvailable(responseDeserialized.getUrl(), responseDeserialized.getTagName()); | ||
return; | ||
} | ||
|
||
SkinFixer.logInfo("You are running the latest version of SkinFixer. Nice work! :D"); | ||
} | ||
|
||
private void updateAvailable(String url, String latestVersion) { | ||
SkinFixer.logWarn(String.format("A SkinFixer update is available. You are running version %s, the latest version is %s. You can download it here: %s", this.plugin.getDescription().getVersion(), latestVersion, url)); | ||
} | ||
} |