Skip to content

Commit

Permalink
Merge pull request #163 from ToberoCat/usage-limits-fix
Browse files Browse the repository at this point in the history
Fixed the issue with a missing transaction
  • Loading branch information
ToberoCat authored Jun 9, 2024
2 parents ff98fda + b9c05e8 commit dbe0a18
Showing 1 changed file with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.toberocat.improvedfactions.utils.options.limit

import io.github.toberocat.improvedfactions.database.DatabaseManager.loggedTransaction
import io.github.toberocat.improvedfactions.translation.sendLocalized
import io.github.toberocat.toberocore.command.exceptions.CommandException
import io.github.toberocat.toberocore.command.options.Option
Expand All @@ -12,18 +13,24 @@ class UsageLimitOption(
private val registry: String,
private val byPassPermissions: String = "factions.bypass.usage-limits.$registry"
) : Option {
override fun execute(sender: CommandSender, args: Array<String>): Array<String> {
return when {
override fun execute(sender: CommandSender, args: Array<String>) = loggedTransaction {
return@loggedTransaction when {
limit < 0 -> args
sender.hasPermission(byPassPermissions) -> args
sender !is Player -> throw CommandException("base.exceptions.sender-must-be-player", emptyMap())
else -> {
val entry = PlayerUsageLimits.getUsageLimit(registry, sender.uniqueId)
if (entry.used >= limit) {
throw CommandException("base.exceptions.command-limit-reached", mapOf("limit" to limit.toString()))
throw CommandException(
"base.exceptions.command-limit-reached",
mapOf("limit" to limit.toString())
)
} else {
entry.used = entry.used.plus(1)
sender.sendLocalized("base.messages.command-usage-limit", mapOf("left" to (limit - entry.used).toString()))
sender.sendLocalized(
"base.messages.command-usage-limit",
mapOf("left" to (limit - entry.used).toString())
)
args
}
}
Expand Down

0 comments on commit dbe0a18

Please sign in to comment.