Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
chore(spawning): Inline check.spawn.gap
Browse files Browse the repository at this point in the history
chore(spawning): Clean up SpawnRegistry to read from a query once in reloadSpawns() instead of using a listener
  • Loading branch information
0ffz committed Mar 28, 2024
1 parent b6c5e5a commit 6b75cc8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[versions]
gearyPaper = "0.30.0"
gearyPaper = "0.30.1-dev.5"

[libraries]
geary-papermc = { module = "com.mineinabyss:geary-papermc", version.ref = "gearyPaper" }
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import com.mineinabyss.geary.datatypes.GearyEntity
import com.mineinabyss.geary.modules.geary
import com.mineinabyss.geary.prefabs.prefabs
import com.mineinabyss.geary.systems.builders.cachedQuery
import com.mineinabyss.geary.systems.builders.listener
import com.mineinabyss.geary.systems.query.GearyQuery
import com.mineinabyss.geary.systems.query.ListenerQuery
import com.mineinabyss.geary.systems.query.Query
import com.sk89q.worldguard.WorldGuard
import com.sk89q.worldguard.protection.regions.ProtectedRegion

Expand All @@ -18,33 +17,42 @@ import com.sk89q.worldguard.protection.regions.ProtectedRegion
class SpawnRegistry {
private val prefabLoader get() = prefabs.loader

private val spawnsWithWGRegion = geary.cachedQuery(object : Query() {
val parentRegions by get<WGRegions>()
val spawn by get<SpawnType>()
})

private val regionContainer = WorldGuard.getInstance().platform.regionContainer
private val regionSpawns: MutableMap<String, MutableSet<GearyEntity>> = HashMap()
private var regionSpawns: Map<String, MutableSet<GearyEntity>> = mapOf()
val spawnConfigsQuery = geary.cachedQuery(SpawnConfigs())

/** Clears [regionSpawns] */
fun unregisterSpawns() = regionSpawns.clear()
fun unregisterSpawns() { regionSpawns = mapOf() }

fun reloadSpawns() {
unregisterSpawns()
spawnConfigsQuery.entities().forEach {
prefabLoader.reload(it)
runCatching {
prefabLoader.reload(it)
}.onFailure {
it.printStackTrace()
}
}
val map = mutableMapOf<String, MutableSet<GearyEntity>>()
spawnsWithWGRegion.mapWithEntity {
parentRegions.keys
}.forEach {
it.data.forEach { regionName ->
map.getOrPut(regionName) { mutableSetOf() }.add(it.entity)
}
}
regionSpawns = map
}

/** Takes a list of spawn region names and converts to a list of [SpawnDefinition]s from those regions */
fun List<ProtectedRegion>.getMobSpawnsForRegions(): List<GearyEntity> =
flatMap { regionSpawns[it.id] ?: setOf() }

val spawnTracker = geary.listener(object : ListenerQuery() {
val parentRegions by get<WGRegions>()
val spawn by get<SpawnType>()
override fun ensure() = event.anySet(::parentRegions, ::spawn)
}).exec {
parentRegions.keys.forEach {
regionSpawns.getOrPut(it) { mutableSetOf() } += entity
}
}

class SpawnConfigs : GearyQuery() {
val config by get<SpawnType>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.mineinabyss.mobzy.spawning.conditions.components

import com.mineinabyss.geary.autoscan.AutoScan
import com.mineinabyss.geary.modules.GearyModule
import com.mineinabyss.geary.serialization.serializers.InnerSerializer
import com.mineinabyss.geary.systems.builders.listener
import com.mineinabyss.geary.systems.query.ListenerQuery
import com.mineinabyss.idofront.serialization.IntRangeSerializer
Expand All @@ -14,12 +15,18 @@ import kotlinx.serialization.Serializable
*
* Ensures that when a mob spawn happens, the gap of air blocks is within a [range] of heights.
*/
@Serializable
@SerialName("mobzy:check.spawn.gap")
@Serializable(with = SpawnGap.Serializer::class)
class SpawnGap(
@Serializable(with = IntRangeSerializer::class)
val range: IntRange
)
) {
class Serializer : InnerSerializer<IntRange, SpawnGap>(
"mobzy:check.spawn.gap",
IntRangeSerializer,
{ SpawnGap(it) },
{ it.range },
)
}

@AutoScan
fun GearyModule.spawnGroupChecker() = listener(object : ListenerQuery() {
Expand Down

0 comments on commit 6b75cc8

Please sign in to comment.