Skip to content

Commit

Permalink
Implement new Version class
Browse files Browse the repository at this point in the history
  • Loading branch information
CyR1en committed Mar 16, 2020
1 parent 8adef8e commit 36865c5
Showing 1 changed file with 93 additions and 11 deletions.
104 changes: 93 additions & 11 deletions kiso-mc/src/main/java/com/cyr1en/kiso/mc/UpdateChecker.java
@@ -1,6 +1,5 @@
package com.cyr1en.kiso.mc;


import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent;
Expand Down Expand Up @@ -46,25 +45,23 @@ public boolean isDisabled() {
return isDisabled;
}

public String getCurrVersion() {
String version = "0.0.0";
public Version getCurrVersion() {
Version version = new Version();
try{
HttpURLConnection connection = buildConnection(Objects.requireNonNull(stringAsUrl()));
InputStreamReader ir = new InputStreamReader(connection.getInputStream());
BufferedReader br = new BufferedReader(ir);
version = br.readLine();
version = version.parse(br.readLine());
} catch (IOException e) {
plugin.getLogger().log(Level.WARNING, "An error occurred while getting update data: ''{0}''!", e.getCause());
}
return version;
}

public boolean newVersionAvailable() {
String curr = plugin.getDescription().getVersion().replaceAll("[a-zA-z ]|:", "").replaceAll("\\.", "");
String arg = getCurrVersion().replaceAll("[a-zA-z ]|:", "").replaceAll("\\.", "");
int iCurr = Integer.parseInt(curr);
int iArg = Integer.parseInt(arg);
return iArg > iCurr;
Version curr = new Version(plugin.getDescription().getVersion());
Version arg = getCurrVersion();
return arg.isNewerThan(curr);
}

private URL stringAsUrl() {
Expand Down Expand Up @@ -99,7 +96,7 @@ public void onJoin(PlayerJoinEvent event) {
public void sendUpdateAvailableMessage(CommandSender sender) {
if (!newVersionAvailable())
return;
String v = getCurrVersion().replaceAll("[a-zA-z: ]", "");
Version v = getCurrVersion();
if (sender instanceof Player && sender.isOp()) {
try {
if (Class.forName("org.spigotmc.SpigotConfig") != null) {
Expand All @@ -111,7 +108,7 @@ public void sendUpdateAvailableMessage(CommandSender sender) {
.color(ChatColor.GOLD)
.append(" A new update is available: ")
.color(ChatColor.AQUA)
.append(v)
.append(v.asString())
.color(ChatColor.YELLOW)
.event(new ClickEvent(ClickEvent.Action.OPEN_URL, String.format(RESOURCE_URL, resourceName, resourceID)))
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Click here download the new version.").create()))
Expand All @@ -124,4 +121,89 @@ public void sendUpdateAvailableMessage(CommandSender sender) {
}
}
}

/**
* Class for representing the version of the plugin.
*
* <p>This class will follow the conventions of Semantic versioning.</p>
*
* @see <a href="https://semver.org/">https://semver.org/</a>
*/
public class Version {
private int major;
private int minor;
private int patch;

public Version(int major, int minor, int patch){
this.major = major;
this.minor = minor;
this.patch = patch;
}

public Version(String versionString) {
this();
parse(versionString);
}

public Version() {
this(0,0,0);
}

public int getMajor() {
return major;
}

public void setMajor(int major) {
this.major = major;
}

public int getMinor() {
return minor;
}

public void setMinor(int minor) {
this.minor = minor;
}

public int getPatch() {
return patch;
}

public void setPatch(int patch) {
this.patch = patch;
}

public String asString() {
return getMajor() + "." + getMinor() + "." + getPatch();
}

public Version parse(String versionString) {
String[] split = versionString.split(".");
assertVersionStringSize(split);
setMajor(Integer.parseInt(split[0]));
setMinor(Integer.parseInt(split[1]));
setPatch(Integer.parseInt(split[2]));
return this;
}

public boolean isNewerThan(Version version) {
if(getMajor() > version.getMajor()) return true;
if(getMinor() > version.getMinor()) return true;
return getPatch() > version.getPatch();
}

private void assertVersionStringSize(String[] strings) {
if(strings.length != 3)
throw new IllegalArgumentException("The string of version must contain a major, minor, and patch.");
}

@Override
public String toString() {
return "Version{" +
"major=" + major +
", minor=" + minor +
", patch=" + patch +
'}';
}
}
}

0 comments on commit 36865c5

Please sign in to comment.