Skip to content

Commit

Permalink
cleanup broadcast
Browse files Browse the repository at this point in the history
  • Loading branch information
Miles Holder committed May 8, 2024
1 parent 1305033 commit b482c02
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.github.spigotbasics.core.command.parsed.arguments

import org.bukkit.command.CommandSender

class GreedyStringArg(name: String) : CommandArgument<String>(name) {
override val greedy: Boolean = true

override fun parse(sender: CommandSender, value: String): String {
return value
}

override fun tabComplete(sender: CommandSender, typing: String): List<String> {
return emptyList()
}
}
8 changes: 8 additions & 0 deletions modules/basics-broadcast/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
| Command | Permission |
|-----------------------------------|-------------------------|
| `/broadcast <message>` | basics.broadcast |
| `/broadcast [--parsed] <message>` | basics.broadcast.parsed |

> [!NOTE]
> the `--parsed` argument while optional also required the `basics.broadcast.parsed`
> permission when being used
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
package com.github.spigotbasics.modules.basicsbroadcast

import com.github.spigotbasics.core.command.parsed.arguments.AnyStringArg
import com.github.spigotbasics.core.command.parsed.arguments.GreedyStringArg
import com.github.spigotbasics.core.command.parsed.arguments.LiteralArg
import com.github.spigotbasics.core.module.AbstractBasicsModule
import com.github.spigotbasics.core.module.loader.ModuleInstantiationContext

class BasicsBroadcastModule(context: ModuleInstantiationContext) : AbstractBasicsModule(context) {
val commandPerm = permissionManager.createSimplePermission("basics.broadcast", "Allows the user to broadcast messages")
val parsedPerm = permissionManager.createSimplePermission("basics.broadcast.parsed", "Allows the user to broadcast parsed messages")
private val commandPerm = permissionManager.createSimplePermission("basics.broadcast", "Allows the user to broadcast messages")
private val parsedPerm = permissionManager.createSimplePermission("basics.broadcast.parsed", "Allows the user to broadcast parsed messages")

override fun onEnable() {
commandFactory.rawCommandBuilder("broadcast", commandPerm)
.description("Broadcasts a message to all players")
.usage("[--parsed] <message>")
.executor(BroadcastExecutor(this))
.register()
commandFactory.parsedCommandBuilder("broadcast", commandPerm)
.mapContext {
usage = "[--parsed] <message>"
description("Broadcast a message to your server")

path {
arguments {
named("message", GreedyStringArg("message"))
}
}

path {
arguments {
permissions(parsedPerm)
named("parsed", LiteralArg("--parsed"))
named("message", GreedyStringArg("message"))
}
}
}.executor(BroadcastCommand(this)).register()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.spigotbasics.modules.basicsbroadcast

import com.github.spigotbasics.core.command.parsed.CommandContextExecutor
import com.github.spigotbasics.core.command.parsed.context.MapContext
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player

class BroadcastCommand(private val module: BasicsBroadcastModule) : CommandContextExecutor<MapContext> {

override fun execute(sender: CommandSender, context: MapContext) {
val parsed = context["parsed"] != null
val rawMessage = context["message"] as String

val message = if (parsed) {
module.messageFactory.createMessage(rawMessage).concerns(sender as? Player)
} else {
module.messageFactory.createPlainMessage(rawMessage)
}

message.sendToAllPlayers()
message.sendToConsole()
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.github.spigotbasics.modules.basicsextinguish

import com.github.spigotbasics.core.command.common.BasicsCommandExecutor
import com.github.spigotbasics.core.command.common.CommandResult
import com.github.spigotbasics.core.command.parsed.arguments.SelectorSinglePlayerArg
import com.github.spigotbasics.core.command.raw.RawCommandContext
import com.github.spigotbasics.core.module.AbstractBasicsModule
import com.github.spigotbasics.core.module.loader.ModuleInstantiationContext

Expand Down Expand Up @@ -40,27 +37,4 @@ class BasicsExtinguishModule(context: ModuleInstantiationContext) : AbstractBasi
}
}.executor(BasicsExtinguishExecutor(this)).register()
}

private inner class ExtinguishExecutor(private val module: BasicsExtinguishModule) : BasicsCommandExecutor(module) {
override fun execute(context: RawCommandContext): CommandResult {
val player =
if (context.args.size == 1) {
requirePermission(context.sender, module.permExtinguishOthers)
requirePlayer(context.args[0])
} else {
requirePlayer(context.sender)
}

player.fireTicks = 0
val message =
if (context.sender == player) {
module.messageExtinguished
} else {
module.messageExtinguishedOther
}

message.concerns(player).sendToSender(context.sender)
return CommandResult.SUCCESS
}
}
}

0 comments on commit b482c02

Please sign in to comment.