Skip to content

Commit

Permalink
Merge pull request #49 from elian1203/update-command
Browse files Browse the repository at this point in the history
Add update command to allow manual updating.
  • Loading branch information
elian1203 authored Oct 9, 2023
2 parents e409927 + 506ea5d commit 7342691
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 13 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
<version>31.1-jre</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
Expand Down
39 changes: 38 additions & 1 deletion src/main/java/me/elian/ezauctions/command/AuctionCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ public class AuctionCommand extends BaseCommand {
private final ConfigController config;
private final MessageController messages;
private final ScoreboardController scoreboard;
private final UpdateController updateController;
private final Database database;
private final TaskScheduler scheduler;

@Inject
public AuctionCommand(Plugin plugin, Logger logger, Economy economy, AuctionController auctionController,
AuctionPlayerController playerController, ConfigController config,
MessageController messages, ScoreboardController scoreboard,
Database database, TaskScheduler scheduler) {
UpdateController updateController, Database database, TaskScheduler scheduler) {
this.plugin = plugin;
this.logger = logger;
this.economy = economy;
Expand All @@ -69,6 +70,7 @@ public AuctionCommand(Plugin plugin, Logger logger, Economy economy, AuctionCont
this.config = config;
this.messages = messages;
this.scoreboard = scoreboard;
this.updateController = updateController;
this.database = database;
this.scheduler = scheduler;
}
Expand Down Expand Up @@ -558,6 +560,41 @@ public void reload(CommandSender sender) {
});
}

@Subcommand("update")
@CommandPermission("ezauctions.auction.update")
public void update(CommandSender sender) {
scheduler.runAsyncTask(() -> {
messages.sendMessage(sender, "command.auction.update.checking");
try {
updateController.fetchLatestSupportedVersion();
String latestVersion = updateController.getLatestSupportedPluginVersion();
String serverVersion = updateController.getServerPluginVersion();
if (latestVersion == null || latestVersion.equals(serverVersion)) {
if (latestVersion == null) {
latestVersion = serverVersion;
}

messages.sendMessage(sender, "command.auction.update.already_up_to_date",
Placeholder.unparsed("latestversion", latestVersion),
Placeholder.unparsed("serverversion", serverVersion));
return;
}

messages.sendMessage(sender, "command.auction.update.latest_version",
Placeholder.unparsed("latestversion", latestVersion),
Placeholder.unparsed("serverversion", serverVersion));

updateController.downloadLatestSupportedVersion();
messages.sendMessage(sender, "command.auction.update.downloaded",
Placeholder.unparsed("latestversion", latestVersion),
Placeholder.unparsed("serverversion", serverVersion));
} catch (Exception e) {
logger.warning("Error while updating plugin", e);
messages.sendMessage(sender, "command.auction.update.error");
}
});
}

