Skip to content

Commit

Permalink
refactor: Cooldown refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
half-nothing committed Sep 26, 2024
1 parent e721513 commit 9ac689d
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 125 deletions.
43 changes: 3 additions & 40 deletions src/main/kotlin/net/superricky/tpaplusplus/async/AsyncCommand.kt
Original file line number Diff line number Diff line change
@@ -1,57 +1,20 @@
package net.superricky.tpaplusplus.async

import net.superricky.tpaplusplus.utility.LevelBoundVec3
import net.superricky.tpaplusplus.utility.getDimension

/**
* Abstract class for asyncCommand
*/
abstract class AsyncCommand {
@JvmField
protected var commandName: String = ""

/**
* This function is used to check if the player's movement
* distance is within the limit when the command is executed
*/
abstract fun checkWindupDistance(asyncCommandData: AsyncCommandData): Boolean

abstract fun getCooldownTime(): Double

abstract fun getDelayTime(): Double

/**
* Command names cannot be hot loaded
*/
fun getCommandName(): String = commandName

protected fun checkWindupDistance(
asyncCommandData: AsyncCommandData,
checkFunction: Function1<AsyncCommandData, Double>,
minDistance: Double
): Boolean {
if (minDistance < 0) {
return true
}
val distance = checkFunction.invoke(asyncCommandData)
if (distance == -1.0) {
return false
}
return distance <= minDistance
}
abstract fun getCooldownTime(): Double

protected fun getSenderDistance(asyncCommandData: AsyncCommandData): Double {
val originPos = asyncCommandData.getPos()
val sender = asyncCommandData.getRequest().sender
val nowPos = LevelBoundVec3(sender.getDimension(), sender.pos)
return originPos.distance(nowPos)
}
abstract fun getDelayTime(): Double

protected fun getReceiverDistance(asyncCommandData: AsyncCommandData): Double {
val originPos = asyncCommandData.getPos()
val receiver = asyncCommandData.getRequest().receiver
require(receiver != null) { "Receiver not found" }
val nowPos = LevelBoundVec3(receiver.getDimension(), receiver.pos)
return originPos.distance(nowPos)
}
abstract fun getMinDistance(): Double
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package net.superricky.tpaplusplus.async

import kotlinx.atomicfu.AtomicBoolean
import kotlinx.atomicfu.atomic
import net.minecraft.server.network.ServerPlayerEntity
import net.minecraft.text.Text
import net.superricky.tpaplusplus.config.CommonSpec
import net.superricky.tpaplusplus.config.Config
Expand All @@ -14,14 +15,20 @@ class AsyncCommandData(
) {
private var canceled: AtomicBoolean = atomic(false)
private var timeout = Config.getConfig()[CommonSpec.tpaTimeout].translateSecondToTick()
private var checkTarget = asyncRequest.sender

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

fun getDelay(): Double = asyncRequest.delay

fun updateCurrentPos() {
val sender = asyncRequest.sender
pos = LevelBoundVec3(sender.getDimension(), sender.pos)
val from = asyncRequest.from
from ?: return
pos = LevelBoundVec3(from.getDimension(), from.pos)
}

fun setCheckTarget(checkTarget: ServerPlayerEntity) {
this.checkTarget = checkTarget
}

fun updateDelay(delay: Double) {
Expand All @@ -48,6 +55,10 @@ class AsyncCommandData(
return
}
when (commandResult) {
AsyncCommandEvent.REQUEST_AFTER_DELAY -> {
AsyncCommandHelper.addCooldown(asyncRequest.sender.uuid, asyncRequest.commandType)
}

AsyncCommandEvent.REQUEST_OUT_DISTANCE -> {
asyncRequest.sender.sendMessage(
Text.translatable(
Expand All @@ -68,13 +79,20 @@ class AsyncCommandData(
)
}

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

fun cancel() {
canceled.value = true
}

fun checkWindupDistance(): Boolean {
val minDistance = asyncRequest.commandType.handler.getMinDistance()
if (minDistance < 0) {
return true
}
return pos.distance(LevelBoundVec3(checkTarget.getDimension(), checkTarget.pos)) <= minDistance
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ object AsyncCommandHelper : CoroutineScope {
}

fun addCooldown(uuid: UUID, type: AsyncCommandType) {
// Check if cooldown is disabled
if (type.handler.getCooldownTime() == 0.0) {
return
}
val playerData = underCooldown[uuid]
if (playerData == null) {
underCooldown[uuid] = mutableMapOf(
type to type.handler.getCooldownTime() * tickRate
)
playerData?.let {
it[type] = type.handler.getCooldownTime() * tickRate
return
}
playerData[type] = type.handler.getCooldownTime() * tickRate
underCooldown[uuid] = mutableMapOf(type to type.handler.getCooldownTime() * tickRate)
}

private fun asyncWindupCheck(
Expand All @@ -66,7 +68,7 @@ object AsyncCommandHelper : CoroutineScope {
val job = launch {
while (true) {
delay(tickDelay)
if (!asyncCommandData.getRequest().commandType.handler.checkWindupDistance(asyncCommandData)) {
if (!asyncCommandData.checkWindupDistance()) {
errorCallback?.invoke(asyncCommandData)
return@launch
}
Expand Down Expand Up @@ -182,6 +184,7 @@ object AsyncCommandHelper : CoroutineScope {
return@launch
}
asyncCommandData.updateCurrentPos()
asyncCommandData.setCheckTarget(asyncCommandData.getRequest().from!!)
asyncWindupCheck(
asyncCommandData,
successCallback = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,7 @@ object AcceptCommand : AsyncCommand(), BuildableCommand {

override fun getDelayTime(): Double = Config.getConfig()[CommandDelaySpec.acceptDelay]

override fun checkWindupDistance(asyncCommandData: AsyncCommandData): Boolean =
checkWindupDistance(
asyncCommandData,
::getSenderDistance,
Config.getConfig()[CommandDistanceSpec.acceptDistance]
)
override fun getMinDistance(): Double = Config.getConfig()[CommandDistanceSpec.acceptDistance]

private fun acceptCommandWithTarget(context: Context): Int {
fun asyncCommandCallback(result: AsyncCommandEvent, asyncCommandData: AsyncCommandData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package net.superricky.tpaplusplus.command.commands

import net.minecraft.server.command.CommandManager.literal
import net.superricky.tpaplusplus.async.AsyncCommand
import net.superricky.tpaplusplus.async.AsyncCommandData
import net.superricky.tpaplusplus.command.BuildableCommand
import net.superricky.tpaplusplus.config.Config
import net.superricky.tpaplusplus.config.command.CommandCooldownSpec
Expand All @@ -24,10 +23,5 @@ object BackCommand : AsyncCommand(), BuildableCommand {

override fun getDelayTime(): Double = Config.getConfig()[CommandDelaySpec.backDelay]

override fun checkWindupDistance(asyncCommandData: AsyncCommandData): Boolean =
checkWindupDistance(
asyncCommandData,
::getSenderDistance,
Config.getConfig()[CommandDistanceSpec.backDistance]
)
override fun getMinDistance(): Double = Config.getConfig()[CommandDistanceSpec.backDistance]
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import net.minecraft.server.command.CommandManager.literal
import net.minecraft.text.Text
import net.superricky.tpaplusplus.TpaPlusPlus
import net.superricky.tpaplusplus.async.AsyncCommand
import net.superricky.tpaplusplus.async.AsyncCommandData
import net.superricky.tpaplusplus.async.AsyncCommandHelper
import net.superricky.tpaplusplus.async.AsyncCommandType
import net.superricky.tpaplusplus.command.BuildableCommand
import net.superricky.tpaplusplus.command.CommandHelper.checkSenderReceiver
import net.superricky.tpaplusplus.command.CommandResult
Expand Down Expand Up @@ -38,13 +39,7 @@ object BlockCommand : AsyncCommand(), BuildableCommand {
override fun getCooldownTime(): Double = Config.getConfig()[CommandCooldownSpec.blockCooldown]

override fun getDelayTime(): Double = Config.getConfig()[CommandDelaySpec.blockDelay]

override fun checkWindupDistance(asyncCommandData: AsyncCommandData): Boolean =
checkWindupDistance(
asyncCommandData,
::getSenderDistance,
Config.getConfig()[CommandDistanceSpec.blockDistance]
)
override fun getMinDistance(): Double = Config.getConfig()[CommandDistanceSpec.blockDistance]

private fun blockPlayer(context: Context): Int {
val source = context.source
Expand Down Expand Up @@ -84,6 +79,7 @@ object BlockCommand : AsyncCommand(), BuildableCommand {
false
)
}
AsyncCommandHelper.addCooldown(sender.uuid, AsyncCommandType.BLOCK)
}
return CommandResult.NORMAL.status
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,7 @@ object CancelCommand : AsyncCommand(), BuildableCommand {
override fun getCooldownTime(): Double = Config.getConfig()[CommandCooldownSpec.cancelCooldown]

override fun getDelayTime(): Double = Config.getConfig()[CommandDelaySpec.cancelDelay]

override fun checkWindupDistance(asyncCommandData: AsyncCommandData): Boolean =
checkWindupDistance(
asyncCommandData,
::getSenderDistance,
Config.getConfig()[CommandDistanceSpec.cancelDistance]
)
override fun getMinDistance(): Double = Config.getConfig()[CommandDistanceSpec.cancelDistance]

private fun cancelRequestWithTarget(context: Context): Int {
fun asyncCommandCallback(result: AsyncCommandEvent, asyncCommandData: AsyncCommandData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,7 @@ object DenyCommand : AsyncCommand(), BuildableCommand {
override fun getCooldownTime(): Double = Config.getConfig()[CommandCooldownSpec.denyCooldown]

override fun getDelayTime(): Double = Config.getConfig()[CommandDelaySpec.denyDelay]

override fun checkWindupDistance(asyncCommandData: AsyncCommandData): Boolean =
checkWindupDistance(
asyncCommandData,
::getSenderDistance,
Config.getConfig()[CommandDistanceSpec.denyDistance]
)
override fun getMinDistance(): Double = Config.getConfig()[CommandDistanceSpec.denyDistance]

private fun refuseRequestWithTarget(context: Context): Int {
fun asyncCommandCallback(result: AsyncCommandEvent, asyncCommandData: AsyncCommandData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import net.minecraft.server.command.CommandManager.literal
import net.minecraft.text.Text
import net.superricky.tpaplusplus.TpaPlusPlus
import net.superricky.tpaplusplus.async.AsyncCommand
import net.superricky.tpaplusplus.async.AsyncCommandData
import net.superricky.tpaplusplus.async.AsyncCommandHelper
import net.superricky.tpaplusplus.async.AsyncCommandType
import net.superricky.tpaplusplus.command.BuildableCommand
import net.superricky.tpaplusplus.command.CommandResult
import net.superricky.tpaplusplus.config.Config
Expand Down Expand Up @@ -40,12 +41,7 @@ object ToggleCommand : AsyncCommand(), BuildableCommand {

override fun getDelayTime(): Double = Config.getConfig()[CommandDelaySpec.toggleDelay]

override fun checkWindupDistance(asyncCommandData: AsyncCommandData): Boolean =
checkWindupDistance(
asyncCommandData,
::getSenderDistance,
Config.getConfig()[CommandDistanceSpec.toggleDistance]
)
override fun getMinDistance(): Double = Config.getConfig()[CommandDistanceSpec.toggleDistance]

private fun switchToggle(context: Context): Int {
val source = context.source
Expand All @@ -58,6 +54,7 @@ object ToggleCommand : AsyncCommand(), BuildableCommand {
} else {
source.sendFeedback({ Text.translatable("command.toggle.success.off") }, false)
}
AsyncCommandHelper.addCooldown(sender.uuid, AsyncCommandType.TOGGLE)
}
return CommandResult.NORMAL.status
}
Expand All @@ -74,6 +71,7 @@ object ToggleCommand : AsyncCommand(), BuildableCommand {
sender.toggleOff()
source.sendFeedback({ Text.translatable("command.toggle.success.off") }, false)
}
AsyncCommandHelper.addCooldown(sender.uuid, AsyncCommandType.TOGGLE)
}
return CommandResult.NORMAL.status
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,25 @@ object TpaCommand : AsyncCommand(), BuildableCommand {
literal(commandName)
.then(
argument("player", EntityArgumentType.player())
.executes { tpaPlayer(it) }
.executes { tpaRequest(it) }
)
.build()

override fun getCooldownTime(): Double = Config.getConfig()[CommandCooldownSpec.tpaCooldown]

override fun getDelayTime(): Double = Config.getConfig()[CommandDelaySpec.tpaDelay]

override fun checkWindupDistance(asyncCommandData: AsyncCommandData): Boolean =
checkWindupDistance(
asyncCommandData,
::getSenderDistance,
Config.getConfig()[CommandDistanceSpec.tpaDistance]
)
override fun getMinDistance(): Double = Config.getConfig()[CommandDistanceSpec.tpaDistance]

@Suppress("LongMethod")
private fun asyncCommandCallback(result: AsyncCommandEvent, asyncCommandData: AsyncCommandData) {
private fun asyncCommandCallback(event: AsyncCommandEvent, asyncCommandData: AsyncCommandData) {
val asyncRequest = asyncCommandData.getRequest()
require(asyncRequest.receiver != null) { "Receiver cannot be null" }
when (result) {
when (event) {
AsyncCommandEvent.REQUEST_AFTER_DELAY -> {
asyncRequest.sender.sendMessageWithPlayerName("command.tpa.request.sender", asyncRequest.receiver)
asyncRequest.receiver.sendMessageWithPlayerName("command.tpa.request.receiver", asyncRequest.sender)
AsyncCommandHelper.addCooldown(asyncRequest.sender.uuid, AsyncCommandType.TPA)
}

AsyncCommandEvent.REQUEST_TIMEOUT -> {
Expand All @@ -66,12 +62,6 @@ object TpaCommand : AsyncCommand(), BuildableCommand {
asyncRequest.sender
)
AsyncCommandHelper.teleport(asyncCommandData)
if (AsyncCommandType.TPA.handler.getCooldownTime() != 0.0) {
AsyncCommandHelper.addCooldown(asyncRequest.sender.uuid, AsyncCommandType.TPA)
}
if (AsyncCommandType.ACCEPT.handler.getCooldownTime() != 0.0) {
AsyncCommandHelper.addCooldown(asyncRequest.receiver.uuid, AsyncCommandType.ACCEPT)
}
}

AsyncCommandEvent.REQUEST_CANCELED -> {
Expand Down Expand Up @@ -110,7 +100,7 @@ object TpaCommand : AsyncCommand(), BuildableCommand {
}
}

private fun tpaPlayer(context: Context): Int {
private fun tpaRequest(context: Context): Int {
val (result, sender, target) = checkSenderReceiver(context)
if (result != CommandResult.NORMAL) return result.status
sender!!
Expand Down
Loading

0 comments on commit 9ac689d

Please sign in to comment.