Skip to content

Commit

Permalink
add new updater
Browse files Browse the repository at this point in the history
  • Loading branch information
ajgeiss0702 committed Jul 9, 2024
1 parent 3820829 commit 1a90ea0
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 21 deletions.
2 changes: 1 addition & 1 deletion api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dependencies {
implementation("net.kyori:adventure-text-serializer-plain:4.15.0")
compileOnly("com.google.guava:guava:30.1.1-jre")

compileOnly("us.ajg0702:ajUtils:1.2.25")
compileOnly("us.ajg0702:ajUtils:1.2.28")
}

publishing {
Expand Down
2 changes: 1 addition & 1 deletion common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies {
compileOnly("net.kyori:adventure-text-serializer-plain:4.15.0")

compileOnly("com.google.guava:guava:30.1.1-jre")
compileOnly("us.ajg0702:ajUtils:1.2.25")
compileOnly("us.ajg0702:ajUtils:1.2.28")

compileOnly("org.slf4j:slf4j-log4j12:1.7.29")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import us.ajg0702.queue.commands.SubCommand;
import us.ajg0702.queue.common.QueueMain;
import us.ajg0702.utils.common.Messages;
import us.ajg0702.utils.common.UpdateManager;

import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -45,6 +46,7 @@ public void execute(ICommandSender sender, String[] args) {
main.getMessages().reload();
try {
main.getConfig().reload();
main.getUpdaterConfig().reload();
} catch (ConfigurateException e) {
sender.sendMessage(Component.text("An error occurred while reloading. Check the console").color(NamedTextColor.RED));
e.printStackTrace();
Expand All @@ -56,7 +58,8 @@ public void execute(ICommandSender sender, String[] args) {
main.getMessages().reload();
main.getSlashServerManager().reload();

main.getUpdater().setEnabled(main.getConfig().getBoolean("enable-updater"));
UpdateManager updateManager = main.getUpdateManager();
if(updateManager != null) updateManager.setUpdateToken(main.getUpdaterConfig().getString("updater-token"));

sender.sendMessage(getMessages().getComponent("commands.reload"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import us.ajg0702.queue.commands.SubCommand;
import us.ajg0702.queue.common.QueueMain;
import us.ajg0702.utils.common.Messages;
import us.ajg0702.utils.common.UpdateManager;
import us.ajg0702.utils.common.Updater;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

public class Update extends SubCommand {

Expand Down Expand Up @@ -40,7 +43,13 @@ public Messages getMessages() {
@Override
public void execute(ICommandSender sender, String[] args) {
if(!checkPermission(sender)) return;
Updater updater = main.getUpdater();
UpdateManager updater = main.getUpdateManager();
if(updater == null) {
sender.sendMessage(
getMessages().getComponent("updater.disabled")
);
return;
}
if(updater.isAlreadyDownloaded()) {
sender.sendMessage(getMessages().getComponent("updater.already-downloaded"));
return;
Expand All @@ -49,11 +58,64 @@ public void execute(ICommandSender sender, String[] args) {
sender.sendMessage(getMessages().getComponent("updater.no-update"));
return;
}
if(updater.downloadUpdate()) {
/*if() {
sender.sendMessage(getMessages().getComponent("updater.success"));
} else {
sender.sendMessage(getMessages().getComponent("updater.fail"));
}
}*/
AtomicBoolean done = new AtomicBoolean(false);

main.getTaskManager().runNow(() -> {
try {
UpdateManager.DownloadCompleteStatus result = updater.downloadUpdate();

done.set(true);

switch(result) {
case SUCCESS:
sender.sendMessage(getMessages().getComponent("updater.success"));
break;
case WARNING_COULD_NOT_DELETE_OLD_JAR:
sender.sendMessage(getMessages().getComponent("updater.warnings.could-not-delete-old-jar"));
break;
case ERROR_NO_UPDATE_AVAILABLE:
sender.sendMessage(getMessages().getComponent("updater.no-update"));
break;
case ERROR_WHILE_CHECKING:
sender.sendMessage(getMessages().getComponent("updater.errors.while-checking"));
break;
case ERROR_ALREADY_DOWNLOADED:
sender.sendMessage(getMessages().getComponent("updater.already-downloaded"));
break;
case ERROR_JAR_NOT_FOUND:
sender.sendMessage(getMessages().getComponent("updater.errors.could-not-find-jar"));
break;
case ERROR_WHILE_DOWNLOADING:
sender.sendMessage(getMessages().getComponent("updater.errors.while-downloading"));
break;
case ERROR_MISSING_UPDATE_TOKEN:
sender.sendMessage(getMessages().getComponent("updater.errors.missing-update-token"));
break;
case ERROR_INVALID_UPDATE_TOKEN:
sender.sendMessage(getMessages().getComponent("updater.errors.invalid-update-token"));
break;
default:
sender.sendMessage(getMessages().getComponent("updater.errors.unknown", "ERROR:"+result));
break;
}
} catch(Exception e) {
sender.sendMessage(getMessages().getComponent("updater.errors.uncaught"));
main.getLogger().warn("Uncaught error while updating:", e);
}

main.getTaskManager().runLater(() -> {
if(done.get()) return;

sender.sendMessage(getMessages().getComponent("updater.slow-feedback"));
}, 1, TimeUnit.SECONDS);
});


}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void handleMessage(AdaptedPlayer receivingPlayer, byte[] data) {
public void onPlayerJoin(AdaptedPlayer player) {
if(player.hasPermission("ajqueue.manage.update")) {
main.getTaskManager().runLater(() -> {
if (main.getUpdater().isUpdateAvailable() && !main.getUpdater().isAlreadyDownloaded()) {
if (main.getUpdateManager().isUpdateAvailable() && !main.getUpdateManager().isAlreadyDownloaded()) {
player.sendMessage(main.getMessages().getComponent("updater.update-available"));
}
}, 2, TimeUnit.SECONDS);
Expand Down
41 changes: 32 additions & 9 deletions common/src/main/java/us/ajg0702/queue/common/QueueMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import us.ajg0702.queue.logic.LogicGetterImpl;
import us.ajg0702.utils.common.Config;
import us.ajg0702.utils.common.Messages;
import us.ajg0702.utils.common.Updater;
import us.ajg0702.utils.common.SimpleConfig;
import us.ajg0702.utils.common.UpdateManager;

import java.io.File;
import java.util.*;
Expand Down Expand Up @@ -134,9 +135,14 @@ public Map<String, List<String>> getQueueServers() {
return r;
}

private Updater updater;
public Updater getUpdater() {
return updater;
private UpdateManager updateManager;
public UpdateManager getUpdateManager() {
return updateManager;
}

private SimpleConfig updaterConfig;
public SimpleConfig getUpdaterConfig() {
return updaterConfig;
}

private final Implementation implementation;
Expand All @@ -152,7 +158,6 @@ public SlashServerManager getSlashServerManager() {
@Override
public void shutdown() {
taskManager.shutdown();
updater.shutdown();
}


Expand Down Expand Up @@ -218,6 +223,15 @@ public QueueMain(Implementation implementation, QueueLogger logger, PlatformMeth
return;
}


try {
updaterConfig = new SimpleConfig(dataFolder, "updater-config.yml", new LogConverter(logger));
} catch (ConfigurateException e) {
logger.warning("Unable to load config:");
e.printStackTrace();
return;
}

constructMessages();

getQueueHolderRegistry().register("default", DefaultQueueHolder.class);
Expand All @@ -239,8 +253,8 @@ public QueueMain(Implementation implementation, QueueLogger logger, PlatformMeth

taskManager.rescheduleTasks();

updater = new Updater(logger, platformMethods.getPluginVersion(), isPremium() ? "ajQueuePlus" : "ajQueue", config.getBoolean("enable-updater"), isPremium() ? 79123 : 78266, dataFolder.getParentFile(), "ajQueue update");

String plugin = isPremium() ? "ajQueuePlus" : "ajQueue";
updateManager = new UpdateManager(logger, platformMethods.getPluginVersion(), plugin, plugin, isPremium() ? updaterConfig.getString("updater-token") : null, dataFolder.getParentFile(), "ajQueue update");
}

private void constructMessages() {
Expand Down Expand Up @@ -355,8 +369,17 @@ private void constructMessages() {
);
d.put("updater.no-update", "<red>There is not an update available");
d.put("updater.success", "<green>The update has been downloaded! Now just restart the server");
d.put("updater.fail", "<red>An error occurred while downloading the update. Check the console for more info.");
d.put("updater.already-downloaded", "<red>The update has already been downloaded.");
d.put("updater.already-downloaded", "&aYou have already downloaded an update! &7Restart the server to apply it");
d.put("updater.slow-feedback", "&7Checking for update and downloading...");
d.put("updater.disabled", "&cThe updater is disabled! &7Please enable it in the config to download updates.");
d.put("updater.warnings.could-not-delete-old-jar", "&aUpdate downloaded&e but the old jar could not be deleted. &7Please delete the old jar before restarting the server.");
d.put("updater.errors.while-checking", "&eAn error occurred while checking for an update. &7See the console for more info.");
d.put("updater.errors.unknown", "&eAn unknown error occurred: {ERROR}");
d.put("updater.errors.could-not-find-jar", "&cCould not find the old jar!&7 Make sure it is named similar to ajQueue-x.x.x.jar");
d.put("updater.errors.while-downloading", "&eAn error occurred while downloading an update. &7See the console for more info.");
d.put("updater.errors.missing-update-token", "&cMissing update token! &7See the&f updater-config.yml&f file for more info.");
d.put("updater.errors.invalid-update-token", "&cInvalid update token! &7See the&f updater-config.yml&f file for more info. If you are lost, please contact support.");
d.put("updater.errors.uncaught", "&cAn error occurred while executing this command. &7See the console.");

List<String> oldProtocolNames = config.getStringList("protocol-names");
for (String oldProtocolName : oldProtocolNames) {
Expand Down
13 changes: 13 additions & 0 deletions common/src/main/resources/updater-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# This is the config for the updater. It was created so that the updater secret would be in a separate file from the config

# The Updater Token is used for updating my premium plugins.
# If you have ajQueuePlus, generate one here: https://verify.ajg0702.us/updaterToken
# If you have ajQueue, this is not needed.
updater-token: ""


# If you want to disable the updater, you can do that in the normal config.yml


# Don't touch this
config-version: 1
2 changes: 1 addition & 1 deletion free/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies {
compileOnly("com.google.guava:guava:30.1.1-jre")
compileOnly("org.spongepowered:configurate-yaml:4.0.0")

implementation("us.ajg0702:ajUtils:1.2.25")
implementation("us.ajg0702:ajUtils:1.2.28")

implementation(project(":platforms:velocity"))
implementation(project(":platforms:bungeecord"))
Expand Down
2 changes: 1 addition & 1 deletion platforms/bungeecord/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ repositories {
dependencies {
compileOnly("net.kyori:adventure-api:4.15.0")
compileOnly("com.google.guava:guava:30.1.1-jre")
compileOnly("us.ajg0702:ajUtils:1.2.25")
compileOnly("us.ajg0702:ajUtils:1.2.28")

compileOnly("net.md-5:bungeecord-api:1.16-R0.4")

Expand Down
2 changes: 1 addition & 1 deletion platforms/velocity/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ repositories {
dependencies {
compileOnly("net.kyori:adventure-api:4.15.0")
compileOnly("com.google.guava:guava:30.1.1-jre")
compileOnly("us.ajg0702:ajUtils:1.2.25")
compileOnly("us.ajg0702:ajUtils:1.2.28")

compileOnly("com.velocitypowered:velocity-api:3.1.1")
annotationProcessor("com.velocitypowered:velocity-api:3.1.1")
Expand Down
2 changes: 1 addition & 1 deletion premium/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ dependencies {

compileOnly("me.TechsCode:FakeUltraPerms:1.0.2")

compileOnly("us.ajg0702:ajUtils:1.2.25")
compileOnly("us.ajg0702:ajUtils:1.2.28")

compileOnly("net.kyori:adventure-api:4.15.0")

Expand Down
2 changes: 1 addition & 1 deletion spigot/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dependencies {

compileOnly("org.spongepowered:configurate-yaml:4.1.2")

compileOnly("us.ajg0702:ajUtils:1.2.25")
compileOnly("us.ajg0702:ajUtils:1.2.28")

compileOnly(group = "org.spigotmc", name = "spigot", version = "1.16.5-R0.1-SNAPSHOT")
compileOnly("me.clip:placeholderapi:2.10.4")
Expand Down

0 comments on commit 1a90ea0

Please sign in to comment.