Skip to content

Commit

Permalink
cleanup gamemode
Browse files Browse the repository at this point in the history
  • Loading branch information
Miles Holder committed May 10, 2024
1 parent 4f97b22 commit a3a5f1d
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 106 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
package com.github.spigotbasics.modules.basicsgamemode

import com.github.spigotbasics.core.command.parsed.CommandContextExecutor
import com.github.spigotbasics.core.command.parsed.arguments.SelectorSinglePlayerArg
import com.github.spigotbasics.core.command.parsed.context.MapContext
import com.github.spigotbasics.core.messages.Message
import com.github.spigotbasics.core.module.AbstractBasicsModule
import com.github.spigotbasics.core.module.loader.ModuleInstantiationContext
import org.bukkit.GameMode
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import org.bukkit.permissions.Permission

class BasicsGamemodeModule(context: ModuleInstantiationContext) : AbstractBasicsModule(context) {
val msgChangedOthers get() = messages.getMessage("gamemode-changed-others")
val msgChangedSelf get() = messages.getMessage("gamemode-changed-self")
val nameSurvival get() = messages.getMessage("survival")
val nameCreative get() = messages.getMessage("creative")
val nameAdventure get() = messages.getMessage("adventure")
val nameSpectator get() = messages.getMessage("spectator")

val perm = permissionManager.createSimplePermission("basics.gamemode", "Allows the player to change their game mode")
private val msgChangedOthers get() = messages.getMessage("gamemode-changed-others")
private val msgChangedSelf get() = messages.getMessage("gamemode-changed-self")
private val nameSurvival get() = messages.getMessage("survival")
private val nameCreative get() = messages.getMessage("creative")
private val nameAdventure get() = messages.getMessage("adventure")
private val nameSpectator get() = messages.getMessage("spectator")

private val perm =
permissionManager.createSimplePermission("basics.gamemode", "Allows the player to change their game mode")
val permSurvival =
permissionManager.createSimplePermission(
"basics.gamemode.survival",
Expand All @@ -37,28 +42,52 @@ class BasicsGamemodeModule(context: ModuleInstantiationContext) : AbstractBasics
"Allows the player to change their game mode to spectator",
)

val permOthers =
private val permOthers =
permissionManager.createSimplePermission(
"basics.gamemode.others",
"Allows the player to change other players' game modes",
)

override fun onEnable() {
commandFactory.rawCommandBuilder("gamemode", perm)
.description("Changes the player's game mode")
.usage("<mode> [player]")
.executor(GamemodeExecutor(this))
.register()
}
val instance = this
commandFactory.parsedCommandBuilder("gamemode", perm).mapContext {
description("Changes the player's game mode")
usage = "<mode> [player]"
path {
playerOnly()
arguments {
named("gamemode", GameModeArgument(instance, "gamemode"))
}

fun toGameMode(input: String): GameMode? {
return when (input) {
"survival", "s", "0" -> GameMode.SURVIVAL
"creative", "c", "1" -> GameMode.CREATIVE
"adventure", "a", "2" -> GameMode.ADVENTURE
"spectator", "sp", "3" -> GameMode.SPECTATOR
else -> null
}
arguments {
permissions(permOthers)
named("gamemode", GameModeArgument(instance, "gamemode"))
named("target", SelectorSinglePlayerArg("target"))
}
}
}.executor(
object : CommandContextExecutor<MapContext> {
override fun execute(
sender: CommandSender,
context: MapContext,
) {
val gameMode = context["gamemode"] as GameMode
val target = if (context["target"] == null) sender as Player else context["target"] as Player

val permission = getPermission(gameMode)
if (!sender.hasPermission(permission)) {
coreMessages.noPermission(permission).concerns(target).sendToSender(sender)
return
}

target.gameMode = gameMode
val message = if (target == sender) msgChangedSelf else msgChangedOthers
message.tagMessage("new-gamemode", getName(gameMode))
.concerns(target)
.sendToSender(sender)
}
},
).register()
}

fun getPermission(gameMode: GameMode): Permission {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.github.spigotbasics.modules.basicsgamemode

import com.github.spigotbasics.core.command.parsed.arguments.CommandArgument
import org.bukkit.GameMode
import org.bukkit.command.CommandSender

class GameModeArgument(private val module: BasicsGamemodeModule, name: String) : CommandArgument<GameMode>(name) {
override fun parse(
sender: CommandSender,
value: String,
): GameMode? {
return when (value) {
"survival", "s", "0" -> GameMode.SURVIVAL
"creative", "c", "1" -> GameMode.CREATIVE
"adventure", "a", "2" -> GameMode.ADVENTURE
"spectator", "sp", "3" -> GameMode.SPECTATOR
else -> null
}
}

override fun tabComplete(
sender: CommandSender,
typing: String,
): List<String> {
val list = mutableListOf<String>()

if (sender.hasPermission(module.permSurvival)) {
list += "survival"
}
if (sender.hasPermission(module.permCreative)) {
list += "creative"
}
if (sender.hasPermission(module.permAdventure)) {
list += "adventure"
}
if (sender.hasPermission(module.permSpectator)) {
list += "spectator"
}

return list
}
}

This file was deleted.

0 comments on commit a3a5f1d

Please sign in to comment.