Skip to content

Commit

Permalink
refactor: Command event callback refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
half-nothing committed Sep 26, 2024
1 parent 9ac689d commit 48a8b3d
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 231 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,39 @@ import net.superricky.tpaplusplus.utility.*
class AsyncCommandData(
private val asyncRequest: AsyncRequest,
private var pos: LevelBoundVec3,
private val callback: Function2<AsyncCommandEvent, AsyncCommandData, Unit>
private val asyncCommandEventFactory: AsyncCommandEventFactory
) {
private var canceled: AtomicBoolean = atomic(false)
private var timeout = Config.getConfig()[CommonSpec.tpaTimeout].translateSecondToTick()
private var checkTarget = asyncRequest.sender

init {
asyncCommandEventFactory
.addListener(AsyncCommandEvent.REQUEST_AFTER_DELAY) {
AsyncCommandHelper.addCooldown(
asyncRequest.sender.uuid,
asyncRequest.commandType
)
}
.addListener(AsyncCommandEvent.REQUEST_OUT_DISTANCE) {
asyncRequest.sender.sendMessage(
Text.translatable(
"command.windup.error.out_distance",
asyncRequest.commandType.handler.getCommandName()
).setStyle(TextColorPallet.error)
)
}
.addListener(AsyncCommandEvent.REQUEST_UPDATE_MESSAGE) {
asyncRequest.sender.sendRemainTime(asyncRequest.delay)
}
.addListener(AsyncCommandEvent.REQUEST_UNDER_COOLDOWN) {
asyncRequest.sender.sendCooldownTime(
asyncRequest.commandType.handler.getCommandName(),
asyncRequest.cooldown.translateTickToSecond()
)
}
}

fun needDelay(): Boolean = asyncRequest.delay != 0.0

fun getDelay(): Double = asyncRequest.delay
Expand Down Expand Up @@ -54,34 +81,7 @@ class AsyncCommandData(
if (isCanceled()) {
return
}
when (commandResult) {
AsyncCommandEvent.REQUEST_AFTER_DELAY -> {
AsyncCommandHelper.addCooldown(asyncRequest.sender.uuid, asyncRequest.commandType)
}

AsyncCommandEvent.REQUEST_OUT_DISTANCE -> {
asyncRequest.sender.sendMessage(
Text.translatable(
"command.windup.error.out_distance",
asyncRequest.commandType.handler.getCommandName()
).setStyle(TextColorPallet.error)
)
}

AsyncCommandEvent.REQUEST_UPDATE_MESSAGE -> {
asyncRequest.sender.sendRemainTime(asyncRequest.delay)
}

AsyncCommandEvent.REQUEST_UNDER_COOLDOWN -> {
asyncRequest.sender.sendCooldownTime(
asyncRequest.commandType.handler.getCommandName(),
asyncRequest.cooldown.translateTickToSecond()
)
}

else -> {}
}
callback.invoke(commandResult, this)
asyncCommandEventFactory.invoke(commandResult, this)
}

fun cancel() {
Expand Down
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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,31 +59,29 @@ object AcceptCommand : AsyncCommand(), BuildableCommand {
AsyncCommandData(
AsyncRequest(sender, receiver, AsyncCommandType.ACCEPT),
LevelBoundVec3(sender.getDimension(), sender.pos),
::asyncCommandCallback
AsyncCommandEventFactory.addListener(AsyncCommandEvent.REQUEST_AFTER_DELAY) {
if (AsyncCommandHelper.acceptRequest(sender, receiver) == AsyncCommandEvent.REQUEST_NOT_FOUND) {
CommandHelper.requestNotFound(sender, receiver)
}
}
)
)
return CommandResult.NORMAL.status
}

private fun acceptCommand(context: Context): Int {
fun asyncCommandCallback(result: AsyncCommandEvent, asyncCommandData: AsyncCommandData) {
val asyncRequest = asyncCommandData.getRequest()
if (result == AsyncCommandEvent.REQUEST_AFTER_DELAY) {
val sender = asyncRequest.sender
if (AsyncCommandHelper.acceptRequest(sender) == AsyncCommandEvent.REQUEST_NOT_FOUND) {
CommandHelper.requestNotFound(sender)
}
}
}

val source = context.source
val sender = source.player
sender ?: return CommandResult.SENDER_NOT_EXIST.status
AsyncCommandHelper.schedule(
AsyncCommandData(
AsyncRequest(sender, null, AsyncCommandType.ACCEPT),
LevelBoundVec3(sender.getDimension(), sender.pos),
::asyncCommandCallback
AsyncCommandEventFactory.addListener(AsyncCommandEvent.REQUEST_AFTER_DELAY) {
if (AsyncCommandHelper.acceptRequest(sender) == AsyncCommandEvent.REQUEST_NOT_FOUND) {
CommandHelper.requestNotFound(sender)
}
}
)
)
return CommandResult.NORMAL.status
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,6 @@ object CancelCommand : AsyncCommand(), BuildableCommand {
override fun getMinDistance(): Double = Config.getConfig()[CommandDistanceSpec.cancelDistance]

private fun cancelRequestWithTarget(context: Context): Int {
fun asyncCommandCallback(result: AsyncCommandEvent, asyncCommandData: AsyncCommandData) {
val asyncRequest = asyncCommandData.getRequest()
if (result == AsyncCommandEvent.REQUEST_AFTER_DELAY) {
val sender = asyncRequest.sender
val receiver = asyncRequest.receiver!!
if (AsyncCommandHelper.cancelRequest(sender, receiver) == AsyncCommandEvent.REQUEST_NOT_FOUND) {
CommandHelper.requestNotFound(sender, receiver)
}
}
}

val source = context.source
val (result, sender, receiver) = checkSenderReceiver(context)
if (result != CommandResult.NORMAL) return result.status
Expand All @@ -58,31 +47,29 @@ object CancelCommand : AsyncCommand(), BuildableCommand {
AsyncCommandData(
AsyncRequest(sender, receiver, AsyncCommandType.CANCEL),
LevelBoundVec3(sender.getDimension(), sender.pos),
::asyncCommandCallback
AsyncCommandEventFactory.addListener(AsyncCommandEvent.REQUEST_AFTER_DELAY) {
if (AsyncCommandHelper.cancelRequest(sender, receiver) == AsyncCommandEvent.REQUEST_NOT_FOUND) {
CommandHelper.requestNotFound(sender, receiver)
}
}
)
)
return CommandResult.NORMAL.status
}

private fun cancelRequest(context: Context): Int {
fun asyncCommandCallback(result: AsyncCommandEvent, asyncCommandData: AsyncCommandData) {
val asyncRequest = asyncCommandData.getRequest()
if (result == AsyncCommandEvent.REQUEST_AFTER_DELAY) {
val sender = asyncRequest.sender
if (AsyncCommandHelper.cancelRequest(sender) == AsyncCommandEvent.REQUEST_NOT_FOUND) {
CommandHelper.requestNotFound(sender)
}
}
}

val source = context.source
val sender = source.player
sender ?: return CommandResult.SENDER_NOT_EXIST.status
AsyncCommandHelper.schedule(
AsyncCommandData(
AsyncRequest(sender, null, AsyncCommandType.CANCEL),
LevelBoundVec3(sender.getDimension(), sender.pos),
::asyncCommandCallback
AsyncCommandEventFactory.addListener(AsyncCommandEvent.REQUEST_AFTER_DELAY) {
if (AsyncCommandHelper.cancelRequest(sender) == AsyncCommandEvent.REQUEST_NOT_FOUND) {
CommandHelper.requestNotFound(sender)
}
}
)
)
return CommandResult.NORMAL.status
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,6 @@ object DenyCommand : AsyncCommand(), BuildableCommand {
override fun getMinDistance(): Double = Config.getConfig()[CommandDistanceSpec.denyDistance]

private fun refuseRequestWithTarget(context: Context): Int {
fun asyncCommandCallback(result: AsyncCommandEvent, asyncCommandData: AsyncCommandData) {
val asyncRequest = asyncCommandData.getRequest()
if (result == AsyncCommandEvent.REQUEST_AFTER_DELAY) {
val sender = asyncRequest.sender
val receiver = asyncRequest.receiver!!
if (AsyncCommandHelper.refuseRequest(sender, receiver) == AsyncCommandEvent.REQUEST_NOT_FOUND) {
CommandHelper.requestNotFound(sender, receiver)
}
}
}

val source = context.source
val (result, sender, receiver) = checkSenderReceiver(context)
if (result != CommandResult.NORMAL) return result.status
Expand All @@ -58,31 +47,29 @@ object DenyCommand : AsyncCommand(), BuildableCommand {
AsyncCommandData(
AsyncRequest(sender, receiver, AsyncCommandType.DENY),
LevelBoundVec3(sender.getDimension(), sender.pos),
::asyncCommandCallback
AsyncCommandEventFactory.addListener(AsyncCommandEvent.REQUEST_AFTER_DELAY) {
if (AsyncCommandHelper.refuseRequest(sender, receiver) == AsyncCommandEvent.REQUEST_NOT_FOUND) {
CommandHelper.requestNotFound(sender, receiver)
}
}
)
)
return CommandResult.NORMAL.status
}

private fun refuseRequest(context: Context): Int {
fun asyncCommandCallback(result: AsyncCommandEvent, asyncCommandData: AsyncCommandData) {
val asyncRequest = asyncCommandData.getRequest()
if (result == AsyncCommandEvent.REQUEST_AFTER_DELAY) {
val sender = asyncRequest.sender
if (AsyncCommandHelper.refuseRequest(sender) == AsyncCommandEvent.REQUEST_NOT_FOUND) {
CommandHelper.requestNotFound(sender)
}
}
}

val source = context.source
val sender = source.player
sender ?: return CommandResult.SENDER_NOT_EXIST.status
AsyncCommandHelper.schedule(
AsyncCommandData(
AsyncRequest(sender, null, AsyncCommandType.DENY),
LevelBoundVec3(sender.getDimension(), sender.pos),
::asyncCommandCallback
AsyncCommandEventFactory.addListener(AsyncCommandEvent.REQUEST_AFTER_DELAY) {
if (AsyncCommandHelper.refuseRequest(sender) == AsyncCommandEvent.REQUEST_NOT_FOUND) {
CommandHelper.requestNotFound(sender)
}
}
)
)
return CommandResult.NORMAL.status
Expand Down
Loading

0 comments on commit 48a8b3d

Please sign in to comment.