Skip to content

Commit

Permalink
bypass permission for bypassing the download limit
Browse files Browse the repository at this point in the history
  • Loading branch information
Gutin1 committed Apr 27, 2024
1 parent b0112dd commit c751f41
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ object MiscStarshipCommands : net.horizonsend.ion.server.command.SLCommand() {
PilotedStarships.tryPilot(sender, starshipData)
}

val uploadCooldown = object : PerPlayerCooldown(60L, TimeUnit.SECONDS) {
private val uploadCooldown = object : PerPlayerCooldown(5L, TimeUnit.SECONDS, bypassPermission = "ion.starship.bypassdownloadlimit") {
override fun cooldownRejected(player: UUID) {
Bukkit.getPlayer(player)?.userError("You're doing that too often!")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ object StarshipComputers : IonServerComponent() {
tryOpenMenu(player, data)
}

private val pilotCooldown = PerPlayerCooldown.messagedCooldown(250, TimeUnit.MILLISECONDS) { Bukkit.getPlayer(it)?.userError("You're doing that too often!") }
private val pilotCooldown = PerPlayerCooldown.callbackCooldown(250, TimeUnit.MILLISECONDS) { Bukkit.getPlayer(it)?.userError("You're doing that too often!") }

private fun handleRightClick(data: StarshipData?, player: Player) {
if (data == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.horizonsend.ion.server.miscellaneous.utils

import net.horizonsend.ion.common.utils.lpHasPermission
import net.horizonsend.ion.server.features.starship.control.controllers.Controller
import net.horizonsend.ion.server.features.starship.damager.Damager
import org.bukkit.entity.Player
Expand All @@ -14,7 +15,7 @@ open class AbstractCooldown <T> (cooldown: Long, timeUnit: TimeUnit = TimeUnit.M

fun tryExec(player: T, block: () -> Unit) = tryExec(player, this.cooldownNanos, TimeUnit.NANOSECONDS, block)

fun tryExec(player: T, cooldown: Long, timeUnit: TimeUnit, block: () -> Unit) {
open fun tryExec(player: T, cooldown: Long, timeUnit: TimeUnit, block: () -> Unit) {
if (nanoTime() - map.getOrElse(player) { 0 } >= timeUnit.toNanos(cooldown)) {
map[player] = nanoTime()
block()
Expand All @@ -24,11 +25,17 @@ open class AbstractCooldown <T> (cooldown: Long, timeUnit: TimeUnit = TimeUnit.M
open fun cooldownRejected(player: T) {}
}

open class PerPlayerCooldown(cooldown: Long, timeUnit: TimeUnit = TimeUnit.MILLISECONDS): AbstractCooldown<UUID>(cooldown, timeUnit) {
open class PerPlayerCooldown(cooldown: Long, timeUnit: TimeUnit = TimeUnit.MILLISECONDS, val bypassPermission: String? = null): AbstractCooldown<UUID>(cooldown, timeUnit) {
fun tryExec(player: Player, block: () -> Unit) = tryExec(player.uniqueId, this.cooldownNanos, TimeUnit.NANOSECONDS, block)

override fun tryExec(player: UUID, cooldown: Long, timeUnit: TimeUnit, block: () -> Unit) {
if (bypassPermission != null && player.lpHasPermission(bypassPermission)) return block()

super.tryExec(player, cooldown, timeUnit, block)
}

companion object {
fun messagedCooldown(delay: Long, unit: TimeUnit, rejected: (UUID) -> Unit): PerPlayerCooldown = object : PerPlayerCooldown(delay, unit) {
fun callbackCooldown(delay: Long, unit: TimeUnit, rejected: (UUID) -> Unit): PerPlayerCooldown = object : PerPlayerCooldown(delay, unit) {
override fun cooldownRejected(player: UUID) = rejected(player)
}
}
Expand Down

0 comments on commit c751f41

Please sign in to comment.