9
0
mirror of https://github.com/Auxilor/EcoMobs.git synced 2025-12-19 15:09:17 +00:00

Improvements to custom spawning

This commit is contained in:
Will FP
2023-11-28 13:57:31 +00:00
parent 7c162f5565
commit 09ea053ef7
4 changed files with 45 additions and 3 deletions

View File

@@ -16,6 +16,7 @@ import com.willfp.libreforge.conditions.Conditions
import com.willfp.libreforge.enumValueOfOrNull
import com.willfp.libreforge.toDispatcher
import org.bukkit.Bukkit
import org.bukkit.entity.LivingEntity
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
import org.bukkit.event.Listener
@@ -76,7 +77,7 @@ object SpawnMethodFactoryCustom : SpawnMethodFactory("custom") {
continue
}
mob.spawn(point.location, SpawnReason.NATURAL)
point.spawn(mob, SpawnReason.NATURAL)
}
}
}

View File

@@ -1,11 +1,29 @@
package com.willfp.ecomobs.category.spawning.spawnpoints
import com.willfp.ecomobs.mob.EcoMob
import com.willfp.ecomobs.mob.LivingMob
import com.willfp.ecomobs.mob.SpawnReason
import org.bukkit.Location
class SpawnPoint(
val location: Location,
val type: SpawnPointType
) {
private var hasBeenUsed = false
/**
* Spawn a mob at this spawnpoint.
*/
fun spawn(mob: EcoMob, reason: SpawnReason): LivingMob? {
if (hasBeenUsed) {
return null
}
hasBeenUsed = true
return mob.spawn(location, reason)
}
override fun hashCode(): Int {
var result = location.hashCode()
result = 31 * result + type.hashCode()

View File

@@ -5,11 +5,13 @@ import com.willfp.eco.core.EcoPlugin
import com.willfp.ecomobs.math.Int3
import com.willfp.ecomobs.plugin
import org.bukkit.Material
import org.bukkit.block.data.Waterlogged
import org.bukkit.entity.Mob
import org.bukkit.entity.Player
import java.util.UUID
import java.util.concurrent.TimeUnit
val spawnPointCache = Caffeine.newBuilder()
private val spawnPointCache = Caffeine.newBuilder()
.expireAfterWrite(2, TimeUnit.SECONDS)
.build<UUID, Set<SpawnPoint>>()
@@ -24,10 +26,20 @@ class SpawnPointGenerator(
private val radius = plugin.configYml.getInt("custom-spawning.radius-around-player")
private val max = plugin.configYml.getInt("custom-spawning.max-points-per-player")
private val maxAttempts = plugin.configYml.getInt("custom-spawning.max-attempts")
private val maxMobs = plugin.configYml.getInt("custom-spawning.max-mobs-per-player")
fun generate(player: Player): Set<SpawnPoint> {
val points = mutableSetOf<SpawnPoint>()
val mobsAroundPlayer = player.location.world
.getNearbyEntities(player.location, radius.toDouble(), radius.toDouble(), radius.toDouble())
.filterIsInstance<Mob>()
.size
if (mobsAroundPlayer >= maxMobs) {
return points
}
for (i in 1..max) {
val point = generatePoint(player) ?: continue
points.add(point)
@@ -63,10 +75,18 @@ class SpawnPointGenerator(
continue
}
if (block.isPassable && blockAbove.isPassable && blockBelow.type.isSolid) {
// Handle land with a massive boolean expression
if (
block.isPassable &&
!block.isLiquid && block.blockData !is Waterlogged &&
blockAbove.isPassable &&
!blockAbove.isLiquid && blockAbove.blockData !is Waterlogged &&
blockBelow.type.isSolid
) {
return SpawnPoint(block.location.add(0.5, 0.5, 0.5), SpawnPointType.LAND)
}
// Handle water
if (block.type == Material.WATER && blockAbove.type == Material.WATER) {
return SpawnPoint(block.location.add(0.5, 0.5, 0.5), SpawnPointType.WATER)
}

View File

@@ -16,5 +16,8 @@ custom-spawning:
# The max amount of spawn points to generate per player
max-points-per-player: 8
# The max amount of mobs to spawn per player
max-mobs-per-player: 24
# The max amount of attempts to generate a spawn point per player
max-attempts: 64