Skip to content

Commit

Permalink
per shield display cooldown
Browse files Browse the repository at this point in the history
  • Loading branch information
Gutin1 committed Jan 9, 2024
1 parent 5bdb87e commit 0d69253
Showing 1 changed file with 45 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import net.horizonsend.ion.common.utils.text.plainText
import net.horizonsend.ion.server.IonServer
import net.horizonsend.ion.server.features.multiblock.InteractableMultiblock
import net.horizonsend.ion.server.features.multiblock.Multiblock
import net.horizonsend.ion.server.miscellaneous.utils.PerPlayerCooldown
import net.horizonsend.ion.server.miscellaneous.utils.AbstractCooldown
import net.horizonsend.ion.server.miscellaneous.utils.Tasks
import net.horizonsend.ion.server.miscellaneous.utils.Vec3i
import net.horizonsend.ion.server.miscellaneous.utils.blockKey
import org.bukkit.Bukkit
import org.bukkit.Material
import org.bukkit.Particle
import org.bukkit.block.Sign
import org.bukkit.entity.Player
import org.bukkit.event.player.PlayerInteractEvent
import java.util.UUID
import java.util.concurrent.TimeUnit

abstract class ShieldMultiblock : Multiblock(), InteractableMultiblock {
Expand Down Expand Up @@ -42,43 +44,51 @@ abstract class ShieldMultiblock : Multiblock(), InteractableMultiblock {
abstract fun getShieldBlocks(sign: Sign): List<Vec3i>

companion object {
private val displayCooldown = PerPlayerCooldown.messagedCooldown(5L, TimeUnit.SECONDS) {
Bukkit.getPlayer(it)?.userError("You're doing that too often!")
val cooldown = object : AbstractCooldown<Pair<UUID, Long>>(5L, TimeUnit.SECONDS) {
override fun cooldownRejected(player: Pair<UUID, Long>) {
val (uuid, _) = player
Bukkit.getPlayer(uuid)?.userError("You're doing that too often!")
}
}
}

override fun onSignInteract(sign: Sign, player: Player, event: PlayerInteractEvent) = displayCooldown.tryExec(player.uniqueId) {
val blocks: List<Vec3i> = getShieldBlocks(sign)

val world = sign.world
val (x0, y0, z0) = Vec3i(sign.location)

val start = System.nanoTime()

val barrier = Material.BARRIER.createBlockData()

Tasks.bukkitRunnable {
for ((dx, dy, dz) in blocks) {
val x = x0 + dx + 0.5
val y = y0 + dy + 0.5
val z = z0 + dz + 0.5
world.spawnParticle(
Particle.BLOCK_MARKER,
x,
y,
z,
1,
0.0,
0.0,
0.0,
0.0,
barrier
)
}
override fun onSignInteract(sign: Sign, player: Player, event: PlayerInteractEvent) {
val (signX, signY, signZ) = Vec3i(sign.location)
val key = blockKey(signX, signY, signZ)

if (System.nanoTime() - start > TimeUnit.SECONDS.toNanos(10L)) {
cancel()
}
}.runTaskTimer(IonServer, 20, 20)
cooldown.tryExec(player.uniqueId to key) {
val blocks: List<Vec3i> = getShieldBlocks(sign)

val world = sign.world
val (x0, y0, z0) = Vec3i(sign.location)

val start = System.nanoTime()

val barrier = Material.BARRIER.createBlockData()

Tasks.bukkitRunnable {
for ((dx, dy, dz) in blocks) {
val x = x0 + dx + 0.5
val y = y0 + dy + 0.5
val z = z0 + dz + 0.5
world.spawnParticle(
Particle.BLOCK_MARKER,
x,
y,
z,
1,
0.0,
0.0,
0.0,
0.0,
barrier
)
}

if (System.nanoTime() - start > TimeUnit.SECONDS.toNanos(10L)) {
cancel()
}
}.runTaskTimer(IonServer, 20, 20)
}
}
}

0 comments on commit 0d69253

Please sign in to comment.