VoidPlugin/VoidTeleport/src/main/kotlin/nl/voidcorp/teleportplugin/commands/HomeCommand.kt

43 lines
1.5 KiB
Kotlin

package nl.voidcorp.teleportplugin.commands
import nl.voidcorp.mainplugin.commands.VoidCommand
import nl.voidcorp.teleportplugin.models.Home
import nl.voidcorp.teleportplugin.models.Homes
import org.bukkit.command.Command
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.transactions.transaction
class HomeCommand : VoidCommand() {
override fun onTabComplete(
sender: CommandSender,
command: Command,
alias: String,
args: Array<out String>
): List<String> {
return if (sender is Player) {
val current = args.lastOrNull()
transaction {
Home.find { (Homes.userid eq sender.uniqueId) and (Homes.name like "$current%") }
.map { it.name }.toList()
}
} else mutableListOf()
}
override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean {
return if (sender is Player) {
val where = args.firstOrNull() ?: "home"
transaction {
val res = Home.find { (Homes.userid eq sender.uniqueId) and (Homes.name eq where) }.firstOrNull()
if (res == null) {
sender.sendMessage("The home '$where' does not exist!")
} else {
sender.teleport(res.location)
}
}
true
} else false
}
}