Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TriggerPlaceholders configuration and some optimization #161

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ItemRefreshListener(

@EventHandler(priority = EventPriority.LOWEST)
fun onItemPickup(event: EntityPickupItemEvent) {
if (!plugin.configYml.getBool("refresh.pickup.enabled")) return
if (plugin.configYml.getBool("refresh.pickup.require-meta")) {
if (!event.item.itemStack.hasItemMeta()) {
return
Expand All @@ -47,6 +48,13 @@ class ItemRefreshListener(

@EventHandler(priority = EventPriority.HIGHEST)
fun onInventoryDrop(event: PlayerDropItemEvent) {
if (!plugin.configYml.getBool("refresh.drop.enabled")) return
if (plugin.configYml.getBool("refresh.drop.require-meta")) {
if (!event.itemDrop.itemStack.hasItemMeta()) {
return
}
}

event.player.toDispatcher().refreshHolders()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.willfp.eco.core.integrations.IntegrationLoader
import com.willfp.eco.core.integrations.afk.AFKManager
import com.willfp.libreforge.commands.CommandLibreforge
import com.willfp.libreforge.configs.ChainsYml
import com.willfp.libreforge.configs.TriggerPlaceholdersYml
import com.willfp.libreforge.configs.lrcdb.CommandLrcdb
import com.willfp.libreforge.display.ItemFlagDisplay
import com.willfp.libreforge.effects.Effects
Expand Down Expand Up @@ -43,6 +44,7 @@ internal lateinit var plugin: LibreforgeSpigotPlugin

class LibreforgeSpigotPlugin : EcoPlugin() {
val chainsYml = ChainsYml(this)
val triggersPlaceholdersYml = TriggerPlaceholdersYml(this)

val dispatchedTriggerFactory = DispatchedTriggerFactory(this)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.willfp.libreforge.arguments
import com.willfp.libreforge.conditions.Condition
import com.willfp.libreforge.get
import com.willfp.libreforge.toDispatcher
import com.willfp.libreforge.triggers.hasCondition
import com.willfp.libreforge.updateEffects
import org.bukkit.attribute.Attribute
import org.bukkit.entity.LivingEntity
Expand Down Expand Up @@ -38,11 +39,19 @@ object ConditionAboveHealthPercent : Condition<NoCompileData>("above_health_perc

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
fun handle(event: EntityRegainHealthEvent) {
event.entity.toDispatcher().updateEffects()
val entity = event.entity.toDispatcher()

if (!entity.hasCondition(this)) return

entity.updateEffects()
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
fun handle(event: EntityDamageEvent) {
event.entity.toDispatcher().updateEffects()
val entity = event.entity.toDispatcher()

if (!entity.hasCondition(this)) return

entity.updateEffects()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.willfp.libreforge.arguments
import com.willfp.libreforge.conditions.Condition
import com.willfp.libreforge.get
import com.willfp.libreforge.toDispatcher
import com.willfp.libreforge.triggers.hasCondition
import com.willfp.libreforge.updateEffects
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
Expand All @@ -33,6 +34,10 @@ object ConditionAboveHungerPercent : Condition<NoCompileData>("above_hunger_perc

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
fun handle(event: FoodLevelChangeEvent) {
event.entity.toDispatcher().updateEffects()
val entity = event.entity.toDispatcher()

if (!entity.hasCondition(this)) return

entity.updateEffects()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.willfp.libreforge.arguments
import com.willfp.libreforge.conditions.Condition
import com.willfp.libreforge.get
import com.willfp.libreforge.toDispatcher
import com.willfp.libreforge.triggers.hasCondition
import com.willfp.libreforge.updateEffects
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
Expand All @@ -33,6 +34,10 @@ object ConditionAboveXPLevel : Condition<NoCompileData>("above_xp_level") {

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
fun handle(event: PlayerExpChangeEvent) {
event.player.toDispatcher().updateEffects()
val player = event.player.toDispatcher()

if (!player.hasCondition(this)) return

player.updateEffects()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.willfp.libreforge.arguments
import com.willfp.libreforge.conditions.Condition
import com.willfp.libreforge.get
import com.willfp.libreforge.toDispatcher
import com.willfp.libreforge.triggers.hasCondition
import com.willfp.libreforge.updateEffects
import org.bukkit.attribute.Attribute
import org.bukkit.entity.LivingEntity
Expand Down Expand Up @@ -38,11 +39,21 @@ object ConditionBelowHealthPercent : Condition<NoCompileData>("below_health_perc

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
fun handle(event: EntityRegainHealthEvent) {
event.entity.toDispatcher().updateEffects()

val entity = event.entity.toDispatcher()

if (!entity.hasCondition(this)) return

entity.updateEffects()
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
fun handle(event: EntityDamageEvent) {
event.entity.toDispatcher().updateEffects()

val entity = event.entity.toDispatcher()

if (!entity.hasCondition(this)) return

entity.updateEffects()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.willfp.libreforge.arguments
import com.willfp.libreforge.conditions.Condition
import com.willfp.libreforge.get
import com.willfp.libreforge.toDispatcher
import com.willfp.libreforge.triggers.hasCondition
import com.willfp.libreforge.updateEffects
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
Expand All @@ -33,6 +34,11 @@ object ConditionBelowHungerPercent : Condition<NoCompileData>("below_hunger_perc

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
fun handle(event: FoodLevelChangeEvent) {
event.entity.toDispatcher().updateEffects()

val entity = event.entity.toDispatcher()

if (!entity.hasCondition(this)) return

entity.updateEffects()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.willfp.libreforge.arguments
import com.willfp.libreforge.conditions.Condition
import com.willfp.libreforge.get
import com.willfp.libreforge.toDispatcher
import com.willfp.libreforge.triggers.hasCondition
import com.willfp.libreforge.updateEffects
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
Expand All @@ -33,6 +34,10 @@ object ConditionBelowXPLevel : Condition<NoCompileData>("below_xp_level") {

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
fun handle(event: PlayerExpChangeEvent) {
event.player.toDispatcher().updateEffects()
val player = event.player.toDispatcher()

if (!player.hasCondition(this)) return

player.updateEffects()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.willfp.libreforge.ProvidedHolder
import com.willfp.libreforge.conditions.Condition
import com.willfp.libreforge.get
import com.willfp.libreforge.toDispatcher
import com.willfp.libreforge.triggers.hasCondition
import com.willfp.libreforge.updateEffects
import org.bukkit.entity.LivingEntity
import org.bukkit.event.EventHandler
Expand All @@ -27,6 +28,10 @@ object ConditionIsGliding : Condition<NoCompileData>("is_gliding") {

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
fun handle(event: EntityToggleGlideEvent) {
event.entity.toDispatcher().updateEffects()
val entity = event.entity.toDispatcher()

if (!entity.hasCondition(this)) return

entity.updateEffects()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.willfp.libreforge.ProvidedHolder
import com.willfp.libreforge.conditions.Condition
import com.willfp.libreforge.get
import com.willfp.libreforge.toDispatcher
import com.willfp.libreforge.triggers.hasCondition
import com.willfp.libreforge.updateEffects
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
Expand All @@ -27,6 +28,11 @@ object ConditionIsSneaking : Condition<NoCompileData>("is_sneaking") {

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
fun handle(event: PlayerToggleSneakEvent) {

val player = event.player.toDispatcher()

if (!player.hasCondition(this)) return

event.player.toDispatcher().updateEffects()
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package com.willfp.libreforge.conditions.impl

import com.willfp.eco.core.config.interfaces.Config
import com.willfp.libreforge.Dispatcher
import com.willfp.libreforge.NoCompileData
import com.willfp.libreforge.ProvidedHolder
import com.willfp.libreforge.*
import com.willfp.libreforge.conditions.Condition
import com.willfp.libreforge.get
import com.willfp.libreforge.toDispatcher
import com.willfp.libreforge.updateEffects
import com.willfp.libreforge.triggers.hasCondition
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
Expand All @@ -27,6 +23,12 @@ object ConditionIsSprinting : Condition<NoCompileData>("is_sprinting") {

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
fun handle(event: PlayerToggleSprintEvent) {
event.player.toDispatcher().updateEffects()
val player = event.player.toDispatcher()

if (!player.hasCondition(this)) {
return
}

player.updateEffects()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.willfp.libreforge.NoCompileData
import com.willfp.libreforge.ProvidedHolder
import com.willfp.libreforge.conditions.Condition
import com.willfp.libreforge.toDispatcher
import com.willfp.libreforge.triggers.hasCondition
import com.willfp.libreforge.updateEffects
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
Expand All @@ -25,7 +26,11 @@ object ConditionIsStorm: Condition<NoCompileData>("is_storm") {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
fun handle(event: WeatherChangeEvent) {
for (entity in event.world.entities) {
entity.toDispatcher().updateEffects()
val dispatcher = entity.toDispatcher()

if (!dispatcher.hasCondition(this)) return

dispatcher.updateEffects()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.willfp.libreforge.ProvidedHolder
import com.willfp.libreforge.conditions.Condition
import com.willfp.libreforge.get
import com.willfp.libreforge.toDispatcher
import com.willfp.libreforge.triggers.hasCondition
import com.willfp.libreforge.updateEffects
import org.bukkit.entity.LivingEntity
import org.bukkit.event.EventHandler
Expand All @@ -27,6 +28,10 @@ object ConditionIsSwimming : Condition<NoCompileData>("is_swimming") {

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
fun handle(event: EntityToggleSwimEvent) {
event.entity.toDispatcher().updateEffects()
val entity = event.entity.toDispatcher()

if (!entity.hasCondition(this)) return

entity.updateEffects()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.willfp.libreforge.ProvidedHolder
import com.willfp.libreforge.conditions.Condition
import com.willfp.libreforge.get
import com.willfp.libreforge.toDispatcher
import com.willfp.libreforge.triggers.hasCondition
import com.willfp.libreforge.updateEffects
import org.bukkit.entity.Entity
import org.bukkit.event.EventHandler
Expand Down Expand Up @@ -35,6 +36,10 @@ object ConditionOnFire : Condition<NoCompileData>("on_fire") {
return
}

event.entity.toDispatcher().updateEffects()
val entity = event.entity.toDispatcher()

if (!entity.hasCondition(this)) return

entity.updateEffects()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.willfp.libreforge.arguments
import com.willfp.libreforge.conditions.Condition
import com.willfp.libreforge.get
import com.willfp.libreforge.toDispatcher
import com.willfp.libreforge.triggers.hasCondition
import com.willfp.libreforge.updateEffects
import org.bukkit.entity.Entity
import org.bukkit.event.EventHandler
Expand Down Expand Up @@ -41,11 +42,19 @@ object ConditionRidingEntity : Condition<Collection<TestableEntity>>("riding_ent

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
fun handle(event: EntityDismountEvent) {
event.entity.toDispatcher().updateEffects()
val entity = event.entity.toDispatcher()

if (!entity.hasCondition(this)) return

entity.updateEffects()
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
fun handle(event: EntityMountEvent) {
event.entity.toDispatcher().updateEffects()
val entity = event.entity.toDispatcher()

if (!entity.hasCondition(this)) return

entity.updateEffects()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ abstract class ElementLike : ConfigurableElement {
}

// Inject placeholders everywhere after mutation
trigger.generatePlaceholders(data)
if (mutators.isNotEmpty()) trigger.generatePlaceholders(data)
injectPlaceholders(trigger.placeholders)

// Filter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.willfp.libreforge.triggers

import com.willfp.libreforge.Dispatcher
import com.willfp.libreforge.conditions.Condition
import com.willfp.libreforge.holders
import org.bukkit.entity.Entity
import org.bukkit.entity.LivingEntity
import org.bukkit.entity.Projectile
Expand All @@ -11,3 +14,7 @@ fun Entity.tryAsLivingEntity(): LivingEntity? {
else -> null
}
}

fun Dispatcher<*>.hasCondition(condition : Condition<*>) : Boolean {
return this.holders.any { it.holder.conditions.any { it.condition.id == condition.id } || it.holder.effects.any { it.conditions.any { it.condition.id == condition.id } } }
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package com.willfp.libreforge.triggers.placeholders.impl

import com.willfp.libreforge.NamedValue
import com.willfp.libreforge.plugin
import com.willfp.libreforge.triggers.TriggerData
import com.willfp.libreforge.triggers.placeholders.TriggerPlaceholder

object TriggerPlaceholderDistance : TriggerPlaceholder("distance") {

private val placeholdersDistance = plugin.triggersPlaceholdersYml.getStringsOrNull("placeholders.${this.id}.aliases.distance") ?: emptyList()

override fun createPlaceholders(data: TriggerData): Collection<NamedValue> {
val victim = data.victim ?: return emptyList()
val player = data.player ?: return emptyList()

return listOf(
NamedValue(
"distance",
player.location.toVector().distance(victim.location.toVector())
)
return listOfNotNull(
if (placeholdersDistance.isNotEmpty()) NamedValue(placeholdersDistance, player.location.toVector().distance(victim.location.toVector())) else null
)
}
}
Loading