From 09ea053ef79ab085aa43733009d4350dd4804222 Mon Sep 17 00:00:00 2001 From: Will FP Date: Tue, 28 Nov 2023 13:57:31 +0000 Subject: [PATCH] Improvements to custom spawning --- .../spawning/impl/SpawnMethodFactoryCustom.kt | 3 ++- .../spawning/spawnpoints/SpawnPoint.kt | 18 ++++++++++++++ .../spawnpoints/SpawnPointGenerator.kt | 24 +++++++++++++++++-- .../core-plugin/src/main/resources/config.yml | 3 +++ 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/category/spawning/impl/SpawnMethodFactoryCustom.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/category/spawning/impl/SpawnMethodFactoryCustom.kt index 46ed5d0..46137b5 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/category/spawning/impl/SpawnMethodFactoryCustom.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/category/spawning/impl/SpawnMethodFactoryCustom.kt @@ -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) } } } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/category/spawning/spawnpoints/SpawnPoint.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/category/spawning/spawnpoints/SpawnPoint.kt index 548cbd7..54adfad 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/category/spawning/spawnpoints/SpawnPoint.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/category/spawning/spawnpoints/SpawnPoint.kt @@ -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() diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/category/spawning/spawnpoints/SpawnPointGenerator.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/category/spawning/spawnpoints/SpawnPointGenerator.kt index 0863f9e..443fa68 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/category/spawning/spawnpoints/SpawnPointGenerator.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/category/spawning/spawnpoints/SpawnPointGenerator.kt @@ -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>() @@ -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 { val points = mutableSetOf() + val mobsAroundPlayer = player.location.world + .getNearbyEntities(player.location, radius.toDouble(), radius.toDouble(), radius.toDouble()) + .filterIsInstance() + .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) } diff --git a/eco-core/core-plugin/src/main/resources/config.yml b/eco-core/core-plugin/src/main/resources/config.yml index 79c545d..b4ab276 100644 --- a/eco-core/core-plugin/src/main/resources/config.yml +++ b/eco-core/core-plugin/src/main/resources/config.yml @@ -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 \ No newline at end of file