Skip to content

Commit

Permalink
Bug fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
toxicity188 committed Oct 7, 2024
1 parent 35a8cf6 commit 4046e44
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 30 deletions.
4 changes: 3 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ allprojects {
apply(plugin = "kotlin")
apply(plugin = "org.jetbrains.dokka")
group = "kr.toxicity.healthbar"
version = "3.2"
version = "3.3"
repositories {
mavenCentral()
maven("https://repo.papermc.io/repository/maven-public/")
Expand Down Expand Up @@ -191,6 +191,8 @@ tasks {
dependencies {
exclude(dependency("org.jetbrains:annotations:13.0"))
}
}
build {
finalizedBy(sourceJar)
finalizedBy(dokkaJar)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@ import kr.toxicity.healthbar.util.PLUGIN
import kr.toxicity.healthbar.util.asyncTaskTimer
import org.bukkit.entity.Player
import java.util.*
import java.util.concurrent.ConcurrentHashMap

class HealthBarPlayerImpl(
private val player: Player
): HealthBarPlayer {
override fun player(): Player = player
override fun compareTo(other: HealthBarPlayer): Int = player.uniqueId.compareTo(other.player().uniqueId)

private val updaterMap = ConcurrentHashMap<UUID, HealthBarUpdaterGroup>()
private val updaterMap = HashMap<UUID, HealthBarUpdaterGroup>()
private val task = asyncTaskTimer(1, 1) {
updaterMap.values.removeIf {
!it.update()
synchronized(updaterMap) {
updaterMap.values.removeIf {
!it.update()
}
}
}

Expand All @@ -37,13 +38,17 @@ class HealthBarPlayerImpl(
}

override fun clear() {
updaterMap.values.removeIf {
it.remove()
true
synchronized(updaterMap) {
updaterMap.values.removeIf {
it.remove()
true
}
}
}

override fun updaterMap(): MutableMap<UUID, HealthBarUpdaterGroup> = updaterMap
override fun updaterMap(): MutableMap<UUID, HealthBarUpdaterGroup> = synchronized(updaterMap) {
updaterMap
}

override fun showHealthBar(healthBar: HealthBar, trigger: HealthBarTrigger, entity: HealthBarEntity) {
if (ConfigManagerImpl.blacklistEntityType().contains(entity.entity().type)) return
Expand All @@ -59,9 +64,11 @@ class HealthBarPlayerImpl(
entity
)
if (!healthBar.condition().apply(data)) return
updaterMap.computeIfAbsent(entity.entity().uniqueId) {
HealthBarUpdaterGroupImpl(this, entity)
}.addHealthBar(data)
synchronized(updaterMap) {
updaterMap.computeIfAbsent(entity.entity().uniqueId) {
HealthBarUpdaterGroupImpl(this, entity)
}.addHealthBar(data)
}
}

override fun equals(other: Any?): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import kr.toxicity.healthbar.api.trigger.PacketTrigger
import net.kyori.adventure.bossbar.BossBar
import net.kyori.adventure.pointer.Pointers
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer
import net.minecraft.network.Connection
import net.minecraft.network.protocol.game.*
import net.minecraft.server.level.ServerLevel
Expand All @@ -27,6 +28,7 @@ import net.minecraft.world.entity.Display.TextDisplay
import net.minecraft.world.entity.EntityType
import net.minecraft.world.entity.LivingEntity
import net.minecraft.world.level.entity.LevelEntityGetter
import net.minecraft.world.level.entity.LevelEntityGetterAdapter
import net.minecraft.world.level.entity.PersistentEntitySectionManager
import org.bukkit.Bukkit
import org.bukkit.GameMode
Expand All @@ -37,6 +39,7 @@ import org.bukkit.craftbukkit.v1_19_R3.CraftWorld
import org.bukkit.craftbukkit.v1_19_R3.entity.CraftLivingEntity
import org.bukkit.craftbukkit.v1_19_R3.entity.CraftPlayer
import org.bukkit.craftbukkit.v1_19_R3.persistence.CraftPersistentDataContainer
import org.bukkit.craftbukkit.v1_19_R3.util.CraftChatMessage
import org.bukkit.entity.Entity
import org.bukkit.entity.Player
import org.bukkit.event.entity.EntityDamageEvent
Expand Down Expand Up @@ -74,6 +77,8 @@ class NMSImpl: NMS {
}
private val entityTracker = ServerLevel::class.java.fields.firstOrNull {
it.type == PersistentEntitySectionManager::class.java
}?.apply {
isAccessible = true
}

private val getEntityById: (LevelEntityGetter<net.minecraft.world.entity.Entity>, Int) -> net.minecraft.world.entity.Entity? = if (plugin.isPaper) EntityLookup::class.java.declaredFields.first {
Expand All @@ -83,7 +88,7 @@ class NMSImpl: NMS {
{ e, i ->
(it[e] as Int2ReferenceOpenHashMap<*>)[i] as? net.minecraft.world.entity.Entity
}
} else PersistentEntitySectionManager::class.java.declaredFields.first {
} else LevelEntityGetterAdapter::class.java.declaredFields.first {
net.minecraft.world.level.entity.EntityLookup::class.java.isAssignableFrom(it.type)
}.let {
it.isAccessible = true
Expand All @@ -99,6 +104,15 @@ class NMSImpl: NMS {
it[p] as Int
}
}
private val textVanilla: (Component) -> net.minecraft.network.chat.Component = if (plugin.isPaper) {
{
PaperAdventure.asVanilla(it)
}
} else {
{
CraftChatMessage.fromJSON(GsonComponentSerializer.gson().serialize(it))
}
}

override fun foliaAdapt(player: Player): Player {
val handle = (player as CraftPlayer).handle
Expand Down Expand Up @@ -226,7 +240,7 @@ class NMSImpl: NMS {
backgroundColor = 0
lineWidth = Int.MAX_VALUE
brightnessOverride = Brightness(15, 15)
text = PaperAdventure.asVanilla(component)
text = textVanilla(component)
moveTo(
location.x,
location.y,
Expand All @@ -250,7 +264,7 @@ class NMSImpl: NMS {
}

override fun text(component: Component) {
display.text = PaperAdventure.asVanilla(component)
display.text = textVanilla(component)
connection.send(ClientboundSetEntityDataPacket(display.id, display.entityData.nonDefaultValues!!))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import kr.toxicity.healthbar.api.trigger.PacketTrigger
import net.kyori.adventure.bossbar.BossBar
import net.kyori.adventure.pointer.Pointers
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer
import net.minecraft.network.Connection
import net.minecraft.network.protocol.game.*
import net.minecraft.server.level.ServerLevel
Expand All @@ -27,6 +28,7 @@ import net.minecraft.world.entity.Display.TextDisplay
import net.minecraft.world.entity.EntityType
import net.minecraft.world.entity.LivingEntity
import net.minecraft.world.level.entity.LevelEntityGetter
import net.minecraft.world.level.entity.LevelEntityGetterAdapter
import net.minecraft.world.level.entity.PersistentEntitySectionManager
import org.bukkit.Bukkit
import org.bukkit.GameMode
Expand All @@ -37,6 +39,7 @@ import org.bukkit.craftbukkit.v1_20_R1.CraftWorld
import org.bukkit.craftbukkit.v1_20_R1.entity.CraftLivingEntity
import org.bukkit.craftbukkit.v1_20_R1.entity.CraftPlayer
import org.bukkit.craftbukkit.v1_20_R1.persistence.CraftPersistentDataContainer
import org.bukkit.craftbukkit.v1_20_R1.util.CraftChatMessage
import org.bukkit.entity.Entity
import org.bukkit.entity.Player
import org.bukkit.event.entity.EntityDamageEvent
Expand Down Expand Up @@ -74,6 +77,8 @@ class NMSImpl: NMS {
}
private val entityTracker = ServerLevel::class.java.fields.firstOrNull {
it.type == PersistentEntitySectionManager::class.java
}?.apply {
isAccessible = true
}

private val getEntityById: (LevelEntityGetter<net.minecraft.world.entity.Entity>, Int) -> net.minecraft.world.entity.Entity? = if (plugin.isPaper) EntityLookup::class.java.declaredFields.first {
Expand All @@ -83,7 +88,7 @@ class NMSImpl: NMS {
{ e, i ->
(it[e] as Int2ReferenceOpenHashMap<*>)[i] as? net.minecraft.world.entity.Entity
}
} else PersistentEntitySectionManager::class.java.declaredFields.first {
} else LevelEntityGetterAdapter::class.java.declaredFields.first {
net.minecraft.world.level.entity.EntityLookup::class.java.isAssignableFrom(it.type)
}.let {
it.isAccessible = true
Expand All @@ -99,6 +104,15 @@ class NMSImpl: NMS {
it[p] as Int
}
}
private val textVanilla: (Component) -> net.minecraft.network.chat.Component = if (plugin.isPaper) {
{
PaperAdventure.asVanilla(it)
}
} else {
{
CraftChatMessage.fromJSON(GsonComponentSerializer.gson().serialize(it))
}
}

override fun foliaAdapt(player: Player): Player {
val handle = (player as CraftPlayer).handle
Expand Down Expand Up @@ -232,7 +246,7 @@ class NMSImpl: NMS {
set(TextDisplay.DATA_LINE_WIDTH_ID, Int.MAX_VALUE)
}
brightnessOverride = Brightness(15, 15)
text = PaperAdventure.asVanilla(component)
text = textVanilla(component)
moveTo(
location.x,
location.y,
Expand All @@ -256,7 +270,7 @@ class NMSImpl: NMS {
}

override fun text(component: Component) {
display.text = PaperAdventure.asVanilla(component)
display.text = textVanilla(component)
connection.send(ClientboundSetEntityDataPacket(display.id, display.entityData.nonDefaultValues!!))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import kr.toxicity.healthbar.api.trigger.PacketTrigger
import net.kyori.adventure.bossbar.BossBar
import net.kyori.adventure.pointer.Pointers
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer
import net.minecraft.network.Connection
import net.minecraft.network.protocol.game.*
import net.minecraft.server.level.ServerLevel
Expand All @@ -27,6 +28,7 @@ import net.minecraft.world.entity.Display.TextDisplay
import net.minecraft.world.entity.EntityType
import net.minecraft.world.entity.LivingEntity
import net.minecraft.world.level.entity.LevelEntityGetter
import net.minecraft.world.level.entity.LevelEntityGetterAdapter
import net.minecraft.world.level.entity.PersistentEntitySectionManager
import org.bukkit.Bukkit
import org.bukkit.GameMode
Expand All @@ -37,6 +39,7 @@ import org.bukkit.craftbukkit.v1_20_R2.CraftWorld
import org.bukkit.craftbukkit.v1_20_R2.entity.CraftLivingEntity
import org.bukkit.craftbukkit.v1_20_R2.entity.CraftPlayer
import org.bukkit.craftbukkit.v1_20_R2.persistence.CraftPersistentDataContainer
import org.bukkit.craftbukkit.v1_20_R2.util.CraftChatMessage
import org.bukkit.entity.Entity
import org.bukkit.entity.Player
import org.bukkit.event.entity.EntityDamageEvent
Expand Down Expand Up @@ -74,6 +77,8 @@ class NMSImpl: NMS {
}
private val entityTracker = ServerLevel::class.java.fields.firstOrNull {
it.type == PersistentEntitySectionManager::class.java
}?.apply {
isAccessible = true
}

private val getEntityById: (LevelEntityGetter<net.minecraft.world.entity.Entity>, Int) -> net.minecraft.world.entity.Entity? = if (plugin.isPaper) EntityLookup::class.java.declaredFields.first {
Expand All @@ -83,7 +88,7 @@ class NMSImpl: NMS {
{ e, i ->
(it[e] as Int2ReferenceOpenHashMap<*>)[i] as? net.minecraft.world.entity.Entity
}
} else PersistentEntitySectionManager::class.java.declaredFields.first {
} else LevelEntityGetterAdapter::class.java.declaredFields.first {
net.minecraft.world.level.entity.EntityLookup::class.java.isAssignableFrom(it.type)
}.let {
it.isAccessible = true
Expand All @@ -99,6 +104,15 @@ class NMSImpl: NMS {
it[p] as Int
}
}
private val textVanilla: (Component) -> net.minecraft.network.chat.Component = if (plugin.isPaper) {
{
PaperAdventure.asVanilla(it)
}
} else {
{
CraftChatMessage.fromJSON(GsonComponentSerializer.gson().serialize(it))
}
}

override fun foliaAdapt(player: Player): Player {
val handle = (player as CraftPlayer).handle
Expand Down Expand Up @@ -228,7 +242,7 @@ class NMSImpl: NMS {
set(TextDisplay.DATA_LINE_WIDTH_ID, Int.MAX_VALUE)
}
brightnessOverride = Brightness(15, 15)
text = PaperAdventure.asVanilla(component)
text = textVanilla(component)
moveTo(
location.x,
location.y,
Expand All @@ -252,7 +266,7 @@ class NMSImpl: NMS {
}

override fun text(component: Component) {
display.text = PaperAdventure.asVanilla(component)
display.text = textVanilla(component)
connection.send(ClientboundSetEntityDataPacket(display.id, display.entityData.nonDefaultValues!!))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import kr.toxicity.healthbar.api.trigger.PacketTrigger
import net.kyori.adventure.bossbar.BossBar
import net.kyori.adventure.pointer.Pointers
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer
import net.minecraft.network.Connection
import net.minecraft.network.protocol.game.*
import net.minecraft.server.level.ServerLevel
Expand All @@ -27,6 +28,7 @@ import net.minecraft.world.entity.Display.TextDisplay
import net.minecraft.world.entity.EntityType
import net.minecraft.world.entity.LivingEntity
import net.minecraft.world.level.entity.LevelEntityGetter
import net.minecraft.world.level.entity.LevelEntityGetterAdapter
import net.minecraft.world.level.entity.PersistentEntitySectionManager
import org.bukkit.Bukkit
import org.bukkit.GameMode
Expand All @@ -37,6 +39,7 @@ import org.bukkit.craftbukkit.v1_20_R3.CraftWorld
import org.bukkit.craftbukkit.v1_20_R3.entity.CraftLivingEntity
import org.bukkit.craftbukkit.v1_20_R3.entity.CraftPlayer
import org.bukkit.craftbukkit.v1_20_R3.persistence.CraftPersistentDataContainer
import org.bukkit.craftbukkit.v1_20_R3.util.CraftChatMessage
import org.bukkit.entity.Entity
import org.bukkit.entity.Player
import org.bukkit.event.entity.EntityDamageEvent
Expand Down Expand Up @@ -74,6 +77,8 @@ class NMSImpl: NMS {
}
private val entityTracker = ServerLevel::class.java.fields.firstOrNull {
it.type == PersistentEntitySectionManager::class.java
}?.apply {
isAccessible = true
}

private val getEntityById: (LevelEntityGetter<net.minecraft.world.entity.Entity>, Int) -> net.minecraft.world.entity.Entity? = if (plugin.isPaper) EntityLookup::class.java.declaredFields.first {
Expand All @@ -83,7 +88,7 @@ class NMSImpl: NMS {
{ e, i ->
(it[e] as Int2ReferenceOpenHashMap<*>)[i] as? net.minecraft.world.entity.Entity
}
} else PersistentEntitySectionManager::class.java.declaredFields.first {
} else LevelEntityGetterAdapter::class.java.declaredFields.first {
net.minecraft.world.level.entity.EntityLookup::class.java.isAssignableFrom(it.type)
}.let {
it.isAccessible = true
Expand All @@ -99,6 +104,15 @@ class NMSImpl: NMS {
it[p] as Int
}
}
private val textVanilla: (Component) -> net.minecraft.network.chat.Component = if (plugin.isPaper) {
{
PaperAdventure.asVanilla(it)
}
} else {
{
CraftChatMessage.fromJSON(GsonComponentSerializer.gson().serialize(it))
}
}

override fun foliaAdapt(player: Player): Player {
val handle = (player as CraftPlayer).handle
Expand Down Expand Up @@ -228,7 +242,7 @@ class NMSImpl: NMS {
set(TextDisplay.DATA_LINE_WIDTH_ID, Int.MAX_VALUE)
}
brightnessOverride = Brightness(15, 15)
text = PaperAdventure.asVanilla(component)
text = textVanilla(component)
moveTo(
location.x,
location.y,
Expand All @@ -252,7 +266,7 @@ class NMSImpl: NMS {
}

override fun text(component: Component) {
display.text = PaperAdventure.asVanilla(component)
display.text = textVanilla(component)
connection.send(ClientboundSetEntityDataPacket(display.id, display.entityData.nonDefaultValues!!))
}

Expand Down
Loading

0 comments on commit 4046e44

Please sign in to comment.