From 728904fa23ba6f0b58e2b02411221224ebd35e72 Mon Sep 17 00:00:00 2001 From: Logan Gorence Date: Wed, 22 Dec 2021 04:49:36 +0000 Subject: [PATCH] Add short gamemode commands. --- .../src/main/resources/plugin.yml | 5 ---- .../foundation/core/FoundationCorePlugin.kt | 16 ++++++++++-- .../foundation/core/command/BackupCommand.kt | 2 +- .../core/command/GamemodeCommand.kt | 26 +++++++++++++++++++ foundation-core/src/main/resources/plugin.yml | 24 +++++++++++++++++ 5 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/command/GamemodeCommand.kt diff --git a/foundation-bifrost/src/main/resources/plugin.yml b/foundation-bifrost/src/main/resources/plugin.yml index e3b99ee..6edcdab 100644 --- a/foundation-bifrost/src/main/resources/plugin.yml +++ b/foundation-bifrost/src/main/resources/plugin.yml @@ -8,8 +8,3 @@ depend: - Foundation authors: - kubelet -commands: - fbackup: - description: Foundation Backup - usage: /fbackup - permission: foundation.backup diff --git a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/FoundationCorePlugin.kt b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/FoundationCorePlugin.kt index 7dda33f..6eb1bc3 100644 --- a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/FoundationCorePlugin.kt +++ b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/FoundationCorePlugin.kt @@ -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, 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 diff --git a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/command/BackupCommand.kt b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/command/BackupCommand.kt index c015855..4420b96 100644 --- a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/command/BackupCommand.kt +++ b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/command/BackupCommand.kt @@ -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() }) diff --git a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/command/GamemodeCommand.kt b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/command/GamemodeCommand.kt new file mode 100644 index 0000000..7cf57d1 --- /dev/null +++ b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/command/GamemodeCommand.kt @@ -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 + ): 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 + } +} diff --git a/foundation-core/src/main/resources/plugin.yml b/foundation-core/src/main/resources/plugin.yml index 466754e..bd633e3 100644 --- a/foundation-core/src/main/resources/plugin.yml +++ b/foundation-core/src/main/resources/plugin.yml @@ -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