Add short gamemode commands.

This commit is contained in:
Logan Gorence 2021-12-22 04:49:36 +00:00
parent 69380906e6
commit 728904fa23
No known key found for this signature in database
GPG Key ID: 9743CEF10935949A
5 changed files with 65 additions and 8 deletions

View File

@ -8,8 +8,3 @@ depend:
- Foundation
authors:
- kubelet
commands:
fbackup:
description: Foundation Backup
usage: /fbackup
permission: foundation.backup

View File

@ -1,7 +1,9 @@
package cloud.kubelet.foundation.core
import cloud.kubelet.foundation.core.command.BackupCommand
import cloud.kubelet.foundation.core.command.GamemodeCommand
import net.kyori.adventure.text.Component
import org.bukkit.GameMode
import org.bukkit.command.CommandExecutor
import org.bukkit.event.Listener
import org.bukkit.plugin.java.JavaPlugin
@ -38,6 +40,10 @@ class FoundationCorePlugin : JavaPlugin(), Listener {
// Register commands.
registerCommandExecutor("fbackup", BackupCommand(this, backupPath))
registerCommandExecutor(listOf("survival", "s"), GamemodeCommand(GameMode.SURVIVAL))
registerCommandExecutor(listOf("creative", "c"), GamemodeCommand(GameMode.CREATIVE))
registerCommandExecutor(listOf("adventure", "a"), GamemodeCommand(GameMode.ADVENTURE))
registerCommandExecutor(listOf("spectator", "sp"), GamemodeCommand(GameMode.SPECTATOR))
val log = slF4JLogger
log.info("Features:")
@ -45,8 +51,14 @@ class FoundationCorePlugin : JavaPlugin(), Listener {
}
private fun registerCommandExecutor(name: String, executor: CommandExecutor) {
val command = getCommand(name) ?: throw Exception("Failed to get $name command")
command.setExecutor(executor)
registerCommandExecutor(listOf(name), executor)
}
private fun registerCommandExecutor(names: List<String>, executor: CommandExecutor) {
for (name in names) {
val command = getCommand(name) ?: throw Exception("Failed to get $name command")
command.setExecutor(executor)
}
}
// TODO: Disabling chat reformatting until I do something with it and figure out how to make it

View File

@ -91,7 +91,7 @@ class BackupCommand(
for (world in worlds) {
val worldPath = world.worldFolder.toPath()
// Save the world.
// Save the world, must be run on the main thread.
server.scheduler.runTask(plugin, Runnable {
world.save()
})

View File

@ -0,0 +1,26 @@
package cloud.kubelet.foundation.core.command
import org.bukkit.GameMode
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
class GamemodeCommand(private val gameMode: GameMode) : CommandExecutor {
override fun onCommand(
sender: CommandSender,
command: Command,
label: String,
args: Array<out String>
): Boolean {
if (sender !is Player) {
sender.sendMessage("You are not a player.")
return true
}
sender.gameMode = gameMode
sender.sendMessage("Switched gamemode to ${gameMode.name.lowercase()}")
return true
}
}

View File

@ -11,3 +11,27 @@ commands:
description: Foundation Backup
usage: /fbackup
permission: foundation.backup
survival:
description: Switch to survival gamemode
usage: /survival
aliases:
- s
permission: foundation.command.survival
creative:
description: Switch to creative gamemode
usage: /creative
aliases:
- c
permission: foundation.command.creative
adventure:
description: Switch to adventure gamemode
usage: /adventure
aliases:
- a
permission: foundation.command.adventure
spectator:
description: Switch to spectator gamemode
usage: /spectator
aliases:
- sp
permission: foundation.command.spectator