Skip to content

Commit

Permalink
Updates to event DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
lare96 committed Dec 30, 2024
1 parent 75a6d7c commit 5303019
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package api.event
package api.event.dsl

import api.event.Matcher
import api.predef.*
import io.luna.game.event.Event
import javax.script.ScriptException
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package api.event
package api.event.dsl

import api.predef.*
import io.luna.game.event.Event
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package api.event
package api.event.dsl

import api.event.Matcher
import io.luna.game.event.Event
import kotlin.reflect.KClass

Expand Down
53 changes: 53 additions & 0 deletions src/main/kotlin/api/event/dsl/InterceptUseItem.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package api.event.dsl

import api.event.Matcher
import io.luna.game.event.impl.UseItemEvent.*

/**
* A model that intercepts [ItemOnItemEvent]s and [ItemOnObjectEvent]s.
*
* @author lare96
*/
class InterceptUseItem(private val id: Int) {

/**
* Intercepts an [ItemOnItemEvent]. **Will match both ways!**
*/
fun onItem(itemId: Int, action: ItemOnItemEvent.() -> Unit) {
val matcher = Matcher.get<ItemOnItemEvent, Pair<Int, Int>>()
matcher[id to itemId] = { action(this) }
matcher[itemId to id] = { action(this) }
}

/**
* Intercepts an [ItemOnObjectEvent].
*/
fun onObject(objectId: Int, action: ItemOnObjectEvent.() -> Unit) {
val matcher = Matcher.get<ItemOnObjectEvent, Pair<Int, Int>>()
matcher[id to objectId] = { action(this) }
}

/**
* Intercepts an [ItemOnNpcEvent].
*/
fun onNpc(npcId: Int, action: ItemOnNpcEvent.() -> Unit) {
val matcher = Matcher.get<ItemOnNpcEvent, Pair<Int, Int>>()
matcher[id to npcId] = { action(this) }
}

/**
* Intercepts an [ItemOnPlayerEvent].
*/
fun onPlayer(action: ItemOnPlayerEvent.() -> Unit) {
val matcher = Matcher.get<ItemOnPlayerEvent, Int>()
matcher[id] = { action(this) }
}

/**
* Intercepts an [ItemOnGroundItemEvent].
*/
fun onGroundItem(itemId: Int, action: ItemOnGroundItemEvent.() -> Unit) {
val matcher = Matcher.get<ItemOnGroundItemEvent, Pair<Int, Int>>()
matcher[id to itemId] = { action(this) }
}
}

0 comments on commit 5303019

Please sign in to comment.