9
0
mirror of https://github.com/Auxilor/EcoMobs.git synced 2026-01-03 22:26:13 +00:00

Added natural spawn method

This commit is contained in:
Auxilor
2023-11-11 17:35:24 +00:00
parent adbb5faf7b
commit 6528e47222
11 changed files with 182 additions and 1 deletions

View File

@@ -1,6 +1,13 @@
package com.willfp.ecomobs.category
import com.willfp.eco.core.registry.KRegistrable
import com.willfp.ecomobs.category.spawning.SpawnMethod
import com.willfp.ecomobs.mob.EcoMob
import com.willfp.ecomobs.mob.EcoMobs
interface MobCategory : KRegistrable {
val mobs: List<EcoMob>
get() = EcoMobs.values().filter { it.category == this }
val spawnMethod: SpawnMethod
}

View File

@@ -3,6 +3,9 @@ package com.willfp.ecomobs.category.impl
import com.willfp.eco.core.config.interfaces.Config
import com.willfp.ecomobs.EcoMobsPlugin
import com.willfp.ecomobs.category.MobCategory
import com.willfp.ecomobs.category.spawning.SpawnMethodFactories
import com.willfp.ecomobs.config.ConfigViolationException
import com.willfp.libreforge.ConfigViolation
import com.willfp.libreforge.ViolationContext
class ConfigDrivenMobCategory(
@@ -11,4 +14,30 @@ class ConfigDrivenMobCategory(
private val config: Config,
private val context: ViolationContext
) : MobCategory {
override val spawnMethod = SpawnMethodFactories[config.getString("spawning.type")]
?.create(this, config.getSubsection("spawning.${config.getString("spawning.type")}"), plugin)
?: throw ConfigViolationException(
ConfigViolation(
"type",
"Invalid spawning type"
)
) {
it.with("spawning")
}
override fun onRegister() {
spawnMethod.start()
}
override fun onRemove() {
spawnMethod.stop()
}
override fun hashCode(): Int {
return id.hashCode()
}
override fun equals(other: Any?): Boolean {
return other is ConfigDrivenMobCategory && other.id == this.id
}
}

View File

@@ -0,0 +1,29 @@
package com.willfp.ecomobs.category.spawning
import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.core.config.interfaces.Config
import com.willfp.ecomobs.category.MobCategory
abstract class SpawnMethod(
val category: MobCategory,
val config: Config,
protected val plugin: EcoPlugin
) {
private var isStarted = false
fun start() {
if (isStarted) {
throw IllegalStateException("Already started!")
}
onStart()
}
fun stop() {
onStop()
}
protected abstract fun onStart()
protected abstract fun onStop()
}

View File

@@ -0,0 +1,10 @@
package com.willfp.ecomobs.category.spawning
import com.willfp.eco.core.registry.Registry
import com.willfp.ecomobs.category.spawning.impl.SpawnMethodFactoryNatural
object SpawnMethodFactories : Registry<SpawnMethodFactory>() {
init {
register(SpawnMethodFactoryNatural)
}
}

View File

@@ -0,0 +1,10 @@
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.registry.KRegistrable
import com.willfp.ecomobs.category.MobCategory
abstract class SpawnMethodFactory(override val id: String) : KRegistrable {
abstract fun create(category: MobCategory, config: Config, plugin: EcoPlugin): SpawnMethod
}

View File

@@ -0,0 +1,61 @@
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.entities.Entities
import com.willfp.ecomobs.category.MobCategory
import com.willfp.ecomobs.category.spawning.SpawnMethod
import com.willfp.ecomobs.category.spawning.SpawnMethodFactory
import com.willfp.ecomobs.mob.SpawnReason
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 SpawnMethodFactoryNatural : SpawnMethodFactory("natural") {
override fun create(category: MobCategory, config: Config, plugin: EcoPlugin): SpawnMethod {
return SpawnMethodNormal(category, config, plugin)
}
class SpawnMethodNormal(
category: MobCategory,
config: Config,
plugin: EcoPlugin
) : SpawnMethod(category, config, plugin), Listener {
private val toReplace = config.getStrings("replace")
.mapNotNull { enumValueOfOrNull<EntityType>(it.uppercase()) }
.toSet()
override fun onStart() {
plugin.eventManager.registerListener(this)
}
override fun onStop() {
plugin.eventManager.unregisterListener(this)
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
fun handle(event: CreatureSpawnEvent) {
// Ignore custom spawns
if (event.spawnReason == CreatureSpawnEvent.SpawnReason.CUSTOM) {
return
}
if (event.entityType !in toReplace) {
return
}
if (Entities.isCustomEntity(event.entity)) {
return
}
val mob = category.mobs.randomOrNull() ?: return
mob.spawn(event.entity.location, SpawnReason.NATURAL)
event.isCancelled = true
}
}
}

View File

@@ -1,6 +1,7 @@
package com.willfp.ecomobs.mob
import com.willfp.eco.core.config.interfaces.Config
import com.willfp.eco.core.entities.CustomEntity
import com.willfp.eco.core.registry.KRegistrable
import com.willfp.ecomobs.category.MobCategory
import com.willfp.ecomobs.integrations.MobIntegration
@@ -44,6 +45,11 @@ interface EcoMob : KRegistrable {
*/
val totemOptions: SpawnTotemOptions?
/**
* The eco custom entity.
*/
val customEntity: CustomEntity
/**
* Get a living mob from a bukkit mob.
*/

View File

@@ -26,6 +26,9 @@ object MobEventKill : MobEvent("kill") {
event = event
)
// Clear vanilla drops
event.deathEvent.drops.clear()
Bukkit.getPluginManager().callEvent(EcoMobKillEvent(living, player))
ecoMob.handleEvent(this, data.dispatch(player))

View File

@@ -1,6 +1,7 @@
package com.willfp.ecomobs.mob.impl
import com.willfp.eco.core.config.interfaces.Config
import com.willfp.eco.core.entities.CustomEntity
import com.willfp.eco.core.entities.Entities
import com.willfp.eco.core.entities.ai.EntityGoals
import com.willfp.eco.core.entities.ai.TargetGoals
@@ -57,6 +58,7 @@ import org.bukkit.Bukkit
import org.bukkit.Location
import org.bukkit.entity.Mob
import org.bukkit.entity.Player
import org.bukkit.event.entity.CreatureSpawnEvent
import org.bukkit.event.entity.EntityDamageEvent.DamageCause
import org.bukkit.inventory.EquipmentSlot
import org.bukkit.persistence.PersistentDataType
@@ -299,6 +301,13 @@ internal class ConfigDrivenEcoMob(
)
}
override val customEntity = CustomEntity(
plugin.createNamespacedKey(this.id),
{ (it as? Mob)?.ecoMob == this }
) {
this.spawn(it, SpawnReason.COMMAND)!!.entity
}
/*
----------
*/

View File

@@ -15,7 +15,6 @@ import org.bukkit.Location
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
import org.bukkit.persistence.PersistentDataType
import sun.jvm.hotspot.oops.CellTypeState.value
class SpawnEgg internal constructor(
val mob: EcoMob,

View File

@@ -3,3 +3,21 @@
# You can place categories anywhere in this folder,
# including in subfolders if you want to organize your category configs
# _example.yml is not loaded.
spawning:
# Available methods:
# natural
# Uses the vanilla spawning system to spawn the mob, by replacing
# the vanilla mob with the custom mob when it spawns.
# custom
# Uses the EcoMobs spawning system.
type: natural
# Options for natural spawning
natural:
# The vanilla mobs to replace
replace:
- zombie
- skeleton