diff --git a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/PaperBridge.kt b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/PaperBridge.kt index 9b3aeee..0fc6e34 100644 --- a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/PaperBridge.kt +++ b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/PaperBridge.kt @@ -6,26 +6,26 @@ import com.mineinabyss.geary.autoscan.autoscan import com.mineinabyss.geary.helpers.component import com.mineinabyss.geary.modules.geary import com.mineinabyss.geary.papermc.GearyPaperConfigModule -import com.mineinabyss.geary.papermc.bridge.actions.DoDamageSystem -import com.mineinabyss.geary.papermc.bridge.actions.DoKnockbackSystem -import com.mineinabyss.geary.papermc.bridge.actions.DoSpawnSystem -import com.mineinabyss.geary.papermc.bridge.actions.ExplosionSystem +import com.mineinabyss.geary.papermc.bridge.actions.createDoDamageAction +import com.mineinabyss.geary.papermc.bridge.actions.createDoKnockbackAction +import com.mineinabyss.geary.papermc.bridge.actions.createDoSpawnAction +import com.mineinabyss.geary.papermc.bridge.actions.createExplosionAction import com.mineinabyss.geary.papermc.bridge.conditions.* -import com.mineinabyss.geary.papermc.bridge.conditions.location.BlockConditionChecker -import com.mineinabyss.geary.papermc.bridge.conditions.location.HeightConditionChecker -import com.mineinabyss.geary.papermc.bridge.conditions.location.LightConditionChecker -import com.mineinabyss.geary.papermc.bridge.conditions.location.TimeConditionChecker +import com.mineinabyss.geary.papermc.bridge.conditions.location.createBlockConditionChecker +import com.mineinabyss.geary.papermc.bridge.conditions.location.createHeightConditionChecker +import com.mineinabyss.geary.papermc.bridge.conditions.location.createLightConditionChecker +import com.mineinabyss.geary.papermc.bridge.conditions.location.createTimeConditionChecker import com.mineinabyss.geary.papermc.bridge.config.OnEvent -import com.mineinabyss.geary.papermc.bridge.config.parsers.CreateDefaultSkills -import com.mineinabyss.geary.papermc.bridge.config.parsers.ParseSkills +import com.mineinabyss.geary.papermc.bridge.config.parsers.createDefaultSkillsListener +import com.mineinabyss.geary.papermc.bridge.config.parsers.createParseSkillsListener import com.mineinabyss.geary.papermc.bridge.events.entities.* import com.mineinabyss.geary.papermc.bridge.events.items.ItemBreakBridge import com.mineinabyss.geary.papermc.bridge.events.items.ItemConsumeBridge import com.mineinabyss.geary.papermc.bridge.events.items.ItemInteractBridge -import com.mineinabyss.geary.papermc.bridge.mythicmobs.RunMMSkillSystem -import com.mineinabyss.geary.papermc.bridge.readers.ReadLocationSystem -import com.mineinabyss.geary.papermc.bridge.readers.ReadTargetBlockSystem -import com.mineinabyss.geary.papermc.bridge.targetselectors.NearbyEntitiesSelector +import com.mineinabyss.geary.papermc.bridge.mythicmobs.createRunMMSkillAction +import com.mineinabyss.geary.papermc.bridge.readers.createLocationReader +import com.mineinabyss.geary.papermc.bridge.readers.createTargetBlockReader +import com.mineinabyss.geary.papermc.bridge.targetselectors.createNearbyEntitiesSelector import com.mineinabyss.geary.papermc.gearyPaper import com.mineinabyss.geary.papermc.tracking.items.components.SetItem import com.mineinabyss.idofront.di.DI @@ -33,7 +33,7 @@ import com.mineinabyss.idofront.plugin.listeners class PaperBridge { companion object : GearyAddonWithDefault { - override fun PaperBridge.install() { + override fun PaperBridge.install() = geary.run { geary { autoscan(this::class.java.classLoader, "com.mineinabyss.geary.papermc.bridge") { systems() @@ -43,29 +43,28 @@ class PaperBridge { addRelation() } } - geary.pipeline.addSystems( - BlockConditionChecker(), - ChanceChecker(), - EntityConditionsChecker(), - HealthConditionChecker(), - HeightConditionChecker(), - LightConditionChecker(), - TimeConditionChecker(), - ExplosionSystem(), - ParseSkills(), - CreateDefaultSkills(), - DoSpawnSystem(), - ReadTargetBlockSystem(), - RunMMSkillSystem(), - DoDamageSystem(), - NearbyEntitiesSelector(), - ReadLocationSystem(), - DoKnockbackSystem(), - ) - geary.pipeline.addSystems( - CooldownChecker(), - PlayerConditionsChecker(), - ) + createBlockConditionChecker() + createChanceChecker() + createEntityConditionsChecker() + createHealthConditionChecker() + createHeightConditionChecker() + createLightConditionChecker() + createTimeConditionChecker() + createCooldownChecker() + createPlayerConditionsChecker() + + createExplosionAction() + createDoSpawnAction() + createRunMMSkillAction() + createDoDamageAction() + createDoKnockbackAction() + + createParseSkillsListener() + createDefaultSkillsListener() + + createTargetBlockReader() + createLocationReader() + createNearbyEntitiesSelector() geary.pipeline.runOnOrAfter(GearyPhase.ENABLE) { DI.getOrNull() ?: return@runOnOrAfter diff --git a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/actions/DoDamage.kt b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/actions/DoDamage.kt index 42cd949..aa394cd 100644 --- a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/actions/DoDamage.kt +++ b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/actions/DoDamage.kt @@ -1,7 +1,8 @@ package com.mineinabyss.geary.papermc.bridge.actions -import com.mineinabyss.geary.systems.GearyListener -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.modules.GearyModule +import com.mineinabyss.geary.systems.builders.listener +import com.mineinabyss.geary.systems.query.ListenerQuery import com.mineinabyss.idofront.serialization.DoubleRangeSerializer import com.mineinabyss.idofront.typealiases.BukkitEntity import com.mineinabyss.idofront.util.DoubleRange @@ -23,19 +24,19 @@ data class DoDamage( val ignoreArmor: Boolean = false, ) - -class DoDamageSystem : GearyListener() { - val Pointers.bukkit by get().on(target) - val Pointers.damage by get().on(source) - - override fun Pointers.handle() { - val living = bukkit as? LivingEntity ?: return - if (living.health > damage.minHealth) { - if (damage.ignoreArmor) { - living.health -= damage.damage.randomOrMin() - } else { - living.damage(damage.damage.randomOrMin()) - } +fun GearyModule.createDoDamageAction() = listener( + object : ListenerQuery() { + val bukkit by get() + val damage by source.get() + } +).exec { + val living = bukkit as? LivingEntity ?: return@exec + if (living.health > damage.minHealth) { + if (damage.ignoreArmor) { + living.health -= damage.damage.randomOrMin() + } else { + living.damage(damage.damage.randomOrMin()) } } + } diff --git a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/actions/DoExplosion.kt b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/actions/DoExplosion.kt index 3c100fc..43ddf5f 100644 --- a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/actions/DoExplosion.kt +++ b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/actions/DoExplosion.kt @@ -1,8 +1,9 @@ package com.mineinabyss.geary.papermc.bridge.actions +import com.mineinabyss.geary.modules.GearyModule import com.mineinabyss.geary.papermc.bridge.config.inputs.Input -import com.mineinabyss.geary.systems.GearyListener -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.systems.builders.listener +import com.mineinabyss.geary.systems.query.ListenerQuery import com.mineinabyss.idofront.spawning.spawn import kotlinx.serialization.Contextual import kotlinx.serialization.SerialName @@ -20,17 +21,18 @@ data class Explosion( val at: Input<@Contextual Location>, ) -class ExplosionSystem : GearyListener() { - private val Pointers.explosion by get().on(source) - - override fun Pointers.handle() { - val location = explosion.at.get(this) - if (explosion.fuseTicks <= 0) location.createExplosion( - explosion.power, explosion.setFire, explosion.breakBlocks - ) - else //only spawn a tnt in if we have a fuse - location.spawn { - fuseTicks = explosion.fuseTicks - } +fun GearyModule.createExplosionAction() = listener( + object : ListenerQuery() { + val explosion by source.get() } +).exec { + val location = explosion.at.get(this) + if (explosion.fuseTicks <= 0) location.createExplosion( + explosion.power, explosion.setFire, explosion.breakBlocks + ) + else //only spawn a tnt in if we have a fuse + location.spawn { + fuseTicks = explosion.fuseTicks + } + } diff --git a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/actions/DoKnockback.kt b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/actions/DoKnockback.kt index 9c03a38..32c6841 100644 --- a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/actions/DoKnockback.kt +++ b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/actions/DoKnockback.kt @@ -1,8 +1,9 @@ package com.mineinabyss.geary.papermc.bridge.actions +import com.mineinabyss.geary.modules.GearyModule import com.mineinabyss.geary.papermc.bridge.config.inputs.Input -import com.mineinabyss.geary.systems.GearyListener -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.systems.builders.listener +import com.mineinabyss.geary.systems.query.ListenerQuery import com.mineinabyss.idofront.operators.minus import com.mineinabyss.idofront.operators.plus import com.mineinabyss.idofront.operators.times @@ -32,27 +33,28 @@ class DoKnockback( val center: Input<@Contextual Location> ) -class DoKnockbackSystem : GearyListener() { - val Pointers.entity by get().on(target) - val Pointers.doKnockback by get().on(source) +fun GearyModule.createDoKnockbackAction() = listener( + object : ListenerQuery() { + val bukkit by get() + val doKnockback by source.get() + } +).exec { + val targetLoc = bukkit.location + val center = doKnockback.center.get(this) + if (targetLoc.world != center.world) return@exec - override fun Pointers.handle() { - val targetLoc = entity.location - val center = doKnockback.center.get(this) - if (targetLoc.world != center.world) return + val delta = center - bukkit.location + delta.y = 0.0 - val delta = center - entity.location - delta.y = 0.0 + val velocity = delta.toVector().multiply(-1).normalize().apply { + y = cos(Math.toRadians(doKnockback.yAngle)) + } * doKnockback.power - val velocity = delta.toVector().multiply(-1).normalize().apply { - y = cos(Math.toRadians(doKnockback.yAngle)) - } * doKnockback.power + // Not sure about this, should probably get all of the - // Not sure about this, should probably get all of the + if (doKnockback.cancelCurrentVelocity) + bukkit.velocity = velocity + else + bukkit.velocity += velocity - if (doKnockback.cancelCurrentVelocity) - entity.velocity = velocity - else - entity.velocity += velocity - } } diff --git a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/actions/DoSpawnEntity.kt b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/actions/DoSpawnEntity.kt index 331063d..8091e91 100644 --- a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/actions/DoSpawnEntity.kt +++ b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/actions/DoSpawnEntity.kt @@ -1,10 +1,11 @@ package com.mineinabyss.geary.papermc.bridge.actions +import com.mineinabyss.geary.modules.GearyModule import com.mineinabyss.geary.papermc.bridge.config.inputs.Input import com.mineinabyss.geary.papermc.tracking.entities.helpers.spawnFromPrefab import com.mineinabyss.geary.serialization.serializers.SerializableGearyEntity -import com.mineinabyss.geary.systems.GearyListener -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.systems.builders.listener +import com.mineinabyss.geary.systems.query.ListenerQuery import kotlinx.serialization.Contextual import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @@ -17,10 +18,11 @@ class DoSpawnEntity( val at: Input<@Contextual Location> ) -class DoSpawnSystem : GearyListener() { - private val Pointers.spawn by get().on(source) - override fun Pointers.handle() { - spawn.at.get(this).spawnFromPrefab(spawn.entity.get(this)) +fun GearyModule.createDoSpawnAction() = listener( + object : ListenerQuery() { + val spawn by source.get() } +).exec { + spawn.at.get(this).spawnFromPrefab(spawn.entity.get(this)) } diff --git a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/actions/RunSkills.kt b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/actions/RunSkills.kt index cb3cfb3..50e5f84 100644 --- a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/actions/RunSkills.kt +++ b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/actions/RunSkills.kt @@ -1,15 +1,14 @@ package com.mineinabyss.geary.papermc.bridge.actions -import com.mineinabyss.geary.annotations.optin.UnsafeAccessors -import com.mineinabyss.geary.autoscan.AutoScan +import com.mineinabyss.geary.modules.GearyModule import com.mineinabyss.geary.papermc.bridge.config.Skill import com.mineinabyss.geary.papermc.bridge.events.EventHelpers import com.mineinabyss.geary.prefabs.PrefabKey import com.mineinabyss.geary.prefabs.prefabs import com.mineinabyss.geary.prefabs.serializers.PrefabKeySerializer import com.mineinabyss.geary.serialization.serializers.InnerSerializer -import com.mineinabyss.geary.systems.GearyListener -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.systems.builders.listener +import com.mineinabyss.geary.systems.query.ListenerQuery import kotlinx.serialization.Serializable import kotlinx.serialization.Transient import kotlinx.serialization.builtins.ListSerializer @@ -31,18 +30,16 @@ class RunSkills( ) } -@AutoScan -class RunSkillSystem : GearyListener() { - private val Pointers.action by get().on(source) - - @OptIn(UnsafeAccessors::class) - override fun Pointers.handle() { - action.skills.forEach { skill -> - EventHelpers.runSkill( - target.entity, - event.entity, - skill, - ) - } +fun GearyModule.createRunSkillAction() = listener( + object : ListenerQuery() { + val action by source.get() + } +).exec { + action.skills.forEach { skill -> + EventHelpers.runSkill( + entity, + event.entity, + skill, + ) } } diff --git a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/actions/particles/DoSpawnParticle.kt b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/actions/particles/DoSpawnParticle.kt index b6e8b4e..c2510fe 100644 --- a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/actions/particles/DoSpawnParticle.kt +++ b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/actions/particles/DoSpawnParticle.kt @@ -1,10 +1,10 @@ package com.mineinabyss.geary.papermc.bridge.actions.particles import com.destroystokyo.paper.ParticleBuilder -import com.mineinabyss.geary.autoscan.AutoScan +import com.mineinabyss.geary.modules.GearyModule import com.mineinabyss.geary.papermc.bridge.config.inputs.Input -import com.mineinabyss.geary.systems.GearyListener -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.systems.builders.listener +import com.mineinabyss.geary.systems.query.ListenerQuery import com.mineinabyss.idofront.serialization.ColorSerializer import com.mineinabyss.idofront.serialization.IntRangeSerializer import com.mineinabyss.idofront.util.DoubleRange @@ -30,21 +30,20 @@ class DoSpawnParticle( val speed: DoubleRange = 0.0..0.0, ) -@AutoScan -class DoSpawnParticleSystem : GearyListener() { - private val Pointers.spawn by get().on(source) - - override fun Pointers.handle() { - val location = spawn.at.get(this) - with(spawn) { - ParticleBuilder(particle) - .location(location) - .offset(offsetX.randomOrMin(), offsetY.randomOrMin(), offsetZ.randomOrMin()) - .color(color) - .count(count.randomOrMin()) - .extra(speed.randomOrMin()) - .receivers(radius) - .spawn() - } +fun GearyModule.createSpawnParticleAction() = listener( + object : ListenerQuery() { + val spawn by source.get() + } +).exec { + val location = spawn.at.get(this) + with(spawn) { + ParticleBuilder(particle) + .location(location) + .offset(offsetX.randomOrMin(), offsetY.randomOrMin(), offsetZ.randomOrMin()) + .color(color) + .count(count.randomOrMin()) + .extra(speed.randomOrMin()) + .receivers(radius) + .spawn() } } diff --git a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/actions/sounds/DoSound.kt b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/actions/sounds/DoSound.kt index 5824a07..a393144 100644 --- a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/actions/sounds/DoSound.kt +++ b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/actions/sounds/DoSound.kt @@ -1,19 +1,15 @@ package com.mineinabyss.geary.papermc.bridge.actions.sounds -import com.mineinabyss.geary.autoscan.AutoScan -import com.mineinabyss.geary.papermc.bridge.config.inputs.Input -import com.mineinabyss.geary.systems.GearyListener -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.modules.GearyModule +import com.mineinabyss.geary.systems.builders.listener +import com.mineinabyss.geary.systems.query.ListenerQuery import com.mineinabyss.idofront.typealiases.BukkitEntity import com.mineinabyss.idofront.util.FloatRange import com.mineinabyss.idofront.util.randomOrMin -import kotlinx.serialization.Contextual import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -import net.kyori.adventure.audience.Audience import net.kyori.adventure.key.Key import net.kyori.adventure.sound.Sound -import org.bukkit.Location import org.bukkit.SoundCategory @Serializable @@ -25,22 +21,21 @@ class DoSound( val pitch: FloatRange = 1.0f..1.0f, ) -@AutoScan -class DoPlaySoundSystem : GearyListener() { - private val Pointers.bukkit by get().on(target) - private val Pointers.sound by get().on(source) - - override fun Pointers.handle() { - with(sound) { - bukkit.world.playSound( - Sound.sound() - .type(Key.key(sound)) - .pitch(pitch.randomOrMin()) - .volume(volume.randomOrMin()) - .source(category) - .build(), - bukkit - ) - } +fun GearyModule.createPlaySoundAction() = listener( + object : ListenerQuery() { + val bukkit by get() + val sound by source.get() + } +).exec { + with(sound) { + bukkit.world.playSound( + Sound.sound() + .type(Key.key(sound)) + .pitch(pitch.randomOrMin()) + .volume(volume.randomOrMin()) + .source(category) + .build(), + bukkit + ) } } diff --git a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/Chance.kt b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/Chance.kt index 0f7e96c..7118f38 100644 --- a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/Chance.kt +++ b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/Chance.kt @@ -1,10 +1,10 @@ package com.mineinabyss.geary.papermc.bridge.conditions -import com.mineinabyss.geary.events.CheckingListener -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.modules.GearyModule +import com.mineinabyss.geary.systems.builders.listener +import com.mineinabyss.geary.systems.query.ListenerQuery import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -import org.bukkit.entity.Player import kotlin.random.Random @JvmInline @@ -12,10 +12,6 @@ import kotlin.random.Random @SerialName("geary:chance") value class Chance(val percentage: Double) -class ChanceChecker : CheckingListener() { - private val Pointers.chance by get().on(source) - - override fun Pointers.check(): Boolean { - return Random.nextDouble() < chance.percentage - } -} +fun GearyModule.createChanceChecker() = listener(object : ListenerQuery() { + val chance by source.get() +}).check { Random.nextDouble() < chance.percentage } diff --git a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/Cooldown.kt b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/Cooldown.kt index 76b67d6..522f382 100644 --- a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/Cooldown.kt +++ b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/Cooldown.kt @@ -1,9 +1,9 @@ package com.mineinabyss.geary.papermc.bridge.conditions -import com.mineinabyss.geary.annotations.optin.UnsafeAccessors import com.mineinabyss.geary.datatypes.GearyEntity -import com.mineinabyss.geary.events.CheckingListener -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.modules.GearyModule +import com.mineinabyss.geary.systems.builders.listener +import com.mineinabyss.geary.systems.query.ListenerQuery import com.mineinabyss.idofront.serialization.DurationSerializer import com.mineinabyss.idofront.serialization.MiniMessageSerializer import kotlinx.serialization.SerialName @@ -35,15 +35,14 @@ class Cooldown( class CooldownStarted(val time: Long, val cooldown: Cooldown) -class CooldownChecker : CheckingListener() { - private val Pointers.cooldownDefinition by get().on(source) - - @OptIn(UnsafeAccessors::class) - override fun Pointers.check(): Boolean { - if (Cooldown.isComplete(target.entity, source!!.entity)) { - Cooldown.start(target.entity, source!!.entity, cooldownDefinition) - return true - } - return false +fun GearyModule.createCooldownChecker() = listener( + object : ListenerQuery() { + val cooldownDefinition by source.get() + } +).check { + if (Cooldown.isComplete(entity, source.entity)) { + Cooldown.start(entity, source.entity, cooldownDefinition) + return@check true } + return@check false } diff --git a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/EntityConditions.kt b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/EntityConditions.kt index be33c64..481f806 100644 --- a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/EntityConditions.kt +++ b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/EntityConditions.kt @@ -1,11 +1,11 @@ package com.mineinabyss.geary.papermc.bridge.conditions -import com.mineinabyss.geary.events.CheckingListener +import com.mineinabyss.geary.modules.GearyModule import com.mineinabyss.geary.papermc.bridge.helpers.nullOrEquals -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.systems.builders.listener +import com.mineinabyss.geary.systems.query.ListenerQuery import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -import org.bukkit.Material import org.bukkit.entity.LivingEntity import org.bukkit.entity.Player @@ -39,13 +39,13 @@ class EntityConditions( val isLeashed: Boolean? = null, ) -class EntityConditionsChecker : CheckingListener() { - private val Pointers.livingEntity by get().on(target) - - private val Pointers.conditions by get().on(source) - - override fun Pointers.check(): Boolean = livingEntity !is Player && - conditions.isSleeping nullOrEquals livingEntity.isSleeping && +fun GearyModule.createEntityConditionsChecker() = listener( + object : ListenerQuery() { + val livingEntity by get() + val conditions by source.get() + } +).check { + conditions.isSleeping nullOrEquals livingEntity.isSleeping && conditions.isSwimming nullOrEquals livingEntity.isSwimming && conditions.isClimbing nullOrEquals livingEntity.isClimbing && conditions.isJumping nullOrEquals livingEntity.isJumping && @@ -65,5 +65,4 @@ class EntityConditionsChecker : CheckingListener() { conditions.isInvurnerable nullOrEquals livingEntity.isInvulnerable && conditions.isSilent nullOrEquals livingEntity.isSilent && conditions.isLeashed nullOrEquals livingEntity.isLeashed - } diff --git a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/HealthConditions.kt b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/HealthConditions.kt index 7b26b0c..87b657c 100644 --- a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/HealthConditions.kt +++ b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/HealthConditions.kt @@ -4,16 +4,16 @@ package com.mineinabyss.geary.papermc.bridge.conditions -import com.mineinabyss.geary.events.CheckingListener +import com.mineinabyss.geary.modules.GearyModule import com.mineinabyss.geary.papermc.bridge.helpers.nullOr -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.systems.builders.listener +import com.mineinabyss.geary.systems.query.ListenerQuery import com.mineinabyss.idofront.serialization.DoubleRangeSerializer import com.mineinabyss.idofront.typealiases.BukkitEntity import com.mineinabyss.idofront.util.DoubleRange import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import kotlinx.serialization.UseSerializers -import org.bukkit.attribute.Attribute import org.bukkit.entity.LivingEntity import org.bukkit.entity.Player @@ -28,15 +28,16 @@ class HealthConditions( val withinPercent: DoubleRange? = null, ) -class HealthConditionChecker : CheckingListener() { - private val Pointers.bukkit by get().on(target) - private val Pointers.health by get().on(source) - - override fun Pointers.check(): Boolean { - val living = bukkit as? LivingEntity ?: return false - - return (health.within nullOr { living.health in it } && health.withinPercent nullOr { - living.health / (living.getAttribute(org.bukkit.attribute.Attribute.GENERIC_MAX_HEALTH)?.value ?: return false) in it - }) +fun GearyModule.createHealthConditionChecker() = listener( + object : ListenerQuery() { + val bukkit by get() + val health by source.get() } +).check { + val living = bukkit as? LivingEntity ?: return@check false + + (health.within nullOr { living.health in it } && health.withinPercent nullOr { + living.health / (living.getAttribute(org.bukkit.attribute.Attribute.GENERIC_MAX_HEALTH)?.value + ?: return@check false) in it + }) } diff --git a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/PlayerConditions.kt b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/PlayerConditions.kt index 78560b0..05c6bff 100644 --- a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/PlayerConditions.kt +++ b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/PlayerConditions.kt @@ -1,8 +1,9 @@ package com.mineinabyss.geary.papermc.bridge.conditions -import com.mineinabyss.geary.events.CheckingListener +import com.mineinabyss.geary.modules.GearyModule import com.mineinabyss.geary.papermc.bridge.helpers.nullOrEquals -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.systems.builders.listener +import com.mineinabyss.geary.systems.query.ListenerQuery import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import org.bukkit.entity.Player @@ -41,11 +42,14 @@ class PlayerConditions( val isOp: Boolean? = null, ) -class PlayerConditionsChecker : CheckingListener() { - private val Pointers.player by get().on(target) - private val Pointers.conditions by get().on(source) - override fun Pointers.check(): Boolean = player.isOnline && // Just to align syntax below +fun GearyModule.createPlayerConditionsChecker() = listener( + object : ListenerQuery() { + val player by get() + val conditions by source.get() + } +).check { + player.isOnline && // Just to align syntax below conditions.isSneaking nullOrEquals player.isSneaking && conditions.isSprinting nullOrEquals player.isSprinting && conditions.isBlocking nullOrEquals player.isBlocking && diff --git a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/location/BlockConditions.kt b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/location/BlockConditions.kt index 60374db..7313ddf 100644 --- a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/location/BlockConditions.kt +++ b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/location/BlockConditions.kt @@ -1,8 +1,9 @@ package com.mineinabyss.geary.papermc.bridge.conditions.location -import com.mineinabyss.geary.events.CheckingListener +import com.mineinabyss.geary.modules.GearyModule import com.mineinabyss.geary.papermc.bridge.config.inputs.Input -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.systems.builders.listener +import com.mineinabyss.geary.systems.query.ListenerQuery import com.mineinabyss.idofront.serialization.MaterialByNameSerializer import kotlinx.serialization.Contextual import kotlinx.serialization.SerialName @@ -18,13 +19,13 @@ class BlockConditions( val at: Input<@Contextual Location> = Input.reference("location"), ) -class BlockConditionChecker : CheckingListener() { - private val Pointers.conditions by get().on(source) - - override fun Pointers.check(): Boolean { - val location = conditions.at.get(this) - return location.block.type.let { - (conditions.allow.isEmpty() || it in conditions.allow) && it !in conditions.deny - } +fun GearyModule.createBlockConditionChecker() = listener( + object : ListenerQuery() { + val conditions by source.get() + } +).check { + val location = conditions.at.get(this) + location.block.type.let { + (conditions.allow.isEmpty() || it in conditions.allow) && it !in conditions.deny } } diff --git a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/location/HeightCondition.kt b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/location/HeightCondition.kt index ae07c73..66f89b6 100644 --- a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/location/HeightCondition.kt +++ b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/location/HeightCondition.kt @@ -1,18 +1,14 @@ package com.mineinabyss.geary.papermc.bridge.conditions.location -import com.mineinabyss.geary.events.CheckingListener -import com.mineinabyss.geary.papermc.bridge.conditions.HealthConditions +import com.mineinabyss.geary.modules.GearyModule import com.mineinabyss.geary.papermc.bridge.config.inputs.Input -import com.mineinabyss.geary.papermc.bridge.helpers.nullOr -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.systems.builders.listener +import com.mineinabyss.geary.systems.query.ListenerQuery import com.mineinabyss.idofront.serialization.IntRangeSerializer -import com.mineinabyss.idofront.typealiases.BukkitEntity import kotlinx.serialization.Contextual import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import org.bukkit.Location -import org.bukkit.attribute.Attribute -import org.bukkit.entity.LivingEntity @Serializable @SerialName("geary:check.height") @@ -21,11 +17,11 @@ class HeightCondition( val at: Input<@Contextual Location> = Input.reference("location") ) -class HeightConditionChecker : CheckingListener() { - private val Pointers.condition by get().on(source) - - override fun Pointers.check(): Boolean { - val location = condition.at.get(this) - return location.y.toInt() in condition.range +fun GearyModule.createHeightConditionChecker() = listener( + object : ListenerQuery() { + val condition by source.get() } +).check { + val location = condition.at.get(this) + location.y.toInt() in condition.range } diff --git a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/location/LightCondition.kt b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/location/LightCondition.kt index c923866..da2ff87 100644 --- a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/location/LightCondition.kt +++ b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/location/LightCondition.kt @@ -1,14 +1,14 @@ package com.mineinabyss.geary.papermc.bridge.conditions.location -import com.mineinabyss.geary.events.CheckingListener +import com.mineinabyss.geary.modules.GearyModule import com.mineinabyss.geary.papermc.bridge.config.inputs.Input -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.systems.builders.listener +import com.mineinabyss.geary.systems.query.ListenerQuery import com.mineinabyss.idofront.serialization.IntRangeSerializer import kotlinx.serialization.Contextual import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import org.bukkit.Location -import org.bukkit.block.BlockFace @Serializable @SerialName("geary:check.light") @@ -18,16 +18,16 @@ class LightCondition( ) -class LightConditionChecker : CheckingListener() { - private val Pointers.condition by get().on(source) - - override fun Pointers.check(): Boolean { - val location = condition.at.get(this) - val block = location.block - // Check the current block's light level or the one above if this block is solid - val check = - if (block.isSolid) block.getRelative(org.bukkit.block.BlockFace.UP) - else block - return check.lightLevel in condition.range +fun GearyModule.createLightConditionChecker() = listener( + object : ListenerQuery() { + val condition by source.get() } +).check { + val location = condition.at.get(this) + val block = location.block + // Check the current block's light level or the one above if this block is solid + val check = + if (block.isSolid) block.getRelative(org.bukkit.block.BlockFace.UP) + else block + check.lightLevel in condition.range } diff --git a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/location/TimeCondition.kt b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/location/TimeCondition.kt index 9d9aa1b..18a0232 100644 --- a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/location/TimeCondition.kt +++ b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/conditions/location/TimeCondition.kt @@ -1,8 +1,9 @@ package com.mineinabyss.geary.papermc.bridge.conditions.location -import com.mineinabyss.geary.events.CheckingListener +import com.mineinabyss.geary.modules.GearyModule import com.mineinabyss.geary.papermc.bridge.config.inputs.Input -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.systems.builders.listener +import com.mineinabyss.geary.systems.query.ListenerQuery import kotlinx.serialization.Contextual import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @@ -17,20 +18,19 @@ class TimeCondition( val at: Input<@Contextual Location> = Input.reference("location"), ) +fun GearyModule.createTimeConditionChecker() = listener( + object : ListenerQuery() { + val condition by source.get() + } +).check { + val location = condition.at.get(this) + val time = location.world.time -class TimeConditionChecker : CheckingListener() { - private val Pointers.condition by get().on(source) - - override fun Pointers.check(): Boolean { - val location = condition.at.get(this) - val time = location.world.time - - // support these two possibilities - // ====max-----min==== - // ----min=====max---- - return with(condition) { - if (min < max) time in min..max - else time !in max..min - } + // support these two possibilities + // ====max-----min==== + // ----min=====max---- + with(condition) { + if (min < max) time in min..max + else time !in max..min } } diff --git a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/config/inputs/Input.kt b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/config/inputs/Input.kt index 783ab4c..1b2098e 100644 --- a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/config/inputs/Input.kt +++ b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/config/inputs/Input.kt @@ -7,8 +7,7 @@ import com.mineinabyss.geary.datatypes.GearyEntity import com.mineinabyss.geary.helpers.componentId import com.mineinabyss.geary.helpers.readableString import com.mineinabyss.geary.helpers.temporaryEntity -import com.mineinabyss.geary.papermc.bridge.config.Skill -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.systems.query.ListenerQuery import kotlinx.serialization.Serializable @Serializable(with = InputSerializer::class) @@ -24,10 +23,10 @@ sealed interface Input { fun get(entities: Entities): T @OptIn(UnsafeAccessors::class) - fun get(pointers: Pointers): T = get( + fun get(pointers: ListenerQuery): T = get( Entities( - pointers.target.entity, - pointers.event.entity, + pointers.unsafeEntity, + pointers.event.unsafeEntity, ) ) @@ -74,7 +73,7 @@ sealed interface Input { inline fun reference(expression: String) = VariableReference(componentId(), expression) - inline fun of(value:T) = - Value(componentId(), value) + inline fun of(value: T) = + Value(componentId(), value) } } diff --git a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/config/parsers/CreateDefaultSkills.kt b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/config/parsers/CreateDefaultSkills.kt index 48d6178..d15dca9 100644 --- a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/config/parsers/CreateDefaultSkills.kt +++ b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/config/parsers/CreateDefaultSkills.kt @@ -1,35 +1,34 @@ package com.mineinabyss.geary.papermc.bridge.config.parsers -import com.mineinabyss.geary.annotations.optin.UnsafeAccessors import com.mineinabyss.geary.datatypes.Entity import com.mineinabyss.geary.datatypes.EntityId import com.mineinabyss.geary.helpers.entity import com.mineinabyss.geary.helpers.toGeary +import com.mineinabyss.geary.modules.GearyModule import com.mineinabyss.geary.papermc.bridge.config.OnEvent import com.mineinabyss.geary.papermc.bridge.config.Skill import com.mineinabyss.geary.prefabs.PrefabKey -import com.mineinabyss.geary.systems.GearyListener -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.systems.builders.listener +import com.mineinabyss.geary.systems.query.ListenerQuery -class CreateDefaultSkills : GearyListener() { - private val Pointers.prefab by get().whenSetOnTarget() - - @OptIn(UnsafeAccessors::class) - override fun Pointers.handle() { - val events = mutableMapOf() - target.entity.type.forEach { component -> - val data = target.entity.get(component) - val onEvents = component.toGeary().getRelations().map { it.target } - onEvents.forEach { event -> - events.getOrPut(event) { entity() }.apply { - if(data != null) set(data, component) - else add(component) - } +fun GearyModule.createDefaultSkillsListener() = listener( + object : ListenerQuery() { + val prefab by get() + } +).exec { + val events = mutableMapOf() + entity.type.forEach { component -> + val data = entity.get(component) + val onEvents = component.toGeary().getRelations().map { it.target } + onEvents.forEach { event -> + events.getOrPut(event) { entity() }.apply { + if (data != null) set(data, component) + else add(component) } } - events.forEach { (event, entity) -> - entity.set(Skill(execute = entity)) - target.entity.addRelation(event, entity.id) - } + } + events.forEach { (event, entity) -> + entity.set(Skill(execute = entity)) + entity.addRelation(event, entity.id) } } diff --git a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/config/parsers/ParseSkills.kt b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/config/parsers/ParseSkills.kt index 9b90c5e..9861454 100644 --- a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/config/parsers/ParseSkills.kt +++ b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/config/parsers/ParseSkills.kt @@ -1,31 +1,30 @@ package com.mineinabyss.geary.papermc.bridge.config.parsers -import com.mineinabyss.geary.annotations.optin.UnsafeAccessors import com.mineinabyss.geary.helpers.entity +import com.mineinabyss.geary.modules.GearyModule import com.mineinabyss.geary.modules.geary -import com.mineinabyss.geary.papermc.bridge.config.EventComponent import com.mineinabyss.geary.papermc.bridge.config.Skills -import com.mineinabyss.geary.systems.GearyListener -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.systems.builders.listener +import com.mineinabyss.geary.systems.query.ListenerQuery -class ParseSkills : GearyListener() { - private var Pointers.skillsComp by get().removable().whenSetOnTarget() - - @OptIn(UnsafeAccessors::class) - override fun Pointers.handle() { - skillsComp?.skills?.forEach { skill -> - val skillEntity = entity { - set(skill) - } +fun GearyModule.createParseSkillsListener() = listener( + object : ListenerQuery() { + val skillsComp by get() + override fun ensure() = event.anySet(::skillsComp) + } +).exec { + skillsComp.skills.forEach { skill -> + val skillEntity = entity { + set(skill) + } - val eventComponent = skill.event ?: run { - geary.logger.w("Skill defined without an event component. It won't trigger!") - return - } - if (eventComponent.data != null) - target.entity.setRelation(eventComponent.type, skillEntity.id, eventComponent.data) - else target.entity.addRelation(eventComponent.type, skillEntity.id) + val eventComponent = skill.event ?: run { + geary.logger.w("Skill defined without an event component. It won't trigger!") + return@exec } - skillsComp = null + if (eventComponent.data != null) + entity.setRelation(eventComponent.type, skillEntity.id, eventComponent.data) + else entity.addRelation(eventComponent.type, skillEntity.id) } + entity.remove() } diff --git a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/mythicmobs/RunMythicMobsSkills.kt b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/mythicmobs/RunMythicMobsSkills.kt index 34eff0f..53c1090 100644 --- a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/mythicmobs/RunMythicMobsSkills.kt +++ b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/mythicmobs/RunMythicMobsSkills.kt @@ -1,9 +1,10 @@ package com.mineinabyss.geary.papermc.bridge.mythicmobs import com.google.common.collect.Lists +import com.mineinabyss.geary.modules.GearyModule import com.mineinabyss.geary.serialization.serializers.InnerSerializer -import com.mineinabyss.geary.systems.GearyListener -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.systems.builders.listener +import com.mineinabyss.geary.systems.query.ListenerQuery import com.mineinabyss.idofront.typealiases.BukkitEntity import io.lumine.mythic.api.adapters.AbstractEntity import io.lumine.mythic.bukkit.BukkitAdapter @@ -15,7 +16,7 @@ import kotlinx.serialization.builtins.ListSerializer import kotlinx.serialization.builtins.serializer import kotlin.jvm.optionals.getOrNull -@Serializable(with= RunMythicMobsSkills.Serializer::class) +@Serializable(with = RunMythicMobsSkills.Serializer::class) class RunMythicMobsSkills( val keys: List, ) { @@ -27,26 +28,26 @@ class RunMythicMobsSkills( ) } -class RunMMSkillSystem: GearyListener() { - val Pointers.bukkit by get().on(target) - val Pointers.skill by get().on(source) - - override fun Pointers.handle() { - skill.keys.forEach { - val line = "[ - $it ]" - val entity = BukkitAdapter.adapt(bukkit) - val caster = MythicBukkit.inst().skillManager.getCaster(entity) - val skill = MythicBukkit.inst().skillManager.getSkill(line).getOrNull() - val meta = SkillMetadataImpl( - SkillTriggers.API, - caster, - entity, - entity.location, - Lists.newArrayList(*arrayOf(entity)), - null, - 1.0f - ) - skill?.execute(meta) - } +fun GearyModule.createRunMMSkillAction() = listener( + object : ListenerQuery() { + val bukkit by get() + val skill by source.get() + } +).exec { + skill.keys.forEach { + val line = "[ - $it ]" + val entity = BukkitAdapter.adapt(bukkit) + val caster = MythicBukkit.inst().skillManager.getCaster(entity) + val skill = MythicBukkit.inst().skillManager.getSkill(line).getOrNull() + val meta = SkillMetadataImpl( + SkillTriggers.API, + caster, + entity, + entity.location, + Lists.newArrayList(*arrayOf(entity)), + null, + 1.0f + ) + skill?.execute(meta) } } diff --git a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/readers/ReadLocation.kt b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/readers/ReadLocation.kt index 5554807..39e1eb9 100644 --- a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/readers/ReadLocation.kt +++ b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/readers/ReadLocation.kt @@ -1,25 +1,23 @@ package com.mineinabyss.geary.papermc.bridge.readers -import com.mineinabyss.geary.annotations.optin.UnsafeAccessors -import com.mineinabyss.geary.autoscan.AutoScan -import com.mineinabyss.geary.systems.GearyListener -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.modules.GearyModule +import com.mineinabyss.geary.systems.builders.listener +import com.mineinabyss.geary.systems.query.ListenerQuery import com.mineinabyss.idofront.typealiases.BukkitEntity import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -import org.bukkit.entity.LivingEntity @Serializable @SerialName("geary:read.location") class ReadLocation -class ReadLocationSystem : GearyListener() { - private val Pointers.bukkit by get().on(target) - private val Pointers.read by get().on(source) - - @OptIn(UnsafeAccessors::class) - override fun Pointers.handle() { - event.entity.set(bukkit.location) +fun GearyModule.createLocationReader() = listener( + object : ListenerQuery() { + val bukkit by get() + val read by source.get() } +).exec { + event.entity.set(bukkit.location) + } diff --git a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/readers/ReadTargetBlock.kt b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/readers/ReadTargetBlock.kt index 4f817ec..0629fda 100644 --- a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/readers/ReadTargetBlock.kt +++ b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/readers/ReadTargetBlock.kt @@ -1,9 +1,8 @@ package com.mineinabyss.geary.papermc.bridge.readers -import com.mineinabyss.geary.annotations.optin.UnsafeAccessors -import com.mineinabyss.geary.autoscan.AutoScan -import com.mineinabyss.geary.systems.GearyListener -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.modules.GearyModule +import com.mineinabyss.geary.systems.builders.listener +import com.mineinabyss.geary.systems.query.ListenerQuery import com.mineinabyss.idofront.typealiases.BukkitEntity import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @@ -16,13 +15,13 @@ class ReadTargetBlock( val maxDistance: Int, ) -class ReadTargetBlockSystem : GearyListener() { - private val Pointers.bukkit by get().on(target) - private val Pointers.read by get().on(source) - - @OptIn(UnsafeAccessors::class) - override fun Pointers.handle() { - val targetBlock = (bukkit as? LivingEntity)?.getTargetBlock(null, read.maxDistance) ?: return - event.entity.set(targetBlock.location) +fun GearyModule.createTargetBlockReader() = listener( + object : ListenerQuery() { + val bukkit by get() + val read by source.get() } +).exec { + val targetBlock = (bukkit as? LivingEntity)?.getTargetBlock(null, read.maxDistance) ?: return@exec + event.entity.set(targetBlock.location) + } diff --git a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/systems/CooldownDisplaySystem.kt b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/systems/CooldownDisplaySystem.kt index 4a16b75..2a7ac86 100644 --- a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/systems/CooldownDisplaySystem.kt +++ b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/systems/CooldownDisplaySystem.kt @@ -1,11 +1,14 @@ package com.mineinabyss.geary.papermc.bridge.systems -import com.mineinabyss.geary.annotations.optin.UnsafeAccessors -import com.mineinabyss.geary.autoscan.AutoScan +import com.mineinabyss.geary.datatypes.GearyEntity +import com.mineinabyss.geary.modules.GearyModule import com.mineinabyss.geary.papermc.bridge.conditions.Cooldown import com.mineinabyss.geary.papermc.bridge.conditions.CooldownStarted -import com.mineinabyss.geary.systems.RepeatingSystem -import com.mineinabyss.geary.systems.accessors.Pointer +import com.mineinabyss.geary.papermc.bridge.systems.CooldownDisplayProps.INTERVAL +import com.mineinabyss.geary.papermc.bridge.systems.CooldownDisplayProps.displayChar +import com.mineinabyss.geary.papermc.bridge.systems.CooldownDisplayProps.displayLength +import com.mineinabyss.geary.systems.builders.system +import com.mineinabyss.geary.systems.query.Query import com.mineinabyss.idofront.time.ticks import net.kyori.adventure.text.Component import net.kyori.adventure.text.JoinConfiguration @@ -16,65 +19,68 @@ import kotlin.time.Duration import kotlin.time.DurationUnit import kotlin.time.toDuration -private val INTERVAL = 1.ticks +class CooldownsToRemove( + val cooldowns: List +) -@AutoScan -class CooldownDisplaySystem : RepeatingSystem(interval = INTERVAL) { - val Pointer.player by get() - val Pointer.cooldowns by getRelationsWithData() - - class CooldownInfo(val display: Component, val timeLeft: Duration, val length: Duration) - - @OptIn(UnsafeAccessors::class) - override fun Pointer.tick() { - //TODO this should be a separate system once we have system priorities set up - val validCooldowns = cooldowns.filter { relation -> - fun removeCooldown() = entity.removeRelation(relation.target) - if (!relation.target.exists()) { - removeCooldown() - return@filter false - } - val cooldown = relation.data.cooldown - val timeLeft = cooldown.length - (System.currentTimeMillis() - relation.data.time) - .toDuration(DurationUnit.MILLISECONDS) - if (timeLeft.isNegative()) { - removeCooldown() - return@filter false - } - true +fun GearyModule.createCooldownDisplaySystem() = system(object : Query() { + val player by get() + val cooldowns by getRelationsWithData() +}).every(INTERVAL).defer { + //TODO this should be a separate system once we have system priorities set up + val validCooldowns = cooldowns.filter { relation -> + if (!relation.target.exists()) { + return@filter false + } + val cooldown = relation.data.cooldown + val timeLeft = cooldown.length - (System.currentTimeMillis() - relation.data.time) + .toDuration(DurationUnit.MILLISECONDS) + if (timeLeft.isNegative()) { + return@filter false } + true + } + val invalidCooldowns = cooldowns - validCooldowns.toSet() - val cooldownsWithDisplay = validCooldowns.mapNotNull { relation -> - val cooldown = relation.target.get() ?: return@mapNotNull null - if (cooldown.displayName == null) return@mapNotNull null + val cooldownsWithDisplay = validCooldowns.mapNotNull { relation -> + val cooldown = relation.target.get() ?: return@mapNotNull null + if (cooldown.displayName == null) return@mapNotNull null - val timeLeft = cooldown.length - (System.currentTimeMillis() - relation.data.time) - .toDuration(DurationUnit.MILLISECONDS) - CooldownInfo(cooldown.displayName, timeLeft, cooldown.length) - } + val timeLeft = cooldown.length - (System.currentTimeMillis() - relation.data.time) + .toDuration(DurationUnit.MILLISECONDS) + CooldownInfo(cooldown.displayName, timeLeft, cooldown.length) + } - player.sendActionBar( - Component.join(JoinConfiguration.commas(true), cooldownsWithDisplay.map { cooldown -> - val squaresLeft = - if (cooldown.timeLeft < INTERVAL) 0 else (cooldown.timeLeft / cooldown.length * displayLength).roundToInt() + player.sendActionBar( + Component.join(JoinConfiguration.commas(true), cooldownsWithDisplay.map { cooldown -> + val squaresLeft = + if (cooldown.timeLeft < INTERVAL) 0 else (cooldown.timeLeft / cooldown.length * displayLength).roundToInt() - val cooldownRender = Component.textOfChildren( - Component.text(displayChar.toString().repeat(displayLength - squaresLeft), NamedTextColor.GREEN), - Component.text(displayChar.toString().repeat(squaresLeft), NamedTextColor.RED), - if (cooldown.timeLeft < INTERVAL) Component.text(" [✔]", NamedTextColor.GREEN) - else Component.text( - " [${cooldown.timeLeft.toString(DurationUnit.SECONDS, 2)}]", - NamedTextColor.GRAY - ) - ).compact() + val cooldownRender = Component.textOfChildren( + Component.text(displayChar.toString().repeat(displayLength - squaresLeft), NamedTextColor.GREEN), + Component.text(displayChar.toString().repeat(squaresLeft), NamedTextColor.RED), + if (cooldown.timeLeft < INTERVAL) Component.text(" [✔]", NamedTextColor.GREEN) + else Component.text( + " [${cooldown.timeLeft.toString(DurationUnit.SECONDS, 2)}]", + NamedTextColor.GRAY + ) + ).compact() - Component.textOfChildren(cooldown.display, Component.space(), cooldownRender) - }) - ) + Component.textOfChildren(cooldown.display, Component.space(), cooldownRender) + }) + ) + CooldownsToRemove(invalidCooldowns.map { it.target }) +}.onFinish { data: CooldownsToRemove, entity -> + data.cooldowns.forEach { target -> + entity.removeRelation(target) } +} - companion object { - private const val displayLength = 10 - private const val displayChar = '■' - } +class CooldownInfo(val display: Component, val timeLeft: Duration, val length: Duration) + + +object CooldownDisplayProps { + const val displayLength = 10 + const val displayChar = '■' + val INTERVAL = 1.ticks } diff --git a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/targetselectors/NearbyEntities.kt b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/targetselectors/NearbyEntities.kt index 81c4614..0539a54 100644 --- a/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/targetselectors/NearbyEntities.kt +++ b/geary-papermc-bridge/src/main/kotlin/com/mineinabyss/geary/papermc/bridge/targetselectors/NearbyEntities.kt @@ -1,9 +1,9 @@ package com.mineinabyss.geary.papermc.bridge.targetselectors -import com.mineinabyss.geary.annotations.optin.UnsafeAccessors +import com.mineinabyss.geary.modules.GearyModule import com.mineinabyss.geary.papermc.tracking.entities.toGearyOrNull -import com.mineinabyss.geary.systems.GearyListener -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.systems.builders.listener +import com.mineinabyss.geary.systems.query.ListenerQuery import com.mineinabyss.idofront.typealiases.BukkitEntity import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @@ -14,18 +14,16 @@ class NearbyEntities( val radius: Double ) -class NearbyEntitiesSelector : GearyListener() { - private val Pointers.bukkit by get().on(target) -// private val Pointers.targets by get().orNull().on(event) - private val Pointers.reader by get().on(source) - - @OptIn(UnsafeAccessors::class) - override fun Pointers.handle() { - val rad = reader.radius - val targets = event.entity.get() ?: EmittedTargets(emptyList()) - val newTargets = EmittedTargets(targets.targets + bukkit.location - .getNearbyEntities(rad, rad, rad) - .mapNotNull { it.toGearyOrNull().takeIf { it != target.entity } }) - event.entity.set(newTargets) +fun GearyModule.createNearbyEntitiesSelector() = listener( + object : ListenerQuery() { + val bukkit by get() + val reader by source.get() } +).exec { + val rad = reader.radius + val targets = event.entity.get() ?: EmittedTargets(emptyList()) + val newTargets = EmittedTargets(targets.targets + bukkit.location + .getNearbyEntities(rad, rad, rad) + .mapNotNull { it.toGearyOrNull().takeIf { it != entity } }) + event.entity.set(newTargets) } diff --git a/geary-papermc-plugin/src/main/kotlin/com/mineinabyss/geary/papermc/plugin/GearyPluginImpl.kt b/geary-papermc-plugin/src/main/kotlin/com/mineinabyss/geary/papermc/plugin/GearyPluginImpl.kt index 590fc40..31b7552 100644 --- a/geary-papermc-plugin/src/main/kotlin/com/mineinabyss/geary/papermc/plugin/GearyPluginImpl.kt +++ b/geary-papermc-plugin/src/main/kotlin/com/mineinabyss/geary/papermc/plugin/GearyPluginImpl.kt @@ -14,11 +14,14 @@ import com.mineinabyss.geary.papermc.datastore.withUUIDSerializer import com.mineinabyss.geary.papermc.plugin.commands.GearyCommands import com.mineinabyss.geary.papermc.tracking.blocks.BlockTracking import com.mineinabyss.geary.papermc.tracking.blocks.gearyBlocks +import com.mineinabyss.geary.papermc.tracking.blocks.helpers.getKeys import com.mineinabyss.geary.papermc.tracking.entities.EntityTracking import com.mineinabyss.geary.papermc.tracking.entities.gearyMobs +import com.mineinabyss.geary.papermc.tracking.entities.helpers.GearyMobPrefabQuery.Companion.getKeys import com.mineinabyss.geary.papermc.tracking.entities.toGearyOrNull import com.mineinabyss.geary.papermc.tracking.items.ItemTracking import com.mineinabyss.geary.papermc.tracking.items.gearyItems +import com.mineinabyss.geary.papermc.tracking.items.helpers.GearyItemPrefabQuery.Companion.getKeys import com.mineinabyss.geary.prefabs.PrefabKey import com.mineinabyss.geary.prefabs.prefabs import com.mineinabyss.geary.serialization.dsl.FileSystemAddon @@ -28,13 +31,11 @@ import com.mineinabyss.geary.uuid.SynchronizedUUID2GearyMap import com.mineinabyss.geary.uuid.UUIDTracking import com.mineinabyss.idofront.di.DI import com.mineinabyss.idofront.messaging.logSuccess -import com.mineinabyss.idofront.plugin.listeners import com.mineinabyss.idofront.serialization.LocationSerializer import com.mineinabyss.idofront.serialization.SerializablePrefabItemService import okio.FileSystem import okio.Path.Companion.toOkioPath import org.bukkit.Location -import org.bukkit.event.Listener import org.bukkit.inventory.ItemStack import kotlin.io.path.isDirectory import kotlin.io.path.listDirectoryEntries @@ -75,12 +76,6 @@ class GearyPluginImpl : GearyPlugin() { components() } - // Auto register Bukkit listeners when they are added as a system - geary.pipeline.interceptSystemAddition { system -> - if (system is Listener) listeners(system) - system - } - // Load prefabs in Geary folder, each subfolder is considered its own namespace dataFolder.toPath().listDirectoryEntries() .filter { it.isDirectory() } diff --git a/geary-papermc-plugin/src/main/kotlin/com/mineinabyss/geary/papermc/plugin/commands/GearyCommands.kt b/geary-papermc-plugin/src/main/kotlin/com/mineinabyss/geary/papermc/plugin/commands/GearyCommands.kt index e10993e..8fb64c4 100644 --- a/geary-papermc-plugin/src/main/kotlin/com/mineinabyss/geary/papermc/plugin/commands/GearyCommands.kt +++ b/geary-papermc-plugin/src/main/kotlin/com/mineinabyss/geary/papermc/plugin/commands/GearyCommands.kt @@ -8,11 +8,14 @@ import com.mineinabyss.geary.modules.archetypes import com.mineinabyss.geary.modules.geary import com.mineinabyss.geary.papermc.gearyPaper import com.mineinabyss.geary.papermc.tracking.entities.gearyMobs +import com.mineinabyss.geary.papermc.tracking.entities.helpers.GearyMobPrefabQuery.Companion.getKeyStrings +import com.mineinabyss.geary.papermc.tracking.entities.helpers.GearyMobPrefabQuery.Companion.getKeys import com.mineinabyss.geary.papermc.tracking.entities.helpers.spawnFromPrefab import com.mineinabyss.geary.papermc.tracking.entities.systems.updatemobtype.UpdateMob import com.mineinabyss.geary.papermc.tracking.entities.toGeary import com.mineinabyss.geary.papermc.tracking.items.cache.PlayerItemCache import com.mineinabyss.geary.papermc.tracking.items.gearyItems +import com.mineinabyss.geary.papermc.tracking.items.helpers.GearyItemPrefabQuery.Companion.getKeys import com.mineinabyss.geary.papermc.tracking.items.inventory.toGeary import com.mineinabyss.geary.prefabs.PrefabKey import com.mineinabyss.geary.prefabs.helpers.inheritPrefabs diff --git a/geary-papermc-plugin/src/main/kotlin/com/mineinabyss/geary/papermc/plugin/commands/GearyItemCommands.kt b/geary-papermc-plugin/src/main/kotlin/com/mineinabyss/geary/papermc/plugin/commands/GearyItemCommands.kt index 31ac476..69ed7e8 100644 --- a/geary-papermc-plugin/src/main/kotlin/com/mineinabyss/geary/papermc/plugin/commands/GearyItemCommands.kt +++ b/geary-papermc-plugin/src/main/kotlin/com/mineinabyss/geary/papermc/plugin/commands/GearyItemCommands.kt @@ -10,7 +10,7 @@ import com.mineinabyss.idofront.messaging.error fun Command.items() { "items" { "give" { - val prefabKey by optionArg(options = gearyItems.prefabs.run { toList { it.key.toString() } }) { + val prefabKey by optionArg(options = gearyItems.prefabs.map { key.toString() }) { parseErrorMessage = { "No such entity: $passed" } } val amount by intArg { default = 1 } diff --git a/geary-papermc-plugin/src/main/kotlin/com/mineinabyss/geary/papermc/plugin/commands/GearyMobCommands.kt b/geary-papermc-plugin/src/main/kotlin/com/mineinabyss/geary/papermc/plugin/commands/GearyMobCommands.kt index 21442d8..0082295 100644 --- a/geary-papermc-plugin/src/main/kotlin/com/mineinabyss/geary/papermc/plugin/commands/GearyMobCommands.kt +++ b/geary-papermc-plugin/src/main/kotlin/com/mineinabyss/geary/papermc/plugin/commands/GearyMobCommands.kt @@ -1,6 +1,7 @@ package com.mineinabyss.geary.papermc.plugin.commands import com.mineinabyss.geary.papermc.tracking.entities.gearyMobs +import com.mineinabyss.geary.papermc.tracking.entities.helpers.GearyMobPrefabQuery.Companion.getKeyStrings import com.mineinabyss.geary.papermc.tracking.entities.helpers.spawnFromPrefab import com.mineinabyss.geary.prefabs.PrefabKey import com.mineinabyss.idofront.commands.Command diff --git a/geary-papermc-plugin/src/main/kotlin/com/mineinabyss/geary/papermc/plugin/commands/LocateCommand.kt b/geary-papermc-plugin/src/main/kotlin/com/mineinabyss/geary/papermc/plugin/commands/LocateCommand.kt index 6e89181..d3bdf6a 100644 --- a/geary-papermc-plugin/src/main/kotlin/com/mineinabyss/geary/papermc/plugin/commands/LocateCommand.kt +++ b/geary-papermc-plugin/src/main/kotlin/com/mineinabyss/geary/papermc/plugin/commands/LocateCommand.kt @@ -15,7 +15,7 @@ import org.bukkit.Bukkit fun Command.locate() { "locate" { - val mobKey by optionArg(options = gearyMobs.prefabs.run { toList { it.key.toString() } }) { + val mobKey by optionArg(options = gearyMobs.prefabs.map { key.toString() }) { parseErrorMessage = { "No such entity: $passed" } } val radius by intArg { diff --git a/geary-papermc-plugin/src/main/kotlin/com/mineinabyss/geary/papermc/plugin/commands/QueryCommand.kt b/geary-papermc-plugin/src/main/kotlin/com/mineinabyss/geary/papermc/plugin/commands/QueryCommand.kt index 2a3141b..9567919 100644 --- a/geary-papermc-plugin/src/main/kotlin/com/mineinabyss/geary/papermc/plugin/commands/QueryCommand.kt +++ b/geary-papermc-plugin/src/main/kotlin/com/mineinabyss/geary/papermc/plugin/commands/QueryCommand.kt @@ -5,6 +5,7 @@ import com.mineinabyss.geary.datatypes.GearyEntity import com.mineinabyss.geary.helpers.toGeary import com.mineinabyss.geary.papermc.gearyPaper import com.mineinabyss.geary.papermc.tracking.entities.gearyMobs +import com.mineinabyss.geary.papermc.tracking.entities.helpers.GearyMobPrefabQuery import com.mineinabyss.geary.papermc.tracking.entities.toGeary import com.mineinabyss.geary.papermc.tracking.entities.toGearyOrNull import com.mineinabyss.geary.prefabs.PrefabKey @@ -32,7 +33,7 @@ fun Command.mobsQuery() { for (world in worlds) for (entity in world.entities) { val geary = entity.toGearyOrNull() ?: continue // Only select entities that are instanced from a gearyMobs registered prefab - if (!gearyMobs.prefabs.isMob(geary)) continue + if (!GearyMobPrefabQuery.isMob(geary)) continue if (types.any { type -> fun excludeDefault() = entity.customName() == null diff --git a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/blocks/BlockTracking.kt b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/blocks/BlockTracking.kt index 828b72b..0fefcb5 100644 --- a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/blocks/BlockTracking.kt +++ b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/blocks/BlockTracking.kt @@ -3,8 +3,10 @@ package com.mineinabyss.geary.papermc.tracking.blocks import com.mineinabyss.geary.addons.dsl.GearyAddonWithDefault import com.mineinabyss.geary.modules.geary import com.mineinabyss.geary.papermc.tracking.blocks.helpers.GearyBlockPrefabQuery -import com.mineinabyss.geary.papermc.tracking.blocks.systems.TrackOnSetBlockComponent +import com.mineinabyss.geary.papermc.tracking.blocks.systems.createTrackOnSetBlockComponentListener import com.mineinabyss.geary.prefabs.PrefabKey +import com.mineinabyss.geary.systems.builders.cachedQuery +import com.mineinabyss.geary.systems.query.CachedQueryRunner import com.mineinabyss.idofront.di.DI import org.bukkit.block.data.BlockData @@ -12,20 +14,18 @@ val gearyBlocks by DI.observe() interface BlockTracking { val block2Prefab: Block2Prefab - val prefabs: GearyBlockPrefabQuery + val prefabs: CachedQueryRunner fun createBlockData(prefabKey: PrefabKey): BlockData? = block2Prefab[prefabKey] companion object : GearyAddonWithDefault { override fun default(): BlockTracking = object : BlockTracking { - override val prefabs = GearyBlockPrefabQuery() + override val prefabs = geary.cachedQuery(GearyBlockPrefabQuery()) override val block2Prefab = Block2Prefab() } override fun BlockTracking.install() { - geary.pipeline.addSystems( - TrackOnSetBlockComponent() - ) + createTrackOnSetBlockComponentListener() } } } diff --git a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/blocks/helpers/GearyBlockPrefabQuery.kt b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/blocks/helpers/GearyBlockPrefabQuery.kt index b0a4722..9f3ab7c 100644 --- a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/blocks/helpers/GearyBlockPrefabQuery.kt +++ b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/blocks/helpers/GearyBlockPrefabQuery.kt @@ -1,17 +1,19 @@ package com.mineinabyss.geary.papermc.tracking.blocks.helpers -import com.mineinabyss.geary.datatypes.family.family import com.mineinabyss.geary.papermc.tracking.blocks.components.SetBlock import com.mineinabyss.geary.prefabs.PrefabKey import com.mineinabyss.geary.prefabs.configuration.components.Prefab -import com.mineinabyss.geary.systems.accessors.Pointer +import com.mineinabyss.geary.systems.query.CachedQueryRunner import com.mineinabyss.geary.systems.query.GearyQuery + class GearyBlockPrefabQuery : GearyQuery() { - val Pointer.key by get() - val Pointer.isMobzy by family { + val key by get() + + override fun ensure() = this { has() has() } - fun getKeys(): List = toList { it.key } } + +fun CachedQueryRunner.getKeys() = map { key } diff --git a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/blocks/systems/TrackOnSetBlockComponent.kt b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/blocks/systems/TrackOnSetBlockComponent.kt index 2d30cf3..47d3bde 100644 --- a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/blocks/systems/TrackOnSetBlockComponent.kt +++ b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/blocks/systems/TrackOnSetBlockComponent.kt @@ -1,22 +1,23 @@ package com.mineinabyss.geary.papermc.tracking.blocks.systems +import com.mineinabyss.geary.modules.geary import com.mineinabyss.geary.papermc.tracking.blocks.components.SetBlock import com.mineinabyss.geary.papermc.tracking.blocks.gearyBlocks import com.mineinabyss.geary.prefabs.PrefabKey -import com.mineinabyss.geary.systems.GearyListener -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.systems.builders.listener +import com.mineinabyss.geary.systems.query.ListenerQuery -class TrackOnSetBlockComponent : GearyListener() { - private val Pointers.block by get().on(target) - private val Pointers.prefab by get().whenSetOnTarget() - - override fun Pointers.handle() { - val blockData = gearyBlocks - .block2Prefab - .blockMap - .getOrDefault(block.blockType, null) - ?.get(block.blockId) ?: return - - gearyBlocks.block2Prefab[blockData] = prefab +fun createTrackOnSetBlockComponentListener() = geary.listener( + object : ListenerQuery() { + val block by get() + val prefab by get() } +).exec { + val blockData = gearyBlocks + .block2Prefab + .blockMap + .getOrDefault(block.blockType, null) + ?.get(block.blockId) ?: return@exec + + gearyBlocks.block2Prefab[blockData] = prefab } diff --git a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/entities/EntityTracking.kt b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/entities/EntityTracking.kt index c51f3ad..7a5c4a5 100644 --- a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/entities/EntityTracking.kt +++ b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/entities/EntityTracking.kt @@ -8,14 +8,14 @@ import com.mineinabyss.geary.modules.geary import com.mineinabyss.geary.papermc.CatchType import com.mineinabyss.geary.papermc.gearyPaper import com.mineinabyss.geary.papermc.tracking.entities.helpers.GearyMobPrefabQuery -import com.mineinabyss.geary.papermc.tracking.entities.systems.EntityWorldEventTracker -import com.mineinabyss.geary.papermc.tracking.entities.systems.TrackOnSetBukkitComponent -import com.mineinabyss.geary.papermc.tracking.entities.systems.UntrackOnRemoveBukkitComponent -import com.mineinabyss.geary.papermc.tracking.entities.systems.attemptspawn.AttemptSpawnListener -import com.mineinabyss.geary.papermc.tracking.entities.systems.attemptspawn.AttemptSpawnMythicMob +import com.mineinabyss.geary.papermc.tracking.entities.systems.* +import com.mineinabyss.geary.papermc.tracking.entities.systems.attemptspawn.createAttemptSpawnListener +import com.mineinabyss.geary.papermc.tracking.entities.systems.attemptspawn.createAttemptSpawnMythicMobListener import com.mineinabyss.geary.papermc.tracking.entities.systems.removevanillamobs.RemoveVanillaMobsListener import com.mineinabyss.geary.papermc.tracking.entities.systems.updatemobtype.ConvertEntityTypesListener import com.mineinabyss.geary.papermc.tracking.entities.systems.updatemobtype.ConvertToMythicMobListener +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.typealiases.BukkitEntity @@ -26,21 +26,19 @@ val gearyMobs by DI.observe() interface EntityTracking { val bukkitEntityComponent: ComponentId val bukkit2Geary: BukkitEntity2Geary - val prefabs: GearyMobPrefabQuery + val prefabs: CachedQueryRunner companion object : GearyAddonWithDefault { override fun default(): EntityTracking = object : EntityTracking { override val bukkitEntityComponent = componentId() override val bukkit2Geary = BukkitEntity2Geary(gearyPaper.config.catch.asyncEntityConversion == CatchType.ERROR) - override val prefabs = GearyMobPrefabQuery() + override val prefabs = geary.cachedQuery(GearyMobPrefabQuery()) } override fun EntityTracking.install() { - geary.pipeline.addSystems( - TrackOnSetBukkitComponent(), - UntrackOnRemoveBukkitComponent(), - AttemptSpawnListener(), - ) + geary.createBukkitEntityRemoveListener() + geary.createBukkitEntitySetListener() + geary.createAttemptSpawnListener() geary.pipeline.runOnOrAfter(GearyPhase.ENABLE) { gearyPaper.plugin.listeners( EntityWorldEventTracker(), @@ -49,9 +47,7 @@ interface EntityTracking { ) if (Bukkit.getPluginManager().plugins.any { it.name == "MythicMobs" }) { - geary.pipeline.addSystems( - AttemptSpawnMythicMob(), - ) + geary.createAttemptSpawnMythicMobListener() gearyPaper.plugin.listeners( ConvertToMythicMobListener(), diff --git a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/entities/helpers/GearyMobPrefabQuery.kt b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/entities/helpers/GearyMobPrefabQuery.kt index 1ae3bcd..a604f97 100644 --- a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/entities/helpers/GearyMobPrefabQuery.kt +++ b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/entities/helpers/GearyMobPrefabQuery.kt @@ -3,32 +3,34 @@ package com.mineinabyss.geary.papermc.tracking.entities.helpers import com.mineinabyss.geary.datatypes.GearyEntity import com.mineinabyss.geary.datatypes.family.family import com.mineinabyss.geary.helpers.contains -import com.mineinabyss.geary.modules.geary import com.mineinabyss.geary.papermc.tracking.entities.components.SetEntityType import com.mineinabyss.geary.papermc.tracking.entities.components.SetMythicMob -import com.mineinabyss.geary.papermc.tracking.entities.gearyMobs import com.mineinabyss.geary.prefabs.PrefabKey import com.mineinabyss.geary.prefabs.configuration.components.Prefab -import com.mineinabyss.geary.systems.accessors.Pointer +import com.mineinabyss.geary.systems.query.CachedQueryRunner import com.mineinabyss.geary.systems.query.GearyQuery class GearyMobPrefabQuery : GearyQuery() { - private val mobQuery = family { - has() - or { - has() - has() - } - } - val Pointer.key by get() - val Pointer.isMob by mobQuery + val key by get() + override fun ensure() = this { add(mobQuery) } + + companion object { - fun getKeys(): List = toList { it.key } - fun getKeyStrings(): List = toList { it.key.toString() } + fun CachedQueryRunner.getKeys(): List = map { key } + fun CachedQueryRunner.getKeyStrings(): List = map { key.toString() } - fun isMob(entity: GearyEntity): Boolean { - return entity.prefabs.any { it.type in mobQuery} + private val mobQuery = family { + has() + or { + has() + has() + } + } + + fun isMob(entity: GearyEntity): Boolean { + return entity.prefabs.any { it.type in mobQuery } + } } } diff --git a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/entities/systems/TrackOnSetBukkitComponent.kt b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/entities/systems/TrackOnSetBukkitComponent.kt index 4394eac..76d1fc3 100644 --- a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/entities/systems/TrackOnSetBukkitComponent.kt +++ b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/entities/systems/TrackOnSetBukkitComponent.kt @@ -1,30 +1,31 @@ package com.mineinabyss.geary.papermc.tracking.entities.systems -import com.mineinabyss.geary.annotations.optin.UnsafeAccessors +import com.mineinabyss.geary.modules.GearyModule +import com.mineinabyss.geary.modules.geary import com.mineinabyss.geary.papermc.datastore.hasComponentsEncoded import com.mineinabyss.geary.papermc.datastore.loadComponentsFrom import com.mineinabyss.geary.papermc.tracking.entities.gearyMobs -import com.mineinabyss.geary.systems.GearyListener -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.systems.builders.listener +import com.mineinabyss.geary.systems.query.ListenerQuery import com.mineinabyss.idofront.typealiases.BukkitEntity -class TrackOnSetBukkitComponent : GearyListener() { - private val Pointers.bukkit by get().whenSetOnTarget() - - @OptIn(UnsafeAccessors::class) - override fun Pointers.handle() { - gearyMobs.bukkit2Geary[bukkit] = target.entity - - // Load persisted components - val pdc = bukkit.persistentDataContainer - if (pdc.hasComponentsEncoded) - target.entity.loadComponentsFrom(pdc) +fun GearyModule.createBukkitEntitySetListener() = listener( + object : ListenerQuery() { + val bukkit by get() + override fun ensure() = event.anySet(::bukkit) + } +).exec { + gearyMobs.bukkit2Geary[bukkit] = entity - // allow us to both get the BukkitEntity and specific class (ex Player) - bukkit.type.entityClass?.kotlin?.let { bukkitClass -> - target.entity.set(bukkit, bukkitClass) - } + // Load persisted components + val pdc = bukkit.persistentDataContainer + if (pdc.hasComponentsEncoded) + entity.loadComponentsFrom(pdc) - target.entity.set(bukkit.uniqueId) + // allow us to both get the BukkitEntity and specific class (ex Player) + bukkit.type.entityClass?.kotlin?.let { bukkitClass -> + entity.set(bukkit, bukkitClass) } + + entity.set(bukkit.uniqueId) } diff --git a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/entities/systems/UntrackOnRemoveBukkitComponent.kt b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/entities/systems/UntrackOnRemoveBukkitComponent.kt index 192657f..3511f04 100644 --- a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/entities/systems/UntrackOnRemoveBukkitComponent.kt +++ b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/entities/systems/UntrackOnRemoveBukkitComponent.kt @@ -1,19 +1,17 @@ package com.mineinabyss.geary.papermc.tracking.entities.systems -import com.mineinabyss.geary.annotations.optin.UnsafeAccessors import com.mineinabyss.geary.components.events.EntityRemoved -import com.mineinabyss.geary.datatypes.family.family +import com.mineinabyss.geary.modules.GearyModule import com.mineinabyss.geary.papermc.tracking.entities.gearyMobs -import com.mineinabyss.geary.systems.GearyListener -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.systems.builders.listener +import com.mineinabyss.geary.systems.query.ListenerQuery import com.mineinabyss.idofront.typealiases.BukkitEntity -class UntrackOnRemoveBukkitComponent : GearyListener() { - private val Pointers.bukkit by get().on(target) - private val Pointers.removed by family { has() }.on(event) - - @OptIn(UnsafeAccessors::class) - override fun Pointers.handle() { - gearyMobs.bukkit2Geary.remove(bukkit.entityId) +fun GearyModule.createBukkitEntityRemoveListener() = listener( + object : ListenerQuery() { + val bukkit by get() + override fun ensure() = event { has() } } +).exec { + gearyMobs.bukkit2Geary.remove(bukkit.entityId) } diff --git a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/entities/systems/attemptspawn/AttemptSpawnListener.kt b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/entities/systems/attemptspawn/AttemptSpawnListener.kt index 8bbfa10..aaaa3a6 100644 --- a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/entities/systems/attemptspawn/AttemptSpawnListener.kt +++ b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/entities/systems/attemptspawn/AttemptSpawnListener.kt @@ -1,11 +1,11 @@ package com.mineinabyss.geary.papermc.tracking.entities.systems.attemptspawn -import com.mineinabyss.geary.annotations.optin.UnsafeAccessors -import com.mineinabyss.geary.datatypes.family.family +import com.mineinabyss.geary.modules.GearyModule +import com.mineinabyss.geary.modules.geary import com.mineinabyss.geary.papermc.tracking.entities.components.AttemptSpawn import com.mineinabyss.geary.papermc.tracking.entities.components.SetEntityType -import com.mineinabyss.geary.systems.GearyListener -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.systems.builders.listener +import com.mineinabyss.geary.systems.query.ListenerQuery import com.mineinabyss.idofront.nms.aliases.toBukkit import com.mineinabyss.idofront.nms.aliases.toNMS import com.mineinabyss.idofront.typealiases.BukkitEntity @@ -13,29 +13,24 @@ import net.minecraft.core.BlockPos import net.minecraft.world.entity.MobSpawnType import org.bukkit.event.entity.CreatureSpawnEvent -class AttemptSpawnListener : GearyListener() { - private val Pointers.mobType by get().on(target) - private val Pointers.attemptSpawn by get().on(event) - - val Pointers.family by family { - not { has() } - }.on(target) - - - @OptIn(UnsafeAccessors::class) - override fun Pointers.handle() { - val loc = attemptSpawn.location - mobType.entityTypeFromRegistry.spawn( - loc.world.toNMS(), - null, - // We set the entity here so that we don't create a separate Geary entity in EntityWorldEventTracker - // This is called before adding to the world. - { mob -> target.entity.set(mob.toBukkit()) }, - BlockPos(loc.blockX, loc.blockY, loc.blockZ), - MobSpawnType.NATURAL, - false, - false, - CreatureSpawnEvent.SpawnReason.COMMAND - ) +fun GearyModule.createAttemptSpawnListener() = listener( + object : ListenerQuery() { + val mobType by get() + val attemptSpawn by event.get() + override fun ensure() = this { not { has() } } } +).exec { + val loc = attemptSpawn.location + mobType.entityTypeFromRegistry.spawn( + loc.world.toNMS(), + null, + // We set the entity here so that we don't create a separate Geary entity in EntityWorldEventTracker + // This is called before adding to the world. + { mob -> entity.set(mob.toBukkit()) }, + BlockPos(loc.blockX, loc.blockY, loc.blockZ), + MobSpawnType.NATURAL, + false, + false, + CreatureSpawnEvent.SpawnReason.COMMAND + ) } diff --git a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/entities/systems/attemptspawn/AttemptSpawnMythicMob.kt b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/entities/systems/attemptspawn/AttemptSpawnMythicMob.kt index 1867691..063b540 100644 --- a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/entities/systems/attemptspawn/AttemptSpawnMythicMob.kt +++ b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/entities/systems/attemptspawn/AttemptSpawnMythicMob.kt @@ -1,32 +1,27 @@ package com.mineinabyss.geary.papermc.tracking.entities.systems.attemptspawn -import com.mineinabyss.geary.annotations.optin.UnsafeAccessors -import com.mineinabyss.geary.datatypes.family.family +import com.mineinabyss.geary.modules.GearyModule +import com.mineinabyss.geary.modules.geary import com.mineinabyss.geary.papermc.tracking.entities.components.AttemptSpawn import com.mineinabyss.geary.papermc.tracking.entities.components.SetMythicMob -import com.mineinabyss.geary.systems.GearyListener -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.systems.builders.listener +import com.mineinabyss.geary.systems.query.ListenerQuery import com.mineinabyss.idofront.typealiases.BukkitEntity import io.lumine.mythic.api.mobs.entities.SpawnReason import io.lumine.mythic.bukkit.BukkitAdapter import io.lumine.mythic.bukkit.MythicBukkit import kotlin.jvm.optionals.getOrNull - -class AttemptSpawnMythicMob : GearyListener() { - private val Pointers.mobType by get().on(target) - private val Pointers.attemptSpawn by get().on(event) - - val Pointers.family by family { - not { has() } - }.on(target) - - @OptIn(UnsafeAccessors::class) - override fun Pointers.handle() { - val mythicMob = MythicBukkit.inst().mobManager.getMythicMob(mobType.id).getOrNull() ?: return - mythicMob.spawn(BukkitAdapter.adapt(attemptSpawn.location), 1.0, SpawnReason.NATURAL) { mob -> - target.entity.set(mob) - target.entity.set(mythicMob) - } +fun GearyModule.createAttemptSpawnMythicMobListener() = listener( + object : ListenerQuery() { + val mobType by get() + val attemptSpawn by event.get() + override fun ensure() = this { not { has() } } + } +).exec { + val mythicMob = MythicBukkit.inst().mobManager.getMythicMob(mobType.id).getOrNull() ?: return@exec + mythicMob.spawn(BukkitAdapter.adapt(attemptSpawn.location), 1.0, SpawnReason.NATURAL) { mob -> + entity.set(mob) + entity.set(mythicMob) } } diff --git a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/BukkitBackedItemTracking.kt b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/BukkitBackedItemTracking.kt index 7d5dc0d..16e4fdd 100644 --- a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/BukkitBackedItemTracking.kt +++ b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/BukkitBackedItemTracking.kt @@ -1,6 +1,7 @@ package com.mineinabyss.geary.papermc.tracking.items import com.mineinabyss.geary.datatypes.GearyEntity +import com.mineinabyss.geary.modules.geary import com.mineinabyss.geary.papermc.tracking.items.cache.BukkitItemCache import com.mineinabyss.geary.papermc.tracking.items.cache.PlayerItemCache import com.mineinabyss.geary.papermc.tracking.items.helpers.GearyItemPrefabQuery @@ -8,13 +9,14 @@ import com.mineinabyss.geary.papermc.tracking.items.inventory.BukkitInventoryCac import com.mineinabyss.geary.papermc.tracking.items.inventory.InventoryCacheWrapper import com.mineinabyss.geary.papermc.tracking.items.migration.ItemMigration import com.mineinabyss.geary.papermc.tracking.items.systems.LoginListener +import com.mineinabyss.geary.systems.builders.cachedQuery import org.bukkit.inventory.ItemStack class BukkitBackedItemTracking : ItemTracking { override val itemProvider = GearyItemProvider() override val migration: ItemMigration = ItemMigration() override val loginListener = LoginListener { BukkitItemCache() } - override val prefabs = GearyItemPrefabQuery() + override val prefabs = geary.cachedQuery(GearyItemPrefabQuery()) override fun getCacheWrapper(entity: GearyEntity): InventoryCacheWrapper? { val cache = entity.get>() ?: return null diff --git a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/ItemTracking.kt b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/ItemTracking.kt index 1b12225..57ea315 100644 --- a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/ItemTracking.kt +++ b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/ItemTracking.kt @@ -7,14 +7,15 @@ import com.mineinabyss.geary.modules.geary import com.mineinabyss.geary.papermc.gearyPaper import com.mineinabyss.geary.papermc.tracking.items.helpers.GearyItemPrefabQuery import com.mineinabyss.geary.papermc.tracking.items.inventory.InventoryCacheWrapper -import com.mineinabyss.geary.papermc.tracking.items.migration.CustomModelDataToPrefabTracker import com.mineinabyss.geary.papermc.tracking.items.migration.ItemMigration import com.mineinabyss.geary.papermc.tracking.items.migration.SetItemIgnoredPropertyListener -import com.mineinabyss.geary.papermc.tracking.items.migration.SetItemMigrationSystem -import com.mineinabyss.geary.papermc.tracking.items.systems.InventoryTrackerSystem +import com.mineinabyss.geary.papermc.tracking.items.migration.createCustomModelDataToPrefabTracker +import com.mineinabyss.geary.papermc.tracking.items.migration.createItemMigrationListener import com.mineinabyss.geary.papermc.tracking.items.systems.LoginListener import com.mineinabyss.geary.papermc.tracking.items.systems.MythicMobDropSystem +import com.mineinabyss.geary.papermc.tracking.items.systems.createInventoryTrackerSystem import com.mineinabyss.geary.prefabs.PrefabKey +import com.mineinabyss.geary.systems.query.CachedQueryRunner import com.mineinabyss.idofront.di.DI import com.mineinabyss.idofront.plugin.listeners import org.bukkit.inventory.ItemStack @@ -25,7 +26,7 @@ interface ItemTracking { val itemProvider: GearyItemProvider val migration: ItemMigration val loginListener: LoginListener - val prefabs: GearyItemPrefabQuery + val prefabs: CachedQueryRunner fun getCacheWrapper(entity: GearyEntity): InventoryCacheWrapper? fun createItem( @@ -36,14 +37,12 @@ interface ItemTracking { companion object : GearyAddonWithDefault { override fun default(): ItemTracking = NMSBackedItemTracking() - override fun ItemTracking.install() { - geary.pipeline.addSystems( - InventoryTrackerSystem(), - CustomModelDataToPrefabTracker(), - SetItemMigrationSystem() - ) + override fun ItemTracking.install() = geary.run { + createItemMigrationListener() + createInventoryTrackerSystem() + createCustomModelDataToPrefabTracker() - geary.pipeline.runOnOrAfter(GearyPhase.ENABLE) { + pipeline.runOnOrAfter(GearyPhase.ENABLE) { gearyPaper.plugin.listeners( loginListener, SetItemIgnoredPropertyListener(), diff --git a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/NMSBackedItemTracking.kt b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/NMSBackedItemTracking.kt index 461e136..dd58bb4 100644 --- a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/NMSBackedItemTracking.kt +++ b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/NMSBackedItemTracking.kt @@ -2,6 +2,7 @@ package com.mineinabyss.geary.papermc.tracking.items import com.mineinabyss.geary.datatypes.GearyEntity import com.mineinabyss.geary.helpers.componentId +import com.mineinabyss.geary.modules.geary import com.mineinabyss.geary.papermc.tracking.items.cache.NMSItemCache import com.mineinabyss.geary.papermc.tracking.items.cache.PlayerItemCache import com.mineinabyss.geary.papermc.tracking.items.helpers.GearyItemPrefabQuery @@ -9,13 +10,14 @@ import com.mineinabyss.geary.papermc.tracking.items.inventory.InventoryCacheWrap import com.mineinabyss.geary.papermc.tracking.items.inventory.NMSInventoryCacheWrapper import com.mineinabyss.geary.papermc.tracking.items.migration.ItemMigration import com.mineinabyss.geary.papermc.tracking.items.systems.LoginListener +import com.mineinabyss.geary.systems.builders.cachedQuery import com.mineinabyss.idofront.nms.aliases.NMSItemStack class NMSBackedItemTracking : ItemTracking { override val itemProvider = GearyItemProvider() override val migration: ItemMigration = ItemMigration() override val loginListener = LoginListener { NMSItemCache() } - override val prefabs = GearyItemPrefabQuery() + override val prefabs = geary.cachedQuery(GearyItemPrefabQuery()) private val itemCacheComponent = componentId>() diff --git a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/helpers/GearyItemPrefabQuery.kt b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/helpers/GearyItemPrefabQuery.kt index 07c4872..ec24e94 100644 --- a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/helpers/GearyItemPrefabQuery.kt +++ b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/helpers/GearyItemPrefabQuery.kt @@ -6,21 +6,24 @@ import com.mineinabyss.geary.helpers.contains import com.mineinabyss.geary.papermc.tracking.items.components.SetItem import com.mineinabyss.geary.prefabs.PrefabKey import com.mineinabyss.geary.prefabs.configuration.components.Prefab -import com.mineinabyss.geary.systems.accessors.Pointer +import com.mineinabyss.geary.systems.query.CachedQueryRunner import com.mineinabyss.geary.systems.query.GearyQuery class GearyItemPrefabQuery : GearyQuery() { - private val itemQuery = family { - has() - has() - } - val Pointer.key by get() - val Pointer.isItem by itemQuery + val key by get() + override fun ensure() = this { add(itemQuery) } + + companion object { + private val itemQuery = family { + has() + has() + } - fun getKeys(): List = toList{ it.key } + fun CachedQueryRunner.getKeys(): List = map { key } - fun isItem(entity: GearyEntity): Boolean { - return entity.prefabs.any { it.type in itemQuery } + fun isItem(entity: GearyEntity): Boolean { + return entity.prefabs.any { it.type in itemQuery } + } } } diff --git a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/migration/CustomModelDataToPrefabTracker.kt b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/migration/CustomModelDataToPrefabTracker.kt index 24075d2..e29879e 100644 --- a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/migration/CustomModelDataToPrefabTracker.kt +++ b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/migration/CustomModelDataToPrefabTracker.kt @@ -1,17 +1,19 @@ package com.mineinabyss.geary.papermc.tracking.items.migration +import com.mineinabyss.geary.modules.GearyModule import com.mineinabyss.geary.papermc.tracking.items.components.SetItem import com.mineinabyss.geary.papermc.tracking.items.gearyItems import com.mineinabyss.geary.prefabs.PrefabKey -import com.mineinabyss.geary.systems.GearyListener -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.systems.builders.listener +import com.mineinabyss.geary.systems.query.ListenerQuery -class CustomModelDataToPrefabTracker : GearyListener() { - private val Pointers.key by get().whenSetOnTarget() - private val Pointers.item by get().whenSetOnTarget() - - override fun Pointers.handle() { - val customModelData = item.item.customModelData ?: return - gearyItems.migration.map[customModelData] = key +fun GearyModule.createCustomModelDataToPrefabTracker() = listener( + object : ListenerQuery() { + val key by get() + val item by get() + override fun ensure() = event.anySet(::key, ::item) } +).exec { + val customModelData = item.item.customModelData ?: return@exec + gearyItems.migration.map[customModelData] = key } diff --git a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/migration/SetItemMigrationSystem.kt b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/migration/SetItemMigrationSystem.kt index f697340..fc6d79d 100644 --- a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/migration/SetItemMigrationSystem.kt +++ b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/migration/SetItemMigrationSystem.kt @@ -1,20 +1,23 @@ package com.mineinabyss.geary.papermc.tracking.items.migration +import com.mineinabyss.geary.modules.GearyModule import com.mineinabyss.geary.papermc.tracking.items.components.SetItem import com.mineinabyss.geary.papermc.tracking.items.components.SetItemIgnoredProperties -import com.mineinabyss.geary.systems.GearyListener -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.systems.builders.listener +import com.mineinabyss.geary.systems.query.ListenerQuery import com.mineinabyss.idofront.serialization.BaseSerializableItemStack import org.bukkit.inventory.ItemStack import java.util.* -class SetItemMigrationSystem : GearyListener() { - private val Pointers.setItem by get().whenSetOnTarget() - private val Pointers.item by get().whenSetOnTarget() - private val Pointers.overrides by get().orNull().on(target) - override fun Pointers.handle() { - val ignoredProperties = - overrides?.ignoreAsEnumSet() ?: EnumSet.noneOf(BaseSerializableItemStack.Properties::class.java) - setItem.item.toItemStack(applyTo = item, ignoredProperties) +fun GearyModule.createItemMigrationListener() = listener( + object : ListenerQuery() { + val setItem by get() + val item by get() + val overrides by get().orNull() + override fun ensure() = event.anySet(::setItem, ::item) } +).exec { + val ignoredProperties = + overrides?.ignoreAsEnumSet() ?: EnumSet.noneOf(BaseSerializableItemStack.Properties::class.java) + setItem.item.toItemStack(applyTo = item, ignoredProperties) } diff --git a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/systems/InventoryTrackerSystem.kt b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/systems/InventoryTrackerSystem.kt index 58f94b6..44105d4 100644 --- a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/systems/InventoryTrackerSystem.kt +++ b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/systems/InventoryTrackerSystem.kt @@ -2,10 +2,11 @@ package com.mineinabyss.geary.papermc.tracking.items.systems +import com.mineinabyss.geary.modules.GearyModule import com.mineinabyss.geary.papermc.tracking.items.cache.PlayerItemCache import com.mineinabyss.geary.papermc.tracking.items.inventory.toGeary -import com.mineinabyss.geary.systems.RepeatingSystem -import com.mineinabyss.geary.systems.accessors.Pointer +import com.mineinabyss.geary.systems.builders.system +import com.mineinabyss.geary.systems.query.Query import com.mineinabyss.idofront.time.ticks import org.bukkit.entity.Player @@ -21,11 +22,11 @@ import org.bukkit.entity.Player * - If an item isn't in our cache, we check the mismatches or deserialize it into the cache. * - All valid items get re-serialized TODO in the future there should be some form of dirty tag so we aren't unnecessarily serializing things */ -class InventoryTrackerSystem : RepeatingSystem(interval = 1.ticks) { - private val Pointer.player by get() - private val Pointer.itemCache by get>() - - override fun Pointer.tick() { - player.inventory.toGeary()?.forceRefresh() +fun GearyModule.createInventoryTrackerSystem() = system( + object : Query() { + val player by get() + val itemCache by get>() } +).every(1.ticks).exec { + player.inventory.toGeary()?.forceRefresh() } diff --git a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/systems/MythicMobDropSystem.kt b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/systems/MythicMobDropSystem.kt index 0fc11fc..4c0aa53 100644 --- a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/systems/MythicMobDropSystem.kt +++ b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/systems/MythicMobDropSystem.kt @@ -1,6 +1,7 @@ package com.mineinabyss.geary.papermc.tracking.items.systems import com.mineinabyss.geary.papermc.tracking.items.gearyItems +import com.mineinabyss.geary.papermc.tracking.items.helpers.GearyItemPrefabQuery.Companion.getKeys import com.mineinabyss.geary.prefabs.PrefabKey import io.lumine.mythic.bukkit.adapters.BukkitItemStack import io.lumine.mythic.bukkit.events.MythicDropLoadEvent diff --git a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/systems/PeriodicSaveSystem.kt b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/systems/PeriodicSaveSystem.kt index 36a0038..25de5f8 100644 --- a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/systems/PeriodicSaveSystem.kt +++ b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/systems/PeriodicSaveSystem.kt @@ -1,19 +1,20 @@ package com.mineinabyss.geary.papermc.tracking.items.systems import com.mineinabyss.geary.components.relations.Persists +import com.mineinabyss.geary.modules.GearyModule import com.mineinabyss.geary.papermc.datastore.encode -import com.mineinabyss.geary.systems.RepeatingSystem -import com.mineinabyss.geary.systems.accessors.Pointer +import com.mineinabyss.geary.systems.builders.system +import com.mineinabyss.geary.systems.query.Query import com.mineinabyss.idofront.items.editItemMeta import org.bukkit.inventory.ItemStack import kotlin.time.Duration.Companion.seconds -class PeriodicSaveSystem : RepeatingSystem(interval = 5.seconds) { - private val Pointer.persisting by getRelationsWithData() - private val Pointer.item by get() - - //TODO better serialization logic than hash - override fun Pointer.tick() { +fun GearyModule.createPeriodicSaveSystem() = system( + object : Query() { + val persisting by getRelationsWithData() + val item by get() + } +).every(5.seconds).exec { // val forceSave = every(iterations = 100) // // if (forceSave) { @@ -21,13 +22,12 @@ class PeriodicSaveSystem : RepeatingSystem(interval = 5.seconds) { // return // } - item.editItemMeta { - persisting.forEach { - val newHash = it.targetData.hashCode() - if (newHash != it.data.hash) { - it.data.hash = newHash - persistentDataContainer.encode(it.targetData) - } + item.editItemMeta { + persisting.forEach { + val newHash = it.targetData.hashCode() + if (newHash != it.data.hash) { + it.data.hash = newHash + persistentDataContainer.encode(it.targetData) } } } diff --git a/geary-tests/src/test/kotlin/com/mineinabyss/geary/papermc/config/VariablesConfigTest.kt b/geary-tests/src/test/kotlin/com/mineinabyss/geary/papermc/config/VariablesConfigTest.kt index 2519736..8d82028 100644 --- a/geary-tests/src/test/kotlin/com/mineinabyss/geary/papermc/config/VariablesConfigTest.kt +++ b/geary-tests/src/test/kotlin/com/mineinabyss/geary/papermc/config/VariablesConfigTest.kt @@ -19,7 +19,8 @@ import com.mineinabyss.geary.serialization.formats.YamlFormat import com.mineinabyss.geary.serialization.serializers.GearyEntitySerializer import com.mineinabyss.geary.serialization.serializers.SerializableGearyEntity import com.mineinabyss.geary.systems.GearyListener -import com.mineinabyss.geary.systems.accessors.Pointers +import com.mineinabyss.geary.systems.builders.listener +import com.mineinabyss.geary.systems.query.ListenerQuery import com.mineinabyss.idofront.di.DI import io.kotest.matchers.nulls.shouldNotBeNull import io.kotest.matchers.shouldBe @@ -78,21 +79,18 @@ class VariablesConfigTest { // arrange val prefab = yamlFormat.decodeFromString(serializer, config) var result: Int? = null - val skillSystem = object : GearyListener() { - val Pointers.input1 by get().on(source) - override fun Pointers.handle() { - result = input1.input.get(this) - } + geary.listener(object : ListenerQuery() { + val input1 by source.get() + }).exec { + result = input1.input.get(this) } - val generateIntSystem = object : GearyListener() { - val Pointers.comp by get().on(source) - override fun Pointers.handle() { - event.entity.set(comp.generate) - } + geary.listener(object : ListenerQuery() { + val comp by source.get() + }).exec { + event.entity.set(comp.generate) } - geary.pipeline.addSystems(skillSystem, generateIntSystem) // act EventHelpers.runSkill(entity { extend(prefab) }) @@ -208,14 +206,12 @@ class VariablesConfigTest { val prefab = yamlFormat.decodeFromString(serializer, config) var result: GearyEntity? = null - val skillSystem = object : GearyListener() { - val Pointers.comp by get().on(source) - override fun Pointers.handle() { - result = comp.entity.get(this) - } + geary.listener(object : ListenerQuery() { + val comp by source.get() + }).exec { + result = comp.entity.get(this) } - geary.pipeline.addSystems(skillSystem) // act EventHelpers.runSkill(entity { extend(prefab) }) diff --git a/geary-tests/src/test/kotlin/com/mineinabyss/geary/papermc/helpers/MockedServerTest.kt b/geary-tests/src/test/kotlin/com/mineinabyss/geary/papermc/helpers/MockedServerTest.kt index 989c9c8..48b0e17 100644 --- a/geary-tests/src/test/kotlin/com/mineinabyss/geary/papermc/helpers/MockedServerTest.kt +++ b/geary-tests/src/test/kotlin/com/mineinabyss/geary/papermc/helpers/MockedServerTest.kt @@ -8,6 +8,9 @@ import com.mineinabyss.idofront.di.DI import org.junit.jupiter.api.AfterAll abstract class MockedServerTest { + init { + MockBukkit.unmock() + } val server = MockBukkit.mock() val plugin = MockBukkit.createMockPlugin("Geary") val world = server.addSimpleWorld("world") diff --git a/geary-tests/src/test/kotlin/com/mineinabyss/geary/papermc/tracking/entities/EntityTrackingTests.kt b/geary-tests/src/test/kotlin/com/mineinabyss/geary/papermc/tracking/entities/EntityTrackingTests.kt index 28b4e22..4ce1361 100644 --- a/geary-tests/src/test/kotlin/com/mineinabyss/geary/papermc/tracking/entities/EntityTrackingTests.kt +++ b/geary-tests/src/test/kotlin/com/mineinabyss/geary/papermc/tracking/entities/EntityTrackingTests.kt @@ -65,7 +65,7 @@ class EntityTrackingTests: MockedServerTest() { val pig = world.spawn(world.spawnLocation, Pig::class.java) // act - EntityAddToWorldEvent(pig, world).callEvent() // Not called by MockBukkit + EntityAddToWorldEvent(pig).callEvent() // Not called by MockBukkit // assert val gearyPig = pig.toGearyOrNull().shouldNotBeNull() @@ -105,7 +105,7 @@ class EntityTrackingTests: MockedServerTest() { @Test fun `should persist components on entities when chunk unloaded`() { val pig = world.spawn(world.spawnLocation, Pig::class.java) - EntityAddToWorldEvent(pig, world).callEvent() + EntityAddToWorldEvent(pig).callEvent() testPersistence( pig.toGeary(), diff --git a/geary-tests/src/test/kotlin/com/mineinabyss/geary/papermc/tracking/items/cache/PlayerItemCacheTest.kt b/geary-tests/src/test/kotlin/com/mineinabyss/geary/papermc/tracking/items/cache/PlayerItemCacheTest.kt index d76e4e6..decf702 100644 --- a/geary-tests/src/test/kotlin/com/mineinabyss/geary/papermc/tracking/items/cache/PlayerItemCacheTest.kt +++ b/geary-tests/src/test/kotlin/com/mineinabyss/geary/papermc/tracking/items/cache/PlayerItemCacheTest.kt @@ -17,6 +17,7 @@ import java.util.* class PlayerItemCacheTest { init { + MockBukkit.unmock() geary(TestEngineModule, TestEngineModule(reuseIDsAfterRemoval = false)) { install(Prefabs) } diff --git a/gradle.properties b/gradle.properties index 180d43a..22b77db 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ kotlin.code.style=official group=com.mineinabyss version=0.29 -idofrontVersion=0.22.0 -gearyVersion=0.25.3 +idofrontVersion=0.22.3 +gearyVersion=0.26.0-dev.0 diff --git a/settings.gradle.kts b/settings.gradle.kts index 3536ef0..0319b35 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -23,6 +23,7 @@ dependencyResolutionManagement { versionCatalogs { create("idofrontLibs") { from("com.mineinabyss:catalog:$idofrontVersion") + version("mockbukkit", "3.9.0") } create("gearyLibs").from("com.mineinabyss:geary-catalog:$gearyVersion") }