mirror of
https://github.com/Auxilor/EcoMobs.git
synced 2025-12-19 23:19:17 +00:00
Improvements to custom spawning
This commit is contained in:
@@ -16,6 +16,7 @@ import com.willfp.libreforge.conditions.Conditions
|
|||||||
import com.willfp.libreforge.enumValueOfOrNull
|
import com.willfp.libreforge.enumValueOfOrNull
|
||||||
import com.willfp.libreforge.toDispatcher
|
import com.willfp.libreforge.toDispatcher
|
||||||
import org.bukkit.Bukkit
|
import org.bukkit.Bukkit
|
||||||
|
import org.bukkit.entity.LivingEntity
|
||||||
import org.bukkit.event.EventHandler
|
import org.bukkit.event.EventHandler
|
||||||
import org.bukkit.event.EventPriority
|
import org.bukkit.event.EventPriority
|
||||||
import org.bukkit.event.Listener
|
import org.bukkit.event.Listener
|
||||||
@@ -76,7 +77,7 @@ object SpawnMethodFactoryCustom : SpawnMethodFactory("custom") {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
mob.spawn(point.location, SpawnReason.NATURAL)
|
point.spawn(mob, SpawnReason.NATURAL)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,29 @@
|
|||||||
package com.willfp.ecomobs.category.spawning.spawnpoints
|
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
|
import org.bukkit.Location
|
||||||
|
|
||||||
class SpawnPoint(
|
class SpawnPoint(
|
||||||
val location: Location,
|
val location: Location,
|
||||||
val type: SpawnPointType
|
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 {
|
override fun hashCode(): Int {
|
||||||
var result = location.hashCode()
|
var result = location.hashCode()
|
||||||
result = 31 * result + type.hashCode()
|
result = 31 * result + type.hashCode()
|
||||||
|
|||||||
@@ -5,11 +5,13 @@ import com.willfp.eco.core.EcoPlugin
|
|||||||
import com.willfp.ecomobs.math.Int3
|
import com.willfp.ecomobs.math.Int3
|
||||||
import com.willfp.ecomobs.plugin
|
import com.willfp.ecomobs.plugin
|
||||||
import org.bukkit.Material
|
import org.bukkit.Material
|
||||||
|
import org.bukkit.block.data.Waterlogged
|
||||||
|
import org.bukkit.entity.Mob
|
||||||
import org.bukkit.entity.Player
|
import org.bukkit.entity.Player
|
||||||
import java.util.UUID
|
import java.util.UUID
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
val spawnPointCache = Caffeine.newBuilder()
|
private val spawnPointCache = Caffeine.newBuilder()
|
||||||
.expireAfterWrite(2, TimeUnit.SECONDS)
|
.expireAfterWrite(2, TimeUnit.SECONDS)
|
||||||
.build<UUID, Set<SpawnPoint>>()
|
.build<UUID, Set<SpawnPoint>>()
|
||||||
|
|
||||||
@@ -24,10 +26,20 @@ class SpawnPointGenerator(
|
|||||||
private val radius = plugin.configYml.getInt("custom-spawning.radius-around-player")
|
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 max = plugin.configYml.getInt("custom-spawning.max-points-per-player")
|
||||||
private val maxAttempts = plugin.configYml.getInt("custom-spawning.max-attempts")
|
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> {
|
fun generate(player: Player): Set<SpawnPoint> {
|
||||||
val points = mutableSetOf<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) {
|
for (i in 1..max) {
|
||||||
val point = generatePoint(player) ?: continue
|
val point = generatePoint(player) ?: continue
|
||||||
points.add(point)
|
points.add(point)
|
||||||
@@ -63,10 +75,18 @@ class SpawnPointGenerator(
|
|||||||
continue
|
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)
|
return SpawnPoint(block.location.add(0.5, 0.5, 0.5), SpawnPointType.LAND)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle water
|
||||||
if (block.type == Material.WATER && blockAbove.type == Material.WATER) {
|
if (block.type == Material.WATER && blockAbove.type == Material.WATER) {
|
||||||
return SpawnPoint(block.location.add(0.5, 0.5, 0.5), SpawnPointType.WATER)
|
return SpawnPoint(block.location.add(0.5, 0.5, 0.5), SpawnPointType.WATER)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,5 +16,8 @@ custom-spawning:
|
|||||||
# The max amount of spawn points to generate per player
|
# The max amount of spawn points to generate per player
|
||||||
max-points-per-player: 8
|
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
|
# The max amount of attempts to generate a spawn point per player
|
||||||
max-attempts: 64
|
max-attempts: 64
|
||||||
Reference in New Issue
Block a user