-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Honestly overkill, allows addons to check for updates though
- Loading branch information
Showing
16 changed files
with
327 additions
and
128 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
102 changes: 0 additions & 102 deletions
102
src/main/java/com/dre/brewery/utility/UpdateChecker.java
This file was deleted.
Oops, something went wrong.
109 changes: 109 additions & 0 deletions
109
src/main/java/com/dre/brewery/utility/releases/ReleaseChecker.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,109 @@ | ||
/* | ||
* BreweryX Bukkit-Plugin for an alternate brewing process | ||
* Copyright (C) 2024 The Brewery Team | ||
* | ||
* This file is part of BreweryX. | ||
* | ||
* BreweryX is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* BreweryX is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with BreweryX. If not, see <http://www.gnu.org/licenses/gpl-3.0.html>. | ||
*/ | ||
|
||
package com.dre.brewery.utility.releases; | ||
|
||
import com.dre.brewery.BreweryPlugin; | ||
import com.dre.brewery.configuration.ConfigManager; | ||
import com.dre.brewery.configuration.files.Config; | ||
import com.dre.brewery.configuration.files.Lang; | ||
import com.dre.brewery.utility.releases.impl.GitHubReleaseChecker; | ||
import com.dre.brewery.utility.releases.impl.NoImplReleaseChecker; | ||
import com.dre.brewery.utility.releases.impl.SpigotReleaseChecker; | ||
import lombok.Getter; | ||
import org.bukkit.command.CommandSender; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
@Getter | ||
public abstract class ReleaseChecker { | ||
|
||
protected static final String CONST_UNRESOLVED = "Unresolved"; | ||
private static ReleaseChecker instance; | ||
|
||
protected String resolvedLatestVersion = CONST_UNRESOLVED; // Latest version of BX resolved from the source | ||
|
||
|
||
public abstract CompletableFuture<String> resolveLatest(); | ||
|
||
public abstract CompletableFuture<Boolean> checkForUpdate(); | ||
|
||
|
||
public void notify(CommandSender receiver) { | ||
if (receiver.hasPermission("brewery.update") && isUpdateAvailable()) { | ||
ConfigManager.getConfig(Lang.class) | ||
.sendEntry(receiver, "Etc_UpdateAvailable", "v" + localVersion(), "v" + resolvedLatestVersion); | ||
} | ||
} | ||
|
||
|
||
public boolean isUpdateAvailable() { | ||
if (resolvedLatestVersion.equals(CONST_UNRESOLVED)) { | ||
return false; | ||
} | ||
int local = parseVersion(localVersion()); | ||
int resolved = parseVersion(resolvedLatestVersion); | ||
return resolved > local; | ||
} | ||
|
||
|
||
// Singleton | ||
|
||
public static ReleaseChecker getInstance() { | ||
if (instance != null) { | ||
return instance; | ||
} | ||
Config config = ConfigManager.getConfig(Config.class); | ||
switch (config.getResolveUpdatesFrom()) { | ||
case GITHUB -> instance = new GitHubReleaseChecker("BreweryTeam", "BreweryX"); | ||
case SPIGOT -> instance = new SpigotReleaseChecker(114777); | ||
case NONE -> instance = new NoImplReleaseChecker(); | ||
} | ||
return instance; | ||
} | ||
|
||
|
||
// Util | ||
public String localVersion() { | ||
String versionString = BreweryPlugin.getInstance().getDescription().getVersion(); | ||
if (versionString.contains(";")) { | ||
// I don't care about the branch | ||
return versionString.split(";")[0]; | ||
} | ||
return versionString; | ||
} | ||
|
||
public int parseVersion(String version) { | ||
StringBuilder sb = new StringBuilder(); | ||
for (char c : version.toCharArray()) { | ||
if (Character.isDigit(c)) { | ||
sb.append(c); | ||
} | ||
} | ||
return Integer.parseInt(sb.toString()); | ||
} | ||
|
||
|
||
public enum ReleaseCheckerType { | ||
GITHUB, | ||
SPIGOT, | ||
NONE | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
src/main/java/com/dre/brewery/utility/releases/impl/GitHubReleaseChecker.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,79 @@ | ||
/* | ||
* BreweryX Bukkit-Plugin for an alternate brewing process | ||
* Copyright (C) 2024 The Brewery Team | ||
* | ||
* This file is part of BreweryX. | ||
* | ||
* BreweryX is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* BreweryX is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with BreweryX. If not, see <http://www.gnu.org/licenses/gpl-3.0.html>. | ||
*/ | ||
|
||
package com.dre.brewery.utility.releases.impl; | ||
|
||
import com.dre.brewery.utility.Logging; | ||
import com.dre.brewery.utility.releases.ReleaseChecker; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class GitHubReleaseChecker extends ReleaseChecker { | ||
|
||
private static final String CONST_URL = "https://api.github.com/repos/%s/%s/releases/latest"; | ||
private static final String CONST_JSON_FIELD = "tag_name"; | ||
|
||
private final String link; | ||
|
||
public GitHubReleaseChecker(String owner, String repo) { | ||
this.link = String.format(CONST_URL, owner, repo); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<String> resolveLatest() { | ||
if (resolvedLatestVersion != null) { | ||
return CompletableFuture.completedFuture(resolvedLatestVersion); | ||
} | ||
HttpClient client = HttpClient.newHttpClient(); | ||
HttpRequest request = HttpRequest.newBuilder() | ||
.uri(URI.create(link)) | ||
.GET() | ||
.build(); | ||
|
||
return CompletableFuture.supplyAsync(() -> { | ||
try { | ||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); | ||
JsonObject jsonResponse = JsonParser.parseString(response.body()).getAsJsonObject(); | ||
this.resolvedLatestVersion = jsonResponse.get(CONST_JSON_FIELD).getAsString(); | ||
return this.resolvedLatestVersion; | ||
} catch (IOException | InterruptedException e) { | ||
Logging.warningLog("Failed to resolve latest BreweryX version from GitHub. (No connection?)"); | ||
return CONST_UNRESOLVED; | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Boolean> checkForUpdate() { | ||
return resolveLatest().thenApply(v -> { | ||
if (v.equals(CONST_UNRESOLVED)) { | ||
return false; | ||
} | ||
return isUpdateAvailable(); | ||
}); | ||
} | ||
} |
Oops, something went wrong.