Skip to content

Commit

Permalink
Refactor on trigger placeholders, fixed placeholders with mutators
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed May 4, 2023
1 parent 22bb042 commit 82d228e
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,19 @@ abstract class ElementLike {
.let { if (!supportsDelay) 0 else it }
.toLong()

// Extremely janky code, but it's cleaner than repeating myself 5 times I think.
// Initial injection into mutators
mutators.map { it.config }.forEach { it.addInjectablePlaceholder(trigger.placeholders) }

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) }

val data = mutators.mutate(trigger.data)

// 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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ data class DispatchedTrigger(
val placeholders: List<InjectablePlaceholder>
get() = _placeholders.flatMap { it.placeholders }

init {
for (placeholder in TriggerPlaceholders) {
_placeholders += placeholder.createPlaceholders(this)
}
}

fun addPlaceholder(placeholder: NamedValue) {
_placeholders += placeholder
}
Expand All @@ -29,10 +23,20 @@ data class DispatchedTrigger(
_placeholders += placeholder
}

companion object {
fun DispatchedTrigger.inheritPlaceholders(other: DispatchedTrigger): DispatchedTrigger {
_placeholders += other._placeholders
return this
fun inheritPlaceholders(other: DispatchedTrigger): DispatchedTrigger {
_placeholders += other._placeholders
return this
}

/**
* Generate placeholders for some [data].
*
* This is called automatically when the trigger is dispatched,
* and again after mutation with the updated [data].
*/
internal fun generatePlaceholders(data: TriggerData = this.data) {
for (placeholder in TriggerPlaceholders) {
_placeholders += placeholder.createPlaceholders(data)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.willfp.libreforge.generatePlaceholders
import com.willfp.libreforge.getProvidedActiveEffects
import com.willfp.libreforge.plugin
import com.willfp.libreforge.providedActiveEffects
import com.willfp.libreforge.triggers.DispatchedTrigger.Companion.inheritPlaceholders
import com.willfp.libreforge.triggers.event.TriggerDispatchEvent
import org.bukkit.Bukkit
import org.bukkit.entity.Player
Expand All @@ -29,6 +28,7 @@ abstract class Trigger(
forceHolders: Collection<ProvidedHolder>? = null
) {
val dispatch = DispatchedTrigger(player, this, data)
dispatch.generatePlaceholders(data)

val dispatchEvent = TriggerDispatchEvent(player, dispatch)
Bukkit.getPluginManager().callEvent(dispatchEvent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ package com.willfp.libreforge.triggers.placeholders
import com.willfp.eco.core.registry.KRegistrable
import com.willfp.libreforge.NamedValue
import com.willfp.libreforge.plugin
import com.willfp.libreforge.triggers.DispatchedTrigger
import com.willfp.libreforge.triggers.TriggerData
import org.bukkit.event.Listener

abstract class TriggerPlaceholder(
override val id: String
) : Listener, KRegistrable {
/**
* Create placeholders for a [trigger].
* Create placeholders for some [data].
*/
abstract fun createPlaceholders(trigger: DispatchedTrigger): Collection<NamedValue>
abstract fun createPlaceholders(data: TriggerData): Collection<NamedValue>

final override fun onRegister() {
plugin.runWhenEnabled {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.willfp.libreforge.triggers.placeholders.impl

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

object TriggerPlaceholderDistance : TriggerPlaceholder("distance") {
override fun createPlaceholders(trigger: DispatchedTrigger): Collection<NamedValue> {
val victim = trigger.data.victim ?: return emptyList()
val player = trigger.data.player ?: return emptyList()
override fun createPlaceholders(data: TriggerData): Collection<NamedValue> {
val victim = data.victim ?: return emptyList()
val player = data.player ?: return emptyList()

return listOf(
NamedValue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.willfp.libreforge.triggers.placeholders.impl

import com.willfp.libreforge.NamedValue
import com.willfp.libreforge.plugin
import com.willfp.libreforge.triggers.DispatchedTrigger
import com.willfp.libreforge.triggers.TriggerData
import com.willfp.libreforge.triggers.event.TriggerDispatchEvent
import com.willfp.libreforge.triggers.impl.TriggerBowAttack
import com.willfp.libreforge.triggers.impl.TriggerMeleeAttack
Expand All @@ -17,9 +17,9 @@ import java.util.UUID
object TriggerPlaceholderHits : TriggerPlaceholder("distance") {
private const val HITS_META_KEY = "libreforge_tracked_hits"

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

return listOf(
NamedValue(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.willfp.libreforge.triggers.placeholders.impl

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

object TriggerPlaceholderLocation : TriggerPlaceholder("location") {
override fun createPlaceholders(trigger: DispatchedTrigger): Collection<NamedValue> {
val location = trigger.data.location ?: return emptyList()
override fun createPlaceholders(data: TriggerData): Collection<NamedValue> {
val location = data.location ?: return emptyList()

return listOf(
NamedValue(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.willfp.libreforge.triggers.placeholders.impl

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

object TriggerPlaceholderText : TriggerPlaceholder("text") {
override fun createPlaceholders(trigger: DispatchedTrigger): Collection<NamedValue> {
override fun createPlaceholders(data: TriggerData): Collection<NamedValue> {
val text = data.text ?: return emptyList()

return listOf(
NamedValue(
listOf("text", "string", "message"),
trigger.data.text ?: ""
text
)
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.willfp.libreforge.triggers.placeholders.impl

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

object TriggerPlaceholderValue : TriggerPlaceholder("trigger_value") {
override fun createPlaceholders(trigger: DispatchedTrigger): Collection<NamedValue> {
override fun createPlaceholders(data: TriggerData): Collection<NamedValue> {
return listOf(
NamedValue(
listOf("trigger_value", "triggervalue", "trigger", "value", "tv", "v", "t"),
trigger.data.value
data.value
)
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.willfp.libreforge.triggers.placeholders.impl

import com.willfp.libreforge.NamedValue
import com.willfp.libreforge.triggers.DispatchedTrigger
import com.willfp.libreforge.triggers.TriggerData
import com.willfp.libreforge.triggers.placeholders.TriggerPlaceholder
import org.bukkit.attribute.Attribute

object TriggerPlaceholderVictim : TriggerPlaceholder("victim") {
override fun createPlaceholders(trigger: DispatchedTrigger): Collection<NamedValue> {
val victim = trigger.data.victim ?: return emptyList()
override fun createPlaceholders(data: TriggerData): Collection<NamedValue> {
val victim = data.victim ?: return emptyList()

return listOf(
NamedValue(
Expand Down

0 comments on commit 82d228e

Please sign in to comment.