mirror of
				https://github.com/GayPizzaSpecifications/foundation.git
				synced 2025-11-04 11:39:39 +00:00 
			
		
		
		
	Add short gamemode commands.
This commit is contained in:
		@ -8,8 +8,3 @@ depend:
 | 
				
			|||||||
  - Foundation
 | 
					  - Foundation
 | 
				
			||||||
authors:
 | 
					authors:
 | 
				
			||||||
  - kubelet
 | 
					  - kubelet
 | 
				
			||||||
commands:
 | 
					 | 
				
			||||||
  fbackup:
 | 
					 | 
				
			||||||
    description: Foundation Backup
 | 
					 | 
				
			||||||
    usage: /fbackup
 | 
					 | 
				
			||||||
    permission: foundation.backup
 | 
					 | 
				
			||||||
 | 
				
			|||||||
@ -1,7 +1,9 @@
 | 
				
			|||||||
package cloud.kubelet.foundation.core
 | 
					package cloud.kubelet.foundation.core
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import cloud.kubelet.foundation.core.command.BackupCommand
 | 
					import cloud.kubelet.foundation.core.command.BackupCommand
 | 
				
			||||||
 | 
					import cloud.kubelet.foundation.core.command.GamemodeCommand
 | 
				
			||||||
import net.kyori.adventure.text.Component
 | 
					import net.kyori.adventure.text.Component
 | 
				
			||||||
 | 
					import org.bukkit.GameMode
 | 
				
			||||||
import org.bukkit.command.CommandExecutor
 | 
					import org.bukkit.command.CommandExecutor
 | 
				
			||||||
import org.bukkit.event.Listener
 | 
					import org.bukkit.event.Listener
 | 
				
			||||||
import org.bukkit.plugin.java.JavaPlugin
 | 
					import org.bukkit.plugin.java.JavaPlugin
 | 
				
			||||||
@ -38,6 +40,10 @@ class FoundationCorePlugin : JavaPlugin(), Listener {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    // Register commands.
 | 
					    // Register commands.
 | 
				
			||||||
    registerCommandExecutor("fbackup", BackupCommand(this, backupPath))
 | 
					    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
 | 
					    val log = slF4JLogger
 | 
				
			||||||
    log.info("Features:")
 | 
					    log.info("Features:")
 | 
				
			||||||
@ -45,8 +51,14 @@ class FoundationCorePlugin : JavaPlugin(), Listener {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private fun registerCommandExecutor(name: String, executor: CommandExecutor) {
 | 
					  private fun registerCommandExecutor(name: String, executor: CommandExecutor) {
 | 
				
			||||||
    val command = getCommand(name) ?: throw Exception("Failed to get $name command")
 | 
					    registerCommandExecutor(listOf(name), executor)
 | 
				
			||||||
    command.setExecutor(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
 | 
					  // TODO: Disabling chat reformatting until I do something with it and figure out how to make it
 | 
				
			||||||
 | 
				
			|||||||
@ -91,7 +91,7 @@ class BackupCommand(
 | 
				
			|||||||
    for (world in worlds) {
 | 
					    for (world in worlds) {
 | 
				
			||||||
      val worldPath = world.worldFolder.toPath()
 | 
					      val worldPath = world.worldFolder.toPath()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      // Save the world.
 | 
					      // Save the world, must be run on the main thread.
 | 
				
			||||||
      server.scheduler.runTask(plugin, Runnable {
 | 
					      server.scheduler.runTask(plugin, Runnable {
 | 
				
			||||||
        world.save()
 | 
					        world.save()
 | 
				
			||||||
      })
 | 
					      })
 | 
				
			||||||
 | 
				
			|||||||
@ -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
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -11,3 +11,27 @@ commands:
 | 
				
			|||||||
    description: Foundation Backup
 | 
					    description: Foundation Backup
 | 
				
			||||||
    usage: /fbackup
 | 
					    usage: /fbackup
 | 
				
			||||||
    permission: foundation.backup
 | 
					    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
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user