Skip to content

Commit

Permalink
(breaking) Use MiniMessage instead of legacy section/ampersand character
Browse files Browse the repository at this point in the history
  • Loading branch information
acrylic-style committed Nov 10, 2024
1 parent 8f807c1 commit 7453153
Show file tree
Hide file tree
Showing 21 changed files with 434 additions and 395 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = "net.azisaba.spicyazisaban"
version = "1.0.0-${getBranch()}-${getGitHash()}${if (hasUncommittedChanges()) "-debug" else ""}"
version = "2.0.0-${getBranch()}-${getGitHash()}${if (hasUncommittedChanges()) "-debug" else ""}"

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(17))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import net.azisaba.spicyAzisaBan.common.ServerInfo
import net.azisaba.spicyAzisaBan.common.chat.Component
import net.azisaba.spicyAzisaBan.common.command.Command
import net.azisaba.spicyAzisaBan.common.scheduler.ScheduledTask
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer
import net.md_5.bungee.api.ProxyServer
import net.md_5.bungee.api.chat.TextComponent
import java.io.File
Expand Down Expand Up @@ -64,4 +65,14 @@ class SpicyAzisaBanBungee: SpicyAzisaBan() {
override fun getConsoleActor(): Actor = BungeeActor(server.console)

override fun getDataFolder(): Path = File("./plugins/SpicyAzisaBan").toPath()

override fun convertComponent(component: net.kyori.adventure.text.Component): Component {
return LegacyComponentSerializer.builder()
.character('§')
.useUnusualXRepeatedCharacterHexFormat()
.build()
.serialize(component)
.let { TextComponent.fromLegacyText(it) }
.let { TextComponent(*it).toCommon() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import net.azisaba.spicyAzisaBan.common.ServerInfo
import net.azisaba.spicyAzisaBan.common.chat.Component
import net.azisaba.spicyAzisaBan.common.command.Command
import net.azisaba.spicyAzisaBan.common.scheduler.ScheduledTask
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer
import java.io.File
import java.io.OutputStream
import java.nio.file.Path
Expand Down Expand Up @@ -66,4 +67,12 @@ class SpicyAzisaBanCLI: SpicyAzisaBan() {
override fun executeCommand(actor: Actor, command: String) = throw AssertionError("Cannot execute command on CLI")
override fun getConsoleActor(): Actor = CLIActor
override fun getDataFolder(): Path = File(".").toPath()
}

override fun convertComponent(component: net.kyori.adventure.text.Component): Component {
return LegacyComponentSerializer.builder()
.character('§')
.build()
.serialize(component)
.let { SimpleComponent.fromLegacyText(it) }
}
}
2 changes: 2 additions & 0 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ dependencies {
}
api("org.ow2.asm:asm:9.7.1")
api("org.ow2.asm:asm-commons:9.7.1")
api("net.kyori:adventure-text-minimessage:4.17.0")
api("net.kyori:adventure-text-serializer-legacy:4.17.0")
compileOnlyApi("org.mariadb.jdbc:mariadb-java-client:3.5.0")
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.azisaba.spicyAzisaBan

import net.azisaba.spicyAzisaBan.common.ChatColor
import net.azisaba.spicyAzisaBan.util.Util
import net.azisaba.spicyAzisaBan.util.Util.toLegacySectionText
import net.azisaba.spicyAzisaBan.util.Util.translate
import util.yaml.YamlConfiguration
import util.yaml.YamlObject
Expand Down Expand Up @@ -35,14 +35,14 @@ object SABMessages {
fun YamlObject.getMessage(key: String, def: String = "<key: $key>"): String {
val raw = this.rawData[key] ?: def
if (raw is String) return raw
return this.getArray(key)?.mapNotNull { o -> o?.toString() }?.joinToString("${ChatColor.RESET}\n") ?: def
return this.getArray(key)?.mapNotNull { o -> o?.toString() }?.joinToString("<reset>\n") ?: def
}

/**
* Replaces the variables (%KEY_IN_UPPERCASE%) in string with value.
*/
fun String.replaceVariables(variables: Map<String, String> = mapOf()): String {
var s = replace("%PREFIX%", SpicyAzisaBan.PREFIX)
var s = replace("%PREFIX%", General.prefix)
.replace("%CMD_PREFIX%", SABConfig.prefix)
variables.forEach { (key, value) -> s = s.replace("%${key.uppercase()}%", value) }
return s
Expand Down Expand Up @@ -79,6 +79,7 @@ object SABMessages {
"millis" to Util.zero(3, c[Calendar.MILLISECOND]),
)
.translate()
.toLegacySectionText()
}

object General {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import net.azisaba.spicyAzisaBan.sql.migrations.DatabaseMigration
import net.azisaba.spicyAzisaBan.struct.EventType
import net.azisaba.spicyAzisaBan.util.TimerTasks
import net.azisaba.spicyAzisaBan.util.Util
import net.azisaba.spicyAzisaBan.util.Util.toLegacySectionText
import net.azisaba.spicyAzisaBan.util.Util.translate
import util.promise.rewrite.Promise
import xyz.acrylicstyle.sql.options.FindOptions
Expand Down Expand Up @@ -105,7 +106,7 @@ abstract class SpicyAzisaBan {
if (wasInitialized) error("Cannot construct SpicyAzisaBan more than once")
wasInitialized = true
instance = this
PREFIX = SABMessages.General.prefix.translate()
PREFIX = SABMessages.General.prefix.translate().toLegacySectionText()
}

private fun initDatabase() {
Expand Down Expand Up @@ -220,6 +221,7 @@ abstract class SpicyAzisaBan {
abstract fun executeCommand(actor: Actor, command: String)
abstract fun getConsoleActor(): Actor
abstract fun getDataFolder(): Path
abstract fun convertComponent(component: net.kyori.adventure.text.Component): Component

class Settings {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import net.azisaba.spicyAzisaBan.common.command.Command
import net.azisaba.spicyAzisaBan.punishment.Punishment
import net.azisaba.spicyAzisaBan.punishment.PunishmentType
import net.azisaba.spicyAzisaBan.util.Util.async
import net.azisaba.spicyAzisaBan.util.Util.convert
import net.azisaba.spicyAzisaBan.util.Util.filterArgKeys
import net.azisaba.spicyAzisaBan.util.Util.filtr
import net.azisaba.spicyAzisaBan.util.Util.send
Expand Down Expand Up @@ -103,13 +104,13 @@ object BanListCommand: Command() {
val backText = Component.text("${if (newPage > 1) newPage - 1 else "-"} << ", ChatColor.GRAY)
if (newPage > 1) {
backText.setColor(ChatColor.YELLOW)
backText.setHoverEvent(HoverEvent.Action.SHOW_TEXT, Component.fromLegacyText(SABMessages.General.previousPage.translate()))
backText.setHoverEvent(HoverEvent.Action.SHOW_TEXT, arrayOf(SABMessages.General.previousPage.translate().convert()))
backText.setClickEvent(ClickEvent.Action.RUN_COMMAND, "/${SABConfig.prefix}banlist page=${newPage - 1} ${if (active) "--active" else ""} ${if (all) "--all" else ""} ${if (server != null) "server=\"$server\"" else ""} ${if (punishmentType != null) "type=${punishmentType.name}" else ""}")
}
val nextText = Component.text(" >> ${if (newPage < maxPage) newPage + 1 else "-"}", ChatColor.GRAY)
if (newPage < maxPage) {
nextText.setColor(ChatColor.YELLOW)
nextText.setHoverEvent(HoverEvent.Action.SHOW_TEXT, Component.fromLegacyText(SABMessages.General.nextPage.translate()))
nextText.setHoverEvent(HoverEvent.Action.SHOW_TEXT, arrayOf(SABMessages.General.nextPage.translate().convert()))
nextText.setClickEvent(ClickEvent.Action.RUN_COMMAND, "/${SABConfig.prefix}banlist page=${newPage + 1} ${if (active) "--active" else ""} ${if (all) "--all" else ""} ${if (server != null) "server=\"$server\"" else ""} ${if (punishmentType != null) "type=${punishmentType.name}" else ""}")
}
text.addChildren(backText)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import net.azisaba.spicyAzisaBan.SABMessages
import net.azisaba.spicyAzisaBan.SABMessages.replaceVariables
import net.azisaba.spicyAzisaBan.SpicyAzisaBan
import net.azisaba.spicyAzisaBan.common.Actor
import net.azisaba.spicyAzisaBan.common.ChatColor
import net.azisaba.spicyAzisaBan.common.command.Command
import net.azisaba.spicyAzisaBan.punishment.Punishment
import net.azisaba.spicyAzisaBan.punishment.PunishmentType
Expand All @@ -15,9 +14,9 @@ import net.azisaba.spicyAzisaBan.util.Util.async
import net.azisaba.spicyAzisaBan.util.Util.filterArgKeys
import net.azisaba.spicyAzisaBan.util.Util.filtr
import net.azisaba.spicyAzisaBan.util.Util.isValidIPAddress
import net.azisaba.spicyAzisaBan.util.Util.plus
import net.azisaba.spicyAzisaBan.util.Util.send
import net.azisaba.spicyAzisaBan.util.Util.sendErrorMessage
import net.azisaba.spicyAzisaBan.util.Util.toMiniMessage
import net.azisaba.spicyAzisaBan.util.Util.translate
import net.azisaba.spicyAzisaBan.util.contexts.Contexts
import net.azisaba.spicyAzisaBan.util.contexts.IPAddressContext
Expand Down Expand Up @@ -51,7 +50,7 @@ object CheckCommand: Command() {
if (pid != null) {
Punishment.fetchPunishmentById(pid).thenDo { p ->
if (p == null) {
return@thenDo actor.send(SABMessages.Commands.General.punishmentNotFound.replaceVariables().translate().format(pid))
return@thenDo actor.send(SABMessages.Commands.General.punishmentNotFound.replaceVariables().format(pid).translate())
}
actor.send(p.getHistoryMessage().complete())
}
Expand Down Expand Up @@ -141,14 +140,14 @@ object CheckCommand: Command() {
.replaceVariables(
"ip" to ip,
"hostname" to InetAddress.getByName(ip).hostName,
"mute_count" to (if (muteCount == 0) ChatColor.GREEN else ChatColor.RED) + muteCount.toString(),
"ban_count" to (if (banCount == 0) ChatColor.GREEN else ChatColor.RED) + banCount.toString(),
"warning_count" to (if (warningCount == 0) ChatColor.GREEN else ChatColor.RED) + warningCount.toString(),
"caution_count" to (if (cautionCount == 0) ChatColor.GREEN else ChatColor.RED) + cautionCount.toString(),
"mute_count" to (if (muteCount == 0) "<green>" else "<red>") + muteCount.toString(),
"ban_count" to (if (banCount == 0) "<green>" else "<red>") + banCount.toString(),
"warning_count" to (if (warningCount == 0) "<green>" else "<red>") + warningCount.toString(),
"caution_count" to (if (cautionCount == 0) "<green>" else "<red>") + cautionCount.toString(),
"kick_count" to kickCount.toString(),
"note_count" to noteCount.toString(),
"ban_info" to banInfo,
"mute_info" to muteInfo,
"ban_info" to banInfo.toMiniMessage(),
"mute_info" to muteInfo.toMiniMessage(),
)
.translate()
)
Expand Down Expand Up @@ -211,14 +210,14 @@ object CheckCommand: Command() {
"uuid" to pd.uniqueId.toString(),
"ip" to pd.ip.toString(),
"hostname" to pd.ip?.let { InetAddress.getByName(it).hostName }.toString(),
"mute_count" to (if (muteCount == 0) ChatColor.GREEN else ChatColor.RED) + muteCount.toString(),
"ban_count" to (if (banCount == 0) ChatColor.GREEN else ChatColor.RED) + banCount.toString(),
"warning_count" to (if (warningCount == 0) ChatColor.GREEN else ChatColor.RED) + warningCount.toString(),
"caution_count" to (if (cautionCount == 0) ChatColor.GREEN else ChatColor.RED) + cautionCount.toString(),
"mute_count" to (if (muteCount == 0) "<green>" else "<red>") + muteCount.toString(),
"ban_count" to (if (banCount == 0) "<green>" else "<red>") + banCount.toString(),
"warning_count" to (if (warningCount == 0) "<green>" else "<red>") + warningCount.toString(),
"caution_count" to (if (cautionCount == 0) "<green>" else "<red>") + cautionCount.toString(),
"kick_count" to kickCount.toString(),
"note_count" to noteCount.toString(),
"ban_info" to banInfo,
"mute_info" to muteInfo,
"ban_info" to banInfo.toMiniMessage(),
"mute_info" to muteInfo.toMiniMessage(),
)
.translate()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import net.azisaba.spicyAzisaBan.punishment.Punishment
import net.azisaba.spicyAzisaBan.sql.SQLConnection
import net.azisaba.spicyAzisaBan.struct.PlayerData
import net.azisaba.spicyAzisaBan.util.Util.async
import net.azisaba.spicyAzisaBan.util.Util.convert
import net.azisaba.spicyAzisaBan.util.Util.filterArgKeys
import net.azisaba.spicyAzisaBan.util.Util.filtr
import net.azisaba.spicyAzisaBan.util.Util.isValidIPAddress
Expand Down Expand Up @@ -177,15 +178,15 @@ object HistoryCommand: Command() {
val backText = Component.text("${if (page > 1) page - 1 else "-"} << ")
if (page > 1) {
backText.setColor(ChatColor.YELLOW)
backText.setHoverEvent(HoverEvent.Action.SHOW_TEXT, Component.fromLegacyText(SABMessages.General.previousPage.translate()))
backText.setHoverEvent(HoverEvent.Action.SHOW_TEXT, arrayOf(SABMessages.General.previousPage.translate().convert()))
backText.setClickEvent(ClickEvent.Action.RUN_COMMAND, "/${SABConfig.prefix}history target=$target page=${page - 1} ${if (active) "--active" else ""} ${if (all) "--all" else ""} ${if (ipOpt) "-i" else ""} ${if (only) "-o" else ""}")
} else {
backText.setColor(ChatColor.GRAY)
}
val nextText = Component.text(" >> ${if (page < maxPage) page + 1 else "-"}")
if (page < maxPage) {
nextText.setColor(ChatColor.YELLOW)
nextText.setHoverEvent(HoverEvent.Action.SHOW_TEXT, Component.fromLegacyText(SABMessages.General.nextPage.translate()))
nextText.setHoverEvent(HoverEvent.Action.SHOW_TEXT, arrayOf(SABMessages.General.nextPage.translate().convert()))
nextText.setClickEvent(ClickEvent.Action.RUN_COMMAND, "/${SABConfig.prefix}history target=$target page=${page + 1} ${if (active) "--active" else ""} ${if (all) "--all" else ""} ${if (ipOpt) "-i" else ""} ${if (only) "-o" else ""}")
} else {
nextText.setColor(ChatColor.GRAY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ object SABCommand: Command() {
private val groupRemoveConfirmation = mutableMapOf<UUID, String>()

private fun Actor.sendHelp() {
send("$PREFIX${ChatColor.GREEN}SpicyAzisaBan commands")
send("$PREFIX<green>SpicyAzisaBan commands".translate())
if (hasPermission("sab.command.spicyazisaban.group")) send("${ChatColor.RED}> ${ChatColor.AQUA}/sab group <group>")
if (hasPermission("sab.command.spicyazisaban.info")) send("${ChatColor.RED}> ${ChatColor.AQUA}/sab info")
if (hasPermission("sab.command.spicyazisaban.creategroup")) send("${ChatColor.RED}> ${ChatColor.AQUA}/sab creategroup <group>")
Expand Down Expand Up @@ -166,7 +166,7 @@ object SABCommand: Command() {
SpicyAzisaBan.instance.connection.punishmentHistory
.delete(FindOptions.Builder().addWhere("id", id).build())
.then { list ->
if (list.isEmpty()) return@then actor.send(SABMessages.Commands.General.punishmentNotFound.replaceVariables().translate().format(id))
if (list.isEmpty()) return@then actor.send(SABMessages.Commands.General.punishmentNotFound.replaceVariables().format(id).translate())
val v = Punishment.fromTableData(list[0]).getVariables().complete()
actor.send(SABMessages.Commands.Sab.removedFromPunishmentHistory.replaceVariables(v).translate())
}
Expand All @@ -181,7 +181,7 @@ object SABCommand: Command() {
SpicyAzisaBan.instance.connection.punishments
.delete(FindOptions.Builder().addWhere("id", id).build())
.then { list ->
if (list.isEmpty()) return@then actor.send(SABMessages.Commands.General.punishmentNotFound.replaceVariables().translate().format(id))
if (list.isEmpty()) return@then actor.send(SABMessages.Commands.General.punishmentNotFound.replaceVariables().format(id).translate())
val v = Punishment.fromTableData(list[0]).getVariables().complete()
actor.send(SABMessages.Commands.Sab.removedFromPunishment.replaceVariables(v).translate())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import net.azisaba.spicyAzisaBan.SABMessages
import net.azisaba.spicyAzisaBan.SABMessages.replaceVariables
import net.azisaba.spicyAzisaBan.SpicyAzisaBan
import net.azisaba.spicyAzisaBan.common.Actor
import net.azisaba.spicyAzisaBan.common.ChatColor
import net.azisaba.spicyAzisaBan.common.command.Command
import net.azisaba.spicyAzisaBan.struct.PlayerData
import net.azisaba.spicyAzisaBan.util.Util
Expand All @@ -17,6 +16,7 @@ import net.azisaba.spicyAzisaBan.util.Util.isValidIPAddress
import net.azisaba.spicyAzisaBan.util.Util.send
import net.azisaba.spicyAzisaBan.util.Util.sendErrorMessage
import net.azisaba.spicyAzisaBan.util.Util.sendOrSuppressErrorMessage
import net.azisaba.spicyAzisaBan.util.Util.toMiniMessage
import net.azisaba.spicyAzisaBan.util.Util.toUUIDOrNull
import net.azisaba.spicyAzisaBan.util.Util.translate
import util.kt.promise.rewrite.catch
Expand Down Expand Up @@ -82,10 +82,10 @@ object SeenCommand: Command() {
val ipd = pd.ip
?.let { PlayerData.getAllByIP(it).catch { e -> actor.sendErrorMessage(e) }.complete() }
?.filter { pd2 -> pd2.uniqueId != pd.uniqueId }
val iPlayers = ipd?.joinToString("${ChatColor.WHITE}, ${ChatColor.GOLD}") {
val iPlayers = ipd?.joinToString("<white>, <gold>") {
var prefix = ""
if (SpicyAzisaBan.instance.getPlayer(it.uniqueId)?.isOnline() == true) prefix += "${ChatColor.GREEN}"
var suffix = "${ChatColor.GOLD}"
if (SpicyAzisaBan.instance.getPlayer(it.uniqueId)?.isOnline() == true) prefix += "<green>"
var suffix = "<gold>"
if (it.ip != pd.ip) suffix += "*"
"$prefix${it.name}$suffix"
}.toString()
Expand All @@ -95,7 +95,7 @@ object SeenCommand: Command() {
"player" to pd.name,
"uuid" to pd.uuid.toString(),
"since" to since,
"status" to status,
"status" to status.toMiniMessage(),
"ip" to pd.ip.toString(),
"hostname" to pd.ip?.let { InetAddress.getByName(pd.ip).hostName }.toString(),
"name_history" to pd.getUsernameHistory().complete().distinct().joinToString(", "),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import net.azisaba.spicyAzisaBan.util.Util.getServerName
import net.azisaba.spicyAzisaBan.util.Util.getServerOrGroupName
import net.azisaba.spicyAzisaBan.util.Util.send
import net.azisaba.spicyAzisaBan.util.Util.sendErrorMessage
import net.azisaba.spicyAzisaBan.util.Util.toLegacySectionText
import net.azisaba.spicyAzisaBan.util.Util.translate
import net.azisaba.spicyAzisaBan.util.contexts.Contexts
import net.azisaba.spicyAzisaBan.util.contexts.PlayerContext
Expand Down Expand Up @@ -88,7 +89,8 @@ object WarningCommand: Command() {
"original_reason" to reason.text,
"time" to ReloadableSABConfig.BanOnWarning.time,
)
.translate())
.translate()
.toLegacySectionText())
val timeContext = parsed.get(Contexts.TIME, actor).complete().apply {
if (!isSuccess) {
SpicyAzisaBan.LOGGER.severe("Failed to parse time: ${ReloadableSABConfig.BanOnWarning.time}")
Expand Down
Loading

0 comments on commit 7453153

Please sign in to comment.