forked from SuperRicky14/TpaPlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Command event callback refactor
- Loading branch information
1 parent
9ac689d
commit 48a8b3d
Showing
7 changed files
with
165 additions
and
231 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
33 changes: 33 additions & 0 deletions
33
src/main/kotlin/net/superricky/tpaplusplus/async/AsyncCommandEventFactory.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,33 @@ | ||
package net.superricky.tpaplusplus.async | ||
|
||
class AsyncCommandEventFactory { | ||
private val listeners: MutableMap<AsyncCommandEvent, MutableSet<Function1<AsyncCommandData, Unit>>> = mutableMapOf() | ||
|
||
fun addListener(event: AsyncCommandEvent, listener: Function1<AsyncCommandData, Unit>): AsyncCommandEventFactory { | ||
if (!listeners.containsKey(event)) { | ||
listeners[event] = HashSet() | ||
} | ||
listeners[event]!!.add(listener) | ||
return this | ||
} | ||
|
||
fun invoke(event: AsyncCommandEvent, asyncCommandData: AsyncCommandData): AsyncCommandEventFactory { | ||
if (!listeners.containsKey(event)) { | ||
listeners[event] = HashSet() | ||
return this | ||
} | ||
for (listener in listeners[event]!!) { | ||
listener.invoke(asyncCommandData) | ||
} | ||
return this | ||
} | ||
|
||
companion object { | ||
fun addListener( | ||
event: AsyncCommandEvent, | ||
listener: Function1<AsyncCommandData, Unit> | ||
): AsyncCommandEventFactory { | ||
return AsyncCommandEventFactory().addListener(event, listener) | ||
} | ||
} | ||
} |
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
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
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
Oops, something went wrong.