Skip to content

Commit

Permalink
Changes attribute.
Browse files Browse the repository at this point in the history
  • Loading branch information
toxicity188 committed Oct 30, 2024
1 parent 401528e commit 1042447
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
4 changes: 2 additions & 2 deletions 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.5"
version = "3.5.1"
repositories {
mavenCentral()
maven("https://repo.papermc.io/repository/maven-public/")
Expand Down Expand Up @@ -176,7 +176,7 @@ tasks {
version(minecraft)
pluginJars(fileTree("plugins"))
downloadPlugins {
modrinth("betterhud2", "1.6.254")
modrinth("betterhud2", "1.7.DEV-264")
hangar("PlaceholderAPI", "2.11.6")
hangar("Skript", "2.9.3")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package kr.toxicity.healthbar.manager
import kr.toxicity.healthbar.api.listener.HealthBarListener
import kr.toxicity.healthbar.api.manager.ListenerManager
import kr.toxicity.healthbar.pack.PackResource
import kr.toxicity.healthbar.util.ATTRIBUTE_MAX_HEALTH
import kr.toxicity.healthbar.util.armor
import kr.toxicity.healthbar.util.ifNull
import org.bukkit.attribute.Attribute
import org.bukkit.configuration.ConfigurationSection
import java.util.concurrent.ConcurrentHashMap
import java.util.function.Function
Expand All @@ -14,17 +15,22 @@ object ListenerManagerImpl : ListenerManager, BetterHealthBerManager {
private val listenerMap = ConcurrentHashMap<String, (ConfigurationSection) -> HealthBarListener>().apply {
put("health") {
HealthBarListener {
it.entity.entity().health / it.entity.entity().getAttribute(Attribute.GENERIC_MAX_HEALTH)!!.value
it.entity.entity().health / it.entity.entity().getAttribute(ATTRIBUTE_MAX_HEALTH)!!.value
}
}
put("health_before") {
HealthBarListener {
(it.entity.entity().health + it.entity.entity().lastDamage) / it.entity.entity().getAttribute(Attribute.GENERIC_MAX_HEALTH)!!.value
(it.entity.entity().health + it.entity.entity().lastDamage) / it.entity.entity().getAttribute(ATTRIBUTE_MAX_HEALTH)!!.value
}
}
put("absorption") {
HealthBarListener {
it.entity.entity().absorptionAmount / it.entity.entity().getAttribute(Attribute.GENERIC_MAX_HEALTH)!!.value
it.entity.entity().absorptionAmount / it.entity.entity().getAttribute(ATTRIBUTE_MAX_HEALTH)!!.value
}
}
put("armor") {
HealthBarListener {
it.entity.entity().armor / 20.0
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import kr.toxicity.healthbar.api.event.HealthBarCreateEvent
import kr.toxicity.healthbar.api.manager.PlaceholderManager
import kr.toxicity.healthbar.api.placeholder.PlaceholderContainer
import kr.toxicity.healthbar.pack.PackResource
import kr.toxicity.healthbar.util.ATTRIBUTE_MAX_HEALTH
import kr.toxicity.healthbar.util.armor
import kr.toxicity.healthbar.util.placeholder
import kr.toxicity.healthbar.util.warn
import kr.toxicity.healthbar.version.MinecraftVersion
import org.bukkit.NamespacedKey
import org.bukkit.Registry
import org.bukkit.attribute.Attribute
import org.bukkit.entity.Player
import org.bukkit.potion.PotionEffectType
import java.util.function.Function
Expand All @@ -23,14 +24,17 @@ object PlaceholderManagerImpl : PlaceholderManager, BetterHealthBerManager {
e.entity.entity().health
}
addPlaceholder("max_health") { e: HealthBarCreateEvent ->
e.entity.entity().getAttribute(Attribute.GENERIC_MAX_HEALTH)!!.value
e.entity.entity().getAttribute(ATTRIBUTE_MAX_HEALTH)!!.value
}
addPlaceholder("health_percentage") { e: HealthBarCreateEvent ->
e.entity.entity().health / e.entity.entity().getAttribute(Attribute.GENERIC_MAX_HEALTH)!!.value
e.entity.entity().health / e.entity.entity().getAttribute(ATTRIBUTE_MAX_HEALTH)!!.value
}
addPlaceholder("absorption") { e: HealthBarCreateEvent ->
e.entity.entity().absorptionAmount
}
addPlaceholder("armor") { e: HealthBarCreateEvent ->
e.entity.entity().armor
}
}
PlaceholderContainer.STRING.run {
addPlaceholder("entity_type") { e: HealthBarCreateEvent ->
Expand Down Expand Up @@ -64,6 +68,9 @@ object PlaceholderManagerImpl : PlaceholderManager, BetterHealthBerManager {
addPlaceholder("is_mythic_mob") { e: HealthBarCreateEvent ->
e.entity.mob() is MythicActiveMob
}
addPlaceholder("is_vanilla_mod") { e: HealthBarCreateEvent ->
e.entity.mob() == null
}
}
}

Expand Down
28 changes: 27 additions & 1 deletion dist/src/main/kotlin/kr/toxicity/healthbar/util/Entities.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ import kr.toxicity.healthbar.api.component.WidthComponent
import kr.toxicity.healthbar.api.event.HealthBarCreateEvent
import kr.toxicity.healthbar.api.nms.VirtualTextDisplay
import kr.toxicity.healthbar.manager.ConfigManagerImpl
import kr.toxicity.healthbar.version.MinecraftVersion
import org.bukkit.Location
import org.bukkit.NamespacedKey
import org.bukkit.Registry
import org.bukkit.entity.LivingEntity
import org.bukkit.inventory.ItemStack
import org.bukkit.util.Vector

val ATTRIBUTE_MAX_HEALTH = Registry.ATTRIBUTE.get(NamespacedKey.minecraft(if (MinecraftVersion.current >= MinecraftVersion.version1_21_2) "max_health" else "generic.max_health"))!!
val ATTRIBUTE_ARMOR = Registry.ATTRIBUTE.get(NamespacedKey.minecraft(if (MinecraftVersion.current >= MinecraftVersion.version1_21_2) "armor" else "generic.armor"))!!

fun HealthBarCreateEvent.toEntityLocation(): Location {
return entity.entity().location.apply {
y += (PLUGIN.modelEngine().height(entity.entity()) ?: entity.entity().eyeHeight) + ConfigManagerImpl.defaultHeight()
Expand All @@ -23,4 +31,22 @@ fun HealthBarCreateEvent.createEntity(component: WidthComponent, layer: Int = 0)
scale
)
}
}
}

val LivingEntity.armor
get(): Double {
var attribute = getAttribute(ATTRIBUTE_ARMOR)?.value ?: 0.0
val inventory = equipment ?: return attribute
fun add(itemStack: ItemStack?) {
itemStack?.itemMeta?.attributeModifiers?.get(ATTRIBUTE_ARMOR)?.sumOf { v ->
v.amount
}?.let {
attribute += it
}
}
add(inventory.helmet)
add(inventory.chestplate)
add(inventory.leggings)
add(inventory.boots)
return attribute
}

0 comments on commit 1042447

Please sign in to comment.