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

Improvements

This commit is contained in:
Will FP
2023-11-28 14:08:48 +00:00
parent 71453bacc4
commit f5f8f56be4
6 changed files with 36 additions and 36 deletions

View File

@@ -1,13 +1,13 @@
package com.willfp.ecomobs.category.spawning package com.willfp.ecomobs.category.spawning
import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.core.config.interfaces.Config import com.willfp.eco.core.config.interfaces.Config
import com.willfp.ecomobs.EcoMobsPlugin
import com.willfp.ecomobs.category.MobCategory import com.willfp.ecomobs.category.MobCategory
abstract class SpawnMethod( abstract class SpawnMethod(
val category: MobCategory, val category: MobCategory,
val config: Config, val config: Config,
protected val plugin: EcoPlugin protected val plugin: EcoMobsPlugin
) { ) {
private var isStarted = false private var isStarted = false

View File

@@ -1,8 +1,8 @@
package com.willfp.ecomobs.category.spawning package com.willfp.ecomobs.category.spawning
import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.core.config.interfaces.Config import com.willfp.eco.core.config.interfaces.Config
import com.willfp.eco.core.registry.KRegistrable import com.willfp.eco.core.registry.KRegistrable
import com.willfp.ecomobs.EcoMobsPlugin
import com.willfp.ecomobs.category.MobCategory import com.willfp.ecomobs.category.MobCategory
import com.willfp.libreforge.ViolationContext import com.willfp.libreforge.ViolationContext
@@ -10,7 +10,7 @@ abstract class SpawnMethodFactory(override val id: String) : KRegistrable {
abstract fun create( abstract fun create(
category: MobCategory, category: MobCategory,
config: Config, config: Config,
plugin: EcoPlugin, plugin: EcoMobsPlugin,
context: ViolationContext context: ViolationContext
): SpawnMethod ): SpawnMethod
} }

View File

@@ -1,9 +1,8 @@
package com.willfp.ecomobs.category.spawning.impl package com.willfp.ecomobs.category.spawning.impl
import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.core.config.interfaces.Config import com.willfp.eco.core.config.interfaces.Config
import com.willfp.eco.core.entities.Entities
import com.willfp.eco.util.randDouble import com.willfp.eco.util.randDouble
import com.willfp.ecomobs.EcoMobsPlugin
import com.willfp.ecomobs.category.MobCategory import com.willfp.ecomobs.category.MobCategory
import com.willfp.ecomobs.category.spawning.SpawnMethod import com.willfp.ecomobs.category.spawning.SpawnMethod
import com.willfp.ecomobs.category.spawning.SpawnMethodFactory import com.willfp.ecomobs.category.spawning.SpawnMethodFactory
@@ -16,18 +15,14 @@ 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.EventPriority
import org.bukkit.event.Listener import org.bukkit.event.Listener
import org.bukkit.event.entity.CreatureSpawnEvent
import org.bukkit.scheduler.BukkitTask import org.bukkit.scheduler.BukkitTask
object SpawnMethodFactoryCustom : SpawnMethodFactory("custom") { object SpawnMethodFactoryCustom : SpawnMethodFactory("custom") {
override fun create( override fun create(
category: MobCategory, category: MobCategory,
config: Config, config: Config,
plugin: EcoPlugin, plugin: EcoMobsPlugin,
context: ViolationContext context: ViolationContext
): SpawnMethod { ): SpawnMethod {
return SpawnMethodCustom(category, config, plugin, context) return SpawnMethodCustom(category, config, plugin, context)
@@ -36,7 +31,7 @@ object SpawnMethodFactoryCustom : SpawnMethodFactory("custom") {
class SpawnMethodCustom( class SpawnMethodCustom(
category: MobCategory, category: MobCategory,
config: Config, config: Config,
plugin: EcoPlugin, plugin: EcoMobsPlugin,
context: ViolationContext context: ViolationContext
) : SpawnMethod(category, config, plugin), Listener { ) : SpawnMethod(category, config, plugin), Listener {
private val spawnRate = plugin.configYml.getInt("custom-spawning.spawn-rate").toLong() private val spawnRate = plugin.configYml.getInt("custom-spawning.spawn-rate").toLong()

View File

@@ -1,32 +1,24 @@
package com.willfp.ecomobs.category.spawning.impl package com.willfp.ecomobs.category.spawning.impl
import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.core.config.interfaces.Config import com.willfp.eco.core.config.interfaces.Config
import com.willfp.eco.core.entities.Entities import com.willfp.ecomobs.EcoMobsPlugin
import com.willfp.ecomobs.category.MobCategory import com.willfp.ecomobs.category.MobCategory
import com.willfp.ecomobs.category.spawning.SpawnMethod import com.willfp.ecomobs.category.spawning.SpawnMethod
import com.willfp.ecomobs.category.spawning.SpawnMethodFactory import com.willfp.ecomobs.category.spawning.SpawnMethodFactory
import com.willfp.ecomobs.mob.SpawnReason
import com.willfp.libreforge.ViolationContext import com.willfp.libreforge.ViolationContext
import com.willfp.libreforge.enumValueOfOrNull
import org.bukkit.entity.EntityType
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
import org.bukkit.event.Listener
import org.bukkit.event.entity.CreatureSpawnEvent
object SpawnMethodFactoryNone : SpawnMethodFactory("replace") { object SpawnMethodFactoryNone : SpawnMethodFactory("replace") {
override fun create( override fun create(
category: MobCategory, category: MobCategory,
config: Config, config: Config,
plugin: EcoPlugin, plugin: EcoMobsPlugin,
context: ViolationContext context: ViolationContext
): SpawnMethod { ): SpawnMethod {
return SpawnMethodNone(category, config, plugin) return SpawnMethodNone(category, config, plugin)
} }
class SpawnMethodNone( class SpawnMethodNone(
category: MobCategory, config: Config, plugin: EcoPlugin category: MobCategory, config: Config, plugin: EcoMobsPlugin
) : SpawnMethod(category, config, plugin) { ) : SpawnMethod(category, config, plugin) {
override fun onStart() { override fun onStart() {

View File

@@ -1,8 +1,8 @@
package com.willfp.ecomobs.category.spawning.impl package com.willfp.ecomobs.category.spawning.impl
import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.core.config.interfaces.Config import com.willfp.eco.core.config.interfaces.Config
import com.willfp.eco.core.entities.Entities import com.willfp.eco.core.entities.Entities
import com.willfp.ecomobs.EcoMobsPlugin
import com.willfp.ecomobs.category.MobCategory import com.willfp.ecomobs.category.MobCategory
import com.willfp.ecomobs.category.spawning.SpawnMethod import com.willfp.ecomobs.category.spawning.SpawnMethod
import com.willfp.ecomobs.category.spawning.SpawnMethodFactory import com.willfp.ecomobs.category.spawning.SpawnMethodFactory
@@ -19,7 +19,7 @@ object SpawnMethodFactoryReplace : SpawnMethodFactory("replace") {
override fun create( override fun create(
category: MobCategory, category: MobCategory,
config: Config, config: Config,
plugin: EcoPlugin, plugin: EcoMobsPlugin,
context: ViolationContext context: ViolationContext
): SpawnMethod { ): SpawnMethod {
return SpawnMethodReplace(category, config, plugin) return SpawnMethodReplace(category, config, plugin)
@@ -28,7 +28,7 @@ object SpawnMethodFactoryReplace : SpawnMethodFactory("replace") {
class SpawnMethodReplace( class SpawnMethodReplace(
category: MobCategory, category: MobCategory,
config: Config, config: Config,
plugin: EcoPlugin plugin: EcoMobsPlugin
) : SpawnMethod(category, config, plugin), Listener { ) : SpawnMethod(category, config, plugin), Listener {
private val toReplace = config.getStrings("replace") private val toReplace = config.getStrings("replace")
.mapNotNull { enumValueOfOrNull<EntityType>(it.uppercase()) } .mapNotNull { enumValueOfOrNull<EntityType>(it.uppercase()) }

View File

@@ -1,10 +1,13 @@
package com.willfp.ecomobs.category.spawning.spawnpoints package com.willfp.ecomobs.category.spawning.spawnpoints
import com.github.benmanes.caffeine.cache.Caffeine import com.github.benmanes.caffeine.cache.Caffeine
import com.sun.tools.javac.jvm.ByteCodes.ret
import com.willfp.eco.core.EcoPlugin 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.Location
import org.bukkit.Material import org.bukkit.Material
import org.bukkit.World
import org.bukkit.block.data.Waterlogged import org.bukkit.block.data.Waterlogged
import org.bukkit.entity.Mob import org.bukkit.entity.Mob
import org.bukkit.entity.Player import org.bukkit.entity.Player
@@ -28,11 +31,14 @@ class SpawnPointGenerator(
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") private val maxMobs = plugin.configYml.getInt("custom-spawning.max-mobs-per-player")
fun generate(player: Player): Set<SpawnPoint> { fun generate(player: Player): Set<SpawnPoint> =
generate(player.location)
fun generate(location: Location): Set<SpawnPoint> {
val points = mutableSetOf<SpawnPoint>() val points = mutableSetOf<SpawnPoint>()
val mobsAroundPlayer = player.location.world val mobsAroundPlayer = location.world
.getNearbyEntities(player.location, radius.toDouble(), radius.toDouble(), radius.toDouble()) .getNearbyEntities(location, radius.toDouble(), radius.toDouble(), radius.toDouble())
.filterIsInstance<Mob>() .filterIsInstance<Mob>()
.size .size
@@ -41,27 +47,34 @@ class SpawnPointGenerator(
} }
for (i in 1..max) { for (i in 1..max) {
val point = generatePoint(player) ?: continue val point = generateAroundLocation(location) ?: continue
points.add(point) points.add(point)
} }
return points return points
} }
private fun generatePoint(player: Player): SpawnPoint? { private fun generateAroundLocation(location: Location): SpawnPoint? {
val playerLocation = player.location val world = location.world ?: return null
val world = playerLocation.world ?: return null
val bottomCorner = Int3( val bottomCorner = Int3(
playerLocation.x.toInt() - radius, playerLocation.y.toInt() - radius, playerLocation.z.toInt() - radius location.x.toInt() - radius,
location.y.toInt() - radius,
location.z.toInt() - radius
) )
val topCorner = Int3( val topCorner = Int3(
playerLocation.x.toInt() + radius, playerLocation.y.toInt() + radius, playerLocation.z.toInt() + radius location.x.toInt() + radius,
location.y.toInt() + radius,
location.z.toInt() + radius
) )
return generateInBox(world, bottomCorner, topCorner)
}
private fun generateInBox(world: World, corner1: Int3, corner2: Int3): SpawnPoint? {
var attempts = 0 var attempts = 0
val iterator = (bottomCorner..topCorner).randomIterator() val iterator = (corner1..corner2).randomIterator()
while (attempts < maxAttempts) { while (attempts < maxAttempts) {
attempts++ attempts++