Skip to content

Commit

Permalink
Optimised trigger dispatching, fixed bug with dynamically registered …
Browse files Browse the repository at this point in the history
…triggers
  • Loading branch information
WillFP committed Jun 18, 2023
1 parent 269aee0 commit 8bfeda0
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.willfp.libreforge.effects

import com.willfp.eco.core.config.interfaces.Config
import com.willfp.libreforge.BlankHolder.effects
import com.willfp.libreforge.ProvidedHolder
import com.willfp.libreforge.conditions.ConditionList
import com.willfp.libreforge.effects.arguments.EffectArgumentList
Expand All @@ -19,7 +20,7 @@ class EffectBlock internal constructor(
override val uuid: UUID,
override val config: Config,
val effects: Chain,
val triggers: Collection<Trigger>,
val triggers: Set<Trigger>,
override val arguments: EffectArgumentList,
override val conditions: ConditionList,
override val mutators: MutatorList,
Expand All @@ -45,13 +46,14 @@ class EffectBlock internal constructor(
) = effects.forEach { it.disable(player, holder, isReload = isReload) }

fun tryTrigger(trigger: DispatchedTrigger) {
if (trigger.trigger !in triggers) {
return
if (canBeTriggeredBy(trigger.trigger)) {
trigger(trigger)
}

trigger(trigger)
}

fun canBeTriggeredBy(trigger: Trigger) =
trigger in triggers

override fun doTrigger(trigger: DispatchedTrigger) =
effects.trigger(trigger)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ object Effects : Registry<Effect<*>>() {
val filters = Filters.compile(config.getSubsection("filters"), context.with("filters"))
val triggers = config.getStrings("triggers").mapNotNull {
Triggers[it]
}
}.toSet()

val effectConfigs = if (config.has("id")) {
listOf(config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,6 @@ abstract class ElementLike : ConfigurableElement {

val data = mutators.mutate(trigger.data)

// Inject placeholders everywhere after mutation
trigger.generatePlaceholders(data)
listOf(arguments, conditions, mutators, filters)
.flatten()
.map { it.config }
.plusElement(config)
.forEach { it.addInjectablePlaceholder(trigger.placeholders) }

// Antigrief check here - not very clean, but it works.
if (data.player != null && data.victim != null && data.victim != data.player) {
if (!config.getBool("disable_antigrief_check")) {
Expand All @@ -90,6 +82,14 @@ abstract class ElementLike : ConfigurableElement {
}
}

// Inject placeholders everywhere after mutation
trigger.generatePlaceholders(data)
listOf(arguments, conditions, mutators, filters)
.flatten()
.map { it.config }
.plusElement(config)
.forEach { it.addInjectablePlaceholder(trigger.placeholders) }

// Filter
val filterResult = if (config.getBool("filters_before_mutation")) {
filters.isMet(trigger.data)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.willfp.libreforge.triggers

import com.sun.tools.javac.jvm.ByteCodes.ret
import com.willfp.eco.core.registry.KRegistrable
import com.willfp.libreforge.BlankHolder.effects
import com.willfp.libreforge.EmptyProvidedHolder.holder
import com.willfp.libreforge.ProvidedHolder
import com.willfp.libreforge.generatePlaceholders
import com.willfp.libreforge.getProvidedActiveEffects
Expand Down Expand Up @@ -41,6 +44,7 @@ abstract class Trigger(
forceHolders: Collection<ProvidedHolder>? = null
) {
val dispatch = plugin.dispatchedTriggerFactory.create(player, this, data) ?: return

dispatch.generatePlaceholders(data)

val dispatchEvent = TriggerDispatchEvent(player, dispatch)
Expand All @@ -52,6 +56,11 @@ abstract class Trigger(
val effects = forceHolders?.getProvidedActiveEffects(player) ?: player.providedActiveEffects

for ((holder, blocks) in effects) {
// Avoid generating placeholders for nothing
if (blocks.none { it.canBeTriggeredBy(this) }) {
continue
}

val withHolder = data.copy(holder = holder)
val dispatchWithHolder = DispatchedTrigger(player, this, withHolder).inheritPlaceholders(dispatch)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ object Triggers : Registry<Trigger>() {

init {
register(TriggerGroupCustom)
register(TriggerGroupStatic)
//register(TriggerGroupStatic)
register(TriggerAltClick)
register(TriggerBite)
register(TriggerBlockItemDrop)
Expand Down

0 comments on commit 8bfeda0

Please sign in to comment.