Skip to content

Commit

Permalink
refactor: change to BrigadierCommands
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Jun 10, 2024
1 parent 9e8c94d commit c4f7863
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 163 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
group=com.mineinabyss
version=1.10
idofrontVersion=0.24.0
version=1.11
idofrontVersion=0.24.6
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[versions]
gearyPaper = "0.30.1"
blocky = "0.9.13-dev.22"
gearyPaper = "0.30.3"
blocky = "0.10.1"

[libraries]
geary-papermc = { module = "com.mineinabyss:geary-papermc", version.ref = "gearyPaper" }
Expand Down
282 changes: 124 additions & 158 deletions src/main/kotlin/com/mineinabyss/bonfire/BonfireCommands.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mineinabyss.bonfire

import com.mineinabyss.blocky.helpers.GenericHelpers.toBlockCenterLocation
import com.mineinabyss.bonfire.components.Bonfire
import com.mineinabyss.bonfire.components.BonfireDebug
import com.mineinabyss.bonfire.components.BonfireRespawn
Expand All @@ -13,6 +14,8 @@ import com.mineinabyss.geary.serialization.setPersisting
import com.mineinabyss.idofront.commands.arguments.intArg
import com.mineinabyss.idofront.commands.arguments.offlinePlayerArg
import com.mineinabyss.idofront.commands.arguments.stringArg
import com.mineinabyss.idofront.commands.brigadier.IdoCommandContext
import com.mineinabyss.idofront.commands.brigadier.commands
import com.mineinabyss.idofront.commands.execution.IdofrontCommandExecutor
import com.mineinabyss.idofront.commands.extensions.actions.playerAction
import com.mineinabyss.idofront.messaging.error
Expand All @@ -21,7 +24,10 @@ import com.mineinabyss.idofront.messaging.success
import com.mineinabyss.idofront.nms.nbt.editOfflinePDC
import com.mineinabyss.idofront.nms.nbt.getOfflinePDC
import com.mineinabyss.idofront.nms.nbt.saveOfflinePDC
import com.mineinabyss.idofront.util.to
import com.mineinabyss.idofront.plugin.actions
import com.mojang.brigadier.arguments.StringArgumentType
import io.papermc.paper.command.brigadier.argument.ArgumentTypes
import org.bukkit.Bukkit
import org.bukkit.Location
import org.bukkit.OfflinePlayer
Expand All @@ -31,184 +37,144 @@ import org.bukkit.command.TabCompleter
import org.bukkit.entity.ItemDisplay
import org.bukkit.entity.Player


