Skip to content

Commit

Permalink
run async tasks together
Browse files Browse the repository at this point in the history
  • Loading branch information
KurtThiemann committed Jul 30, 2021
1 parent d66f6ea commit 392bf0e
Showing 1 changed file with 31 additions and 31 deletions.
62 changes: 31 additions & 31 deletions src/main/java/com/exaroton/bungee/ExarotonPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ public void onEnable() {
}
if (this.createExarotonClient()) {
this.registerCommands();
this.startWatchingServers();
this.autoStartServers();
this.runAsyncTasks();
}
}

Expand Down Expand Up @@ -259,10 +258,13 @@ public void listenToStatus(Server server, CommandSender sender, String name) {
/**
* start watching servers in the bungee config
*/
public void startWatchingServers() {
if (config.getBoolean("watch-servers")) {
this.getProxy().getScheduler().runAsync(this, this::watchServers);
}
public void runAsyncTasks() {
this.getProxy().getScheduler().runAsync(this, () -> {
if(config.getBoolean("watch-servers")) {
this.watchServers();
}
this.autoStartServers();
});
}

/**
Expand Down Expand Up @@ -299,37 +301,35 @@ public void watchServers() {
*/
public void autoStartServers() {
if (!config.getBoolean("auto-start.enabled")) return;
this.getProxy().getScheduler().runAsync(this, () -> {
for (String query: config.getStringList("auto-start.servers")) {
try {
Server server = this.findServer(query);

if (server == null) {
logger.log(Level.WARNING, "Can't start " + query + ": Server not found");
continue;
}

if (server.hasStatus(new int[]{ServerStatus.ONLINE, ServerStatus.STARTING,
ServerStatus.LOADING, ServerStatus.PREPARING, ServerStatus.RESTARTING})) {
logger.log(Level.INFO, server.getAddress() + " is already online or starting!");
this.listenToStatus(server, null, findServerName(server.getAddress()));
return;
}
for (String query: config.getStringList("auto-start.servers")) {
try {
Server server = this.findServer(query);

if (!server.hasStatus(new int[]{ServerStatus.OFFLINE, ServerStatus.CRASHED})) {
logger.log(Level.SEVERE, "Can't start " + server.getAddress() + ": Server isn't offline.");
continue;
}
if (server == null) {
logger.log(Level.WARNING, "Can't start " + query + ": Server not found");
continue;
}

logger.log(Level.INFO, "Starting "+ server.getAddress());
if (server.hasStatus(new int[]{ServerStatus.ONLINE, ServerStatus.STARTING,
ServerStatus.LOADING, ServerStatus.PREPARING, ServerStatus.RESTARTING})) {
logger.log(Level.INFO, server.getAddress() + " is already online or starting!");
this.listenToStatus(server, null, findServerName(server.getAddress()));
server.start();
return;
}

} catch (APIException e) {
logger.log(Level.SEVERE, "Failed to start start "+ query +"!", e);
if (!server.hasStatus(new int[]{ServerStatus.OFFLINE, ServerStatus.CRASHED})) {
logger.log(Level.SEVERE, "Can't start " + server.getAddress() + ": Server isn't offline.");
continue;
}

logger.log(Level.INFO, "Starting "+ server.getAddress());
this.listenToStatus(server, null, findServerName(server.getAddress()));
server.start();

} catch (APIException e) {
logger.log(Level.SEVERE, "Failed to start start "+ query +"!", e);
}
});
}
}

/**
Expand Down

0 comments on commit 392bf0e

Please sign in to comment.