net.projecttl.inventory.util.ObservableHashMap.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of InventoryGUI-api Show documentation
Show all versions of InventoryGUI-api Show documentation
This is minecraft gui library
package net.projecttl.inventory.util
/**
* An observable type of hashmaps. Triggers observers on data input.
*/
class ObservableHashMap: HashMap() {
private val observers = ArrayList<(Pair) -> Unit>()
fun trigger(key: K, value: V) {
observers.forEach {
it.invoke(key to value)
}
}
fun addObserver(observer: (Pair) -> Unit) {
observers.add(observer)
}
override fun put(key: K, value: V): V? {
val result = super.put(key, value)
trigger(key, value)
return result
}
}