9
0
mirror of https://github.com/Auxilor/EcoMobs.git synced 2025-12-19 15:09:17 +00:00

Added preliminary client-side display name support (will merge into eco)

This commit is contained in:
Auxilor
2023-11-13 16:02:01 +00:00
parent 38f9b6df64
commit 7a6e0cf828
9 changed files with 115 additions and 7 deletions

View File

@@ -16,9 +16,8 @@ base {
}
dependencies {
project(":eco-core").dependencyProject.subprojects {
implementation(this)
}
implementation(project(":eco-core:core-plugin"))
implementation(project(path = ":eco-core:core-nms:v1_20_R1", configuration = "reobf"))
}
allprojects {

View File

@@ -0,0 +1,12 @@
plugins {
id("io.papermc.paperweight.userdev") version "1.5.3" apply false
}
group = "com.willfp"
version = rootProject.version
subprojects {
dependencies {
compileOnly(project(":eco-core:core-plugin"))
}
}

View File

@@ -0,0 +1,10 @@
plugins {
id("io.papermc.paperweight.userdev")
}
group = "com.willfp"
version = rootProject.version
dependencies {
paperweight.paperDevBundle("1.20.1-R0.1-SNAPSHOT")
}

View File

@@ -0,0 +1,66 @@
package com.willfp.ecomobs.nms.v1_20_R1
import com.willfp.eco.core.packet.Packet
import com.willfp.eco.core.packet.sendPacket
import com.willfp.ecomobs.name.DisplayNameProxy
import io.papermc.paper.adventure.PaperAdventure
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_20_R1.entity.CraftMob
import org.bukkit.entity.Mob
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>
override fun setDisplayName(mob: Mob, player: Player, displayName: Component, visible: Boolean) {
if (mob !is CraftMob) {
return
}
val nmsComponent = PaperAdventure.asVanilla(displayName)
?: throw IllegalStateException("Display name component is null!")
val nmsEntity = mob.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> SynchedEntityData.forceSet(
accessor: EntityDataAccessor<T>,
value: T
) {
if (!this.hasItem(accessor)) {
this.define(accessor, value)
}
this[accessor] = value
this.markDirty(accessor)
}
}

View File

@@ -4,7 +4,7 @@ group = "com.willfp"
version = rootProject.version
dependencies {
compileOnly("io.papermc.paper:paper-api:1.19.3-R0.1-SNAPSHOT")
compileOnly("io.papermc.paper:paper-api:1.20.2-R0.1-SNAPSHOT")
compileOnly("com.github.lokka30:LevelledMobs:3.1.4")
compileOnly("com.ticxo.modelengine:api:R3.1.8")
compileOnly("LibsDisguises:LibsDisguises:10.0.38")

View File

@@ -0,0 +1,14 @@
package com.willfp.ecomobs.name
import com.willfp.eco.util.toComponent
import com.willfp.ecomobs.plugin
import net.kyori.adventure.text.Component
import org.bukkit.entity.Mob
import org.bukkit.entity.Player
interface DisplayNameProxy {
fun setDisplayName(mob: Mob, player: Player, displayName: Component, visible: Boolean)
}
fun Mob.setClientDisplayName(player: Player, name: String, visible: Boolean) =
plugin.getProxy(DisplayNameProxy::class.java).setDisplayName(this, player, name.toComponent(), visible)

View File

@@ -1,14 +1,17 @@
package com.willfp.ecomobs.tick
import com.willfp.ecomobs.mob.LivingMob
import com.willfp.ecomobs.name.setClientDisplayName
import org.bukkit.entity.Player
class TickHandlerDisplayName: TickHandler {
class TickHandlerDisplayName : TickHandler {
override fun tick(mob: LivingMob, tick: Int) {
if (tick % 5 != 0) {
return
}
@Suppress("DEPRECATION")
mob.entity.customName = mob.displayName
mob.entity.getNearbyEntities(20.0, 20.0, 20.0)
.filterIsInstance<Player>()
.forEach { mob.entity.setClientDisplayName(it, mob.displayName, false) }
}
}

View File

@@ -6,3 +6,4 @@ options:
resource-id: 525
bstats-id: 10635
color: "&9"
proxy-package: "com.willfp.ecomobs.nms"

View File

@@ -4,6 +4,7 @@ pluginManagement {
mavenLocal()
maven("https://repo.jpenilla.xyz/snapshots/")
maven("https://repo.auxilor.io/repository/maven-public/")
maven("https://repo.papermc.io/repository/maven-public/")
}
}
@@ -12,3 +13,5 @@ rootProject.name = "EcoMobs"
// Core
include(":eco-core")
include(":eco-core:core-plugin")
include(":eco-core:core-nms")
include(":eco-core:core-nms:v1_20_R1")