class BonfireCommands : IdofrontCommandExecutor(), TabCompleter {
override val commands = commands(bonfire.plugin) {
command("bonfire") {
"debug" {
playerAction {
when {
player.toGeary().has<BonfireDebug>() -> {
player.toGeary().remove<BonfireDebug>()
sender.error("Bonfire debug mode disabled")
}
else -> {
player.toGeary().setPersisting(BonfireDebug())
sender.success("Bonfire debug mode enabled")
object BonfireCommands {
fun registerCommands() {
bonfire.plugin.commands {
"bonfire" {
"debug" {
playerExecutes {
when {
player.toGeary().has<BonfireDebug>() -> {
player.toGeary().remove<BonfireDebug>()
sender.error("Bonfire debug mode disabled")
}
else -> {
player.toGeary().setPersisting(BonfireDebug())
sender.success("Bonfire debug mode enabled")
}
}
}
}
}
"reload" {
actions(bonfire.logger) {
bonfire.plugin.registerBonfireContext()
sender.success("Bonfire configs have been reloaded!")
}
}
"respawn" {
val offlinePlayer: OfflinePlayer by offlinePlayerArg()
"get" {
action {
val respawn = when {
offlinePlayer.isOnline -> offlinePlayer.player?.toGearyOrNull()?.get<BonfireRespawn>()
else -> offlinePlayer.getOfflinePDC()?.decode<BonfireRespawn>()
}?.bonfireLocation
?: return@action sender.error("Could not find BonfireRespawn for the given Player")
sender.info("Bonfire-Respawn for ${offlinePlayer.name} is at ${respawn.x}, ${respawn.y}, ${respawn.z} in ${respawn.world.name}")
"reload" {
executes {
bonfire.plugin.registerBonfireContext()
sender.success("Bonfire configs have been reloaded!")
}
}
"set" {
val x: Int by intArg { default = (sender as? Player)?.location?.toCenterLocation()?.blockX }
val y: Int by intArg { default = (sender as? Player)?.location?.toCenterLocation()?.blockY }
val z: Int by intArg { default = (sender as? Player)?.location?.toCenterLocation()?.blockZ }
val worldName: String by stringArg { default = (sender as? Player)?.world?.name ?: "world" }
action {
// Ensures the player has a datafile, aka joined the server before, so we can save the bonfire location
offlinePlayer.getOfflinePDC() ?: return@action sender.error("Could not find PDC for the given OfflinePlayer")
val world =
Bukkit.getWorld(worldName) ?: return@action sender.error("Could not find world $worldName")
val tempBonfireLoc =
Location(world, x.toDouble(), y.toDouble(), z.toDouble()).toCenterLocation()

tempBonfireLoc.world.getChunkAtAsync(tempBonfireLoc).thenAccept {
val bonfireEntity =
world.getNearbyEntitiesByType(ItemDisplay::class.java, tempBonfireLoc, 0.5)
.firstOrNull()
"players" {
val location by ArgumentTypes.blockPosition().suggests {
(context.source.executor as? Player)?.location?.let {
suggestFiltering("${it.blockX} ${it.blockY} ${it.blockZ}")
}
}
fun IdoCommandContext.runPlayersCommand(location: Location) {
val bonfireLoc = location.toBlockCenterLocation()
val (x,y,z) = bonfireLoc.blockX to bonfireLoc.blockY to bonfireLoc.blockZ

bonfireEntity?.toGearyOrNull()?.get<Bonfire>()?.let { bonfire ->
when {
offlinePlayer.uniqueId in bonfire.bonfirePlayers ->
sender.error("Player is already registered to this bonfire")
bonfire.bonfirePlayers.size >= bonfire.maxPlayerCount ->
sender.error("Bonfire is full")
else -> {
offlinePlayer.editOfflinePDC {
encode(BonfireRespawn(bonfireEntity.uniqueId, bonfireEntity.location))
}
bonfire.bonfirePlayers += offlinePlayer.uniqueId
bonfireEntity.updateBonfireState()
sender.success("Set respawn point for ${offlinePlayer.name} to $x $y $z in $worldName")
}
}
location.world.getChunkAtAsync(location).thenAccept {
val bonfireEntity = bonfireLoc.getNearbyEntitiesByType(ItemDisplay::class.java, 5.0).firstOrNull()
bonfireEntity?.toGeary()?.get<Bonfire>()?.let { bonfire ->
sender.info(
"Players with their respawn set at this bonfire: ${
bonfire.bonfirePlayers.joinToString(", ") { Bukkit.getOfflinePlayer(it).name ?: "Unknown" }
}"
)
} ?: sender.error("Could not find bonfire at $x $y $z")
}
}
executes { runPlayersCommand(location()?.resolve(context.source)?.toLocation(context.source.location.world)!!) }
val world by ArgumentTypes.world().suggests {
suggest(Bukkit.getWorlds().map { it.key.asString() })
}
executes { runPlayersCommand(location()?.resolve(context.source)?.toLocation(world()!!)!!) }
}
"remove" {
action {
val respawn = when {
offlinePlayer.isOnline -> {
val respawn = offlinePlayer.player?.toGearyOrNull()?.get<BonfireRespawn>()
offlinePlayer.player?.toGeary()?.remove<BonfireRespawn>()
respawn
}
else -> {
val pdc = offlinePlayer.getOfflinePDC() ?: return@action sender.error("Could not find PDC for the given OfflinePlayer")
val respawn = pdc.decode<BonfireRespawn>() ?: return@action sender.error("OfflinePlayer has no bonfire set")
pdc.remove<BonfireRespawn>()
offlinePlayer.saveOfflinePDC(pdc)
respawn
"respawn" {
val offlinePlayer by StringArgumentType.word()
"get" {
executes {
val offlinePlayer = Bukkit.getOfflinePlayer(offlinePlayer())
val respawn = when {
offlinePlayer.isOnline -> offlinePlayer.player?.toGearyOrNull()?.get<BonfireRespawn>()
else -> offlinePlayer.getOfflinePDC()?.decode<BonfireRespawn>()
}?.bonfireLocation ?: return@executes sender.error("Could not find BonfireRespawn for the given Player")
sender.info("Bonfire-Respawn for ${offlinePlayer.name} is at ${respawn.x}, ${respawn.y}, ${respawn.z} in ${respawn.world.name}")
}
}
"set" {
val location by ArgumentTypes.blockPosition().suggests {
(context.source.executor as? Player)?.location?.let {
suggestFiltering("${it.blockX} ${it.blockY} ${it.blockZ}")
}
} ?: return@action sender.error("Player has no respawn point set")
}

// Remove component of bonfire if it exists still
respawn.bonfireLocation.world.getChunkAtAsync(respawn.bonfireLocation).thenAccept {
val bonfireEntity = Bukkit.getEntity(respawn.bonfireUuid) as? ItemDisplay
bonfireEntity?.toGeary()?.get<Bonfire>()?.let { bonfire ->
bonfire.bonfirePlayers -= offlinePlayer.uniqueId
if (bonfire.bonfirePlayers.isEmpty()) bonfireEntity.updateBonfireState()
fun IdoCommandContext.handleRespawnSet(offlinePlayer: OfflinePlayer, location: Location) {
// Ensures the player has a datafile, aka joined the server before, so we can save the bonfire location
offlinePlayer.getOfflinePDC() ?: return sender.error("Could not find PDC for the given OfflinePlayer")
val world = location.world
val bonfireLoc = location.toBlockCenterLocation()
val (x,y,z) = bonfireLoc.blockX to bonfireLoc.blockY to bonfireLoc.blockZ

world.getChunkAtAsync(location).thenAccept {
val bonfireEntity = bonfireLoc.getNearbyEntitiesByType(ItemDisplay::class.java, 0.5).firstOrNull()

bonfireEntity?.toGearyOrNull()?.get<Bonfire>()?.let { bonfire ->
when {
offlinePlayer.uniqueId in bonfire.bonfirePlayers ->
sender.error("Player is already registered to this bonfire")
bonfire.bonfirePlayers.size >= bonfire.maxPlayerCount ->
sender.error("Bonfire is full")
else -> {
offlinePlayer.editOfflinePDC {
encode(BonfireRespawn(bonfireEntity.uniqueId, bonfireEntity.location))
}
bonfire.bonfirePlayers += offlinePlayer.uniqueId
bonfireEntity.updateBonfireState()
sender.success("Set respawn point for ${offlinePlayer.name} to $x $y $z in ${world.name}")
}
}
} ?: sender.error("Could not find bonfire at $x $y $z")
}
}
executes {
handleRespawnSet(Bukkit.getOfflinePlayer(offlinePlayer()), location()!!.resolve(context.source).toLocation(context.source.location.world))
}
val world by ArgumentTypes.world().suggests {
suggest(Bukkit.getWorlds().map { it.key.asString() })
}
executes {
handleRespawnSet(Bukkit.getOfflinePlayer(offlinePlayer()), location()!!.resolve(context.source).toLocation(world()!!))
}
}
}
}
"players" {
val x: Int by intArg { default = (sender as? Player)?.location?.toCenterLocation()?.blockX }
val y: Int by intArg { default = (sender as? Player)?.location?.toCenterLocation()?.blockY }
val z: Int by intArg { default = (sender as? Player)?.location?.toCenterLocation()?.blockZ }
val worldName: String by stringArg { default = (sender as? Player)?.world?.name ?: "world" }
action {
val bonfireLocation = Location(
Bukkit.getWorld(worldName) ?: return@action sender.error("Could not find world $worldName"),
x.toDouble(),
y.toDouble(),
z.toDouble()
).toCenterLocation()
"remove" {
executes {
val offlinePlayer = Bukkit.getOfflinePlayer(offlinePlayer())
val respawn = when {
offlinePlayer.isOnline -> {
val respawn = offlinePlayer.player?.toGearyOrNull()?.get<BonfireRespawn>()
offlinePlayer.player?.toGeary()?.remove<BonfireRespawn>()
respawn
}
else -> {
val pdc = offlinePlayer.getOfflinePDC() ?: return@executes sender.error("Could not find PDC for the given OfflinePlayer")
val respawn = pdc.decode<BonfireRespawn>() ?: return@executes sender.error("OfflinePlayer has no bonfire set")
pdc.remove<BonfireRespawn>()
offlinePlayer.saveOfflinePDC(pdc)
respawn
}
} ?: return@executes sender.error("Player has no respawn point set")

bonfireLocation.world.getChunkAtAsync(bonfireLocation).thenAccept {
val bonfireEntity =
bonfireLocation.world.getNearbyEntitiesByType(ItemDisplay::class.java, bonfireLocation, 0.5)
.firstOrNull()
bonfireEntity?.toGeary()?.get<Bonfire>()?.let { bonfire ->
sender.info(
"Players with their respawn set at this bonfire: ${
bonfire.bonfirePlayers.joinToString(
", "
) { Bukkit.getOfflinePlayer(it).name ?: "Unknown" }
}"
)
// Remove component of bonfire if it exists still
respawn.bonfireLocation.world.getChunkAtAsync(respawn.bonfireLocation).thenAccept {
val bonfireEntity = Bukkit.getEntity(respawn.bonfireUuid) as? ItemDisplay
bonfireEntity?.toGeary()?.get<Bonfire>()?.let { bonfire ->
bonfire.bonfirePlayers -= offlinePlayer.uniqueId
if (bonfire.bonfirePlayers.isEmpty()) bonfireEntity.updateBonfireState()
}
}
}
}
}
}
}
}

override fun onTabComplete(
sender: CommandSender,
command: Command,
alias: String,
args: Array<out String>
): List<String> {
if (command.name != "bonfire") return emptyList()
return when (args.size) {
1 -> listOf("reload", "respawn", "players", "debug").filter { it.startsWith(args[0]) }
2 -> when (args[0]) {
"respawn" -> listOf("get", "set", "remove").filter { it.startsWith(args[1]) }
"players" -> listOf((sender as? Player)?.location?.blockX.toString())
"cooldown" -> listOf("clear")
else -> emptyList()
}

3 -> when (args[0]) {
"players" -> listOf((sender as? Player)?.location?.blockY.toString())
"respawn" -> Bukkit.getOnlinePlayers().map { it.name }.filter { it.startsWith(args[2]) }
else -> emptyList()
}

4 -> when {
args[0] == "players" -> listOf((sender as? Player)?.location?.blockZ.toString())
args[1] == "set" -> listOf((sender as? Player)?.location?.blockX.toString())
else -> emptyList()
}

5 -> when {
args[0] == "players" -> listOf((sender as? Player)?.world?.name ?: "world")
args[1] == "set" -> listOf((sender as? Player)?.location?.blockY.toString())
else -> emptyList()
}

6 -> when(args[1]) {
"set" -> listOf((sender as? Player)?.location?.blockZ.toString())
else -> emptyList()
}

7 -> when (args[1]) {
"set" -> listOf((sender as? Player)?.location?.world?.name ?: "world")
else -> emptyList()
}

else -> emptyList()
}
}
}
1 change: 1 addition & 0 deletions src/main/kotlin/com/mineinabyss/bonfire/BonfireConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.mineinabyss.bonfire
import com.mineinabyss.idofront.serialization.DurationSerializer
import com.mineinabyss.idofront.serialization.VectorSerializer
import kotlinx.serialization.Serializable
import org.bukkit.Bukkit
import org.bukkit.SoundCategory
import org.bukkit.util.Vector
import kotlin.time.Duration
Expand Down
Loading

0 comments on commit c4f7863

Please sign in to comment.