diff --git a/src/main/kotlin/org/trackedout/citadel/ScheduledTaskRunner.kt b/src/main/kotlin/org/trackedout/citadel/ScheduledTaskRunner.kt index 6fd53c4..5864b76 100644 --- a/src/main/kotlin/org/trackedout/citadel/ScheduledTaskRunner.kt +++ b/src/main/kotlin/org/trackedout/citadel/ScheduledTaskRunner.kt @@ -54,6 +54,7 @@ class ScheduledTaskRunner( } "execute-command" -> { + runCommandsOnSubsequentTicks(ArrayDeque(task.arguments!!)) task.arguments?.forEach { plugin.logger.info("Executing command from dunga-dunga: $it") plugin.server.dispatchCommand(plugin.server.consoleSender, it) @@ -91,6 +92,28 @@ class ScheduledTaskRunner( } }.runTask(plugin) } + + private fun runCommandsOnSubsequentTicks(commands: ArrayDeque) { + object : BukkitRunnable() { + override fun run() { + if (commands.isEmpty()) { + return + } + + val command = commands.removeFirst() + try { + plugin.logger.info("Running command: $command") + plugin.server.dispatchCommand(plugin.server.consoleSender, command) + plugin.logger.info("Successfully executed command: $command") + + runCommandsOnSubsequentTicks(commands) + } catch (e: Exception) { + plugin.logger.severe("Failed to execute command: $command") + e.printStackTrace() + } + } + }.runTask(plugin) + } } fun Task.updateState(api: TasksApi, state: String) {