Skip to content

Commit

Permalink
Evaluate report cooldown only if last report was present
Browse files Browse the repository at this point in the history
and only populate usage cache on report success
and do not block the main thread to post the report
  • Loading branch information
chatasma committed Jul 4, 2024
1 parent d5f869d commit 47d0caa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
6 changes: 4 additions & 2 deletions src/main/kotlin/network/warzone/mars/report/ReportFeature.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ object ReportFeature : Feature<Report>(), Listener {


@EventHandler
fun onReportCreate(event: PlayerReportEvent) = runBlocking {
fun onReportCreate(event: PlayerReportEvent) {
val onlineStaff = Bukkit.getOnlinePlayers().filter { it.hasPermission("pgm.staff") }
.map { SimplePlayer(it.uniqueId, it.name) }.toSet()
ReportService.create(event.player.simple, event.sender.simple, event.reason, onlineStaff)
Mars.async {
ReportService.create(event.player.simple, event.sender.simple, event.reason, onlineStaff)
}
}

override fun getCommands(): List<Any> = listOf(ReportCommands())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,22 @@ class ReportCommands {
fun report(@Sender sender: Player, target: Player, @Text reason: String) {
val matchPlayer = sender.matchPlayer
val match = matchPlayer.match
if (!sender.hasPermission(Permissions.STAFF)) {
val needsCooldown = !sender.hasPermission(Permissions.STAFF)
if (needsCooldown) {
// Check for cooldown
val lastReport: Instant? = LAST_REPORT_SENT.getIfPresent(sender.uniqueId)
lastReport ?: LAST_REPORT_SENT.put(sender.uniqueId, Instant.now())

val timeSinceReport: Duration = Duration.between(lastReport, Instant.now())
val secondsRemaining: Long = REPORT_COOLDOWN_SECONDS - timeSinceReport.getSeconds()
if (secondsRemaining > 0) {
val secondsComponent: TextComponent = text(secondsRemaining.toString())
val secondsLeftComponent: TextComponent = text()
if (lastReport != null) {
val timeSinceReport: Duration = Duration.between(lastReport, Instant.now())
val secondsRemaining: Long = REPORT_COOLDOWN_SECONDS - timeSinceReport.getSeconds()
if (secondsRemaining > 0) {
val secondsComponent: TextComponent = text(secondsRemaining.toString())
val secondsLeftComponent: TextComponent = text()
.append(secondsComponent)
.append(text(if (secondsRemaining != 1L) "misc.seconds" else "misc.second")).build()
matchPlayer.sendWarning(text("Please wait ").append(secondsLeftComponent).append(text(" before running that command again")))
return
matchPlayer.sendWarning(text("Please wait ").append(secondsLeftComponent).append(text(" before running that command again")))
return
}
}

}
val accused: MatchPlayer? = match.getPlayer(target)

Expand Down Expand Up @@ -115,6 +115,9 @@ class ReportCommands {
reason = reason
)
)
if (needsCooldown) {
LAST_REPORT_SENT.put(sender.uniqueId, Instant.now())
}
ChatDispatcher.broadcastAdminChatMessage(component, match, Optional.of(REPORT_NOTIFY_SOUND))
}

Expand Down

0 comments on commit 47d0caa

Please sign in to comment.