Skip to content

Commit 1813d2a

Browse files
authored
🐛 Fixes (#1)
* 🐛 Fixes * 🔥 Remove .gradle * 🐛 Fix errors reported in PR * fix other problems from comments * Other fixes
1 parent a341a07 commit 1813d2a

File tree

4 files changed

+51
-49
lines changed

4 files changed

+51
-49
lines changed

src/main/java/com/eternalcode/eternalupdater/EternalUpdater.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,27 @@
77
import org.json.simple.JSONObject;
88

99
public class EternalUpdater {
10-
private PluginData pluginData;
10+
private final PluginData pluginData;
1111

1212
/**
1313
* @param pluginName Name of the checked plugin
1414
* @param pluginVersion Current version the plugin
1515
* @param githubRepo Github remote repository name (ex. eternalcodeteam/eternalcore)
1616
*/
1717
public EternalUpdater(@NotNull String pluginName, @NotNull String pluginVersion, @NotNull String githubRepo) {
18-
this.pluginData = new PluginData();
19-
this.pluginData.setPluginName(pluginName);
20-
this.pluginData.setPluginVersion(pluginVersion);
21-
this.pluginData.setGithubRepository(githubRepo);
18+
this.pluginData = new PluginData(githubRepo, pluginVersion, pluginName);
2219
}
2320

2421
public RemoteInformation checkUpdates() {
2522
JSONObject response = HttpClient.doRequest("repos/" + this.pluginData.getGithubRepository() + "/releases/latest");
26-
RemoteInformation remoteInformation = new RemoteInformation();
23+
boolean newVersionAvailable = this.pluginData.getPluginVersion() != (String) response.get("tag_name");
24+
String latestTag = (String) response.get("tag_name");
25+
String newDownloadUri = (String) response.get("zipball_url");
2726

28-
remoteInformation.setCurrentVersion(response.isEmpty() ? this.pluginData.getPluginVersion() : (String) response.get("tag_name"));
29-
remoteInformation.setAvailableNewVersion(response.isEmpty() ? false : this.pluginData.getPluginVersion() != (String) response.get("tag_name"));
30-
remoteInformation.setDownloadUri(response.isEmpty() ? "https://github.com/" + this.pluginData.getGithubRepository() : (String) response.get("zipball_url"));
31-
32-
return remoteInformation;
27+
return new RemoteInformation(
28+
newVersionAvailable,
29+
latestTag,
30+
newDownloadUri
31+
);
3332
}
3433
}
Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
11
package com.eternalcode.eternalupdater.data;
22

33
public class PluginData {
4-
private String githubRepository;
5-
private String pluginVersion;
6-
private String pluginName;
74

8-
public String getGithubRepository() {
9-
return githubRepository;
10-
}
5+
private final String githubRepository;
6+
private final String pluginVersion;
7+
private final String pluginName;
118

12-
public void setGithubRepository(String githubRepository) {
9+
public PluginData(String githubRepository, String pluginVersion, String pluginName) {
1310
this.githubRepository = githubRepository;
11+
this.pluginVersion = pluginVersion;
12+
this.pluginName = pluginName;
1413
}
1514

16-
public String getPluginVersion() {
17-
return pluginVersion;
15+
public String getGithubRepository() {
16+
return this.githubRepository;
1817
}
1918

20-
public void setPluginVersion(String pluginVersion) {
21-
this.pluginVersion = pluginVersion;
19+
public String getPluginVersion() {
20+
return this.pluginVersion;
2221
}
2322

2423
public String getPluginName() {
25-
return pluginName;
24+
return this.pluginName;
2625
}
2726

28-
public void setPluginName(String pluginName) {
29-
this.pluginName = pluginName;
30-
}
3127
}
Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
11
package com.eternalcode.eternalupdater.data;
22

33
public class RemoteInformation {
4-
private boolean isAvailableNewVersion;
5-
private String currentVersion;
6-
private String downloadUri;
74

8-
public String getDownloadUri() {
9-
return downloadUri;
10-
}
5+
private final boolean isAvailableNewVersion;
6+
private final String currentVersion;
7+
private final String downloadUri;
118

12-
public void setDownloadUri(String downloadUri) {
9+
public RemoteInformation(boolean isAvailableNewVersion, String currentVersion, String downloadUri) {
10+
this.isAvailableNewVersion = isAvailableNewVersion;
11+
this.currentVersion = currentVersion;
1312
this.downloadUri = downloadUri;
1413
}
1514

16-
public boolean isAvailableNewVersion() {
17-
return isAvailableNewVersion;
15+
public String getDownloadUri() {
16+
return this.downloadUri;
1817
}
1918

20-
public void setAvailableNewVersion(boolean availableNewVersion) {
21-
isAvailableNewVersion = availableNewVersion;
19+
public boolean isAvailableNewVersion() {
20+
return this.isAvailableNewVersion;
2221
}
2322

2423
public String getCurrentVersion() {
25-
return currentVersion;
24+
return this.currentVersion;
2625
}
2726

28-
public void setCurrentVersion(String currentVersion) {
29-
this.currentVersion = currentVersion;
30-
}
3127
}

src/main/java/com/eternalcode/eternalupdater/util/HttpClient.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,27 @@
77
import org.json.simple.parser.JSONParser;
88

99
public class HttpClient {
10-
public static String baseUri = "https://api.github.com/";
11-
public static OkHttpClient client = new OkHttpClient();
1210

13-
public static JSONObject doRequest(String url)
14-
{
15-
Request request = new Request.Builder().url(baseUri + "/" + url).build();
16-
try (Response response = client.newCall(request).execute()){
17-
return response.body() != null ? (JSONObject) new JSONParser().parse(response.body().string()) : null;
11+
private final static String baseUri = "https://api.github.com/";
12+
private final static OkHttpClient client = new OkHttpClient();
13+
14+
public static JSONObject doRequest(String url) {
15+
Request request = new Request
16+
.Builder()
17+
.url(baseUri + "" + url)
18+
.build();
19+
20+
try (Response response = client.newCall(request).execute()) {
21+
JSONObject jsonResponse = (JSONObject) new JSONParser().parse(response.body().string());
22+
23+
if (jsonResponse.containsKey("message")) {
24+
throw new Exception("[EternalUpdater] Provided repository was not found");
25+
}
26+
else {
27+
return jsonResponse;
28+
}
1829
} catch (Exception e) {
19-
return null;
30+
throw new RuntimeException(e);
2031
}
2132
}
2233

0 commit comments

Comments
 (0)