Skip to content

Commit

Permalink
Added clientside entity display name API for all versions
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed Dec 8, 2023
1 parent 9c4a65d commit ad272a2
Show file tree
Hide file tree
Showing 20 changed files with 574 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)!!
}
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)!!
}
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)!!
}
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)!!
}
}
}
Loading

0 comments on commit ad272a2

Please sign in to comment.