From 9db6360a4b90ae0304579ad3c9a21409f6b1dc6a Mon Sep 17 00:00:00 2001 From: Boy Date: Tue, 13 Aug 2024 12:45:00 +0200 Subject: [PATCH] refactor: move StaminaTask to StaminaModule to refresh configs on reload --- gradle.properties | 2 +- gradle/libs.versions.toml | 2 +- .../staminaclimb/StaminaClimbPlugin.kt | 9 ++-- .../staminaclimb/StaminaCommands.kt | 6 +++ .../staminaclimb/climbing/ClimbBehaviour.kt | 2 +- .../staminaclimb/config/StaminaConfig.kt | 2 +- .../staminaclimb/modules/StaminaModule.kt | 2 + .../modules/StaminaPaperModule.kt | 6 +-- .../staminaclimb/stamina/StaminaBar.kt | 41 ++++++--------- .../staminaclimb/stamina/StaminaTask.kt | 51 ++++++++++--------- 10 files changed, 59 insertions(+), 64 deletions(-) diff --git a/gradle.properties b/gradle.properties index 632820b..330a2c3 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ group=com.mineinabyss version=0.23 -idofrontVersion=0.24.16 +idofrontVersion=0.24.24 diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 12bc3fd..475c465 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,5 +1,5 @@ [versions] -geary = "0.30.10-dev.1" +geary = "0.30.11" [libraries] geary-papermc = { module = "com.mineinabyss:geary-papermc", version.ref = "geary" } diff --git a/src/main/kotlin/com/mineinabyss/staminaclimb/StaminaClimbPlugin.kt b/src/main/kotlin/com/mineinabyss/staminaclimb/StaminaClimbPlugin.kt index 43db6e3..87b80f7 100644 --- a/src/main/kotlin/com/mineinabyss/staminaclimb/StaminaClimbPlugin.kt +++ b/src/main/kotlin/com/mineinabyss/staminaclimb/StaminaClimbPlugin.kt @@ -1,20 +1,17 @@ package com.mineinabyss.staminaclimb -import com.github.shynixn.mccoroutine.bukkit.launch import com.mineinabyss.geary.autoscan.autoscan import com.mineinabyss.geary.modules.geary import com.mineinabyss.idofront.di.DI import com.mineinabyss.idofront.plugin.listeners -import com.mineinabyss.idofront.time.ticks import com.mineinabyss.staminaclimb.climbing.ClimbBehaviour import com.mineinabyss.staminaclimb.climbing.ClimbBehaviour.stopClimbing import com.mineinabyss.staminaclimb.modules.StaminaClimbModule import com.mineinabyss.staminaclimb.modules.StaminaPaperModule +import com.mineinabyss.staminaclimb.modules.stamina import com.mineinabyss.staminaclimb.nms.Tags import com.mineinabyss.staminaclimb.stamina.StaminaBar import com.mineinabyss.staminaclimb.stamina.StaminaBar.registerBar -import com.mineinabyss.staminaclimb.stamina.StaminaTask -import kotlinx.coroutines.delay import org.bukkit.Bukkit import org.bukkit.plugin.java.JavaPlugin @@ -31,10 +28,10 @@ class StaminaClimbPlugin : JavaPlugin() { } override fun onEnable() { - StaminaTask().runTaskTimer(this@StaminaClimbPlugin, 0, 1) + stamina.staminaTask.runTaskTimer(this@StaminaClimbPlugin, 0, 1) // toggle system on for all online players (for plugin reload) - Bukkit.getOnlinePlayers().forEach { registerBar(it) } + Bukkit.getOnlinePlayers().forEach(::registerBar) listeners(ClimbBehaviour, StaminaBar) StaminaCommands() diff --git a/src/main/kotlin/com/mineinabyss/staminaclimb/StaminaCommands.kt b/src/main/kotlin/com/mineinabyss/staminaclimb/StaminaCommands.kt index 1dcffc9..b31f7b0 100644 --- a/src/main/kotlin/com/mineinabyss/staminaclimb/StaminaCommands.kt +++ b/src/main/kotlin/com/mineinabyss/staminaclimb/StaminaCommands.kt @@ -4,8 +4,10 @@ import com.mineinabyss.idofront.commands.execution.IdofrontCommandExecutor import com.mineinabyss.idofront.commands.extensions.actions.playerAction import com.mineinabyss.idofront.messaging.info import com.mineinabyss.idofront.messaging.success +import com.mineinabyss.staminaclimb.climbing.ClimbBehaviour import com.mineinabyss.staminaclimb.modules.stamina import com.mineinabyss.staminaclimb.nms.Tags +import com.mineinabyss.staminaclimb.stamina.StaminaBar import org.bukkit.command.Command import org.bukkit.command.CommandSender import org.bukkit.command.TabCompleter @@ -24,7 +26,11 @@ class StaminaCommands : IdofrontCommandExecutor(), TabCompleter { "staminaclimb" { "reload" { action { + stamina.staminaTask.cancel() stamina.plugin.createClimbContext() + stamina.staminaTask.runTaskTimer(stamina.plugin, 0, 1) + StaminaBar.conf = stamina.config + ClimbBehaviour.conf = stamina.config sender.success("Config has been reloaded!") } } diff --git a/src/main/kotlin/com/mineinabyss/staminaclimb/climbing/ClimbBehaviour.kt b/src/main/kotlin/com/mineinabyss/staminaclimb/climbing/ClimbBehaviour.kt index 8953cb7..5f740c9 100644 --- a/src/main/kotlin/com/mineinabyss/staminaclimb/climbing/ClimbBehaviour.kt +++ b/src/main/kotlin/com/mineinabyss/staminaclimb/climbing/ClimbBehaviour.kt @@ -20,7 +20,7 @@ import java.util.* import java.util.concurrent.ConcurrentHashMap object ClimbBehaviour : Listener { - private val conf = stamina.config + internal var conf = stamina.config val canClimb: MutableMap = mutableMapOf() val isClimbing: MutableMap = ConcurrentHashMap() val cooldown: MutableMap = HashMap() diff --git a/src/main/kotlin/com/mineinabyss/staminaclimb/config/StaminaConfig.kt b/src/main/kotlin/com/mineinabyss/staminaclimb/config/StaminaConfig.kt index e9c6c8d..4bd2564 100644 --- a/src/main/kotlin/com/mineinabyss/staminaclimb/config/StaminaConfig.kt +++ b/src/main/kotlin/com/mineinabyss/staminaclimb/config/StaminaConfig.kt @@ -12,7 +12,7 @@ class StaminaConfig( val staminaRegenInAir: Float = 0.003f, val staminaRemovePerTick: Float = 0.005f, val staminaRemoveWhileMoving: Float = 0.005f, - val staminaRemoveWhileOnLadder: Float = 0.01f, + val staminaRemoveWhileOnClimbable: Float = 0.01f, val baseBarColor: Color = Color.GREEN, val baseOverlay: Overlay = Overlay.NOTCHED_10, val barRed: Float = 0.02f, diff --git a/src/main/kotlin/com/mineinabyss/staminaclimb/modules/StaminaModule.kt b/src/main/kotlin/com/mineinabyss/staminaclimb/modules/StaminaModule.kt index 5775346..400e6cb 100644 --- a/src/main/kotlin/com/mineinabyss/staminaclimb/modules/StaminaModule.kt +++ b/src/main/kotlin/com/mineinabyss/staminaclimb/modules/StaminaModule.kt @@ -3,6 +3,7 @@ package com.mineinabyss.staminaclimb.modules import com.mineinabyss.idofront.di.DI import com.mineinabyss.staminaclimb.StaminaClimbPlugin import com.mineinabyss.staminaclimb.config.StaminaConfig +import com.mineinabyss.staminaclimb.stamina.StaminaTask import net.minecraft.core.Registry import net.minecraft.resources.ResourceKey import net.minecraft.tags.TagNetworkSerialization.NetworkPayload @@ -12,6 +13,7 @@ val stamina: StaminaClimbModule by DI.observe() interface StaminaClimbModule { val plugin: StaminaClimbPlugin val config: StaminaConfig + val staminaTask: StaminaTask val disabledClimbingTags: MutableMap?>, NetworkPayload> val initialTags: MutableMap?>, NetworkPayload> } diff --git a/src/main/kotlin/com/mineinabyss/staminaclimb/modules/StaminaPaperModule.kt b/src/main/kotlin/com/mineinabyss/staminaclimb/modules/StaminaPaperModule.kt index 80ac82a..8aa8bf3 100644 --- a/src/main/kotlin/com/mineinabyss/staminaclimb/modules/StaminaPaperModule.kt +++ b/src/main/kotlin/com/mineinabyss/staminaclimb/modules/StaminaPaperModule.kt @@ -1,20 +1,18 @@ package com.mineinabyss.staminaclimb.modules -import com.mineinabyss.idofront.config.IdofrontConfig import com.mineinabyss.idofront.config.config import com.mineinabyss.staminaclimb.StaminaClimbPlugin import com.mineinabyss.staminaclimb.config.StaminaConfig -import com.mineinabyss.staminaclimb.nms.Tags -import it.unimi.dsi.fastutil.ints.IntArrayList +import com.mineinabyss.staminaclimb.stamina.StaminaTask import net.minecraft.core.Registry import net.minecraft.resources.ResourceKey -import net.minecraft.resources.ResourceLocation import net.minecraft.tags.TagNetworkSerialization.NetworkPayload class StaminaPaperModule( override val plugin: StaminaClimbPlugin ) : StaminaClimbModule { override val config by config("config", plugin.dataPath, StaminaConfig()) + override val staminaTask: StaminaTask = StaminaTask(config) override val disabledClimbingTags: MutableMap?>, NetworkPayload> = mutableMapOf() override val initialTags: MutableMap?>, NetworkPayload> = mutableMapOf() } diff --git a/src/main/kotlin/com/mineinabyss/staminaclimb/stamina/StaminaBar.kt b/src/main/kotlin/com/mineinabyss/staminaclimb/stamina/StaminaBar.kt index 144e3c6..5eff3c6 100644 --- a/src/main/kotlin/com/mineinabyss/staminaclimb/stamina/StaminaBar.kt +++ b/src/main/kotlin/com/mineinabyss/staminaclimb/stamina/StaminaBar.kt @@ -9,13 +9,10 @@ import com.mineinabyss.staminaclimb.modules.stamina import com.mineinabyss.staminaclimb.nms.Tags import kotlinx.coroutines.delay import net.kyori.adventure.bossbar.BossBar -import net.minecraft.core.registries.Registries -import net.minecraft.network.protocol.common.ClientboundUpdateTagsPacket import org.bukkit.Bukkit import org.bukkit.GameMode.ADVENTURE import org.bukkit.GameMode.SURVIVAL import org.bukkit.Location -import org.bukkit.craftbukkit.entity.CraftPlayer import org.bukkit.entity.Player import org.bukkit.event.EventHandler import org.bukkit.event.Listener @@ -30,7 +27,7 @@ import kotlin.math.pow import kotlin.time.Duration.Companion.seconds object StaminaBar : Listener { - private val conf = stamina.config + internal var conf = stamina.config private val disabledPlayers: MutableList = mutableListOf() //TODO persist @PublishedApi @@ -102,36 +99,28 @@ object StaminaBar : Listener { @EventHandler fun PlayerMoveEvent.onPlayerMove() { val uuid = player.uniqueId - val climbDisabled = Tags.disabledPlayers.contains(player) + val climbDisabled = player in Tags.disabledPlayers val vel = player.velocity.y - if (vel < -0.1) { - velocities[uuid] = vel - } + if (vel < -0.1) velocities[uuid] = vel - if (player.isClimbing && !player.climbEnabled) { - if (!climbDisabled) { - fallDist[uuid] = player.location - Tags.disableClimb(player) - applyClimbDamage(player) - } + if (player.isClimbing && !player.climbEnabled && !climbDisabled) { + fallDist[uuid] = player.location + Tags.disableClimb(player) + applyClimbDamage(player) } - if (player.isClimbing && !uuid.canClimb) { - if (!climbDisabled) { - fallDist[uuid] = player.location - Tags.disableClimb(player) - stamina.plugin.launch { - while (!uuid.canClimb) { - delay(1.seconds) - } - Tags.enableClimb(player) - } - applyClimbDamage(player) + if (player.isClimbing && !uuid.canClimb && !climbDisabled) { + fallDist[uuid] = player.location + Tags.disableClimb(player) + stamina.plugin.launch { + while (!uuid.canClimb) delay(1.seconds) + Tags.enableClimb(player) } + applyClimbDamage(player) } if (player.isClimbing && uuid.canClimb && from.distanceSquared(to) > 0.007) - player.removeStamina(conf.staminaRemoveWhileOnLadder) + player.removeStamina(conf.staminaRemoveWhileOnClimbable) if (!player.isClimbing && uuid.isClimbing && from.distanceSquared(to) > 0.007) player.removeStamina(conf.staminaRemoveWhileMoving) diff --git a/src/main/kotlin/com/mineinabyss/staminaclimb/stamina/StaminaTask.kt b/src/main/kotlin/com/mineinabyss/staminaclimb/stamina/StaminaTask.kt index 65d2586..f820013 100644 --- a/src/main/kotlin/com/mineinabyss/staminaclimb/stamina/StaminaTask.kt +++ b/src/main/kotlin/com/mineinabyss/staminaclimb/stamina/StaminaTask.kt @@ -2,11 +2,12 @@ package com.mineinabyss.staminaclimb.stamina import com.mineinabyss.geary.papermc.tracking.items.inventory.toGeary import com.mineinabyss.idofront.entities.toPlayer +import com.mineinabyss.idofront.location.down import com.mineinabyss.idofront.textcomponents.miniMsg import com.mineinabyss.idofront.time.inWholeTicks import com.mineinabyss.staminaclimb.* import com.mineinabyss.staminaclimb.climbing.ClimbBehaviour -import com.mineinabyss.staminaclimb.modules.stamina +import com.mineinabyss.staminaclimb.config.StaminaConfig import net.kyori.adventure.bossbar.BossBar import org.bukkit.GameMode import org.bukkit.potion.PotionEffect @@ -14,8 +15,7 @@ import org.bukkit.potion.PotionEffectType import org.bukkit.scheduler.BukkitRunnable import kotlin.time.Duration.Companion.nanoseconds -class StaminaTask : BukkitRunnable() { - private val conf = stamina.config +class StaminaTask(private val config: StaminaConfig) : BukkitRunnable() { private var lastTickNano = System.nanoTime() private var timeSinceLastColorFlip = 0L private var lastTime = System.currentTimeMillis() @@ -42,29 +42,28 @@ class StaminaTask : BukkitRunnable() { if (!uuid.isClimbing) bar.addProgress( when { - player.location.clone().apply { y -= 0.0625 }.block.isSolid -> conf.staminaRegen - !player.isInClimbableBlock -> conf.staminaRegenInAir + player.location.down(0.0625).block.isSolid -> config.staminaRegen + !player.isInClimbableBlock -> config.staminaRegenInAir else -> 0f } ) when { - progress <= conf.barRed -> { //Changing bar colors and effects on player depending on its progress + progress <= config.barRed -> { //Changing bar colors and effects on player depending on its progress bar.color(BossBar.Color.RED) bar.name(redBar) if (uuid.isClimbing) player.stopClimbing() - uuid.canClimb = - false //If player reaches red zone, they can't climb until they get back in green zone + uuid.canClimb = false//If player is in red zone, they can't climb until they get back in green zone player.addPotionEffect(PotionEffect(PotionEffectType.SLOWNESS, 110, 2, false, false)) player.addPotionEffect(PotionEffect(PotionEffectType.WEAKNESS, 110, 2, false, false)) } progress < 1 && !uuid.canClimb -> bar.color(BossBar.Color.RED) //Keep Stamina Bar red even in yellow zone while it's regenerating - (uuid.isClimbing || player.isInClimbableBlock) && progress <= conf.barBlink2 -> { + (uuid.isClimbing || player.isInClimbableBlock) && progress <= config.barBlink2 -> { val deltaTime = System.currentTimeMillis() - lastTime lastTime = System.currentTimeMillis() - if (timeSinceLastColorFlip < conf.barBlinkSpeed2) + if (timeSinceLastColorFlip < config.barBlinkSpeed2) timeSinceLastColorFlip += deltaTime else { flipColor(bar) @@ -72,10 +71,10 @@ class StaminaTask : BukkitRunnable() { } } - (uuid.isClimbing || player.isInClimbableBlock) && progress <= conf.barBlink1 -> { + (uuid.isClimbing || player.isInClimbableBlock) && progress <= config.barBlink1 -> { val deltaTime = System.currentTimeMillis() - lastTime lastTime = System.currentTimeMillis() - if (timeSinceLastColorFlip < conf.barBlinkSpeed1) + if (timeSinceLastColorFlip < config.barBlinkSpeed1) timeSinceLastColorFlip += deltaTime else { flipColor(bar) @@ -84,7 +83,7 @@ class StaminaTask : BukkitRunnable() { } else -> { - bar.color(conf.baseBarColor) + bar.color(config.baseBarColor) bar.name(baseBar) uuid.canClimb = true } @@ -102,10 +101,11 @@ class StaminaTask : BukkitRunnable() { } //prevent player from climbing if they have fallen far enough or in a invalid state - if (!player.isFlying && isClimbing || player.fallDistance > conf.maxFallDist) { + if (!player.isFlying && isClimbing || player.fallDistance > config.maxFallDist) { player.stopClimbing() return@forEach } + val atWallMultiplier = player.wallDifficulty if (atWallMultiplier >= 0) { if (uuid.climbCooldownDone) uuid.restartCooldown() @@ -121,13 +121,13 @@ class StaminaTask : BukkitRunnable() { player.allowFlight = false } //only prevent air jump after AIR_TIME ms - else if (uuid.climbCooldown + conf.airTime < 0) { + else if (uuid.climbCooldown + config.airTime < 0) { player.stopClimbing() return@forEach } } - val stamina = (-tickDuration * conf.staminaRemovePerTick * atWallMultiplier).let { base -> + val stamina = (-tickDuration * config.staminaRemovePerTick * atWallMultiplier).let { base -> player.inventory.toGeary()?.getEquipmentModifiers(base) ?: base } @@ -137,14 +137,17 @@ class StaminaTask : BukkitRunnable() { } private fun flipColor(bar: BossBar) { - if (bar.color() == BossBar.Color.RED) { - bar.color(conf.baseBarColor) - bar.overlay(conf.baseOverlay) - bar.name(baseBar) - } else { - bar.color(BossBar.Color.RED) - bar.name(redBar) //Make Stamina title red - bar.overlay(conf.baseOverlay) + when (BossBar.Color.RED) { + bar.color() -> { + bar.color(config.baseBarColor) + bar.name(baseBar) + } + + else -> { + bar.color(BossBar.Color.RED) + bar.name(redBar) //Make Stamina title red + } } + bar.overlay(config.baseOverlay) } }