diff --git a/build.gradle.kts b/build.gradle.kts index 7e4153b..aaf9390 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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 { diff --git a/eco-core/core-nms/build.gradle.kts b/eco-core/core-nms/build.gradle.kts new file mode 100644 index 0000000..730d500 --- /dev/null +++ b/eco-core/core-nms/build.gradle.kts @@ -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")) + } +} diff --git a/eco-core/core-nms/v1_20_R1/build.gradle.kts b/eco-core/core-nms/v1_20_R1/build.gradle.kts new file mode 100644 index 0000000..f661da8 --- /dev/null +++ b/eco-core/core-nms/v1_20_R1/build.gradle.kts @@ -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") +} diff --git a/eco-core/core-nms/v1_20_R1/src/main/kotlin/com/willfp/ecomobs/nms/v1_20_R1/DisplayName.kt b/eco-core/core-nms/v1_20_R1/src/main/kotlin/com/willfp/ecomobs/nms/v1_20_R1/DisplayName.kt new file mode 100644 index 0000000..290d679 --- /dev/null +++ b/eco-core/core-nms/v1_20_R1/src/main/kotlin/com/willfp/ecomobs/nms/v1_20_R1/DisplayName.kt @@ -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> + + private val customNameVisibleAccessor = Entity::class.java + .declaredFields + .filter { it.type == EntityDataAccessor::class.java } + .toList()[3] + .apply { isAccessible = true } + .get(null) as EntityDataAccessor + + 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 SynchedEntityData.forceSet( + accessor: EntityDataAccessor, + value: T + ) { + if (!this.hasItem(accessor)) { + this.define(accessor, value) + } + this[accessor] = value + this.markDirty(accessor) + } +} diff --git a/eco-core/core-plugin/build.gradle.kts b/eco-core/core-plugin/build.gradle.kts index 5e7b62e..744651a 100644 --- a/eco-core/core-plugin/build.gradle.kts +++ b/eco-core/core-plugin/build.gradle.kts @@ -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") diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/name/DisplayName.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/name/DisplayName.kt new file mode 100644 index 0000000..8a16c07 --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/name/DisplayName.kt @@ -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) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/tick/TickHandlerDisplayName.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/tick/TickHandlerDisplayName.kt index 26c533a..8b0a2c8 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/tick/TickHandlerDisplayName.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/tick/TickHandlerDisplayName.kt @@ -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() + .forEach { mob.entity.setClientDisplayName(it, mob.displayName, false) } } } diff --git a/eco-core/core-plugin/src/main/resources/eco.yml b/eco-core/core-plugin/src/main/resources/eco.yml index be43918..f196fdf 100644 --- a/eco-core/core-plugin/src/main/resources/eco.yml +++ b/eco-core/core-plugin/src/main/resources/eco.yml @@ -6,3 +6,4 @@ options: resource-id: 525 bstats-id: 10635 color: "&9" + proxy-package: "com.willfp.ecomobs.nms" diff --git a/settings.gradle.kts b/settings.gradle.kts index d9d6655..28a4d29 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -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")