@@ -3,48 +3,69 @@ package de.randombyte.commandutils.alias
3
3
import de.randombyte.commandutils.ConfigAccessor
4
4
import de.randombyte.commandutils.execute
5
5
import de.randombyte.commandutils.executeForPlayer
6
+ import de.randombyte.kosp.config.serializers.duration.SimpleDurationTypeSerializer
7
+ import de.randombyte.kosp.extensions.red
6
8
import de.randombyte.kosp.extensions.toText
7
9
import org.slf4j.Logger
8
10
import org.spongepowered.api.command.CommandSource
9
11
import org.spongepowered.api.entity.living.player.Player
10
12
import org.spongepowered.api.event.Listener
11
13
import org.spongepowered.api.event.command.SendCommandEvent
12
14
import org.spongepowered.api.event.filter.cause.First
15
+ import java.time.Instant
16
+ import java.util.*
13
17
14
18
class CommandListener (
15
19
val logger : Logger ,
16
20
val configAccessor : ConfigAccessor
17
21
) {
18
22
@Listener
19
23
fun onCommand (event : SendCommandEvent , @First commandSource : CommandSource ) {
24
+ val config = configAccessor.get()
25
+
20
26
val wholeCommand = event.wholeCommand
21
- val matchedAliasedMap = configAccessor.get() .alias.aliases.mapNotNull { (alias, aliasConfig) ->
27
+ val matchedAliasedMap = config .alias.aliases.mapNotNull { (alias, aliasConfig) ->
22
28
(alias to aliasConfig) to (AliasParser .parse(alias, wholeCommand) ? : return @mapNotNull null )
23
29
}.toList()
24
30
25
31
if (matchedAliasedMap.isEmpty()) return // doesn't match any of our aliases
26
32
33
+ event.isCancelled = true
34
+
27
35
if (matchedAliasedMap.size > 1 ) {
28
36
val matchedAliasesString = matchedAliasedMap.joinToString(separator = " , " , prefix = " [" , postfix = " ]" , transform = { " '${it.first.first} '" })
29
37
logger.error(" More than one alias matched! command: '$wholeCommand '; matched aliases: $matchedAliasesString " )
30
38
throw IllegalArgumentException (" More than one alias matched, report to admin!" )
31
39
}
32
40
33
41
val (aliasEntry, arguments) = matchedAliasedMap.single()
34
- val (_ , aliasConfig) = aliasEntry
42
+ val (alias , aliasConfig) = aliasEntry
35
43
if (! commandSource.hasPermission(aliasConfig.permission)) {
36
44
commandSource.sendMessage(" You don't have the permission to execute this command!" .toText())
37
45
return
38
46
}
39
47
48
+ if (aliasConfig.cooldown != null && commandSource is Player ) {
49
+ val lastExecution = config.lastAliasExecutionsConfig.get(commandSource.uniqueId, alias)
50
+ if (lastExecution != null ) {
51
+ val remainingCooldown = config.lastAliasExecutionsConfig.remainingCooldown(lastExecution, aliasConfig.cooldown)
52
+ if (! remainingCooldown.isNegative) {
53
+ val cooldownString = SimpleDurationTypeSerializer .serialize(remainingCooldown, outputMilliseconds = false )
54
+ commandSource.sendMessage(" The cooldown for this command is still up! Wait another $cooldownString ." .red())
55
+ return
56
+ }
57
+ }
58
+
59
+ val newLastExecutionsConfig = config.lastAliasExecutionsConfig.add(commandSource.uniqueId, alias, Date .from(Instant .now()))
60
+ configAccessor.save(config.copy(lastAliasExecutionsConfig = newLastExecutionsConfig))
61
+ }
62
+
40
63
aliasConfig.commands.forEach {
41
64
var modifiedWholeCommand = it
42
65
arguments.forEach { (parameter, argument) -> modifiedWholeCommand = modifiedWholeCommand.replace(parameter, argument) }
43
66
if (commandSource is Player ) executeForPlayer(modifiedWholeCommand, commandSource)
44
67
else execute(modifiedWholeCommand, commandSource)
45
68
}
46
-
47
- event.isCancelled = true
48
69
}
49
70
50
71
private val SendCommandEvent .wholeCommand: String
0 commit comments