Cleanup ability context

This commit is contained in:
Laurin
2025-12-01 19:45:49 +01:00
parent 7634694b2e
commit ac185e35ce
5 changed files with 147 additions and 73 deletions

View File

@@ -12,47 +12,64 @@ class AbilityContext(
key: String, key: String,
requiredHits: Int? = null, requiredHits: Int? = null,
cooldownSeconds: Int? = null cooldownSeconds: Int? = null
): AbilityResult { ): AbilityResult
if (cooldownSeconds != null && cooldownManager.isOnCooldown(player, key)) { {
val remaining = cooldownManager.getRemainingSeconds(player, key) if ( cooldownSeconds != null && cooldownManager.isOnCooldown( player, key ))
return AbilityResult.onCooldown(remaining) {
val remaining = cooldownManager.getRemainingSeconds( player, key )
return AbilityResult.onCooldown( remaining )
} }
if (requiredHits != null && !hitCounterManager.hasReachedThreshold(player, key, requiredHits)) { if ( requiredHits != null && !hitCounterManager.hasReachedThreshold( player, key, requiredHits ))
val missing = hitCounterManager.getRemainingHits(player, key, requiredHits) {
return AbilityResult.insufficientHits(missing) val missing = hitCounterManager.getRemainingHits( player, key, requiredHits )
return AbilityResult.insufficientHits( missing )
} }
if (requiredHits != null) { if ( requiredHits != null )
hitCounterManager.resetHits(player, key) hitCounterManager.resetHits( player, key )
}
if (cooldownSeconds != null) { if ( cooldownSeconds != null )
cooldownManager.startCooldown(player, key, cooldownSeconds) cooldownManager.startCooldown( player, key, cooldownSeconds )
}
return AbilityResult.success() return AbilityResult.success()
} }
fun incrementHit(player: Player, key: String): Int { fun incrementHit(
return hitCounterManager.incrementHit(player, key) player: Player,
key: String
): Int
{
return hitCounterManager.incrementHit( player, key )
} }
fun getHits(player: Player, key: String): Int { fun getHits(
return hitCounterManager.getHits(player, key) player: Player,
key: String
): Int
{
return hitCounterManager.getHits( player, key )
} }
fun getRemainingCooldown(player: Player, key: String): Double { fun getRemainingCooldown(
return cooldownManager.getRemainingSeconds(player, key) player: Player,
key: String
): Double
{
return cooldownManager.getRemainingSeconds( player, key )
} }
fun clearPlayerData(player: Player) { fun clearPlayerData(
cooldownManager.clearAllCooldowns(player) player: Player
hitCounterManager.clearAllHits(player) ) {
cooldownManager.clearAllCooldowns( player )
hitCounterManager.clearAllHits( player )
} }
fun clearAll() { fun clearAll()
{
cooldownManager.clearAll() cooldownManager.clearAll()
hitCounterManager.clearAll() hitCounterManager.clearAll()
} }
} }

View File

@@ -1,22 +1,27 @@
package club.mcscrims.speedhg.ability package club.mcscrims.speedhg.ability
import club.mcscrims.speedhg.SpeedHG
import org.bukkit.entity.Player import org.bukkit.entity.Player
import org.bukkit.event.EventHandler import org.bukkit.event.EventHandler
import org.bukkit.event.Listener import org.bukkit.event.Listener
import org.bukkit.event.entity.EntityDamageByEntityEvent import org.bukkit.event.entity.EntityDamageByEntityEvent
class AbilityHitListener(private val abilityContext: AbilityContext) : Listener { class AbilityHitListener(
private val plugin: SpeedHG,
private val abilityContext: AbilityContext
) : Listener {
@EventHandler @EventHandler
fun onEntityDamage(event: EntityDamageByEntityEvent) { fun onEntityDamage(
event: EntityDamageByEntityEvent
) {
val damager = event.damager val damager = event.damager
val victim = event.entity val victim = event.entity
if (damager !is Player || victim !is Player) { if ( damager !is Player || victim !is Player )
return return
}
val hitCount = abilityContext.incrementHit(damager, "melee_hit") val hitCount = abilityContext.incrementHit( damager, "melee_hit" )
val result = abilityContext.canUseAbility( val result = abilityContext.canUseAbility(
player = damager, player = damager,
@@ -25,12 +30,18 @@ class AbilityHitListener(private val abilityContext: AbilityContext) : Listener
cooldownSeconds = 10 cooldownSeconds = 10
) )
if (result.success) { if ( result.success )
damager.sendMessage("§aAbility activated! You hit ${victim.name}") {
} else if (result.missingHits > 0) { plugin.chatManager.sendMessage( damager, "ability.activated", "{player}" to victim.name )
damager.sendMessage("§e${result.missingHits} more hit(s) needed (Current: ${hitCount})") }
} else if (result.remainingCooldown > 0) { else if ( result.missingHits > 0 )
damager.sendMessage("§cAbility on cooldown: ${String.format("%.1f", result.remainingCooldown)}s remaining") {
plugin.chatManager.sendMessage( damager, "ability.more_hits", "{current}" to hitCount.toString(), "{needed}" to result.missingHits.toString() )
}
else if ( result.remainingCooldown > 0 )
{
plugin.chatManager.sendMessage( damager, "ability.cooldown", "{cooldown}" to result.remainingCooldown.toString() )
} }
} }
} }

View File

