Skip to content

Commit

Permalink
feat: Inheriting prefabs from MythicMobs mobs
Browse files Browse the repository at this point in the history
feat: Embedded geary entity definition in MythicMobs
  • Loading branch information
0ffz committed Dec 14, 2024
1 parent ee671a1 commit 1ea2ea7
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 16 deletions.
1 change: 1 addition & 0 deletions geary-papermc-mythicmobs/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<PrefabKey>) {
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)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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<YamlMap>(config.key) ?: return null

// Get special nodes and decode them
val gearyNode = mobNode.get<YamlMap>(NODE_GEARY)
val observeNode = mobNode.get<YamlMap>(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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -24,7 +24,7 @@ class MythicMobsFeature(context: FeatureContext) : Feature(context) {

listeners(
MythicMobDropListener(),
MythicSkillRegisterListener(),
MythicPrefabsListeners(logger),
)
}
}
Original file line number Diff line number Diff line change
@@ -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")
}
}

This file was deleted.

0 comments on commit 1ea2ea7

Please sign in to comment.