Added clientside entity display name API for all versions
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
package com.willfp.eco.internal.spigot.proxy.common
|
||||
|
||||
import com.willfp.eco.core.Prerequisite
|
||||
import com.willfp.eco.core.entities.ai.EntityGoal
|
||||
import com.willfp.eco.core.entities.ai.TargetGoal
|
||||
import com.willfp.eco.internal.spigot.proxy.common.ai.EntityGoalFactory
|
||||
import com.willfp.eco.internal.spigot.proxy.common.ai.TargetGoalFactory
|
||||
import io.papermc.paper.adventure.PaperAdventure
|
||||
import net.kyori.adventure.text.Component
|
||||
import net.minecraft.nbt.CompoundTag
|
||||
import net.minecraft.resources.ResourceLocation
|
||||
import net.minecraft.server.level.ServerPlayer
|
||||
@@ -68,6 +71,9 @@ fun CompoundTag.setPdc(pdc: PersistentDataContainer?, item: net.minecraft.world.
|
||||
fun Player.toNMS(): ServerPlayer =
|
||||
impl.toNMS(this)
|
||||
|
||||
fun Component.toNMS(): net.minecraft.network.chat.Component =
|
||||
if (Prerequisite.HAS_PAPER.isMet) PaperAdventure.asVanilla(this) else impl.toNMS(this)
|
||||
|
||||
interface CommonsProvider {
|
||||
val nbtTagString: Int
|
||||
|
||||
@@ -101,6 +107,8 @@ interface CommonsProvider {
|
||||
|
||||
fun toNMS(player: Player): ServerPlayer
|
||||
|
||||
fun toNMS(component: Component): net.minecraft.network.chat.Component
|
||||
|
||||
companion object {
|
||||
fun setIfNeeded(provider: CommonsProvider) {
|
||||
if (::impl.isInitialized) {
|
||||
|
||||
@@ -5,6 +5,8 @@ import com.willfp.eco.internal.spigot.proxy.CommonsInitializerProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.common.CommonsProvider
|
||||
import com.willfp.eco.internal.spigot.proxy.common.packet.PacketInjectorListener
|
||||
import com.willfp.eco.internal.spigot.proxy.common.toResourceLocation
|
||||
import net.kyori.adventure.text.Component
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer
|
||||
import net.minecraft.core.Registry
|
||||
import net.minecraft.nbt.CompoundTag
|
||||
import net.minecraft.nbt.Tag
|
||||
@@ -155,5 +157,10 @@ class CommonsInitializer : CommonsInitializerProxy {
|
||||
override fun toNMS(player: Player): ServerPlayer {
|
||||
return (player as CraftPlayer).handle
|
||||
}
|
||||
|
||||
override fun toNMS(component: Component): net.minecraft.network.chat.Component {
|
||||
val json = GsonComponentSerializer.gson().serialize(component)
|
||||
return net.minecraft.network.chat.Component.Serializer.fromJson(json)!!
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
package com.willfp.eco.internal.spigot.proxy.v1_17_R1
|
||||
|
||||
import com.willfp.eco.core.packet.Packet
|
||||
import com.willfp.eco.core.packet.sendPacket
|
||||
import com.willfp.eco.internal.spigot.proxy.DisplayNameProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.common.toNMS
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap
|
||||
import net.kyori.adventure.text.Component
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket
|
||||
import net.minecraft.network.syncher.EntityDataAccessor
|
||||
import net.minecraft.network.syncher.SynchedEntityData
|
||||
import net.minecraft.world.entity.Entity
|
||||
import org.bukkit.craftbukkit.v1_17_R1.entity.CraftLivingEntity
|
||||
import org.bukkit.entity.LivingEntity
|
||||
import org.bukkit.entity.Player
|
||||
import java.util.Optional
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
class DisplayName : DisplayNameProxy {
|
||||
private val displayNameAccessor = Entity::class.java
|
||||
.declaredFields
|
||||
.filter { it.type == EntityDataAccessor::class.java }
|
||||
.toList()[2]
|
||||
.apply { isAccessible = true }
|
||||
.get(null) as EntityDataAccessor<Optional<net.minecraft.network.chat.Component>>
|
||||
|
||||
private val customNameVisibleAccessor = Entity::class.java
|
||||
.declaredFields
|
||||
.filter { it.type == EntityDataAccessor::class.java }
|
||||
.toList()[3]
|
||||
.apply { isAccessible = true }
|
||||
.get(null) as EntityDataAccessor<Boolean>
|
||||
|
||||
private val itemsByIDMapField = SynchedEntityData::class.java
|
||||
.declaredFields
|
||||
.filter { it.type == Int2ObjectMap::class.java }
|
||||
.toList()[0]
|
||||
.apply { isAccessible = true }
|
||||
|
||||
override fun setClientsideDisplayName(
|
||||
entity: LivingEntity,
|
||||
player: Player,
|
||||
displayName: Component,
|
||||
visible: Boolean
|
||||
) {
|
||||
if (entity !is CraftLivingEntity) {
|
||||
return
|
||||
}
|
||||
|
||||
val nmsComponent = displayName.toNMS()
|
||||
|
||||
val nmsEntity = entity.handle
|
||||
nmsEntity.isCustomNameVisible
|
||||
val entityData = SynchedEntityData(nmsEntity)
|
||||
|
||||
entityData.forceSet(displayNameAccessor, Optional.of(nmsComponent))
|
||||
entityData.forceSet(customNameVisibleAccessor, visible)
|
||||
|
||||
val packet = ClientboundSetEntityDataPacket(
|
||||
nmsEntity.id,
|
||||
entityData,
|
||||
true
|
||||
)
|
||||
|
||||
player.sendPacket(Packet(packet))
|
||||
}
|
||||
|
||||
private fun <T : Any> SynchedEntityData.forceSet(
|
||||
accessor: EntityDataAccessor<T>,
|
||||
value: T
|
||||
) {
|
||||
if (!this.hasItem(accessor)) {
|
||||
this.define(accessor, value)
|
||||
}
|
||||
this[accessor] = value
|
||||
this.markDirty(accessor)
|
||||
}
|
||||
|
||||
private fun <T : Any> SynchedEntityData.hasItem(accessor: EntityDataAccessor<T>): Boolean {
|
||||
val itemsByIDMap = itemsByIDMapField.get(this) as Int2ObjectMap<Any>
|
||||
return itemsByIDMap.containsKey(accessor.id)
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,8 @@ import com.willfp.eco.internal.spigot.proxy.CommonsInitializerProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.common.CommonsProvider
|
||||
import com.willfp.eco.internal.spigot.proxy.common.packet.PacketInjectorListener
|
||||
import com.willfp.eco.internal.spigot.proxy.common.toResourceLocation
|
||||
import net.kyori.adventure.text.Component
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer
|
||||
import net.minecraft.core.Registry
|
||||
import net.minecraft.nbt.CompoundTag
|
||||
import net.minecraft.nbt.Tag
|
||||
@@ -155,5 +157,10 @@ class CommonsInitializer : CommonsInitializerProxy {
|
||||
override fun toNMS(player: Player): ServerPlayer {
|
||||
return (player as CraftPlayer).handle
|
||||
}
|
||||
|
||||
override fun toNMS(component: Component): net.minecraft.network.chat.Component {
|
||||
val json = GsonComponentSerializer.gson().serialize(component)
|
||||
return net.minecraft.network.chat.Component.Serializer.fromJson(json)!!
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
package com.willfp.eco.internal.spigot.proxy.v1_18_R1
|
||||
|
||||
import com.willfp.eco.core.packet.Packet
|
||||
import com.willfp.eco.core.packet.sendPacket
|
||||
import com.willfp.eco.internal.spigot.proxy.DisplayNameProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.common.toNMS
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap
|
||||
import net.kyori.adventure.text.Component
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket
|
||||
import net.minecraft.network.syncher.EntityDataAccessor
|
||||
import net.minecraft.network.syncher.SynchedEntityData
|
||||
import net.minecraft.world.entity.Entity
|
||||
import org.bukkit.craftbukkit.v1_18_R1.entity.CraftLivingEntity
|
||||
import org.bukkit.entity.LivingEntity
|
||||
import org.bukkit.entity.Player
|
||||
import java.util.Optional
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
class DisplayName : DisplayNameProxy {
|
||||
private val displayNameAccessor = Entity::class.java
|
||||
.declaredFields
|
||||
.filter { it.type == EntityDataAccessor::class.java }
|
||||
.toList()[2]
|
||||
.apply { isAccessible = true }
|
||||
.get(null) as EntityDataAccessor<Optional<net.minecraft.network.chat.Component>>
|
||||
|
||||
private val customNameVisibleAccessor = Entity::class.java
|
||||
.declaredFields
|
||||
.filter { it.type == EntityDataAccessor::class.java }
|
||||
.toList()[3]
|
||||
.apply { isAccessible = true }
|
||||
.get(null) as EntityDataAccessor<Boolean>
|
||||
|
||||
private val itemsByIDMapField = SynchedEntityData::class.java
|
||||
.declaredFields
|
||||
.filter { it.type == Int2ObjectMap::class.java }
|
||||
.toList()[0]
|
||||
.apply { isAccessible = true }
|
||||
|
||||
override fun setClientsideDisplayName(
|
||||
entity: LivingEntity,
|
||||
player: Player,
|
||||
displayName: Component,
|
||||
visible: Boolean
|
||||
) {
|
||||
if (entity !is CraftLivingEntity) {
|
||||
return
|
||||
}
|
||||
|
||||
val nmsComponent = displayName.toNMS()
|
||||
|
||||
val nmsEntity = entity.handle
|
||||
nmsEntity.isCustomNameVisible
|
||||
val entityData = SynchedEntityData(nmsEntity)
|
||||
|
||||
entityData.forceSet(displayNameAccessor, Optional.of(nmsComponent))
|
||||
entityData.forceSet(customNameVisibleAccessor, visible)
|
||||
|
||||
val packet = ClientboundSetEntityDataPacket(
|
||||
nmsEntity.id,
|
||||
entityData,
|
||||
true
|
||||
)
|
||||
|
||||
player.sendPacket(Packet(packet))
|
||||
}
|
||||
|
||||
private fun <T : Any> SynchedEntityData.forceSet(
|
||||
accessor: EntityDataAccessor<T>,
|
||||
value: T
|
||||
) {
|
||||
if (!this.hasItem(accessor)) {
|
||||
this.define(accessor, value)
|
||||
}
|
||||
this[accessor] = value
|
||||
this.markDirty(accessor)
|
||||
}
|
||||
|
||||
private fun <T : Any> SynchedEntityData.hasItem(accessor: EntityDataAccessor<T>): Boolean {
|
||||
val itemsByIDMap = itemsByIDMapField.get(this) as Int2ObjectMap<Any>
|
||||
return itemsByIDMap.containsKey(accessor.id)
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,8 @@ import com.willfp.eco.internal.spigot.proxy.CommonsInitializerProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.common.CommonsProvider
|
||||
import com.willfp.eco.internal.spigot.proxy.common.packet.PacketInjectorListener
|
||||
import com.willfp.eco.internal.spigot.proxy.common.toResourceLocation
|
||||
import net.kyori.adventure.text.Component
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer
|
||||
import net.minecraft.core.Registry
|
||||
import net.minecraft.nbt.CompoundTag
|
||||
import net.minecraft.nbt.Tag
|
||||
@@ -155,5 +157,10 @@ class CommonsInitializer : CommonsInitializerProxy {
|
||||
override fun toNMS(player: Player): ServerPlayer {
|
||||
return (player as CraftPlayer).handle
|
||||
}
|
||||
|
||||
override fun toNMS(component: Component): net.minecraft.network.chat.Component {
|
||||
val json = GsonComponentSerializer.gson().serialize(component)
|
||||
return net.minecraft.network.chat.Component.Serializer.fromJson(json)!!
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
package com.willfp.eco.internal.spigot.proxy.v1_18_R2
|
||||
|
||||
import com.willfp.eco.core.packet.Packet
|
||||
import com.willfp.eco.core.packet.sendPacket
|
||||
import com.willfp.eco.internal.spigot.proxy.DisplayNameProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.common.toNMS
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap
|
||||
import net.kyori.adventure.text.Component
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket
|
||||
import net.minecraft.network.syncher.EntityDataAccessor
|
||||
import net.minecraft.network.syncher.SynchedEntityData
|
||||
import net.minecraft.world.entity.Entity
|
||||
import org.bukkit.craftbukkit.v1_18_R2.entity.CraftLivingEntity
|
||||
import org.bukkit.entity.LivingEntity
|
||||
import org.bukkit.entity.Player
|
||||
import java.util.Optional
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
class DisplayName : DisplayNameProxy {
|
||||
private val displayNameAccessor = Entity::class.java
|
||||
.declaredFields
|
||||
.filter { it.type == EntityDataAccessor::class.java }
|
||||
.toList()[2]
|
||||
.apply { isAccessible = true }
|
||||
.get(null) as EntityDataAccessor<Optional<net.minecraft.network.chat.Component>>
|
||||
|
||||
private val customNameVisibleAccessor = Entity::class.java
|
||||
.declaredFields
|
||||
.filter { it.type == EntityDataAccessor::class.java }
|
||||
.toList()[3]
|
||||
.apply { isAccessible = true }
|
||||
.get(null) as EntityDataAccessor<Boolean>
|
||||
|
||||
private val itemsByIDMapField = SynchedEntityData::class.java
|
||||
.declaredFields
|
||||
.filter { it.type == Int2ObjectMap::class.java }
|
||||
.toList()[0]
|
||||
.apply { isAccessible = true }
|
||||
|
||||
override fun setClientsideDisplayName(
|
||||
entity: LivingEntity,
|
||||
player: Player,
|
||||
displayName: Component,
|
||||
visible: Boolean
|
||||
) {
|
||||
if (entity !is CraftLivingEntity) {
|
||||
return
|
||||
}
|
||||
|
||||
val nmsComponent = displayName.toNMS()
|
||||
|
||||
val nmsEntity = entity.handle
|
||||
nmsEntity.isCustomNameVisible
|
||||
val entityData = SynchedEntityData(nmsEntity)
|
||||
|
||||
entityData.forceSet(displayNameAccessor, Optional.of(nmsComponent))
|
||||
entityData.forceSet(customNameVisibleAccessor, visible)
|
||||
|
||||
val packet = ClientboundSetEntityDataPacket(
|
||||
nmsEntity.id,
|
||||
entityData,
|
||||
true
|
||||
)
|
||||
|
||||
player.sendPacket(Packet(packet))
|
||||
}
|
||||
|
||||
private fun <T : Any> SynchedEntityData.forceSet(
|
||||
accessor: EntityDataAccessor<T>,
|
||||
value: T
|
||||
) {
|
||||
if (!this.hasItem(accessor)) {
|
||||
this.define(accessor, value)
|
||||
}
|
||||
this[accessor] = value
|
||||
this.markDirty(accessor)
|
||||
}
|
||||
|
||||
private fun <T : Any> SynchedEntityData.hasItem(accessor: EntityDataAccessor<T>): Boolean {
|
||||
val itemsByIDMap = itemsByIDMapField.get(this) as Int2ObjectMap<Any>
|
||||
return itemsByIDMap.containsKey(accessor.id)
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,8 @@ import com.willfp.eco.internal.spigot.proxy.CommonsInitializerProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.common.CommonsProvider
|
||||
import com.willfp.eco.internal.spigot.proxy.common.packet.PacketInjectorListener
|
||||
import com.willfp.eco.internal.spigot.proxy.common.toResourceLocation
|
||||
import net.kyori.adventure.text.Component
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer
|
||||
import net.minecraft.core.Registry
|
||||
import net.minecraft.nbt.CompoundTag
|
||||
import net.minecraft.nbt.Tag
|
||||
@@ -155,5 +157,10 @@ class CommonsInitializer : CommonsInitializerProxy {
|
||||
override fun toNMS(player: Player): ServerPlayer {
|
||||
return (player as CraftPlayer).handle
|
||||
}
|
||||
|
||||
override fun toNMS(component: Component): net.minecraft.network.chat.Component {
|
||||
val json = GsonComponentSerializer.gson().serialize(component)
|
||||
return net.minecraft.network.chat.Component.Serializer.fromJson(json)!!
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
package com.willfp.eco.internal.spigot.proxy.v1_19_R1
|
||||
|
||||
import com.willfp.eco.core.packet.Packet
|
||||
import com.willfp.eco.core.packet.sendPacket
|
||||
import com.willfp.eco.internal.spigot.proxy.DisplayNameProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.common.toNMS
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap
|
||||
import net.kyori.adventure.text.Component
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket
|
||||
import net.minecraft.network.syncher.EntityDataAccessor
|
||||
import net.minecraft.network.syncher.SynchedEntityData
|
||||
import net.minecraft.world.entity.Entity
|
||||
import org.bukkit.craftbukkit.v1_19_R1.entity.CraftLivingEntity
|
||||
import org.bukkit.entity.LivingEntity
|
||||
import org.bukkit.entity.Player
|
||||
import java.util.Optional
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
class DisplayName : DisplayNameProxy {
|
||||
private val displayNameAccessor = Entity::class.java
|
||||
.declaredFields
|
||||
.filter { it.type == EntityDataAccessor::class.java }
|
||||
.toList()[2]
|
||||
.apply { isAccessible = true }
|
||||
.get(null) as EntityDataAccessor<Optional<net.minecraft.network.chat.Component>>
|
||||
|
||||
private val customNameVisibleAccessor = Entity::class.java
|
||||
.declaredFields
|
||||
.filter { it.type == EntityDataAccessor::class.java }
|
||||
.toList()[3]
|
||||
.apply { isAccessible = true }
|
||||
.get(null) as EntityDataAccessor<Boolean>
|
||||
|
||||
private val itemsByIDMapField = SynchedEntityData::class.java
|
||||
.declaredFields
|
||||
.filter { it.type == Int2ObjectMap::class.java }
|
||||
.toList()[0]
|
||||
.apply { isAccessible = true }
|
||||
|
||||
override fun setClientsideDisplayName(
|
||||
entity: LivingEntity,
|
||||
player: Player,
|
||||
displayName: Component,
|
||||
visible: Boolean
|
||||
) {
|
||||
if (entity !is CraftLivingEntity) {
|
||||
return
|
||||
}
|
||||
|
||||
val nmsComponent = displayName.toNMS()
|
||||
|
||||
val nmsEntity = entity.handle
|
||||
nmsEntity.isCustomNameVisible
|
||||
val entityData = SynchedEntityData(nmsEntity)
|
||||
|
||||
entityData.forceSet(displayNameAccessor, Optional.of(nmsComponent))
|
||||
entityData.forceSet(customNameVisibleAccessor, visible)
|
||||
|
||||
val packet = ClientboundSetEntityDataPacket(
|
||||
nmsEntity.id,
|
||||
entityData,
|
||||
true
|
||||
)
|
||||
|
||||
player.sendPacket(Packet(packet))
|
||||
}
|
||||
|
||||
private fun <T : Any> SynchedEntityData.forceSet(
|
||||
accessor: EntityDataAccessor<T>,
|
||||
value: T
|
||||
) {
|
||||
if (!this.hasItem(accessor)) {
|
||||
this.define(accessor, value)
|
||||
}
|
||||
this[accessor] = value
|
||||
this.markDirty(accessor)
|
||||
}
|
||||
|
||||
private fun <T : Any> SynchedEntityData.hasItem(accessor: EntityDataAccessor<T>): Boolean {
|
||||
val itemsByIDMap = itemsByIDMapField.get(this) as Int2ObjectMap<Any>
|
||||
return itemsByIDMap.containsKey(accessor.id)
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,8 @@ import com.willfp.eco.internal.spigot.proxy.common.CommonsProvider
|
||||
import com.willfp.eco.internal.spigot.proxy.common.packet.PacketInjectorListener
|
||||
import com.willfp.eco.internal.spigot.proxy.common.toResourceLocation
|
||||
import io.netty.channel.Channel
|
||||
import net.kyori.adventure.text.Component
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer
|
||||
import net.minecraft.core.registries.BuiltInRegistries
|
||||
import net.minecraft.nbt.CompoundTag
|
||||
import net.minecraft.nbt.Tag
|
||||
@@ -157,5 +159,10 @@ class CommonsInitializer : CommonsInitializerProxy {
|
||||
override fun toNMS(player: Player): ServerPlayer {
|
||||
return (player as CraftPlayer).handle
|
||||
}
|
||||
|
||||
override fun toNMS(component: Component): net.minecraft.network.chat.Component {
|
||||
val json = GsonComponentSerializer.gson().serialize(component)
|
||||
return net.minecraft.network.chat.Component.Serializer.fromJson(json)!!
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.willfp.eco.internal.spigot.proxy.v1_19_R2
|
||||
|
||||
import com.willfp.eco.core.packet.Packet
|
||||
import com.willfp.eco.core.packet.sendPacket
|
||||
import com.willfp.eco.internal.spigot.proxy.DisplayNameProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.common.toNMS
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap
|
||||
import net.kyori.adventure.text.Component
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket
|
||||
import net.minecraft.network.syncher.EntityDataAccessor
|
||||
import net.minecraft.network.syncher.SynchedEntityData
|
||||
import net.minecraft.world.entity.Entity
|
||||
import org.bukkit.craftbukkit.v1_19_R2.entity.CraftLivingEntity
|
||||
import org.bukkit.entity.LivingEntity
|
||||
import org.bukkit.entity.Player
|
||||
import java.util.Optional
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
class DisplayName : DisplayNameProxy {
|
||||
private val displayNameAccessor = Entity::class.java
|
||||
.declaredFields
|
||||
.filter { it.type == EntityDataAccessor::class.java }
|
||||
.toList()[2]
|
||||
.apply { isAccessible = true }
|
||||
.get(null) as EntityDataAccessor<Optional<net.minecraft.network.chat.Component>>
|
||||
|
||||
private val customNameVisibleAccessor = Entity::class.java
|
||||
.declaredFields
|
||||
.filter { it.type == EntityDataAccessor::class.java }
|
||||
.toList()[3]
|
||||
.apply { isAccessible = true }
|
||||
.get(null) as EntityDataAccessor<Boolean>
|
||||
|
||||
private val itemsByIDMapField = SynchedEntityData::class.java
|
||||
.declaredFields
|
||||
.filter { it.type == Int2ObjectMap::class.java }
|
||||
.toList()[0]
|
||||
.apply { isAccessible = true }
|
||||
|
||||
override fun setClientsideDisplayName(
|
||||
entity: LivingEntity,
|
||||
player: Player,
|
||||
displayName: Component,
|
||||
visible: Boolean
|
||||
) {
|
||||
if (entity !is CraftLivingEntity) {
|
||||
return
|
||||
}
|
||||
|
||||
val nmsComponent = displayName.toNMS()
|
||||
|
||||
val nmsEntity = entity.handle
|
||||
nmsEntity.isCustomNameVisible
|
||||
val entityData = SynchedEntityData(nmsEntity)
|
||||
|
||||
entityData.forceSet(displayNameAccessor, Optional.of(nmsComponent))
|
||||
entityData.forceSet(customNameVisibleAccessor, visible)
|
||||
|
||||
val packet = ClientboundSetEntityDataPacket(
|
||||
nmsEntity.id,
|
||||
entityData.packDirty() ?: throw IllegalStateException("No packed entity data")
|
||||
)
|
||||
|
||||
player.sendPacket(Packet(packet))
|
||||
}
|
||||
|
||||
private fun <T : Any> SynchedEntityData.forceSet(
|
||||
accessor: EntityDataAccessor<T>,
|
||||
value: T
|
||||
) {
|
||||
if (!this.hasItem(accessor)) {
|
||||
this.define(accessor, value)
|
||||
}
|
||||
this[accessor] = value
|
||||
this.markDirty(accessor)
|
||||
}
|
||||
|
||||
private fun <T : Any> SynchedEntityData.hasItem(accessor: EntityDataAccessor<T>): Boolean {
|
||||
val itemsByIDMap = itemsByIDMapField.get(this) as Int2ObjectMap<Any>
|
||||
return itemsByIDMap.containsKey(accessor.id)
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,8 @@ import com.willfp.eco.internal.spigot.proxy.CommonsInitializerProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.common.CommonsProvider
|
||||
import com.willfp.eco.internal.spigot.proxy.common.packet.PacketInjectorListener
|
||||
import com.willfp.eco.internal.spigot.proxy.common.toResourceLocation
|
||||
import net.kyori.adventure.text.Component
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer
|
||||
import net.minecraft.core.registries.BuiltInRegistries
|
||||
import net.minecraft.nbt.CompoundTag
|
||||
import net.minecraft.nbt.Tag
|
||||
@@ -155,5 +157,10 @@ class CommonsInitializer : CommonsInitializerProxy {
|
||||
override fun toNMS(player: Player): ServerPlayer {
|
||||
return (player as CraftPlayer).handle
|
||||
}
|
||||
|
||||
override fun toNMS(component: Component): net.minecraft.network.chat.Component {
|
||||
val json = GsonComponentSerializer.gson().serialize(component)
|
||||
return net.minecraft.network.chat.Component.Serializer.fromJson(json)!!
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.willfp.eco.internal.spigot.proxy.v1_19_R3
|
||||
|
||||
import com.willfp.eco.core.packet.Packet
|
||||
import com.willfp.eco.core.packet.sendPacket
|
||||
import com.willfp.eco.internal.spigot.proxy.DisplayNameProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.common.toNMS
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap
|
||||
import net.kyori.adventure.text.Component
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket
|
||||
import net.minecraft.network.syncher.EntityDataAccessor
|
||||
import net.minecraft.network.syncher.SynchedEntityData
|
||||
import net.minecraft.world.entity.Entity
|
||||
import org.bukkit.craftbukkit.v1_19_R3.entity.CraftLivingEntity
|
||||
import org.bukkit.entity.LivingEntity
|
||||
import org.bukkit.entity.Player
|
||||
import java.util.Optional
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
class DisplayName : DisplayNameProxy {
|
||||
private val displayNameAccessor = Entity::class.java
|
||||
.declaredFields
|
||||
.filter { it.type == EntityDataAccessor::class.java }
|
||||
.toList()[2]
|
||||
.apply { isAccessible = true }
|
||||
.get(null) as EntityDataAccessor<Optional<net.minecraft.network.chat.Component>>
|
||||
|
||||
private val customNameVisibleAccessor = Entity::class.java
|
||||
.declaredFields
|
||||
.filter { it.type == EntityDataAccessor::class.java }
|
||||
.toList()[3]
|
||||
.apply { isAccessible = true }
|
||||
.get(null) as EntityDataAccessor<Boolean>
|
||||
|
||||
private val itemsByIDMapField = SynchedEntityData::class.java
|
||||
.declaredFields
|
||||
.filter { it.type == Int2ObjectMap::class.java }
|
||||
.toList()[0]
|
||||
.apply { isAccessible = true }
|
||||
|
||||
override fun setClientsideDisplayName(
|
||||
entity: LivingEntity,
|
||||
player: Player,
|
||||
displayName: Component,
|
||||
visible: Boolean
|
||||
) {
|
||||
if (entity !is CraftLivingEntity) {
|
||||
return
|
||||
}
|
||||
|
||||
val nmsComponent = displayName.toNMS()
|
||||
|
||||
val nmsEntity = entity.handle
|
||||
nmsEntity.isCustomNameVisible
|
||||
val entityData = SynchedEntityData(nmsEntity)
|
||||
|
||||
entityData.forceSet(displayNameAccessor, Optional.of(nmsComponent))
|
||||
entityData.forceSet(customNameVisibleAccessor, visible)
|
||||
|
||||
val packet = ClientboundSetEntityDataPacket(
|
||||
nmsEntity.id,
|
||||
entityData.packDirty() ?: throw IllegalStateException("No packed entity data")
|
||||
)
|
||||
|
||||
player.sendPacket(Packet(packet))
|
||||
}
|
||||
|
||||
private fun <T : Any> SynchedEntityData.forceSet(
|
||||
accessor: EntityDataAccessor<T>,
|
||||
value: T
|
||||
) {
|
||||
if (!this.hasItem(accessor)) {
|
||||
this.define(accessor, value)
|
||||
}
|
||||
this[accessor] = value
|
||||
this.markDirty(accessor)
|
||||
}
|
||||
|
||||
private fun <T : Any> SynchedEntityData.hasItem(accessor: EntityDataAccessor<T>): Boolean {
|
||||
val itemsByIDMap = itemsByIDMapField.get(this) as Int2ObjectMap<Any>
|
||||
return itemsByIDMap.containsKey(accessor.id)
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,8 @@ import com.willfp.eco.internal.spigot.proxy.CommonsInitializerProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.common.CommonsProvider
|
||||
import com.willfp.eco.internal.spigot.proxy.common.packet.PacketInjectorListener
|
||||
import com.willfp.eco.internal.spigot.proxy.common.toResourceLocation
|
||||
import net.kyori.adventure.text.Component
|
||||
import net.kyori.adventure.text.serializer.json.JSONComponentSerializer
|
||||
import net.minecraft.core.registries.BuiltInRegistries
|
||||
import net.minecraft.nbt.CompoundTag
|
||||
import net.minecraft.nbt.Tag
|
||||
@@ -155,5 +157,10 @@ class CommonsInitializer : CommonsInitializerProxy {
|
||||
override fun toNMS(player: Player): ServerPlayer {
|
||||
return (player as CraftPlayer).handle
|
||||
}
|
||||
|
||||
override fun toNMS(component: Component): net.minecraft.network.chat.Component {
|
||||
val json = JSONComponentSerializer.json().serialize(component)
|
||||
return net.minecraft.network.chat.Component.Serializer.fromJson(json)!!
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.willfp.eco.internal.spigot.proxy.v1_20_R1
|
||||
import com.willfp.eco.core.packet.Packet
|
||||
import com.willfp.eco.core.packet.sendPacket
|
||||
import com.willfp.eco.internal.spigot.proxy.DisplayNameProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.common.toNMS
|
||||
import io.papermc.paper.adventure.PaperAdventure
|
||||
import net.kyori.adventure.text.Component
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket
|
||||
@@ -40,8 +41,7 @@ class DisplayName : DisplayNameProxy {
|
||||
return
|
||||
}
|
||||
|
||||
val nmsComponent = PaperAdventure.asVanilla(displayName)
|
||||
?: throw IllegalStateException("Display name component is null!")
|
||||
val nmsComponent = displayName.toNMS()
|
||||
|
||||
val nmsEntity = entity.handle
|
||||
nmsEntity.isCustomNameVisible
|
||||
|
||||
@@ -5,6 +5,8 @@ import com.willfp.eco.internal.spigot.proxy.CommonsInitializerProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.common.CommonsProvider
|
||||
import com.willfp.eco.internal.spigot.proxy.common.packet.PacketInjectorListener
|
||||
import com.willfp.eco.internal.spigot.proxy.common.toResourceLocation
|
||||
import net.kyori.adventure.text.Component
|
||||
import net.kyori.adventure.text.serializer.json.JSONComponentSerializer
|
||||
import net.minecraft.core.registries.BuiltInRegistries
|
||||
import net.minecraft.nbt.CompoundTag
|
||||
import net.minecraft.nbt.Tag
|
||||
@@ -155,5 +157,10 @@ class CommonsInitializer : CommonsInitializerProxy {
|
||||
override fun toNMS(player: Player): ServerPlayer {
|
||||
return (player as CraftPlayer).handle
|
||||
}
|
||||
|
||||
override fun toNMS(component: Component): net.minecraft.network.chat.Component {
|
||||
val json = JSONComponentSerializer.json().serialize(component)
|
||||
return net.minecraft.network.chat.Component.Serializer.fromJson(json)!!
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.willfp.eco.internal.spigot.proxy.v1_20_R2
|
||||
import com.willfp.eco.core.packet.Packet
|
||||
import com.willfp.eco.core.packet.sendPacket
|
||||
import com.willfp.eco.internal.spigot.proxy.DisplayNameProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.common.toNMS
|
||||
import io.papermc.paper.adventure.PaperAdventure
|
||||
import net.kyori.adventure.text.Component
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket
|
||||
@@ -37,8 +38,7 @@ class DisplayName : DisplayNameProxy {
|
||||
return
|
||||
}
|
||||
|
||||
val nmsComponent = PaperAdventure.asVanilla(displayName)
|
||||
?: throw IllegalStateException("Display name component is null!")
|
||||
val nmsComponent = displayName.toNMS()
|
||||
|
||||
val nmsEntity = entity.handle
|
||||
nmsEntity.isCustomNameVisible
|
||||
|
||||
@@ -5,6 +5,8 @@ import com.willfp.eco.internal.spigot.proxy.CommonsInitializerProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.common.CommonsProvider
|
||||
import com.willfp.eco.internal.spigot.proxy.common.packet.PacketInjectorListener
|
||||
import com.willfp.eco.internal.spigot.proxy.common.toResourceLocation
|
||||
import net.kyori.adventure.text.Component
|
||||
import net.kyori.adventure.text.serializer.json.JSONComponentSerializer
|
||||
import net.minecraft.core.registries.BuiltInRegistries
|
||||
import net.minecraft.nbt.CompoundTag
|
||||
import net.minecraft.nbt.Tag
|
||||
@@ -155,5 +157,10 @@ class CommonsInitializer : CommonsInitializerProxy {
|
||||
override fun toNMS(player: Player): ServerPlayer {
|
||||
return (player as CraftPlayer).handle
|
||||
}
|
||||
|
||||
override fun toNMS(component: Component): net.minecraft.network.chat.Component {
|
||||
val json = JSONComponentSerializer.json().serialize(component)
|
||||
return net.minecraft.network.chat.Component.Serializer.fromJson(json)!!
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.willfp.eco.internal.spigot.proxy.v1_20_R3
|
||||
import com.willfp.eco.core.packet.Packet
|
||||
import com.willfp.eco.core.packet.sendPacket
|
||||
import com.willfp.eco.internal.spigot.proxy.DisplayNameProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.common.toNMS
|
||||
import io.papermc.paper.adventure.PaperAdventure
|
||||
import net.kyori.adventure.text.Component
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket
|
||||
@@ -37,8 +38,7 @@ class DisplayName : DisplayNameProxy {
|
||||
return
|
||||
}
|
||||
|
||||
val nmsComponent = PaperAdventure.asVanilla(displayName)
|
||||
?: throw IllegalStateException("Display name component is null!")
|
||||
val nmsComponent = displayName.toNMS()
|
||||
|
||||
val nmsEntity = entity.handle
|
||||
nmsEntity.isCustomNameVisible
|
||||
|
||||
@@ -352,7 +352,5 @@ class EcoImpl : EcoSpigotPlugin(), Eco {
|
||||
placeholderParser.getPlaceholderResult(plugin, args, context)
|
||||
|
||||
override fun setClientsideDisplayName(entity: LivingEntity, player: Player, name: Component, visible: Boolean) =
|
||||
if (Prerequisite.HAS_PAPER.isMet && Prerequisite.HAS_1_20.isMet)
|
||||
this.getProxy(DisplayNameProxy::class.java).setClientsideDisplayName(entity, player, name, visible)
|
||||
else Unit
|
||||
this.getProxy(DisplayNameProxy::class.java).setClientsideDisplayName(entity, player, name, visible)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user