@@ -6,13 +6,16 @@ data class AbilityResult(
val remainingCooldown: Double = 0.0, val remainingCooldown: Double = 0.0,
val missingHits: Int = 0 val missingHits: Int = 0
) { ) {
companion object { companion object
fun success(): AbilityResult = AbilityResult(true, null) {
fun onCooldown(remaining: Double): AbilityResult = fun success(): AbilityResult = AbilityResult( true, null )
AbilityResult(false, "Ability is on cooldown", remainingCooldown = remaining)
fun onCooldown( remaining: Double ): AbilityResult =
AbilityResult( false, "Ability is on cooldown", remainingCooldown = remaining )
fun insufficientHits( missing: Int ): AbilityResult =
AbilityResult( false, "Not enough hits", missingHits = missing )
fun insufficientHits(missing: Int): AbilityResult =
AbilityResult(false, "Not enough hits", missingHits = missing)
} }
} }

View File

@@ -8,43 +8,61 @@ class CooldownManager {
private val cooldowns = ConcurrentHashMap<UUID, ConcurrentHashMap<String, Long>>() private val cooldowns = ConcurrentHashMap<UUID, ConcurrentHashMap<String, Long>>()
fun startCooldown(player: Player, key: String, seconds: Int) { fun startCooldown(
val expiryTime = System.currentTimeMillis() + (seconds * 1000L) player: Player,
cooldowns.computeIfAbsent(player.uniqueId) { ConcurrentHashMap() }[key] = expiryTime key: String,
seconds: Int
) {
val expiryTime = System.currentTimeMillis() + ( seconds * 1000L )
cooldowns.computeIfAbsent( player.uniqueId ) { ConcurrentHashMap() }[ key ] = expiryTime
} }
fun isOnCooldown(player: Player, key: String): Boolean { fun isOnCooldown(
player: Player,
key: String
): Boolean
{
val playerCooldowns = cooldowns[player.uniqueId] ?: return false val playerCooldowns = cooldowns[player.uniqueId] ?: return false
val expiryTime = playerCooldowns[key] ?: return false val expiryTime = playerCooldowns[key] ?: return false
if (System.currentTimeMillis() >= expiryTime) { if ( System.currentTimeMillis() >= expiryTime )
playerCooldowns.remove(key) {
if (playerCooldowns.isEmpty()) { playerCooldowns.remove( key )
cooldowns.remove(player.uniqueId) if ( playerCooldowns.isEmpty() ) cooldowns.remove( player.uniqueId )
}
return false return false
} }
return true return true
} }
fun getRemainingSeconds(player: Player, key: String): Double { fun getRemainingSeconds(
player: Player,
key: String
): Double
{
val playerCooldowns = cooldowns[player.uniqueId] ?: return 0.0 val playerCooldowns = cooldowns[player.uniqueId] ?: return 0.0
val expiryTime = playerCooldowns[key] ?: return 0.0 val expiryTime = playerCooldowns[key] ?: return 0.0
val remaining = (expiryTime - System.currentTimeMillis()) / 1000.0 val remaining = ( expiryTime - System.currentTimeMillis() ) / 1000.0
return if (remaining > 0) remaining else 0.0 return if ( remaining > 0 ) remaining else 0.0
} }
fun clearCooldown(player: Player, key: String) { fun clearCooldown(
cooldowns[player.uniqueId]?.remove(key) player: Player,
key: String
) {
cooldowns[player.uniqueId]?.remove( key )
} }
fun clearAllCooldowns(player: Player) { fun clearAllCooldowns(
cooldowns.remove(player.uniqueId) player: Player
) {
cooldowns.remove( player.uniqueId )
} }
fun clearAll() { fun clearAll()
{
cooldowns.clear() cooldowns.clear()
} }
} }

View File

@@ -8,34 +8,59 @@ class HitCounterManager {
private val hitCounts = ConcurrentHashMap<UUID, ConcurrentHashMap<String, Int>>() private val hitCounts = ConcurrentHashMap<UUID, ConcurrentHashMap<String, Int>>()
fun incrementHit(player: Player, key: String): Int { fun incrementHit(
val playerHits = hitCounts.computeIfAbsent(player.uniqueId) { ConcurrentHashMap() } player: Player,
val newCount = playerHits.compute(key) { _, current -> (current ?: 0) + 1 } ?: 1 key: String
): Int
{
val playerHits = hitCounts.computeIfAbsent( player.uniqueId ) { ConcurrentHashMap() }
val newCount = playerHits.compute( key ) { _, current -> ( current ?: 0 ) + 1 } ?: 1
return newCount return newCount
} }
fun getHits(player: Player, key: String): Int { fun getHits(
return hitCounts[player.uniqueId]?.get(key) ?: 0 player: Player,
key: String
): Int
{
return hitCounts[player.uniqueId]?.get( key ) ?: 0
} }
fun resetHits(player: Player, key: String) { fun resetHits(
hitCounts[player.uniqueId]?.remove(key) player: Player,
key: String
) {
hitCounts[player.uniqueId]?.remove( key )
} }
fun hasReachedThreshold(player: Player, key: String, requiredHits: Int): Boolean { fun hasReachedThreshold(
return getHits(player, key) >= requiredHits player: Player,
key: String,
requiredHits: Int
): Boolean
{
return getHits( player, key ) >= requiredHits
} }
fun getRemainingHits(player: Player, key: String, requiredHits: Int): Int { fun getRemainingHits(
val current = getHits(player, key) player: Player,
return maxOf(0, requiredHits - current) key: String,
requiredHits: Int
): Int
{
val current = getHits( player, key )
return maxOf( 0, requiredHits - current )
} }
fun clearAllHits(player: Player) { fun clearAllHits(
hitCounts.remove(player.uniqueId) player: Player
) {
hitCounts.remove( player.uniqueId )
} }
fun clearAll() { fun clearAll()
{
hitCounts.clear() hitCounts.clear()
} }
} }