Skip to content

Commit

Permalink
v2.2.4:
Browse files Browse the repository at this point in the history
- Fixed workaround for SpongePowered/Sponge#1922, the commands got out of order
  • Loading branch information
randombyte-developer committed Jun 27, 2018
1 parent 359e231 commit 272705c
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 19 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if (!isJitpack) {
}

group "de.randombyte"
version "2.2.3"
version "2.2.4"

repositories {
jcenter()
Expand Down
7 changes: 3 additions & 4 deletions src/main/kotlin/de/randombyte/commandutils/CommandUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import de.randombyte.commandutils.executeonserverstartup.ServerStartupListener
import de.randombyte.commandutils.service.CommandUtilsService
import de.randombyte.commandutils.service.CommandUtilsServiceImpl
import de.randombyte.kosp.extensions.toText
import org.bstats.sponge.Metrics
import org.slf4j.Logger
import org.spongepowered.api.Sponge
import org.spongepowered.api.command.args.GenericArguments.*
Expand All @@ -42,13 +41,13 @@ import java.nio.file.Path
dependencies = [(Dependency(id = PLACEHOLDER_API_ID, optional = true))])
class CommandUtils @Inject constructor(
val logger: Logger,
@ConfigDir(sharedRoot = false) configPath: Path,
val bStats: Metrics
@ConfigDir(sharedRoot = false) configPath: Path
//val bStats: Metrics
) {
companion object {
const val ID = "command-utils"
const val NAME = "CommandUtils"
const val VERSION = "2.2.3"
const val VERSION = "2.2.4"
const val AUTHOR = "RandomByte"

const val PLACEHOLDER_API_ID = "placeholderapi"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class CommandListener {
configAccessor.lastAliasExecutions.save(newLastExecutionsConfig)
}

aliasConfig.commands.forEach { command ->
aliasConfig.commands.forEachIndexed { index, command ->
val replacements = if (commandSource is Player) {
// adding the legacy '$p'
arguments + Pair("\$p", commandSource.name)
Expand All @@ -67,7 +67,8 @@ class CommandListener {
executeCommand(
command = command,
commandSource = commandSource,
replacements = replacements
replacements = replacements,
commandIndex = index
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class DelayCommand : CommandExecutor {
.delay(delayMilliseconds, TimeUnit.MILLISECONDS)
.execute { -> executeCommand(
command = command,
commandSource = src
commandSource = src,
commandIndex = 0
) }
.submit(CommandUtils.INSTANCE)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class IfCommand(val inverted: Boolean) : CommandExecutor {

executeCommand(
command = conditionCommand,
commandSource = src
commandSource = src,
commandIndex = 0
) { commandResult -> // workaround for https://github.com/SpongePowered/SpongeCommon/issues/1922
var truthy = commandResult.isTruthy()

Expand All @@ -24,7 +25,8 @@ class IfCommand(val inverted: Boolean) : CommandExecutor {
if (truthy) {
commands.forEach { executeCommand(
command = it,
commandSource = src
commandSource = src,
commandIndex = 0
) }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class ParseCommand : CommandExecutor {
executeCommand(
command = command,
commandSource = src,
target = target
target = target,
commandIndex = 0
)

return CommandResult.success()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ class PlayerJoinListener {

val currentConfig = commandUtils.configAccessor.executeWhenOnline.get()

currentConfig.commands[playerUuid]?.forEach { command ->
currentConfig.commands[playerUuid]?.forEachIndexed { index, command ->
executeCommand(
command = command,
commandSource = player,
replacements = mapOf("\$p" to player.name)
replacements = mapOf("\$p" to player.name),
commandIndex = index
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ class ServerStartupListener {
.delayTicks(1)
.execute { ->
CommandUtils.INSTANCE.configAccessor.executeOnServerStartup.get().commands
.forEach { executeCommand(
command = it,
commandSource = Sponge.getServer().console
.forEachIndexed { index, command -> executeCommand(
command = command,
commandSource = Sponge.getServer().console,
commandIndex = index
) }
}
.submit(CommandUtils.INSTANCE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class CommandUtilsServiceImpl(val configAccessor: ConfigAccessor) : CommandUtils
executeCommand(
command = newCommand,
commandSource = player,
replacements = mapOf("\$p" to player.name)
replacements = mapOf("\$p" to player.name),
commandIndex = 0
)
return
}
Expand Down
11 changes: 9 additions & 2 deletions src/main/kotlin/de/randombyte/commandutils/utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@ import org.spongepowered.api.scheduler.Task

private const val EXECUTE_AS_CONSOLE_PREFIX = "*"

/**
* When working around https://github.com/SpongePowered/SpongeCommon/issues/1922 you have to keep
* the order of execution in mind. [commandIndex] must be incremented for each command that should
* keep its order.
*/
fun executeCommand(
command: String,
commandSource: CommandSource,
replacements: Map<String, String> = emptyMap(),
target: Any = commandSource,
commandResultCallback: (CommandResult) -> Unit = { }) {
commandIndex: Int,
commandResultCallback: (CommandResult) -> Unit = { }
) {

val unprefixedCommand = command.removePrefix(EXECUTE_AS_CONSOLE_PREFIX)
val executeAsConsole = command.startsWith(EXECUTE_AS_CONSOLE_PREFIX)
Expand Down Expand Up @@ -43,6 +50,6 @@ fun executeCommand(
val commandResult = finalCommandSource.executeCommand(processedCommand)
commandResultCallback(commandResult)
}
.delayTicks(1)
.delayTicks(commandIndex.toLong())
.submit(CommandUtils.INSTANCE)
}

0 comments on commit 272705c

Please sign in to comment.