From 1a99e0cb0a1ed41e3c9befd7d2a925a815aca541 Mon Sep 17 00:00:00 2001 From: Danielle Voznyy Date: Wed, 3 Apr 2024 18:33:08 -0400 Subject: [PATCH] feat: Track bounding box for entities using MEG models fix: Don't check bounding box when it isn't set in Geary --- .../mobzy/modelengine/MEGMobQuery.kt | 8 +++++ .../mobzy/modelengine/ModelEngineSupport.kt | 32 +++++++++++++++++++ .../mobzy/spawning/SpawnDefinition.kt | 12 ++++--- 3 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 mobzy-modelengine/src/main/kotlin/com/mineinabyss/mobzy/modelengine/MEGMobQuery.kt diff --git a/mobzy-modelengine/src/main/kotlin/com/mineinabyss/mobzy/modelengine/MEGMobQuery.kt b/mobzy-modelengine/src/main/kotlin/com/mineinabyss/mobzy/modelengine/MEGMobQuery.kt new file mode 100644 index 00000000..5e1df3cf --- /dev/null +++ b/mobzy-modelengine/src/main/kotlin/com/mineinabyss/mobzy/modelengine/MEGMobQuery.kt @@ -0,0 +1,8 @@ +package com.mineinabyss.mobzy.modelengine + +import com.mineinabyss.geary.systems.query.Query +import com.mineinabyss.mobzy.modelengine.intializers.SetModelEngineModel + +class MEGMobQuery : Query() { + val model by get() +} diff --git a/mobzy-modelengine/src/main/kotlin/com/mineinabyss/mobzy/modelengine/ModelEngineSupport.kt b/mobzy-modelengine/src/main/kotlin/com/mineinabyss/mobzy/modelengine/ModelEngineSupport.kt index d16fc394..b260b288 100644 --- a/mobzy-modelengine/src/main/kotlin/com/mineinabyss/mobzy/modelengine/ModelEngineSupport.kt +++ b/mobzy-modelengine/src/main/kotlin/com/mineinabyss/mobzy/modelengine/ModelEngineSupport.kt @@ -1,7 +1,12 @@ package com.mineinabyss.mobzy.modelengine +import com.github.shynixn.mccoroutine.bukkit.launch import com.mineinabyss.geary.addons.dsl.GearyAddonWithDefault +import com.mineinabyss.geary.components.relations.NoInherit +import com.mineinabyss.geary.helpers.fastForEach import com.mineinabyss.geary.modules.geary +import com.mineinabyss.geary.systems.builders.cachedQuery +import com.mineinabyss.geary.systems.query.CachedQueryRunner import com.mineinabyss.idofront.di.DI import com.mineinabyss.idofront.plugin.listeners import com.mineinabyss.idofront.plugin.service @@ -13,15 +18,21 @@ import com.mineinabyss.mobzy.modelengine.intializers.ModelEngineWorldListener import com.mineinabyss.mobzy.modelengine.intializers.createModelEngineListener import com.mineinabyss.mobzy.modelengine.nametag.ModelEngineNameTagListener import com.mineinabyss.mobzy.modelengine.riding.ModelEngineRidingListener +import com.ticxo.modelengine.api.ModelEngineAPI +import com.ticxo.modelengine.api.generator.ModelGenerator +import org.bukkit.util.BoundingBox +import org.bukkit.util.Vector val mobzyModelEngine: ModelEngineSupport? by DI.observe() interface ModelEngineSupport { val animationController: AnimationController + val megMobs: CachedQueryRunner companion object : GearyAddonWithDefault { override fun default() = object : ModelEngineSupport { override val animationController = ModelEngineAnimationController() + override val megMobs = geary.cachedQuery(MEGMobQuery()) } override fun ModelEngineSupport.install(): Unit = geary.run { @@ -38,6 +49,27 @@ interface ModelEngineSupport { ) service(ModelEngineAnimationController()) } + ModelEngineAPI.getAPI().modelGenerator.queueTask(ModelGenerator.Phase.FINISHED) { + mobzy.plugin.launch { + updateBoundingBoxes() + } + } + } + } + + fun updateBoundingBoxes() { + mobzy.logger.i("Reading bounding boxes from ModelEngine models...") + megMobs.mapWithEntity { + ModelEngineAPI.getBlueprint(model.modelId) + .mainHitbox + .createBoundingBox(Vector()) + }.fastForEach { (boundingBox, entity) -> + runCatching { + entity.addRelation() + entity.set(boundingBox) + }.onFailure { + it.printStackTrace() + } } } } diff --git a/mobzy-spawning/src/main/kotlin/com/mineinabyss/mobzy/spawning/SpawnDefinition.kt b/mobzy-spawning/src/main/kotlin/com/mineinabyss/mobzy/spawning/SpawnDefinition.kt index e49ce276..4ff1e406 100644 --- a/mobzy-spawning/src/main/kotlin/com/mineinabyss/mobzy/spawning/SpawnDefinition.kt +++ b/mobzy-spawning/src/main/kotlin/com/mineinabyss/mobzy/spawning/SpawnDefinition.kt @@ -150,10 +150,14 @@ fun GearyModule.spawnRequestListener() = listener(object : ListenerQuery() { getSpawnInRadius(location, radius) ?: location else location - val suitableLoc = ensureSuitableLocationOrNull( - chosenLoc, - (boundingBox?.clone()?.shift(chosenLoc)) ?: BoundingBox.of(chosenLoc.toVector(), 1.0, 2.0, 1.0) - ) ?: return@repeat + val suitableLoc = if (boundingBox == null) { + chosenLoc + } else { + ensureSuitableLocationOrNull( + chosenLoc, + (boundingBox?.clone()?.shift(chosenLoc)) ?: BoundingBox.of(chosenLoc.toVector(), 1.0, 2.0, 1.0) + ) ?: return@repeat + } suitableLoc.spawnFromPrefab(prefab).onSuccess { spawnCount++ } }