Skip to content

Commit

Permalink
Add warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
andyksaw committed May 7, 2024
1 parent 823ea8f commit 78ef956
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 151 deletions.
28 changes: 28 additions & 0 deletions pcbridge-spigot/src/main/kotlin/com/projectcitybuild/Container.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -114,6 +118,7 @@ fun pluginModule(_plugin: JavaPlugin) = module {
telemetry()
utilities()
warps()
warnings()
}

private fun Module.spigot(plugin: JavaPlugin) {
Expand Down Expand Up @@ -634,4 +639,27 @@ private fun Module.utilities() {
factory {
PCBridgeCommand.TabCompleter()
}
}

private fun Module.warnings() {
factory {
PlayerWarningRepository(
playerWarningHttpService = get<HttpService>().playerWarning,
)
}

factory {
WarningAcknowledgeCommand(
warningRepository = get(),
)
}

factory {
NotifyWarningsOnJoinListener(
getUnacknowledgedWarnings = GetUnacknowledgedWarnings(
dateTimeFormatter = get(),
warningRepository = get(),
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -148,6 +150,10 @@ private class Lifecycle: KoinComponent {
handler = get<SyncOtherCommand>(),
argsParser = SyncOtherCommand.Args.Parser(),
)
register(
handler = get<WarningAcknowledgeCommand>(),
argsParser = WarningAcknowledgeCommand.Args.Parser(),
)
}
listenerRegistry.register(
get<AnnounceJoinListener>(),
Expand All @@ -165,6 +171,7 @@ private class Lifecycle: KoinComponent {
get<SyncBadgesOnJoinListener>(),
get<FormatNameChatListener>(),
get<AnnouncementEnableListener>(),
get<NotifyWarningsOnJoinListener>(),
)

get<DynmapIntegration>().enable()
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -19,7 +19,7 @@ class GetUnacknowledgedWarnings(
playerUUID: UUID,
playerName: String
): List<FormattedWarning> {
return playerWarningRepository
return warningRepository
.get(playerUUID, playerName)
.filter { !it.isAcknowledged }
.map {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<WarningAcknowledgeCommand.Args> {
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<Args> {
override fun parse(args: List<String>): Args {
if (args.isEmpty()) {
throw BadCommandUsageException()
}
return Args(id = args[0].toInt())
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
}
Expand Down

0 comments on commit 78ef956

Please sign in to comment.