Skip to content

Commit

Permalink
added cooldown example
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-MX committed May 6, 2024
1 parent 864a2c3 commit f4e5479
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ open class SimpleCommandBuilder(
private var suggests: (CommandInvocation.() -> List<String>?)? = null
private var execute: (CommandInvocation.() -> Unit)? = null
private var unknown: (CommandInvocation.() -> Unit)? = null
private val cooldown: Optional<ActionCoolDown<CommandSender>> = Optional.empty()
private var cooldown: Optional<ActionCoolDown<CommandSender>> = Optional.empty()
private var cooldownCallback: (CommandInvocation.() -> Unit)? = null
var noPermissions: (CommandInvocation.() -> Unit)? = null
private set
Expand Down Expand Up @@ -65,6 +65,10 @@ open class SimpleCommandBuilder(
return this
}

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

fun cooldownCallback(invocation: CommandInvocation) {
this.cooldownCallback?.invoke(invocation)
}
Expand Down Expand Up @@ -192,6 +196,9 @@ class CommandInvocation(
val alias: String,
val coolDownExpires: Date? = null
) {
val player: Player
get() = source as Player

fun player() : Player {
return source as Player
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package com.mattmx.ktgui.guiconfig

import com.mattmx.ktgui.utils.InstancePackageClassCache
import org.bukkit.configuration.file.FileConfiguration
import org.bukkit.plugin.java.JavaPlugin

class GuiConfigManager {
val cache = InstancePackageClassCache<FileConfiguration>()

inline fun <reified T : Any> T.setConfigFile(file: FileConfiguration) {
inline fun <reified T : JavaPlugin> setConfigFile(file: FileConfiguration) {
cache.cacheInstance(T::class.java, file)
}

inline fun <reified T : Any> T.getConfigFile() =
inline fun <reified T : Any> getConfigFile() =
cache.getInstance(T::class.java)

fun <T : Any> getConfigFile(clazz: Class<T>) =
Expand Down
4 changes: 2 additions & 2 deletions api/src/main/kotlin/com/mattmx/ktgui/guiconfig/util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import org.bukkit.configuration.file.FileConfiguration
import org.bukkit.event.inventory.InventoryType

inline fun <reified T : Any> T.configGui(path: String, block: GuiConfigScreen.() -> Unit): GuiConfigScreen {
val config = GuiManager.guiConfigManager.getConfigFile(T::class.java)
val config = GuiManager.guiConfigManager.getConfigFile<T>()
return configGui(config, path, block)
}

Expand All @@ -36,7 +36,7 @@ inline fun <T : GuiButton<T>, reified V : Any> V.configButton(
path: String,
block: GuiButton<T>.() -> Unit
): T {
val config = GuiManager.guiConfigManager.getConfigFile(V::class.java)
val config = GuiManager.guiConfigManager.getConfigFile<V>()
return configButton(config, path, block)
}

Expand Down
26 changes: 21 additions & 5 deletions plugin/src/main/kotlin/com/mattmx/ktgui/KotlinGui.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.mattmx.ktgui

import com.mattmx.ktgui.commands.rawCommand
import com.mattmx.ktgui.commands.simpleCommand
import com.mattmx.ktgui.examples.*
import com.mattmx.ktgui.utils.not
import org.bukkit.Bukkit
import org.bukkit.plugin.java.JavaPlugin
import java.time.Duration
import java.util.logging.Logger

class KotlinGui : JavaPlugin() {
Expand All @@ -18,6 +20,8 @@ class KotlinGui : JavaPlugin() {
GuiManager.init(this)
saveDefaultConfig()

GuiManager.guiConfigManager.setConfigFile<KotlinGui>(config)

val mainColor = "&#7F52FF"
val subColor = "&#E24462"

Expand All @@ -44,8 +48,7 @@ class KotlinGui : JavaPlugin() {
)
GuiHookExample.registerListener(this)

simpleCommand {
name = "ktgui"
rawCommand("ktgui") {
permission = "ktgui.command"
playerOnly = true
suggestSubCommands = true
Expand All @@ -54,8 +57,7 @@ class KotlinGui : JavaPlugin() {
source.sendMessage(!"${mainColor}You are running ${subColor}KtGUI v${pluginMeta.version}")
}

subCommands += simpleCommand {
name = "example"
subCommands += rawCommand("example") {
permission = "ktgui.command.example"
playerOnly = true

Expand All @@ -64,14 +66,28 @@ class KotlinGui : JavaPlugin() {
?: return@executes source.sendMessage(!"${mainColor}Please provide a valid example id.")

val example = examples[exampleId]
?:return@executes source.sendMessage(!"${mainColor}Please provide a valid example id.")
?: return@executes source.sendMessage(!"${mainColor}Please provide a valid example id.")

example().run(player())
}
suggests {
examples.keys.filter { ex -> ex.startsWith(it.lastArg) }
}
}

subCommands += rawCommand("cooldown-example") {
permission = "ktgui.command.cooldown-example"
playerOnly = true
cooldown(Duration.ofSeconds(2))

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

onCooldown {
player.sendMessage(!"&cPlease wait before doing that again.")
}
}
}.register(false)
}

Expand Down

0 comments on commit f4e5479

Please sign in to comment.