-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Inheriting prefabs from MythicMobs mobs
feat: Embedded geary entity definition in MythicMobs
- Loading branch information
Showing
6 changed files
with
143 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...mobs/src/main/kotlin/com/mineinabyss/geary/papermc/mythicmobs/GearyMythicConfigOptions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...obs/src/main/kotlin/com/mineinabyss/geary/papermc/mythicmobs/MythicEmbeddedGearyEntity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...src/main/kotlin/com/mineinabyss/geary/papermc/mythicmobs/skills/MythicPrefabsListeners.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
14 changes: 0 additions & 14 deletions
14
...ain/kotlin/com/mineinabyss/geary/papermc/mythicmobs/skills/MythicSkillRegisterListener.kt
This file was deleted.
Oops, something went wrong.