From f15fb4d667adee0da21b0a5f05ff2d219a8cbbac Mon Sep 17 00:00:00 2001 From: Will FP Date: Fri, 8 Dec 2023 12:24:47 +0000 Subject: [PATCH] Migrations and updates --- build.gradle.kts | 7 +- eco-core/core-nms/build.gradle.kts | 12 ---- eco-core/core-nms/v1_20_R1/build.gradle.kts | 10 --- .../ecomobs/nms/v1_20_R1/DisplayName.kt | 66 ------------------- .../com/willfp/ecomobs/EcoMobsPlugin.kt | 2 + .../willfp/ecomobs/mob/damage/TopDamagers.kt | 58 ++++++++++++++++ .../willfp/ecomobs/mob/impl/LivingMobImpl.kt | 6 +- .../com/willfp/ecomobs/name/DisplayName.kt | 14 ---- .../ecomobs/tick/TickHandlerDisplayName.kt | 5 +- .../main/resources/categories/_example.yml | 2 +- .../src/main/resources/mobs/_example.yml | 2 + .../src/main/resources/paper-plugin.yml | 65 ------------------ settings.gradle.kts | 2 - 13 files changed, 74 insertions(+), 177 deletions(-) delete mode 100644 eco-core/core-nms/build.gradle.kts delete mode 100644 eco-core/core-nms/v1_20_R1/build.gradle.kts delete mode 100644 eco-core/core-nms/v1_20_R1/src/main/kotlin/com/willfp/ecomobs/nms/v1_20_R1/DisplayName.kt create mode 100644 eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/mob/damage/TopDamagers.kt delete mode 100644 eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/name/DisplayName.kt delete mode 100644 eco-core/core-plugin/src/main/resources/paper-plugin.yml diff --git a/build.gradle.kts b/build.gradle.kts index 5f18f9f..a0993d1 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -2,7 +2,7 @@ plugins { java `java-library` `maven-publish` - kotlin("jvm") version "1.7.10" + kotlin("jvm") version "1.9.20" id("com.github.johnrengelman.shadow") version "8.0.0" id("com.willfp.libreforge-gradle-plugin") version "1.0.0" } @@ -17,7 +17,6 @@ base { dependencies { implementation(project(":eco-core:core-plugin")) - implementation(project(path = ":eco-core:core-nms:v1_20_R1", configuration = "reobf")) } allprojects { @@ -37,9 +36,9 @@ allprojects { } dependencies { - compileOnly("com.willfp:eco:6.65.0") + compileOnly("com.willfp:eco:6.67.0") compileOnly("org.jetbrains:annotations:23.0.0") - compileOnly("org.jetbrains.kotlin:kotlin-stdlib:1.7.10") + compileOnly("org.jetbrains.kotlin:kotlin-stdlib:1.9.20") compileOnly("com.github.ben-manes.caffeine:caffeine:3.1.5") } diff --git a/eco-core/core-nms/build.gradle.kts b/eco-core/core-nms/build.gradle.kts deleted file mode 100644 index 730d500..0000000 --- a/eco-core/core-nms/build.gradle.kts +++ /dev/null @@ -1,12 +0,0 @@ -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 deleted file mode 100644 index f661da8..0000000 --- a/eco-core/core-nms/v1_20_R1/build.gradle.kts +++ /dev/null @@ -1,10 +0,0 @@ -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 deleted file mode 100644 index 290d679..0000000 --- a/eco-core/core-nms/v1_20_R1/src/main/kotlin/com/willfp/ecomobs/nms/v1_20_R1/DisplayName.kt +++ /dev/null @@ -1,66 +0,0 @@ -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/src/main/kotlin/com/willfp/ecomobs/EcoMobsPlugin.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/EcoMobsPlugin.kt index 44a6e6a..7593cb2 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/EcoMobsPlugin.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/EcoMobsPlugin.kt @@ -18,6 +18,7 @@ import com.willfp.ecomobs.integrations.levelledmobs.IntegrationLevelledMobs import com.willfp.ecomobs.integrations.libsdisguises.IntegrationLibsDisguises import com.willfp.ecomobs.integrations.modelengine.IntegrationModelEngine import com.willfp.ecomobs.mob.EcoMobs +import com.willfp.ecomobs.mob.damage.TopDamagerHandler import com.willfp.libreforge.loader.LibreforgePlugin import com.willfp.libreforge.loader.configs.ConfigCategory import org.bukkit.event.Listener @@ -27,6 +28,7 @@ internal lateinit var plugin: EcoMobsPlugin class EcoMobsPlugin : LibreforgePlugin() { val spawnPointGenerator = SpawnPointGenerator(this) + val topDamagerHandler = TopDamagerHandler(this) init { plugin = this diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/mob/damage/TopDamagers.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/mob/damage/TopDamagers.kt new file mode 100644 index 0000000..58e73fc --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/mob/damage/TopDamagers.kt @@ -0,0 +1,58 @@ +package com.willfp.ecomobs.mob.damage + +import com.willfp.eco.util.savedDisplayName +import com.willfp.eco.util.tryAsPlayer +import com.willfp.ecomobs.EcoMobsPlugin +import com.willfp.libreforge.NamedValue +import org.bukkit.Bukkit +import org.bukkit.entity.Mob +import org.bukkit.event.EventHandler +import org.bukkit.event.EventPriority +import org.bukkit.event.Listener +import org.bukkit.event.entity.EntityDamageByEntityEvent +import java.util.UUID + +/* +Old code mostly ported from EcoBosses, can't be bothered to write it again + */ + +data class Damager( + val uuid: UUID, var damage: Double +) + +private const val metaKey = "TOP_DAMAGERS" + +class TopDamagerHandler( + private val plugin: EcoMobsPlugin +) : Listener { + @Suppress("UNCHECKED_CAST") + private var Mob.topDamagers: List + get() = (this.getMetadata(metaKey).getOrNull(0)?.value() as? List) ?: emptyList() + set(value) { + this.removeMetadata(metaKey, plugin) + this.setMetadata(metaKey, plugin.metadataValueFactory.create(value)) + } + + @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR) + fun handle(event: EntityDamageByEntityEvent) { + val uuid = event.damager.tryAsPlayer()?.uniqueId ?: return + val victim = event.entity as? Mob ?: return + + val topDamagers = victim.topDamagers.toMutableList() + + val damager = topDamagers.firstOrNull { it.uuid == uuid } ?: Damager(uuid, 0.0) + damager.damage += event.damage + topDamagers.removeIf { it.uuid == uuid } + topDamagers.add(damager) + victim.topDamagers = topDamagers.sortedByDescending { it.damage } + } + + fun generatePlaceholders(mob: Mob): List { + return mob.topDamagers + .mapIndexed { index, damager -> + NamedValue("top_damager_${index + 1}_name", Bukkit.getOfflinePlayer(damager.uuid).name ?: "Unknown") + NamedValue("top_damager_${index + 1}_display", Bukkit.getOfflinePlayer(damager.uuid).savedDisplayName) + NamedValue("top_damager_${index + 1}_damage", damager.damage.toString()) + } + } +} diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/mob/impl/LivingMobImpl.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/mob/impl/LivingMobImpl.kt index 22fdd69..1eeb22d 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/mob/impl/LivingMobImpl.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/mob/impl/LivingMobImpl.kt @@ -16,7 +16,7 @@ import org.bukkit.entity.Mob import org.bukkit.entity.Player internal class LivingMobImpl( - plugin: EcoMobsPlugin, + private val plugin: EcoMobsPlugin, override val mob: EcoMob, override val entity: Mob, private val deathCallback: () -> Unit @@ -70,6 +70,10 @@ internal class LivingMobImpl( trigger.addPlaceholder(NamedValue(placeholder.id, placeholder.getValue(this))) } + for (placeholder in plugin.topDamagerHandler.generatePlaceholders(entity)) { + trigger.addPlaceholder(placeholder) + } + mob.handleEvent(event, trigger) } 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 deleted file mode 100644 index 8a16c07..0000000 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/name/DisplayName.kt +++ /dev/null @@ -1,14 +0,0 @@ -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 8b0a2c8..1ddc631 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,7 +1,8 @@ package com.willfp.ecomobs.tick +import com.willfp.eco.util.setClientsideDisplayName +import com.willfp.eco.util.toComponent import com.willfp.ecomobs.mob.LivingMob -import com.willfp.ecomobs.name.setClientDisplayName import org.bukkit.entity.Player class TickHandlerDisplayName : TickHandler { @@ -12,6 +13,6 @@ class TickHandlerDisplayName : TickHandler { mob.entity.getNearbyEntities(20.0, 20.0, 20.0) .filterIsInstance() - .forEach { mob.entity.setClientDisplayName(it, mob.displayName, false) } + .forEach { mob.entity.setClientsideDisplayName(it, mob.displayName.toComponent(), false) } } } diff --git a/eco-core/core-plugin/src/main/resources/categories/_example.yml b/eco-core/core-plugin/src/main/resources/categories/_example.yml index f4a6c6b..2b37bff 100644 --- a/eco-core/core-plugin/src/main/resources/categories/_example.yml +++ b/eco-core/core-plugin/src/main/resources/categories/_example.yml @@ -17,7 +17,7 @@ spawning: # none # Disables natural spawning - type: replace + type: custom # Options for replace spawning replace: diff --git a/eco-core/core-plugin/src/main/resources/mobs/_example.yml b/eco-core/core-plugin/src/main/resources/mobs/_example.yml index 81f3f7a..788a3c0 100644 --- a/eco-core/core-plugin/src/main/resources/mobs/_example.yml +++ b/eco-core/core-plugin/src/main/resources/mobs/_example.yml @@ -55,6 +55,8 @@ custom-ai: # Effects are done from the player's perspective: to treat the player as the victim, # either use self_as_victim in args, or use player_as_victim in mutators. # You can use display name placeholders in effects +# You can also use top damager placeholders: +# %top_damager__name%, %top_damager__damage%, %top_damager__display% effects: # Effects ran when the mob spawns spawn: [ ] diff --git a/eco-core/core-plugin/src/main/resources/paper-plugin.yml b/eco-core/core-plugin/src/main/resources/paper-plugin.yml deleted file mode 100644 index 6d716ab..0000000 --- a/eco-core/core-plugin/src/main/resources/paper-plugin.yml +++ /dev/null @@ -1,65 +0,0 @@ -name: ${pluginName} -version: ${version} -main: com.willfp.ecomobs.EcoMobsPlugin -api-version: 1.19 - -dependencies: - - name: eco - required: true - bootstrap: false - - - name: libreforge - required: false - bootstrap: false - - - name: LevelledMobs - required: false - bootstrap: false - - - name: ModelEngine - required: false - bootstrap: false - - - name: LibsDisguises - required: false - bootstrap: false - -load-after: - - name: eco - bootstrap: false - - -permissions: - ecomobs.*: - description: All ecomobs permissions - default: op - children: - ecomobs.command.*: true - ecomobs.command.*: - description: All ecomobs commands - default: op - children: - ecomobs.command.ecomobs: true - ecomobs.command.reload: true - ecomobs.command.spawn: true - ecomobs.command.give: true - - ecomobs.command.ecomobs: - description: Allows the use of /ecomobs - default: true - - ecomobs.command.give: - description: Allows the use of /ecomobs give - default: op - - ecomobs.command.spawn: - description: Allows the use of /ecomobs spawn - default: op - - ecomobs.command.killall: - description: Allows the use of /ecomobs killall - default: op - - ecomobs.command.reload: - description: Allows the use of /ecomobs reload - default: op diff --git a/settings.gradle.kts b/settings.gradle.kts index 28a4d29..748951c 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -13,5 +13,3 @@ 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")