Skip to content

Commit

Permalink
fix: Don't run cooldown display system if there are no active cooldow…
Browse files Browse the repository at this point in the history
…ns on player

fix: Use PlayerArmSwingEvent to detect item left clicks on air
  • Loading branch information
0ffz committed Jul 29, 2024
1 parent e2576c9 commit ad94c89
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 244 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ eclipse
*.iws

logs
/truckconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.mineinabyss.geary.papermc.bridge.actions

import com.mineinabyss.geary.actions.Action
import com.mineinabyss.geary.actions.ActionGroupContext
import com.mineinabyss.geary.actions.expressions.Expression
import com.mineinabyss.idofront.serialization.MiniMessageSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import net.kyori.adventure.text.Component
import org.bukkit.entity.Player

@Serializable
@SerialName("geary:send_action_bar")
class SendActionBarAction(
val text: Expression<@Serializable(with = MiniMessageSerializer::class) Component>,
) : Action {
override fun ActionGroupContext.execute() {
val player = entity.get<Player>() ?: return
player.sendActionBar(eval(text))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,33 @@ import org.bukkit.entity.Player
import kotlin.math.roundToInt
import kotlin.time.DurationUnit

fun GearyModule.cooldownDisplaySystem() = system(query<Player, Cooldowns>()).every(INTERVAL).exec { (player, cooldowns) ->
val cooldownsWithDisplay = cooldowns.cooldowns.values.filter {
it.display != null
}
fun GearyModule.cooldownDisplaySystem() =
system(query<Player, Cooldowns>()).every(INTERVAL).exec { (player, cooldowns) ->
val cooldownsWithDisplay = cooldowns.cooldowns.values.filter {
it.display != null
}

player.sendActionBar(
Component.join(JoinConfiguration.commas(true), cooldownsWithDisplay.map { cooldown ->
val squaresLeft =
if (cooldown.timeLeft < INTERVAL) 0 else (cooldown.timeLeft / cooldown.length * displayLength).roundToInt()
if (cooldownsWithDisplay.isEmpty()) return@exec

val cooldownRender = Component.textOfChildren(
Component.text(displayChar.toString().repeat(displayLength - squaresLeft), NamedTextColor.GREEN),
Component.text(displayChar.toString().repeat(squaresLeft), NamedTextColor.RED),
if (cooldown.timeLeft < INTERVAL) Component.text(" [✔]", NamedTextColor.GREEN)
else Component.text(
" [${cooldown.timeLeft.toString(DurationUnit.SECONDS, 2)}]",
NamedTextColor.GRAY
)
).compact()
player.sendActionBar(
Component.join(JoinConfiguration.commas(true), cooldownsWithDisplay.map { cooldown ->
val squaresLeft =
if (cooldown.timeLeft < INTERVAL) 0 else (cooldown.timeLeft / cooldown.length * displayLength).roundToInt()

Component.textOfChildren(cooldown.display!!, Component.space(), cooldownRender)
})
)
}
val cooldownRender = Component.textOfChildren(
Component.text(displayChar.toString().repeat(displayLength - squaresLeft), NamedTextColor.GREEN),
Component.text(displayChar.toString().repeat(squaresLeft), NamedTextColor.RED),
if (cooldown.timeLeft < INTERVAL) Component.text(" [✔]", NamedTextColor.GREEN)
else Component.text(
" [${cooldown.timeLeft.toString(DurationUnit.SECONDS, 2)}]",
NamedTextColor.GRAY
)
).compact()

Component.textOfChildren(cooldown.display!!, Component.space(), cooldownRender)
})
)
}

object CooldownDisplayProps {
const val displayLength = 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.mineinabyss.geary.papermc.tracking.entities.toGearyOrNull
import com.mineinabyss.geary.papermc.tracking.items.inventory.toGeary
import com.mineinabyss.idofront.entities.leftClicked
import com.mineinabyss.idofront.entities.rightClicked
import io.papermc.paper.event.player.PlayerArmSwingEvent
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
Expand All @@ -21,6 +22,11 @@ class OnItemInteract
@SerialName("geary:item_left_click")
class OnItemLeftClick

@Serializable
@SerialName("geary:item_left_click_block")
class OnItemLeftClickBlock


@Serializable
@SerialName("geary:item_right_click")
class OnItemRightClick
Expand All @@ -38,6 +44,13 @@ class ItemInteractBridge : Listener {
heldItem.emit<OnItemRightClickEntity>()
}


@EventHandler
fun PlayerArmSwingEvent.onLeftClick() {
val heldItem = player.inventory.toGeary()?.get(hand) ?: return
heldItem.emit<OnItemLeftClick>()
}

@EventHandler(ignoreCancelled = true)
fun PlayerInteractEvent.onClick() {
val gearyPlayer = player.toGearyOrNull() ?: return
Expand All @@ -56,7 +69,7 @@ class ItemInteractBridge : Listener {

heldItem.emit<OnItemInteract>()

if (leftClicked) heldItem.emit<OnItemLeftClick>()
if (leftClicked) heldItem.emit<OnItemLeftClickBlock>()
if (rightClicked()) heldItem.emit<OnItemRightClick>()
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import com.mineinabyss.geary.papermc.features.entities.displayname.ShowDisplayNa
import com.mineinabyss.geary.papermc.features.entities.prevent.PreventEventsFeature
import com.mineinabyss.geary.papermc.features.entities.sounds.AmbientSoundsFeature
import com.mineinabyss.geary.papermc.features.entities.taming.TamingListener
import com.mineinabyss.geary.papermc.features.general.cooldown.CooldownFeature
import com.mineinabyss.geary.papermc.features.items.backpack.BackpackListener
import com.mineinabyss.geary.papermc.features.items.food.ReplaceBurnedDropListener
import com.mineinabyss.geary.papermc.features.items.holdsentity.SpawnHeldPrefabSystem
Expand Down Expand Up @@ -163,7 +162,6 @@ class GearyPluginImpl : GearyPlugin() {

if (gearyPaper.config.trackEntities) {
geary {
install(CooldownFeature)
install(AmbientSoundsFeature)
install(PreventEventsFeature)
}
Expand Down

This file was deleted.

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.26.3-dev.0"
geary = "0.26.3-dev.3"

[libraries]
geary-core = { module = "com.mineinabyss:geary-core", version.ref = "geary" }
Expand Down

0 comments on commit ad94c89

Please sign in to comment.