-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaced TriggerPlaceholderListener with new TriggerPlaceholder system
- Loading branch information
Showing
11 changed files
with
238 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 0 additions & 153 deletions
153
core/src/main/kotlin/com/willfp/libreforge/TriggerPlaceholderListener.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
core/src/main/kotlin/com/willfp/libreforge/triggers/placeholders/TriggerPlaceholder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
core/src/main/kotlin/com/willfp/libreforge/triggers/placeholders/TriggerPlaceholders.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...ain/kotlin/com/willfp/libreforge/triggers/placeholders/impl/TriggerPlaceholderDistance.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
) | ||
) | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...rc/main/kotlin/com/willfp/libreforge/triggers/placeholders/impl/TriggerPlaceholderHits.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...ain/kotlin/com/willfp/libreforge/triggers/placeholders/impl/TriggerPlaceholderLocation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ?: "" | ||
) | ||
) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...rc/main/kotlin/com/willfp/libreforge/triggers/placeholders/impl/TriggerPlaceholderText.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ?: "" | ||
) | ||
) | ||
} | ||
} |
Oops, something went wrong.