private boolean reachedQueueLimit(Player player) {
Collection<AuctionData> queue = auctionController.getAuctionQueue();

Expand Down
43 changes: 31 additions & 12 deletions src/main/java/me/elian/ezauctions/controller/UpdateController.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
@Singleton
public class UpdateController implements Listener {
private static final String GITHUB_REPO = "elian1203/ezAuctions";
// these two fields need to be static if the plugin is reloaded using /reload or PlugMan at runtime
private static Thread shutdownHook;
private static Path downloadedFilePath;

private final Plugin plugin;
private final TaskScheduler scheduler;
Expand All @@ -42,12 +45,10 @@ public class UpdateController implements Listener {
private final MessageController messages;
private final String serverMinecraftVersion;
private final String serverPluginVersion;
private final HttpClient client;

private HttpClient client;
private String latestSupportedPluginVersion;

private Path downloadedFilePath;

@Inject
public UpdateController(Plugin plugin, TaskScheduler scheduler, Logger logger, ConfigController config,
MessageController messages) {
Expand All @@ -59,6 +60,15 @@ public UpdateController(Plugin plugin, TaskScheduler scheduler, Logger logger, C

serverMinecraftVersion = getServerMinecraftVersion();
serverPluginVersion = plugin.getDescription().getVersion();
client = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.NORMAL).build();
}

public String getServerPluginVersion() {
return serverPluginVersion;
}

public String getLatestSupportedPluginVersion() {
return latestSupportedPluginVersion;
}

private String getServerMinecraftVersion() {
Expand All @@ -78,10 +88,8 @@ public void checkForUpdates() {
return;

scheduler.runAsyncTask(() -> {
client = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.NORMAL).build();

try {
latestSupportedPluginVersion = getLatestSupportedVersion();
fetchLatestSupportedVersion();

if (latestSupportedPluginVersion == null || latestSupportedPluginVersion.equals(serverPluginVersion))
return;
Expand Down Expand Up @@ -116,7 +124,7 @@ private boolean versionIsEqualOrNewer(String oldVersion, String newVersion) {
return newVersionParsed.compareTo(oldVersionParsed) >= 0;
}

private String getLatestSupportedVersion() throws ExecutionException, InterruptedException {
public void fetchLatestSupportedVersion() throws ExecutionException, InterruptedException {
String url = "https://api.github.com/repos/" + GITHUB_REPO + "/tags";
HttpRequest request = HttpRequest.newBuilder(URI.create(url)).build();

Expand All @@ -136,13 +144,14 @@ private String getLatestSupportedVersion() throws ExecutionException, Interrupte
String tagName = object.get("name").getAsString();

if (versionIsEqualOrNewer(serverPluginVersion, tagName) && isTagSupported(tagName)) {
return tagName;
latestSupportedPluginVersion = tagName;
return;
}

index++;
}

return null;
latestSupportedPluginVersion = null;
}

private boolean isTagSupported(String tagName) throws ExecutionException, InterruptedException {
Expand All @@ -162,7 +171,11 @@ private boolean isTagSupported(String tagName) throws ExecutionException, Interr
return versionIsEqualOrNewer(apiVersion, serverMinecraftVersion);
}

private void downloadLatestSupportedVersion() throws ExecutionException, InterruptedException {
public void downloadLatestSupportedVersion() throws ExecutionException, InterruptedException {
if (downloadedFilePath != null) {
downloadedFilePath.toFile().delete();
}

logger.info("Downloading plugin update...");
String releaseUrl =
"https://api.github.com/repos/" + GITHUB_REPO + "/releases/tags/" + latestSupportedPluginVersion;
Expand Down Expand Up @@ -213,11 +226,15 @@ private void downloadLatestSupportedVersion() throws ExecutionException, Interru
}

private void moveDownloadedUpdateToPluginsDir() {
if (shutdownHook != null) {
Runtime.getRuntime().removeShutdownHook(shutdownHook);
}

if (downloadedFilePath == null)
return;

// use shutdown hook to wait until lock on jar is released
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
shutdownHook = new Thread(() -> {
Path newFilePath = Path.of(plugin.getDataFolder().getParentFile().getAbsolutePath(),
downloadedFilePath.toFile().getName());
try {
Expand All @@ -233,7 +250,9 @@ private void moveDownloadedUpdateToPluginsDir() {
} catch (IOException e) {
logger.severe("Could not move new jar file!", e);
}
}));
});

Runtime.getRuntime().addShutdownHook(shutdownHook);
}

@EventHandler
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ help:
- info
- queue
- reload
- update
- remove
- save
- scoreboard
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ command.auction.info.help=<hover:show_text:"/auction info"><click:suggest_comman
command.auction.reload.help=<hover:show_text:"/auction reload"><click:suggest_command:"/auction help"><aqua>/auction reload <dark_blue>: <blue>Reload all configuration and messages.</click></hover>
command.auction.reload={prefix}<blue>Reloaded configuration and messages!

command.auction.update.help=<hover:show_text:"/auction update"><click:suggest_command:"/auction update"><aqua>/auction update <dark_blue>: <blue>Checks for updates to the plugin and downloads them.</click></hover>
command.auction.update.checking={prefix}<blue>Checking latest version...
command.auction.update.already_up_to_date={prefix}<blue>Plugin is already up-to-date.
command.auction.update.latest_version={prefix}<blue>Latest version found: <green><latestversion></green>. You are currently running <green><serverversion></green>. Downloading update...
command.auction.update.downloaded={prefix}<blue>New version <green><latestversion></green> downloaded. Update will be automatically applied when the server is stopped or restarted.
command.auction.update.error={prefix}<red>Error while updating plugin! See console for error details.

command.auction.remove.help=<hover:show_text:"/auction remove"><click:suggest_command:"/auction remove"><aqua>/auction remove <dark_blue>: <blue>Remove your queued auction.</click></hover>
command.auction.remove.not_in_queue={prefix}<red>You do not have an auction in the queue.
command.auction.remove.success={prefix}<blue>Your auction has been removed from the queue.
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ permissions:
ezauctions.auction.reload: true
ezauctions.updatemessage: true
ezauctions.auction.end: true
ezauctions.auction.update: true
ezauctions.player:
default: true
children:
Expand Down Expand Up @@ -75,6 +76,8 @@ permissions:
default: false
ezauctions.auction.reload:
default: false
ezauctions.auction.update:
default: false
ezauctions.bid:
default: false
ezauctions.updatemessage:
Expand Down

0 comments on commit 7342691

Please sign in to comment.