Skip to content

Commit

Permalink
cooldown example fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-MX committed May 6, 2024
1 parent f4e5479 commit a418094
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ open class SimpleCommandBuilder(
}

fun cooldown(duration: Duration?) = apply {
cooldown.ifPresent {
ActionCoolDown.unregister(it)
}
cooldown = Optional.ofNullable(duration?.let { ActionCoolDown(duration) })
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class ActionCoolDown<T>(
registeredCoolDowns.add(coolDown)
}

fun unregister(coolDown: ActionCoolDown<*>) {
registeredCoolDowns.remove(coolDown)
}

fun removeUsers(vararg user: Any) {
registeredCoolDowns.forEach { it.removeAnyUsers(*user) }
}
Expand Down
12 changes: 11 additions & 1 deletion plugin/src/main/kotlin/com/mattmx/ktgui/KotlinGui.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.mattmx.ktgui.commands.rawCommand
import com.mattmx.ktgui.commands.simpleCommand
import com.mattmx.ktgui.examples.*
import com.mattmx.ktgui.utils.not
import com.mattmx.ktgui.utils.pretty
import org.bukkit.Bukkit
import org.bukkit.plugin.java.JavaPlugin
import java.time.Duration
Expand Down Expand Up @@ -81,7 +82,16 @@ class KotlinGui : JavaPlugin() {
cooldown(Duration.ofSeconds(2))

executes {
player.sendMessage(!"&aNot on cool-down!")
source.sendMessage(!"&aNot on cool-down!")

if (args.isNotEmpty()) {
val newCoolDown = args.first().toLongOrNull()
?: return@executes

val dur = Duration.ofMillis(newCoolDown)
source.sendMessage(!"&aNew cool-down set: ${dur.pretty()}")
cooldown(dur)
}
}

onCooldown {
Expand Down

0 comments on commit a418094

Please sign in to comment.