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:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
/*
|
||||
----------
|
||||
*/
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user