Skip to content

Commit

Permalink
feat: Added MiniPlaceholders support in plugin messages
Browse files Browse the repository at this point in the history
  • Loading branch information
4drian3d committed Jul 9, 2023
1 parent ebdf2e2 commit 68b4c27
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 46 deletions.
38 changes: 35 additions & 3 deletions src/main/kotlin/io/github/_4drian3d/clientcatcher/Extensions.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,47 @@
package io.github._4drian3d.clientcatcher

import io.github.miniplaceholders.api.MiniPlaceholders
import net.kyori.adventure.audience.Audience
import net.kyori.adventure.text.minimessage.MiniMessage
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver

fun String.asMiniMessage() = MiniMessage.miniMessage().deserialize(this)
fun String.asMiniMessage(vararg resolvers: TagResolver) = MiniMessage.miniMessage().deserialize(this, *resolvers)
fun String.asMiniMessage(resolver: TagResolver) = MiniMessage.miniMessage().deserialize(this, resolver)
val hasMiniPlaceholders by lazy {
try {
Class.forName("io.github.miniplaceholders.api.MiniPlaceholders")
true
} catch (e: ClassNotFoundException) {
false
}
}

private fun String.asMiniMessage(resolvers: Array<out TagResolver>) = MiniMessage.miniMessage().deserialize(this, *resolvers)
private fun String.asMiniMessage(resolver: TagResolver) = MiniMessage.miniMessage().deserialize(this, resolver)

fun Audience.sendMini(message: String, resolver: TagResolver) {
if (message.isNotBlank()) {
val builder = TagResolver.builder().resolver(resolver)
if (hasMiniPlaceholders) {
builder.resolver(MiniPlaceholders.getAudienceGlobalPlaceholders(this))
}
this.sendMessage(message.asMiniMessage(resolver))
}
}

fun Audience.sendMini(message: String, vararg resolver: TagResolver) {
if (message.isNotBlank()) {
val builder = TagResolver.builder().resolvers(*resolver)
if (hasMiniPlaceholders) {
builder.resolver(MiniPlaceholders.getAudienceGlobalPlaceholders(this))
}
this.sendMessage(message.asMiniMessage(resolver))
}
}

fun Audience.sendMini(message: String) {
if (message.isNotBlank()) {
val resolver =
if (hasMiniPlaceholders) MiniPlaceholders.getAudienceGlobalPlaceholders(this)
else TagResolver.empty()
this.sendMessage(message.asMiniMessage(resolver))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.velocitypowered.api.command.BrigadierCommand
import com.velocitypowered.api.command.CommandManager
import com.velocitypowered.api.command.CommandSource
import io.github._4drian3d.clientcatcher.ClientCatcher
import io.github._4drian3d.clientcatcher.asMiniMessage
import io.github._4drian3d.clientcatcher.sendMini
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder
import kotlin.jvm.optionals.getOrNull

Expand All @@ -21,16 +21,16 @@ class ClientCatcherCommand(
val command = BrigadierCommand(literal<CommandSource>("clientcatcher")
.requires { it.hasPermission("clientcatcher.command") }
.executes {
it.source.sendMessage(plugin.messages.command.usage.asMiniMessage())
it.source.sendMini(plugin.messages.command.usage)
Command.SINGLE_SUCCESS
}
.then(literal<CommandSource>("reload")
.requires { it.hasPermission("clientcatcher.command.reload") }
.executes {
plugin.loadConfig().thenApplyAsync { result ->
if (result) plugin.messages.reload.successfully.asMiniMessage()
else plugin.messages.reload.error.asMiniMessage()
}.thenAcceptAsync(it.source::sendMessage)
if (result) plugin.messages.reload.successfully
else plugin.messages.reload.error
}.thenAcceptAsync(it.source::sendMini)
Command.SINGLE_SUCCESS
}
)
Expand All @@ -41,26 +41,20 @@ class ClientCatcherCommand(
.executes { ctx ->
val name = getString(ctx, "player")
plugin.proxyServer.getPlayer(name).getOrNull()?.let { player ->
ctx.source.sendMessage(
with(plugin.messages.command) {
if (player.modInfo.isPresent) client.withMods.asMiniMessage(
Placeholder.unparsed("player", player.username),
Placeholder.unparsed("client", player.clientBrand ?: "UNKNOWN"),
Placeholder.unparsed("mods",
player.modInfo.get().mods.joinToString(", ") { "${it.id}:${it.version}" })
)
else client.client.asMiniMessage(
Placeholder.unparsed("player", player.username),
Placeholder.unparsed("client", player.clientBrand ?: "UNKNOWN")
)
}
)
} ?: {
ctx.source.sendMessage(
plugin.messages.command.unknownPlayer.asMiniMessage(
Placeholder.unparsed("name", name)
with(plugin.messages.command) {
if (player.modInfo.isPresent) ctx.source.sendMini(client.withMods,
Placeholder.unparsed("player", player.username),
Placeholder.unparsed("client", player.clientBrand ?: "UNKNOWN"),
Placeholder.unparsed("mods",
player.modInfo.get().mods.joinToString(", ") { "${it.id}:${it.version}" })
)
)
else ctx.source.sendMini(client.client,
Placeholder.unparsed("player", player.username),
Placeholder.unparsed("client", player.clientBrand ?: "UNKNOWN")
)
}
} ?: {
ctx.source.sendMini(plugin.messages.command.unknownPlayer, Placeholder.unparsed("name", name))
}
Command.SINGLE_SUCCESS
}
Expand All @@ -73,26 +67,18 @@ class ClientCatcherCommand(
.executes { ctx ->
val name = getString(ctx, "player")
plugin.proxyServer.getPlayer(name).getOrNull()?.let { player ->
ctx.source.sendMessage(
with(plugin.messages.command.mods) {
if (player.modInfo.isPresent) found.asMiniMessage(
Placeholder.unparsed("player", player.username),
Placeholder.unparsed("mods",
player.modInfo.get().mods.joinToString(", ") { "${it.id}:${it.version}" })
)
else notFound.asMiniMessage(
Placeholder.unparsed(
"player",
player.username
)
)
}
)
} ?: {
ctx.source.sendMessage(
plugin.messages.command.unknownPlayer.asMiniMessage(
Placeholder.unparsed("name", name)
with(plugin.messages.command.mods) {
if (player.modInfo.isPresent) ctx.source.sendMini(found,
Placeholder.unparsed("player", player.username),
Placeholder.unparsed("mods",
player.modInfo.get().mods.joinToString(", ") { "${it.id}:${it.version}" })
)
else ctx.source.sendMini(notFound, Placeholder.unparsed("player", player.username))
}
} ?: {
ctx.source.sendMini(
plugin.messages.command.unknownPlayer,
Placeholder.unparsed("name", name)
)
}
Command.SINGLE_SUCCESS
Expand Down

0 comments on commit 68b4c27

Please sign in to comment.