Add InteractionListener.onUseWithWildcard.

This commit is contained in:
Avi Weinstock
2022-01-29 11:31:19 -05:00
parent 89ec02ca45
commit 14528411a7
2 changed files with 28 additions and 4 deletions
@@ -30,6 +30,11 @@ abstract class InteractionListener : Listener{
fun onUseWith(type: Int, used: IntArray, vararg with: Int, handler: (player: Player, used: Node, with: Node) -> Boolean){
InteractionListeners.add(type,used,with,handler)
}
// Note: wildcard listeners incur overhead on every use-with interaction, only use them as a space-time tradeoff when something
// is actually supposed to have a response to every item used with it (e.g. imp boxes, certain quest npcs)
fun onUseWithWildcard(type: Int, predicate: (used: Int, with: Int) -> Boolean, handler: (player: Player, used: Node, with: Node) -> Boolean) {
InteractionListeners.addWildcard(type, predicate, handler)
}
fun onEquip(id: Int, handler: (player: Player, node: Node) -> Boolean){
InteractionListeners.addEquip(id,handler)
}
@@ -53,4 +58,4 @@ abstract class InteractionListener : Listener{
fun onDig(location: Location,method: (player: Player) -> Unit){
SpadeDigListener.registerListener(location,method)
}
}
}
@@ -12,6 +12,7 @@ import rs09.game.system.SystemLogger
object InteractionListeners {
private val listeners = HashMap<String,(Player, Node) -> Boolean>(1000)
private val useWithListeners = HashMap<String,(Player,Node,Node) -> Boolean>(1000)
private val useWithWildcardListeners = HashMap<Int, ArrayList<Pair<(Int, Int) -> Boolean, (Player, Node, Node) -> Boolean>>>(10)
private val destinationOverrides = HashMap<String,(Entity, Node) -> Location>(100)
private val equipListeners = HashMap<String,(Player,Node) -> Boolean>(10)
@@ -55,6 +56,14 @@ object InteractionListeners {
}
}
@JvmStatic
fun addWildcard(type: Int, predicate: (used: Int, with: Int) -> Boolean, handler: (player: Player, used: Node, with: Node) -> Boolean) {
if(!useWithWildcardListeners.containsKey(type)) {
useWithWildcardListeners.put(type, ArrayList())
}
useWithWildcardListeners[type]!!.add(Pair(predicate, handler))
}
@JvmStatic
fun addEquip(id: Int,method: (Player, Node) -> Boolean){
equipListeners["equip:$id"] = method
@@ -76,8 +85,18 @@ object InteractionListeners {
}
@JvmStatic
fun get(used: Int, with: Int, type: Int): ((Player,Node,Node) -> Boolean)?{
return useWithListeners["$used:$with:$type"]
fun get(used: Int, with: Int, type: Int): ((Player,Node,Node) -> Boolean)? {
var method = useWithListeners["$used:$with:$type"]
if(method != null) {
return method
}
val handlers = useWithWildcardListeners[type] ?: return null
for(pair in handlers) {
if(pair.first(used, with)) {
return pair.second
}
}
return null
}
@JvmStatic
@@ -214,4 +233,4 @@ object InteractionListeners {
}
}
}
}
}