Skip to content

Commit e5a51b5

Browse files
committed
feat: Add fallback /mimic command
1 parent d4ad1ea commit e5a51b5

File tree

5 files changed

+68
-13
lines changed

5 files changed

+68
-13
lines changed

mimic-bukkit/src/main/kotlin/command/ConfigMessage.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import ru.endlesscode.mimic.config.StringSetConfigProperty
1212
import ru.endlesscode.mimic.internal.append
1313
import ru.endlesscode.mimic.internal.appendClickable
1414
import ru.endlesscode.mimic.internal.appendLine
15-
import ru.endlesscode.mimic.internal.buildTextComponent
15+
import ru.endlesscode.mimic.internal.text
1616

1717
@OptIn(ExperimentalMimicApi::class)
18-
internal fun buildConfigMessage(mimic: Mimic, config: MimicConfig): TextComponent = buildTextComponent {
18+
internal fun buildConfigMessage(mimic: Mimic, config: MimicConfig): TextComponent = text {
1919
appendLine("----[ Mimic Config ]----")
2020
appendSelectablePropertyConfig(
2121
property = MimicConfig.LEVEL_SYSTEM,
@@ -53,7 +53,7 @@ private fun TextComponent.Builder.appendSelectablePropertyConfig(
5353
availableOptions,
5454
hint = "Click to select",
5555
color = NamedTextColor.GRAY,
56-
resolveCommand = { "/mimic config ${property.path} $it" },
56+
resolveCommand = { "/$COMMAND_NAME config ${property.path} $it" },
5757
)
5858
}
5959

@@ -76,14 +76,14 @@ private fun TextComponent.Builder.appendSetPropertyConfig(
7676
hint = "Click to add",
7777
color = NamedTextColor.GRAY,
7878
nonClickableOptions = permanentOptions,
79-
resolveCommand = { "/mimic config ${property.path} add $it" },
79+
resolveCommand = { "/$COMMAND_NAME config ${property.path} add $it" },
8080
)
8181

8282
appendLine()
8383
appendPropertyPath(property)
8484
append("[")
8585
appendSelectableOptions(values, hint = "Click to remove") {
86-
"/mimic config ${property.path} remove $it"
86+
"/$COMMAND_NAME config ${property.path} remove $it"
8787
}
8888
append("]")
8989
}
@@ -103,7 +103,7 @@ private fun TextComponent.Builder.appendSelectableOptions(
103103
resolveCommand: (String) -> String,
104104
) = append(
105105
options.mapIndexed { index, option ->
106-
buildTextComponent {
106+
text {
107107
color(color)
108108
if (index != 0) append(", ")
109109
if (option !in nonClickableOptions) {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package ru.endlesscode.mimic.command
2+
3+
import net.kyori.adventure.text.Component
4+
import net.kyori.adventure.text.event.ClickEvent
5+
import net.kyori.adventure.text.event.HoverEvent
6+
import net.kyori.adventure.text.format.NamedTextColor
7+
import net.kyori.adventure.text.format.TextDecoration
8+
import org.bukkit.command.CommandSender
9+
import org.bukkit.command.defaults.BukkitCommand
10+
import ru.endlesscode.mimic.internal.append
11+
import ru.endlesscode.mimic.internal.appendLine
12+
import ru.endlesscode.mimic.internal.text
13+
14+
internal class FallbackCommand(
15+
private val pluginFullName: String
16+
) : BukkitCommand(
17+
COMMAND_NAME,
18+
COMMAND_INFO_DESCRIPTION,
19+
"/$COMMAND_NAME",
20+
emptyList(),
21+
) {
22+
23+
override fun execute(sender: CommandSender, commandLabel: String, args: Array<out String>?): Boolean {
24+
val message = text {
25+
appendLine(pluginFullName, NamedTextColor.GREEN)
26+
color(NamedTextColor.GRAY)
27+
append("Install ")
28+
append(commandApiLink())
29+
append(" to unlock all Mimic commands.")
30+
}
31+
sender.sendMessage(message)
32+
return true
33+
}
34+
35+
private fun commandApiLink() = text {
36+
color(NamedTextColor.YELLOW)
37+
append("CommandAPI", TextDecoration.UNDERLINED)
38+
hoverEvent(HoverEvent.showText(Component.text("Open CommandAPI website")))
39+
clickEvent(ClickEvent.openUrl("https://docs.commandapi.dev/user-setup/install"))
40+
}
41+
}

mimic-bukkit/src/main/kotlin/command/MainCommand.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@ import ru.endlesscode.mimic.config.MimicConfig
88
import ru.endlesscode.mimic.internal.append
99
import ru.endlesscode.mimic.internal.appendClickable
1010
import ru.endlesscode.mimic.internal.appendLine
11-
import ru.endlesscode.mimic.internal.buildTextComponent
11+
import ru.endlesscode.mimic.internal.text
12+
13+
internal const val COMMAND_NAME = "mimic"
14+
internal const val COMMAND_INFO_DESCRIPTION = "Show info about Mimic"
1215

1316
/** Registers command '/mimic' and all subcommands. */
1417
internal fun registerCommand(
1518
mimic: Mimic,
1619
config: MimicConfig,
1720
pluginFullName: String,
18-
) = commandAPICommand("mimic") {
21+
) = commandAPICommand(COMMAND_NAME) {
1922
withPermission("mimic.admin")
20-
withShortDescription("Show info about Mimic")
23+
withShortDescription(COMMAND_INFO_DESCRIPTION)
2124
executes(infoExecutor(pluginFullName))
2225

2326
configSubcommand(mimic, config)
@@ -28,7 +31,7 @@ internal fun registerCommand(
2831
}
2932

3033
private fun infoExecutor(pluginFullName: String) = CommandExecutor { sender, _ ->
31-
val message = buildTextComponent {
34+
val message = text {
3235
appendLine(pluginFullName, NamedTextColor.GREEN)
3336
color(NamedTextColor.GRAY)
3437
append("Use ")
@@ -38,9 +41,9 @@ private fun infoExecutor(pluginFullName: String) = CommandExecutor { sender, _ -
3841
sender.sendMessage(message)
3942
}
4043

41-
private fun createClickableCommand() = buildTextComponent {
44+
private fun createClickableCommand() = text {
4245
color(NamedTextColor.YELLOW)
4346
appendClickable(CONFIG_COMMAND, "Click to execute", CONFIG_COMMAND)
4447
}
4548

46-
private const val CONFIG_COMMAND = "/mimic config"
49+
private const val CONFIG_COMMAND = "/$COMMAND_NAME config"

mimic-bukkit/src/main/kotlin/command/MimicCommands.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ internal class MimicCommands {
1919
if (commandApiPlugin == null) {
2020
Log.w("CommandAPI not found. Mimic commands won't be registered.")
2121
Log.w("Consider installing CommandAPI: https://docs.commandapi.dev/")
22+
registerFallbackCommand(plugin)
2223
return
2324
} else if (!commandApiPlugin.isEnabled) {
2425
Log.w("CommandAPI loaded, but not enabled. Mimic commands won't be registered.")
26+
registerFallbackCommand(plugin)
2527
return
2628
}
2729

@@ -33,6 +35,13 @@ internal class MimicCommands {
3335
)
3436
}
3537

38+
private fun registerFallbackCommand(plugin: JavaPlugin) {
39+
plugin.server.commandMap.register(
40+
plugin.name.lowercase(),
41+
FallbackCommand(plugin.pluginMeta.displayName),
42+
)
43+
}
44+
3645
fun unregister() {
3746
if (registered) CommandAPI.unregister("mimic")
3847
}

mimic-bukkit/src/main/kotlin/internal/TextComponent.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import net.kyori.adventure.text.event.HoverEvent
77
import net.kyori.adventure.text.format.TextColor
88
import net.kyori.adventure.text.format.TextDecoration
99

10-
internal fun buildTextComponent(builder: TextComponent.Builder.() -> Unit): TextComponent = Component.text(builder)
10+
// See: https://github.com/KyoriPowered/adventure/tree/main/4/extra-kotlin
11+
12+
internal fun text(builder: TextComponent.Builder.() -> Unit): TextComponent = Component.text(builder)
1113

1214
internal fun TextComponent.Builder.appendLine(
1315
text: String,

0 commit comments

Comments
 (0)