From adbb5faf7b3ea6eb5db89c4c09083c29ffe67ccc Mon Sep 17 00:00:00 2001 From: Auxilor Date: Tue, 7 Nov 2023 17:05:32 +0000 Subject: [PATCH] Added spawn totems --- .../com/willfp/ecomobs/EcoMobsPlugin.kt | 4 +- .../ecomobs/handler/SpawnTotemHandler.kt | 51 +++++++++++++++++++ .../kotlin/com/willfp/ecomobs/mob/EcoMob.kt | 5 ++ .../com/willfp/ecomobs/mob/SpawnTotem.kt | 15 ++++++ .../ecomobs/mob/impl/ConfigDrivenEcoMob.kt | 22 ++++++++ 5 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/handler/SpawnTotemHandler.kt create mode 100644 eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/mob/SpawnTotem.kt diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/EcoMobsPlugin.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/EcoMobsPlugin.kt index 59e843f..fa55b4c 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/EcoMobsPlugin.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/EcoMobsPlugin.kt @@ -11,6 +11,7 @@ import com.willfp.ecomobs.goals.entity.EntityGoalRandomTeleport import com.willfp.ecomobs.handler.DamageModifierHandler import com.willfp.ecomobs.handler.MountHandler import com.willfp.ecomobs.handler.SpawnEggHandler +import com.willfp.ecomobs.handler.SpawnTotemHandler import com.willfp.ecomobs.handler.VanillaCompatibilityHandlers import com.willfp.ecomobs.integrations.levelledmobs.IntegrationLevelledMobs import com.willfp.ecomobs.integrations.libsdisguises.IntegrationLibsDisguises @@ -45,7 +46,8 @@ class EcoMobsPlugin : LibreforgePlugin() { MountHandler(), VanillaCompatibilityHandlers(), DiscoverRecipeListener(this), - SpawnEggHandler(this) + SpawnEggHandler(this), + SpawnTotemHandler() ) } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/handler/SpawnTotemHandler.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/handler/SpawnTotemHandler.kt new file mode 100644 index 0000000..96e7cc6 --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/handler/SpawnTotemHandler.kt @@ -0,0 +1,51 @@ +package com.willfp.ecomobs.handler + +import com.willfp.ecomobs.mob.EcoMobs +import com.willfp.ecomobs.mob.SpawnReason +import com.willfp.ecomobs.mob.SpawnTotem +import com.willfp.libreforge.triggers.TriggerData +import org.bukkit.block.Block +import org.bukkit.event.EventHandler +import org.bukkit.event.Listener +import org.bukkit.event.block.BlockPlaceEvent + +class SpawnTotemHandler : Listener { + @EventHandler(ignoreCancelled = true) + fun handle(event: BlockPlaceEvent) { + val block = event.block + val blockBelow = block.getRelative(0, -1, 0) + val blockTwoBelow = block.getRelative(0, -2, 0) + val blockAbove = block.getRelative(0, 1, 0) + val blockTwoAbove = block.getRelative(0, 2, 0) + + val player = event.player + val location = block.location.add(0.0, 1.5, 0.0) + + val totemPositions = listOf( + SpawnTotem(block.type, blockBelow.type, blockTwoBelow.type), + SpawnTotem(blockTwoAbove.type, blockAbove.type, block.type), + SpawnTotem(blockAbove.type, block.type, blockBelow.type) + ) + + for (totem in totemPositions) { + for (mob in EcoMobs.values()) { + val options = mob.totemOptions ?: continue + + if (options.totem != totem) { + continue + } + + if (!options.conditions.areMetAndTrigger( + TriggerData( + player = player + ).dispatch(player) + ) + ) { + continue + } + + mob.spawn(location, SpawnReason.TOTEM) + } + } + } +} diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/mob/EcoMob.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/mob/EcoMob.kt index 4083bc0..d98bf21 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/mob/EcoMob.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/mob/EcoMob.kt @@ -39,6 +39,11 @@ interface EcoMob : KRegistrable { */ val spawnEgg: SpawnEgg? + /** + * The spawn totem options. + */ + val totemOptions: SpawnTotemOptions? + /** * Get a living mob from a bukkit mob. */ diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/mob/SpawnTotem.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/mob/SpawnTotem.kt new file mode 100644 index 0000000..e9de8ed --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/mob/SpawnTotem.kt @@ -0,0 +1,15 @@ +package com.willfp.ecomobs.mob + +import com.willfp.libreforge.conditions.ConditionList +import org.bukkit.Material + +data class SpawnTotem( + val top: Material, + val middle: Material, + val bottom: Material +) + +data class SpawnTotemOptions( + val conditions: ConditionList, + val totem: SpawnTotem +) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/mob/impl/ConfigDrivenEcoMob.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/mob/impl/ConfigDrivenEcoMob.kt index bf1ecc9..977a34b 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/mob/impl/ConfigDrivenEcoMob.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecomobs/mob/impl/ConfigDrivenEcoMob.kt @@ -33,6 +33,8 @@ import com.willfp.ecomobs.mob.EcoMob import com.willfp.ecomobs.mob.EcoMobs import com.willfp.ecomobs.mob.LivingMob import com.willfp.ecomobs.mob.SpawnReason +import com.willfp.ecomobs.mob.SpawnTotem +import com.willfp.ecomobs.mob.SpawnTotemOptions import com.willfp.ecomobs.mob.addGoal import com.willfp.ecomobs.mob.event.MobEvent import com.willfp.ecomobs.mob.event.MobEvents @@ -277,6 +279,26 @@ internal class ConfigDrivenEcoMob( ) } + override val totemOptions = config.getBool("spawn.totem.enabled").ifTrue { + val conditions = Conditions.compile( + config.getSubsections("spawn.totem.conditions"), + context.with("spawn totem conditions") + ) + + val top = Items.lookup(config.getString("spawn.totem.top")) + val middle = Items.lookup(config.getString("spawn.totem.middle")) + val bottom = Items.lookup(config.getString("spawn.totem.bottom")) + + SpawnTotemOptions( + conditions, + SpawnTotem( + top.item.type, + middle.item.type, + bottom.item.type + ) + ) + } + /* ---------- */