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

Commit

Permalink
feat: Forced despawn when far away component
Browse files Browse the repository at this point in the history
chore(spawning): Don't spawn in unloaded chunks
  • Loading branch information
0ffz committed Apr 1, 2024
1 parent 692316f commit ac61a68
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.mineinabyss.mobzy.features.despawning

import com.mineinabyss.geary.modules.GearyModule
import com.mineinabyss.geary.papermc.tracking.entities.toGeary
import com.mineinabyss.geary.systems.builders.system
import com.mineinabyss.geary.systems.query.Query
import com.mineinabyss.idofront.typealiases.BukkitEntity
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.world.EntitiesUnloadEvent
import kotlin.time.Duration.Companion.seconds

class DespawnSystems : Listener {
//TODO this event fires when chunk unloaded but at ChunkUnloadEvent there are no entities in the chunk???
@EventHandler
fun EntitiesUnloadEvent.removeCustomOnChunkUnload() {
if (!chunk.isLoaded) return

for (entity in chunk.entities) {
val removeOnUnload = entity.toGeary().get<RemoveOnChunkUnload>() ?: continue
if (!(removeOnUnload.keepIfRenamed && entity.customName() != null))
entity.remove()
}
}
}

fun GearyModule.removeWhenFarAway() = system(object : Query() {
val bukkit by get<BukkitEntity>()
val remove by get<RemoveWhenFarAway>()
}).every(3.seconds).exec {
if (bukkit.customName == null && bukkit.world.getNearbyPlayers(bukkit.location, remove.distance).isEmpty())
bukkit.remove()
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import kotlinx.serialization.Serializable
* Specifies this entity should be completely removed when the chunk it is in unloads.
*/
@Serializable
@SerialName("mobzy:remove_on_chunk_unload")
@SerialName("mobzy:despawn.on_chunk_unload")
data class RemoveOnChunkUnload(
val keepIfRenamed: Boolean = true
)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.mineinabyss.mobzy.features.despawning

import kotlinx.serialization.EncodeDefault
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
@SerialName("mobzy:despawn.when_far_away")
data class RemoveWhenFarAway(
@EncodeDefault(EncodeDefault.Mode.NEVER)
val keepIfRenamed: Boolean = true,
@EncodeDefault(EncodeDefault.Mode.NEVER)
val distance: Double = 128.0,
)
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,7 @@ fun GearyModule.spawnRequestListener() = listener(object : ListenerQuery() {
val prefab = type.prefab.toEntity()
val boundingBox = prefab.get<BoundingBox>()

// Original location always gets a spawn, we assume all conditions, including no suffocation are met there.
if (spawns > 0) {
location.spawnFromPrefab(prefab).onSuccess { spawnCount++ }
}

// Other locations only need to meet the suffocation condition
repeat(spawns - 1) {
repeat(spawns) {
val chosenLoc =
if (spawnPos != SpawnPosition.AIR)
getSpawnInRadius(location, radius) ?: location
Expand All @@ -166,7 +160,7 @@ fun GearyModule.spawnRequestListener() = listener(object : ListenerQuery() {
event.entity.set(
Spawned(
spawnCount,
prefab.collectPrefabs().intersect(mobzySpawning.spawnRegistry.spawnCategories.keys)
prefab.collectPrefabs()
)
)
}
Expand All @@ -185,7 +179,6 @@ fun ensureSuitableLocationOrNull(
bb: BoundingBox,
extraAttemptsUp: Int = mobzySpawning.config.retriesUpWhenInsideBlock
): Location? {
return chosenLoc
val bb = bb.clone()
// We shrink the box by a bit since overlap checks are strict inequalities
val bbShrunk = bb.clone().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ object SpawnableChunks {
}
}
return positions
.filter { (x, z) -> world.isChunkLoaded(x, z) }
.map { (x, z) -> world.getChunkAt(x, z) }
}
}
9 changes: 7 additions & 2 deletions src/main/kotlin/com/mineinabyss/mobzy/MobzyPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import com.mineinabyss.idofront.messaging.observeLogger
import com.mineinabyss.idofront.plugin.Plugins
import com.mineinabyss.idofront.plugin.actions
import com.mineinabyss.idofront.plugin.listeners
import com.mineinabyss.mobzy.features.despawning.RemoveOnChunkUnloadSystem
import com.mineinabyss.mobzy.features.despawning.DespawnSystems
import com.mineinabyss.mobzy.features.despawning.removeWhenFarAway
import com.mineinabyss.mobzy.features.displayname.ShowDisplayNameOnKillerSystem
import com.mineinabyss.mobzy.features.prevent.breeding.PreventBreedingSystem
import com.mineinabyss.mobzy.features.prevent.interaction.PreventInteractionSystem
Expand Down Expand Up @@ -58,7 +59,7 @@ class MobzyPlugin : JavaPlugin() {

listeners(
PreventBreedingSystem(),
RemoveOnChunkUnloadSystem(),
DespawnSystems(),
ShowDisplayNameOnKillerSystem(),
TamingBukkitListener(),
PreventRidingSystem(),
Expand All @@ -67,6 +68,10 @@ class MobzyPlugin : JavaPlugin() {
OverrideMobSoundsBukkitListener(),
)

geary.apply {
removeWhenFarAway()
}

geary {
if (Plugins.isEnabled("ModelEngine")) {
install(ModelEngineSupport)
Expand Down

0 comments on commit ac61a68

Please sign in to comment.