From 78ef95600ccbd8e7b1c5f7224b9c523b22c93748 Mon Sep 17 00:00:00 2001 From: andyksaw Date: Tue, 7 May 2024 19:57:22 +0900 Subject: [PATCH] Add warnings --- .../kotlin/com/projectcitybuild/Container.kt | 28 +++++++++ .../kotlin/com/projectcitybuild/PCBridge.kt | 7 +++ .../moderation/warnings/WarningsModule.kt | 43 ------------- .../warnings/actions/AcknowledgeWarning.kt | 14 ----- .../commands/WarningAcknowledgeCommand.kt | 14 ----- .../listeners/NotifyWarningsOnJoinListener.kt | 59 ------------------ .../actions/GetUnacknowledgedWarnings.kt | 8 +-- .../commands/WarningAcknowledgeCommand.kt | 34 +++++++++++ .../listeners/NotifyWarningsOnJoinListener.kt | 60 +++++++++++++++++++ .../repositories/PlayerWarningRepository.kt | 18 +----- 10 files changed, 134 insertions(+), 151 deletions(-) delete mode 100644 pcbridge-spigot/src/main/kotlin/com/projectcitybuild/deprecated/modules/moderation/warnings/WarningsModule.kt delete mode 100644 pcbridge-spigot/src/main/kotlin/com/projectcitybuild/deprecated/modules/moderation/warnings/actions/AcknowledgeWarning.kt delete mode 100644 pcbridge-spigot/src/main/kotlin/com/projectcitybuild/deprecated/modules/moderation/warnings/commands/WarningAcknowledgeCommand.kt delete mode 100644 pcbridge-spigot/src/main/kotlin/com/projectcitybuild/deprecated/modules/moderation/warnings/listeners/NotifyWarningsOnJoinListener.kt rename pcbridge-spigot/src/main/kotlin/com/projectcitybuild/{deprecated/modules/moderation => features}/warnings/actions/GetUnacknowledgedWarnings.kt (79%) create mode 100644 pcbridge-spigot/src/main/kotlin/com/projectcitybuild/features/warnings/commands/WarningAcknowledgeCommand.kt create mode 100644 pcbridge-spigot/src/main/kotlin/com/projectcitybuild/features/warnings/listeners/NotifyWarningsOnJoinListener.kt rename pcbridge-spigot/src/main/kotlin/com/projectcitybuild/{deprecated => features/warnings}/repositories/PlayerWarningRepository.kt (53%) diff --git a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/Container.kt b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/Container.kt index c1ee543cb..da00fe523 100644 --- a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/Container.kt +++ b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/Container.kt @@ -73,6 +73,10 @@ import com.projectcitybuild.pcbridge.core.modules.datetime.time.Time import com.projectcitybuild.pcbridge.core.storage.JsonStorage import com.projectcitybuild.pcbridge.http.HttpService import com.projectcitybuild.features.bans.repositories.PlayerUUIDRepository +import com.projectcitybuild.features.warnings.actions.GetUnacknowledgedWarnings +import com.projectcitybuild.features.warnings.commands.WarningAcknowledgeCommand +import com.projectcitybuild.features.warnings.listeners.NotifyWarningsOnJoinListener +import com.projectcitybuild.features.warnings.repositories.PlayerWarningRepository import com.projectcitybuild.support.spigot.SpigotCommandRegistry import com.projectcitybuild.support.spigot.SpigotListenerRegistry import com.projectcitybuild.support.spigot.SpigotLogger @@ -114,6 +118,7 @@ fun pluginModule(_plugin: JavaPlugin) = module { telemetry() utilities() warps() + warnings() } private fun Module.spigot(plugin: JavaPlugin) { @@ -634,4 +639,27 @@ private fun Module.utilities() { factory { PCBridgeCommand.TabCompleter() } +} + +private fun Module.warnings() { + factory { + PlayerWarningRepository( + playerWarningHttpService = get().playerWarning, + ) + } + + factory { + WarningAcknowledgeCommand( + warningRepository = get(), + ) + } + + factory { + NotifyWarningsOnJoinListener( + getUnacknowledgedWarnings = GetUnacknowledgedWarnings( + dateTimeFormatter = get(), + warningRepository = get(), + ) + ) + } } \ No newline at end of file diff --git a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/PCBridge.kt b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/PCBridge.kt index 4a2c948f4..473c33fc5 100644 --- a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/PCBridge.kt +++ b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/PCBridge.kt @@ -30,6 +30,8 @@ import com.projectcitybuild.features.sync.commands.SyncOtherCommand import com.projectcitybuild.features.sync.listener.SyncRankOnJoinListener import com.projectcitybuild.features.telemetry.listeners.TelemetryPlayerConnectListener import com.projectcitybuild.features.utilities.commands.PCBridgeCommand +import com.projectcitybuild.features.warnings.commands.WarningAcknowledgeCommand +import com.projectcitybuild.features.warnings.listeners.NotifyWarningsOnJoinListener import com.projectcitybuild.features.warps.commands.WarpCommand import com.projectcitybuild.features.warps.commands.WarpsCommand import com.projectcitybuild.integrations.DynmapIntegration @@ -148,6 +150,10 @@ private class Lifecycle: KoinComponent { handler = get(), argsParser = SyncOtherCommand.Args.Parser(), ) + register( + handler = get(), + argsParser = WarningAcknowledgeCommand.Args.Parser(), + ) } listenerRegistry.register( get(), @@ -165,6 +171,7 @@ private class Lifecycle: KoinComponent { get(), get(), get(), + get(), ) get().enable() diff --git a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/deprecated/modules/moderation/warnings/WarningsModule.kt b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/deprecated/modules/moderation/warnings/WarningsModule.kt deleted file mode 100644 index a29f1a075..000000000 --- a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/deprecated/modules/moderation/warnings/WarningsModule.kt +++ /dev/null @@ -1,43 +0,0 @@ -// package com.projectcitybuild.modules.moderation.warnings -// -// import com.projectcitybuild.entities.Permissions -// import com.projectcitybuild.modules.moderation.warnings.actions.AcknowledgeWarning -// import com.projectcitybuild.modules.moderation.warnings.actions.GetUnacknowledgedWarnings -// import com.projectcitybuild.modules.moderation.warnings.commands.WarningAcknowledgeCommand -// import com.projectcitybuild.modules.moderation.warnings.listeners.NotifyWarningsOnJoinListener -// import com.projectcitybuild.support.commandapi.suspendExecutesPlayer -// import com.projectcitybuild.support.modules.ModuleDeclaration -// import com.projectcitybuild.support.modules.PluginModule -// import dev.jorel.commandapi.arguments.IntegerArgument -// import dev.jorel.commandapi.arguments.MultiLiteralArgument -// -// class WarningsModule: PluginModule { -// -// override fun register(module: ModuleDeclaration) = module { -// command("warning") { -// withPermission(Permissions.COMMAND_WARNING_ACKNOWLEDGE) -// withShortDescription("Acknowledges a given warning") -// withArguments( -// MultiLiteralArgument("action", listOf("acknowledge")), -// IntegerArgument("id"), -// ) -// suspendExecutesPlayer(container.plugin) { player, args -> -// WarningAcknowledgeCommand( -// AcknowledgeWarning(container.playerWarningRepository) -// ).execute( -// commandSender = player, -// warningId = args.get("id") as Int, -// ) -// } -// } -// -// listener( -// NotifyWarningsOnJoinListener( -// GetUnacknowledgedWarnings( -// container.playerWarningRepository, -// container.dateTimeFormatter, -// ), -// ), -// ) -// } -// } \ No newline at end of file diff --git a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/deprecated/modules/moderation/warnings/actions/AcknowledgeWarning.kt b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/deprecated/modules/moderation/warnings/actions/AcknowledgeWarning.kt deleted file mode 100644 index ec4b28cd5..000000000 --- a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/deprecated/modules/moderation/warnings/actions/AcknowledgeWarning.kt +++ /dev/null @@ -1,14 +0,0 @@ -package com.projectcitybuild.modules.moderation.warnings.actions - -import com.projectcitybuild.pcbridge.core.utils.Result -import com.projectcitybuild.pcbridge.core.utils.Success -import com.projectcitybuild.repositories.PlayerWarningRepository - -class AcknowledgeWarning( - private val playerWarningRepository: PlayerWarningRepository, -) { - suspend fun execute(warningId: Int): Result { - playerWarningRepository.acknowledge(warningId = warningId) - return Success(Unit) - } -} diff --git a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/deprecated/modules/moderation/warnings/commands/WarningAcknowledgeCommand.kt b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/deprecated/modules/moderation/warnings/commands/WarningAcknowledgeCommand.kt deleted file mode 100644 index 7cdd55671..000000000 --- a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/deprecated/modules/moderation/warnings/commands/WarningAcknowledgeCommand.kt +++ /dev/null @@ -1,14 +0,0 @@ -// package com.projectcitybuild.modules.moderation.warnings.commands -// -// import com.projectcitybuild.modules.moderation.warnings.actions.AcknowledgeWarning -// import com.projectcitybuild.support.textcomponent.send -// import org.bukkit.entity.Player -// -// class WarningAcknowledgeCommand( -// private val acknowledgeWarning: AcknowledgeWarning, -// ) { -// suspend fun execute(commandSender: Player, warningId: Int) { -// acknowledgeWarning.execute(warningId) -// commandSender.send().success("Warning acknowledged and hidden") -// } -// } diff --git a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/deprecated/modules/moderation/warnings/listeners/NotifyWarningsOnJoinListener.kt b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/deprecated/modules/moderation/warnings/listeners/NotifyWarningsOnJoinListener.kt deleted file mode 100644 index a4bbd3f68..000000000 --- a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/deprecated/modules/moderation/warnings/listeners/NotifyWarningsOnJoinListener.kt +++ /dev/null @@ -1,59 +0,0 @@ -// package com.projectcitybuild.modules.moderation.warnings.listeners -// -// import com.projectcitybuild.modules.moderation.warnings.actions.GetUnacknowledgedWarnings -// import com.projectcitybuild.support.spigot.listeners.SpigotListener -// import com.projectcitybuild.support.textcomponent.add -// import kotlinx.coroutines.CoroutineScope -// import kotlinx.coroutines.Dispatchers -// import kotlinx.coroutines.launch -// import net.md_5.bungee.api.ChatColor -// import net.md_5.bungee.api.chat.ClickEvent -// import net.md_5.bungee.api.chat.TextComponent -// import org.bukkit.event.EventHandler -// import org.bukkit.event.player.PlayerJoinEvent -// -// class NotifyWarningsOnJoinListener( -// private val getUnacknowledgedWarnings: GetUnacknowledgedWarnings, -// ) : SpigotListener { -// -// @EventHandler -// override suspend fun handle(event: PlayerJoinEvent) { -// val player = event.player -// -// CoroutineScope(Dispatchers.IO).launch { -// val warnings = getUnacknowledgedWarnings.execute( -// playerUUID = player.uniqueId, -// playerName = player.name, -// ) -// if (warnings.isEmpty()) { -// return@launch -// } -// val tc = TextComponent() -// .add("You have ") { it.color = ChatColor.RED } -// .add(warnings.size) { it.isBold = true } -// .add(" unacknowledged warnings\n") { it.color = ChatColor.RED } -// .add("---\n") -// -// warnings.forEach { warning -> -// tc.add("${warning.reason}\n") -// tc.add("Date: ") { it.color = ChatColor.GRAY } -// tc.add("${warning.createdAt}\n\n") -// tc.add("[I ACKNOWLEDGE]") { -// it.isUnderlined = true -// it.isBold = true -// it.color = ChatColor.GOLD -// it.clickEvent = ClickEvent(ClickEvent.Action.RUN_COMMAND, "/warning acknowledge ${warning.id}") -// } -// tc.add("\n") -// tc.add("---\n") -// } -// -// tc.add("Click the 'acknowledge' button to mark it as read and hide it") { -// it.color = ChatColor.GRAY -// it.isItalic = true -// } -// -// player.spigot().sendMessage(tc) -// } -// } -// } diff --git a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/deprecated/modules/moderation/warnings/actions/GetUnacknowledgedWarnings.kt b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/features/warnings/actions/GetUnacknowledgedWarnings.kt similarity index 79% rename from pcbridge-spigot/src/main/kotlin/com/projectcitybuild/deprecated/modules/moderation/warnings/actions/GetUnacknowledgedWarnings.kt rename to pcbridge-spigot/src/main/kotlin/com/projectcitybuild/features/warnings/actions/GetUnacknowledgedWarnings.kt index 902cf21ff..29ebe04b4 100644 --- a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/deprecated/modules/moderation/warnings/actions/GetUnacknowledgedWarnings.kt +++ b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/features/warnings/actions/GetUnacknowledgedWarnings.kt @@ -1,11 +1,11 @@ -package com.projectcitybuild.modules.moderation.warnings.actions +package com.projectcitybuild.features.warnings.actions import com.projectcitybuild.pcbridge.core.modules.datetime.formatter.DateTimeFormatter -import com.projectcitybuild.repositories.PlayerWarningRepository +import com.projectcitybuild.features.warnings.repositories.PlayerWarningRepository import java.util.UUID class GetUnacknowledgedWarnings( - private val playerWarningRepository: PlayerWarningRepository, + private val warningRepository: PlayerWarningRepository, private val dateTimeFormatter: DateTimeFormatter, ) { data class FormattedWarning( @@ -19,7 +19,7 @@ class GetUnacknowledgedWarnings( playerUUID: UUID, playerName: String ): List { - return playerWarningRepository + return warningRepository .get(playerUUID, playerName) .filter { !it.isAcknowledged } .map { diff --git a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/features/warnings/commands/WarningAcknowledgeCommand.kt b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/features/warnings/commands/WarningAcknowledgeCommand.kt new file mode 100644 index 000000000..7faf25e1c --- /dev/null +++ b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/features/warnings/commands/WarningAcknowledgeCommand.kt @@ -0,0 +1,34 @@ +package com.projectcitybuild.features.warnings.commands + +import com.projectcitybuild.features.warnings.repositories.PlayerWarningRepository +import com.projectcitybuild.support.messages.CommandHelpBuilder +import com.projectcitybuild.support.spigot.BadCommandUsageException +import com.projectcitybuild.support.spigot.CommandArgsParser +import com.projectcitybuild.support.spigot.SpigotCommand +import org.bukkit.command.CommandSender + +class WarningAcknowledgeCommand( + private val warningRepository: PlayerWarningRepository, +): SpigotCommand { + override val label = "warning" + + override val usage = CommandHelpBuilder() + + override suspend fun run(sender: CommandSender, args: Args) { + warningRepository.acknowledge(args.id) + sender.sendMessage("Warning acknowledged and hidden") + } + + data class Args( + val id: Int, + ) { + class Parser: CommandArgsParser { + override fun parse(args: List): Args { + if (args.isEmpty()) { + throw BadCommandUsageException() + } + return Args(id = args[0].toInt()) + } + } + } +} diff --git a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/features/warnings/listeners/NotifyWarningsOnJoinListener.kt b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/features/warnings/listeners/NotifyWarningsOnJoinListener.kt new file mode 100644 index 000000000..d0534b0b6 --- /dev/null +++ b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/features/warnings/listeners/NotifyWarningsOnJoinListener.kt @@ -0,0 +1,60 @@ +package com.projectcitybuild.features.warnings.listeners + +import com.projectcitybuild.features.warnings.actions.GetUnacknowledgedWarnings +import net.kyori.adventure.text.Component +import net.kyori.adventure.text.event.ClickEvent +import net.kyori.adventure.text.format.NamedTextColor +import net.kyori.adventure.text.format.TextDecoration +import org.bukkit.event.EventHandler +import org.bukkit.event.Listener +import org.bukkit.event.player.PlayerJoinEvent + +class NotifyWarningsOnJoinListener( + private val getUnacknowledgedWarnings: GetUnacknowledgedWarnings, +) : Listener { + @EventHandler + suspend fun onPlayerJoin(event: PlayerJoinEvent) { + val player = event.player + val warnings = getUnacknowledgedWarnings.execute( + playerUUID = player.uniqueId, + playerName = player.name, + ) + if (warnings.isEmpty()) { + return + } + + val message = Component.text() + .append( + Component.text("You have ").color(NamedTextColor.RED), + Component.text(warnings.size).decorate(TextDecoration.BOLD), + Component.text(" unacknowledged warnings").color(NamedTextColor.RED), + Component.newline(), + Component.text("---").color(NamedTextColor.GRAY), + Component.newline(), + ) + + warnings.forEach { warning -> + message.append( + Component.text(warning.reason), + Component.newline(), + Component.text("Date: ").color(NamedTextColor.GRAY), + Component.text(warning.createdAt), + Component.newline(), + Component.newline(), + Component.text("[I ACKNOWLEDGE]") + .color(NamedTextColor.GOLD) + .decorate(TextDecoration.BOLD, TextDecoration.UNDERLINED) + .clickEvent(ClickEvent.runCommand("/warning acknowledge ${warning.id}")), + Component.newline(), + Component.text("---").color(NamedTextColor.GRAY), + Component.newline(), + ) + } + message.append( + Component.text("Click the 'acknowledge' button to mark it as read and hide it") + .color(NamedTextColor.GRAY) + .decorate(TextDecoration.ITALIC), + ) + player.sendMessage(message) + } +} diff --git a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/deprecated/repositories/PlayerWarningRepository.kt b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/features/warnings/repositories/PlayerWarningRepository.kt similarity index 53% rename from pcbridge-spigot/src/main/kotlin/com/projectcitybuild/deprecated/repositories/PlayerWarningRepository.kt rename to pcbridge-spigot/src/main/kotlin/com/projectcitybuild/features/warnings/repositories/PlayerWarningRepository.kt index eb1fc320f..2d66e3a90 100644 --- a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/deprecated/repositories/PlayerWarningRepository.kt +++ b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/features/warnings/repositories/PlayerWarningRepository.kt @@ -1,4 +1,4 @@ -package com.projectcitybuild.repositories +package com.projectcitybuild.features.warnings.repositories import com.projectcitybuild.pcbridge.http.responses.PlayerWarning import com.projectcitybuild.pcbridge.http.services.pcb.PlayerWarningHttpService @@ -14,22 +14,6 @@ class PlayerWarningRepository( ) } - suspend fun create( - warnedPlayerUUID: UUID, - warnedPlayerName: String, - warnerPlayerUUID: UUID, - warnerPlayerName: String, - reason: String, - ): PlayerWarning? { - return playerWarningHttpService.create( - warnedPlayerUUID = warnedPlayerUUID, - warnedPlayerName = warnedPlayerName, - warnerPlayerUUID = warnerPlayerUUID, - warnerPlayerName = warnerPlayerName, - reason = reason, - ) - } - suspend fun acknowledge(warningId: Int): PlayerWarning? { return playerWarningHttpService.acknowledge(warningId) }