Skip to content

Commit

Permalink
Replaced TriggerPlaceholderListener with new TriggerPlaceholder system
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed May 4, 2023
1 parent 6ac5355 commit 22bb042
Show file tree
Hide file tree
Showing 11 changed files with 238 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ class LibreforgeSpigotPlugin : EcoPlugin() {

override fun loadListeners(): List<Listener> {
return listOf(
TriggerPlaceholderListener,
EffectCollisionFixer,
ItemRefreshListener(this),
CounterHandler
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.willfp.libreforge.triggers

import com.willfp.eco.core.placeholder.InjectablePlaceholder
import com.willfp.libreforge.NamedValue
import com.willfp.libreforge.triggers.placeholders.TriggerPlaceholders
import org.bukkit.entity.Player

data class DispatchedTrigger(
Expand All @@ -14,10 +15,20 @@ 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
}

fun addPlaceholders(placeholder: Iterable<NamedValue>) {
_placeholders += placeholder
}

companion object {
fun DispatchedTrigger.inheritPlaceholders(other: DispatchedTrigger): DispatchedTrigger {
_placeholders += other._placeholders
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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 org.bukkit.event.Listener

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

final override fun onRegister() {
plugin.runWhenEnabled {
plugin.eventManager.registerListener(this)
postRegister()
}
}

open fun postRegister() {
// Override when needed.
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.willfp.libreforge.triggers.placeholders

import com.willfp.eco.core.registry.Registry
import com.willfp.libreforge.triggers.placeholders.impl.TriggerPlaceholderDistance
import com.willfp.libreforge.triggers.placeholders.impl.TriggerPlaceholderHits
import com.willfp.libreforge.triggers.placeholders.impl.TriggerPlaceholderLocation
import com.willfp.libreforge.triggers.placeholders.impl.TriggerPlaceholderText
import com.willfp.libreforge.triggers.placeholders.impl.TriggerPlaceholderValue
import com.willfp.libreforge.triggers.placeholders.impl.TriggerPlaceholderVictim

object TriggerPlaceholders : Registry<TriggerPlaceholder>() {
init {
register(TriggerPlaceholderDistance)
register(TriggerPlaceholderHits)
register(TriggerPlaceholderLocation)
register(TriggerPlaceholderText)
register(TriggerPlaceholderValue)
register(TriggerPlaceholderVictim)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.willfp.libreforge.triggers.placeholders.impl

import com.willfp.libreforge.NamedValue
import com.willfp.libreforge.triggers.DispatchedTrigger
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()

return listOf(
NamedValue(
"distance",
player.location.toVector().distance(victim.location.toVector())
)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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.event.TriggerDispatchEvent
import com.willfp.libreforge.triggers.impl.TriggerBowAttack
import com.willfp.libreforge.triggers.impl.TriggerMeleeAttack
import com.willfp.libreforge.triggers.impl.TriggerTridentAttack
import com.willfp.libreforge.triggers.placeholders.TriggerPlaceholder
import org.bukkit.attribute.Attribute
import org.bukkit.entity.LivingEntity
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
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()

return listOf(
NamedValue(
"hits",
victim.getHits(player)
)
)
}

@EventHandler
fun trackHits(event: TriggerDispatchEvent) {
if (event.trigger.trigger !in listOf(
TriggerMeleeAttack,
TriggerBowAttack,
TriggerTridentAttack
)
) {
return
}

val player = event.trigger.data.player ?: return
val entity = event.trigger.data.victim ?: return

@Suppress("UNCHECKED_CAST")
val map = entity.getMetadata(HITS_META_KEY).firstOrNull()?.value() as? MutableMap<UUID, Int> ?: mutableMapOf()
val hits = entity.getHits(player)
if (entity.health >= entity.getAttribute(Attribute.GENERIC_MAX_HEALTH)!!.value) {
map[player.uniqueId] = 1
} else {
map[player.uniqueId] = hits + 1
}

entity.removeMetadata(HITS_META_KEY, plugin)
entity.setMetadata(HITS_META_KEY, plugin.createMetadataValue(map))
}

private fun LivingEntity.getHits(player: Player): Int {
@Suppress("UNCHECKED_CAST")
val map = this.getMetadata(HITS_META_KEY).firstOrNull()?.value() as? MutableMap<UUID, Int> ?: mutableMapOf()
return map[player.uniqueId] ?: 0
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.willfp.libreforge.triggers.placeholders.impl

import com.willfp.libreforge.NamedValue
import com.willfp.libreforge.triggers.DispatchedTrigger
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()

return listOf(
NamedValue(
listOf("location_x", "loc_x", "x"),
location.x
),
NamedValue(
listOf("location_block_x", "loc_b_x", "block_x", "bx"),
location.blockX
),
NamedValue(
listOf("location_y", "loc_y", "y"),
location.y
),
NamedValue(
listOf("location_block_y", "loc_b_y", "block_y", "by"),
location.blockY
),
NamedValue(
listOf("location_z", "loc_z", "z"),
location.z
),
NamedValue(
listOf("location_block_z", "loc_b_z", "block_z", "bz"),
location.blockZ
),
NamedValue(
listOf("location_world", "loc_w", "world"),
location.world?.name ?: ""
)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.willfp.libreforge.triggers.placeholders.impl

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

object TriggerPlaceholderText : TriggerPlaceholder("text") {
override fun createPlaceholders(trigger: DispatchedTrigger): Collection<NamedValue> {
return listOf(
NamedValue(
listOf("text", "string", "message"),
trigger.data.text ?: ""
)
)
}
}
Loading

0 comments on commit 22bb042

Please sign in to comment.