From 1ea2ea797771de984abf774e76643b88dad3b5cf Mon Sep 17 00:00:00 2001 From: Danielle Voznyy Date: Fri, 13 Dec 2024 19:07:10 -0500 Subject: [PATCH] feat: Inheriting prefabs from MythicMobs mobs feat: Embedded geary entity definition in MythicMobs --- geary-papermc-mythicmobs/build.gradle.kts | 1 + .../mythicmobs/GearyMythicConfigOptions.kt | 29 ++++++++++ .../mythicmobs/MythicEmbeddedGearyEntity.kt | 58 +++++++++++++++++++ .../papermc/mythicmobs/MythicMobsFeature.kt | 4 +- .../skills/MythicPrefabsListeners.kt | 53 +++++++++++++++++ .../skills/MythicSkillRegisterListener.kt | 14 ----- 6 files changed, 143 insertions(+), 16 deletions(-) create mode 100644 geary-papermc-mythicmobs/src/main/kotlin/com/mineinabyss/geary/papermc/mythicmobs/GearyMythicConfigOptions.kt create mode 100644 geary-papermc-mythicmobs/src/main/kotlin/com/mineinabyss/geary/papermc/mythicmobs/MythicEmbeddedGearyEntity.kt create mode 100644 geary-papermc-mythicmobs/src/main/kotlin/com/mineinabyss/geary/papermc/mythicmobs/skills/MythicPrefabsListeners.kt delete mode 100644 geary-papermc-mythicmobs/src/main/kotlin/com/mineinabyss/geary/papermc/mythicmobs/skills/MythicSkillRegisterListener.kt diff --git a/geary-papermc-mythicmobs/build.gradle.kts b/geary-papermc-mythicmobs/build.gradle.kts index dbcc093a..6f7ee212 100644 --- a/geary-papermc-mythicmobs/build.gradle.kts +++ b/geary-papermc-mythicmobs/build.gradle.kts @@ -14,6 +14,7 @@ dependencies { // Other deps compileOnly(idofrontLibs.kotlinx.serialization.json) + compileOnly(idofrontLibs.kotlinx.serialization.kaml) compileOnly(idofrontLibs.minecraft.mccoroutine) compileOnly(libs.geary.actions) diff --git a/geary-papermc-mythicmobs/src/main/kotlin/com/mineinabyss/geary/papermc/mythicmobs/GearyMythicConfigOptions.kt b/geary-papermc-mythicmobs/src/main/kotlin/com/mineinabyss/geary/papermc/mythicmobs/GearyMythicConfigOptions.kt new file mode 100644 index 00000000..961be2a0 --- /dev/null +++ b/geary-papermc-mythicmobs/src/main/kotlin/com/mineinabyss/geary/papermc/mythicmobs/GearyMythicConfigOptions.kt @@ -0,0 +1,29 @@ +package com.mineinabyss.geary.papermc.mythicmobs + +import com.github.shynixn.mccoroutine.bukkit.launch +import com.mineinabyss.geary.papermc.gearyPaper +import com.mineinabyss.geary.papermc.toGeary +import com.mineinabyss.geary.papermc.tracking.entities.toGeary +import com.mineinabyss.geary.prefabs.PrefabKey +import com.mineinabyss.geary.prefabs.entityOfOrNull +import io.lumine.mythic.api.mobs.MythicMob +import io.lumine.mythic.bukkit.BukkitAdapter +import io.lumine.mythic.core.mobs.ActiveMob + +object GearyMythicConfigOptions { + val MythicMob.prefabs + get() = config.getStringList("Prefabs") + .mapNotNull { PrefabKey.ofOrNull(it) } + + fun ActiveMob.addPrefabs(prefabs: List) { + if (prefabs.isEmpty()) return + val bukkit = BukkitAdapter.adapt(entity) + with(bukkit.world.toGeary()) { + val gearyMob = bukkit.toGeary() + gearyPaper.plugin.launch { + prefabs.mapNotNull { entityOfOrNull(it) } + .forEach(gearyMob::extend) + } + } + } +} diff --git a/geary-papermc-mythicmobs/src/main/kotlin/com/mineinabyss/geary/papermc/mythicmobs/MythicEmbeddedGearyEntity.kt b/geary-papermc-mythicmobs/src/main/kotlin/com/mineinabyss/geary/papermc/mythicmobs/MythicEmbeddedGearyEntity.kt new file mode 100644 index 00000000..e2169351 --- /dev/null +++ b/geary-papermc-mythicmobs/src/main/kotlin/com/mineinabyss/geary/papermc/mythicmobs/MythicEmbeddedGearyEntity.kt @@ -0,0 +1,58 @@ +package com.mineinabyss.geary.papermc.mythicmobs + +import com.charleskorn.kaml.YamlMap +import com.charleskorn.kaml.yamlMap +import com.mineinabyss.geary.actions.event_binds.EntityObservers +import com.mineinabyss.geary.datatypes.GearyEntity +import com.mineinabyss.geary.helpers.entity +import com.mineinabyss.geary.modules.Geary +import com.mineinabyss.geary.papermc.toEntityOrNull +import com.mineinabyss.geary.prefabs.PrefabKey +import com.mineinabyss.geary.prefabs.Prefabs +import com.mineinabyss.geary.serialization.SerializableComponents +import com.mineinabyss.geary.serialization.formats.YamlFormat +import com.mineinabyss.geary.serialization.serializers.PolymorphicListAsMapSerializer +import io.lumine.mythic.api.mobs.MythicMob + +object MythicEmbeddedGearyEntity { + const val NODE_GEARY = "Geary" + const val NODE_OBSERVE = "Observe" + + /** + * Gets the embedded prefab based on the mob name or parses and loads it. + */ + fun getOrLoadEmbeddedPrefab(world: Geary, mob: MythicMob): GearyEntity? = with(world) { + // Check for existing prefab + val config = mob.config + val prefabKey = PrefabKey.of("mythicmobs", config.key) + prefabKey.toEntityOrNull()?.let { return it } + + // Load prefab + // TODO create some helper functions for this in PrefabLoader + val prefabs = getAddon(Prefabs) + val yamlFormat = getAddon(SerializableComponents).formats["yml"] as? YamlFormat ?: return@with null + val mobNode = yamlFormat.regularYaml.parseToYamlNode(config.fileConfiguration.saveToString()) + .yamlMap.get(config.key) ?: return null + + // Get special nodes and decode them + val gearyNode = mobNode.get(NODE_GEARY) + val observeNode = mobNode.get(NODE_OBSERVE) + + if (gearyNode == null && observeNode == null) return@with null + + val serializer = PolymorphicListAsMapSerializer.ofComponents() + val yaml = yamlFormat.regularYaml + val components = gearyNode?.let { yaml.decodeFromYamlNode(serializer, it) } + val observeComponent = observeNode?.let { yaml.decodeFromYamlNode(EntityObservers.serializer(), it) } + + // Register and return + val prefabEntity = entity { + components?.forEach { + set(it, it::class) + } + observeComponent?.let { set(it, it::class) } + } + prefabs.manager.registerPrefab(prefabKey, prefabEntity) + prefabEntity + } +} diff --git a/geary-papermc-mythicmobs/src/main/kotlin/com/mineinabyss/geary/papermc/mythicmobs/MythicMobsFeature.kt b/geary-papermc-mythicmobs/src/main/kotlin/com/mineinabyss/geary/papermc/mythicmobs/MythicMobsFeature.kt index 821d632d..ae6b1dd2 100644 --- a/geary-papermc-mythicmobs/src/main/kotlin/com/mineinabyss/geary/papermc/mythicmobs/MythicMobsFeature.kt +++ b/geary-papermc-mythicmobs/src/main/kotlin/com/mineinabyss/geary/papermc/mythicmobs/MythicMobsFeature.kt @@ -6,7 +6,7 @@ import com.mineinabyss.geary.papermc.configure import com.mineinabyss.geary.papermc.gearyPaper import com.mineinabyss.geary.papermc.mythicmobs.actions.runMMSkillAction import com.mineinabyss.geary.papermc.mythicmobs.items.MythicMobDropListener -import com.mineinabyss.geary.papermc.mythicmobs.skills.MythicSkillRegisterListener +import com.mineinabyss.geary.papermc.mythicmobs.skills.MythicPrefabsListeners import com.mineinabyss.geary.papermc.mythicmobs.spawning.markMMAsCustomMob import com.mineinabyss.geary.papermc.mythicmobs.spawning.mythicMobSpawner @@ -24,7 +24,7 @@ class MythicMobsFeature(context: FeatureContext) : Feature(context) { listeners( MythicMobDropListener(), - MythicSkillRegisterListener(), + MythicPrefabsListeners(logger), ) } } diff --git a/geary-papermc-mythicmobs/src/main/kotlin/com/mineinabyss/geary/papermc/mythicmobs/skills/MythicPrefabsListeners.kt b/geary-papermc-mythicmobs/src/main/kotlin/com/mineinabyss/geary/papermc/mythicmobs/skills/MythicPrefabsListeners.kt new file mode 100644 index 00000000..522e2cea --- /dev/null +++ b/geary-papermc-mythicmobs/src/main/kotlin/com/mineinabyss/geary/papermc/mythicmobs/skills/MythicPrefabsListeners.kt @@ -0,0 +1,53 @@ +package com.mineinabyss.geary.papermc.mythicmobs.skills + +import co.touchlab.kermit.Logger +import com.mineinabyss.geary.papermc.mythicmobs.GearyMythicConfigOptions.addPrefabs +import com.mineinabyss.geary.papermc.mythicmobs.GearyMythicConfigOptions.prefabs +import com.mineinabyss.geary.papermc.mythicmobs.MythicEmbeddedGearyEntity +import com.mineinabyss.geary.papermc.toGeary +import com.mineinabyss.geary.papermc.tracking.entities.toGeary +import io.lumine.mythic.bukkit.BukkitAdapter +import io.lumine.mythic.bukkit.events.MythicMechanicLoadEvent +import io.lumine.mythic.bukkit.events.MythicMobSpawnEvent +import io.lumine.mythic.bukkit.events.MythicTriggerEvent +import io.lumine.mythic.core.mobs.ActiveMob +import io.lumine.mythic.core.skills.SkillTriggers +import org.bukkit.event.EventHandler +import org.bukkit.event.Listener + +class MythicPrefabsListeners( + val logger: Logger, +) : Listener { + @EventHandler + fun MythicMechanicLoadEvent.onMechanicLoad() { + when (mechanicName.lowercase()) { + "prefabs" -> register(PrefabsMechanic(config)) + } + } + + private fun addPrefabs(mob: ActiveMob, event: String) { + val bukkit = BukkitAdapter.adapt(mob.entity) + val inherit = mob.type.prefabs + val embedded = MythicEmbeddedGearyEntity.getOrLoadEmbeddedPrefab(bukkit.world.toGeary(), mob.type) + if (embedded != null) { + bukkit.toGeary().extend(embedded) + logger.d { "$event event - ${mob.type.internalName} loaded an embedded prefab." } + } + + mob.addPrefabs(inherit) + if (inherit.isNotEmpty()) + logger.d { "$event event - ${mob.type.internalName} added prefabs: $inherit" } + } + + @EventHandler + fun MythicMobSpawnEvent.onSpawn() { + addPrefabs(mob, "Spawn") + } + + @EventHandler + fun MythicTriggerEvent.onLoad() { + if (trigger != SkillTriggers.LOAD) return + val mob = skillMetadata.caster as? ActiveMob ?: return + addPrefabs(mob, "Load") + } +} diff --git a/geary-papermc-mythicmobs/src/main/kotlin/com/mineinabyss/geary/papermc/mythicmobs/skills/MythicSkillRegisterListener.kt b/geary-papermc-mythicmobs/src/main/kotlin/com/mineinabyss/geary/papermc/mythicmobs/skills/MythicSkillRegisterListener.kt deleted file mode 100644 index c648a31e..00000000 --- a/geary-papermc-mythicmobs/src/main/kotlin/com/mineinabyss/geary/papermc/mythicmobs/skills/MythicSkillRegisterListener.kt +++ /dev/null @@ -1,14 +0,0 @@ -package com.mineinabyss.geary.papermc.mythicmobs.skills - -import io.lumine.mythic.bukkit.events.MythicMechanicLoadEvent -import org.bukkit.event.EventHandler -import org.bukkit.event.Listener - -class MythicSkillRegisterListener: Listener { - @EventHandler - fun MythicMechanicLoadEvent.onMechanicLoad() { - when(mechanicName.lowercase()) { - "prefabs" -> register(PrefabsMechanic(config)) - } - } -}