Skip to content

Commit

Permalink
refactor: move StaminaTask to StaminaModule to refresh configs on reload
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Aug 13, 2024
1 parent cf7ac90 commit 9db6360
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 64 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
group=com.mineinabyss
version=0.23
idofrontVersion=0.24.16
idofrontVersion=0.24.24
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -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" }
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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!")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<UUID, Boolean> = mutableMapOf()
val isClimbing: MutableMap<UUID, Boolean> = ConcurrentHashMap()
val cooldown: MutableMap<UUID, Long> = HashMap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -12,6 +13,7 @@ val stamina: StaminaClimbModule by DI.observe()
interface StaminaClimbModule {
val plugin: StaminaClimbPlugin
val config: StaminaConfig
val staminaTask: StaminaTask
val disabledClimbingTags: MutableMap<ResourceKey<out Registry<*>?>, NetworkPayload>
val initialTags: MutableMap<ResourceKey<out Registry<*>?>, NetworkPayload>
}
Original file line number Diff line number Diff line change
@@ -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<ResourceKey<out Registry<*>?>, NetworkPayload> = mutableMapOf()
override val initialTags: MutableMap<ResourceKey<out Registry<*>?>, NetworkPayload> = mutableMapOf()
}
41 changes: 15 additions & 26 deletions src/main/kotlin/com/mineinabyss/staminaclimb/stamina/StaminaBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<UUID> = mutableListOf() //TODO persist

@PublishedApi
Expand Down Expand Up @@ -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)
Expand Down
51 changes: 27 additions & 24 deletions src/main/kotlin/com/mineinabyss/staminaclimb/stamina/StaminaTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ 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
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()
Expand All @@ -42,40 +42,39 @@ 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)
timeSinceLastColorFlip = 0
}
}

(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)
Expand All @@ -84,7 +83,7 @@ class StaminaTask : BukkitRunnable() {
}

else -> {
bar.color(conf.baseBarColor)
bar.color(config.baseBarColor)
bar.name(baseBar)
uuid.canClimb = true
}
Expand All @@ -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()
Expand All @@ -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
}

Expand All @@ -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)
}
}

0 comments on commit 9db6360

Please sign in to comment.