Skip to content

Commit

Permalink
feat: Finish command back, mod finished(maybe)
Browse files Browse the repository at this point in the history
  • Loading branch information
half-nothing committed Sep 27, 2024
1 parent 48a8b3d commit 9205c26
Show file tree
Hide file tree
Showing 24 changed files with 287 additions and 169 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ tasks {
val minecraftVersion: String by project
val loaderVersion: String by project
val kotlinLoaderVersion: String by project
val serverTranslatesApi: String by project

inputs.property("version", project.version)
inputs.property("minecraft_version", minecraftVersion)
inputs.property("loader_version", loaderVersion)
inputs.property("server_translates_api", serverTranslatesApi)
filteringCharset = "UTF-8"

filesMatching("fabric.mod.json") {
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ minecraftVersion=1.21.1
loaderVersion=0.16.5
kotlinLoaderVersion=1.12.1+kotlin.2.0.20
fabricVersion=0.104.0+1.21.1
serverTranslatesApi=2.3.1+1.21-pre2

modVersion = 1.0.0
mavenGroup = net.superricky
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ class AsyncCommandData(
asyncRequest.cooldown.translateTickToSecond()
)
}
if (asyncRequest.isTeleportRequest()) {
asyncCommandEventFactory
.addListener(AsyncCommandEvent.TELEPORT_OUT_DISTANCE) {
asyncRequest.from?.sendMessage(
Text.translatable("command.teleport.out_distance").setStyle(TextColorPallet.error)
)
}
.addListener(AsyncCommandEvent.TELEPORT_UPDATE_MESSAGE) {
asyncRequest.from?.sendTeleportTime(it.getRequest().delay)
}
}
}

fun needDelay(): Boolean = asyncRequest.delay != 0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import kotlinx.atomicfu.AtomicBoolean
import kotlinx.atomicfu.atomic
import kotlinx.coroutines.*
import net.minecraft.server.network.ServerPlayerEntity
import net.minecraft.text.Text
import net.superricky.tpaplusplus.GlobalConst.ONE_SECOND
import net.superricky.tpaplusplus.GlobalConst.logger
import net.superricky.tpaplusplus.TpaPlusPlus
import net.superricky.tpaplusplus.config.CommonSpec
import net.superricky.tpaplusplus.config.Config
import net.superricky.tpaplusplus.utility.TextColorPallet
import net.superricky.tpaplusplus.utility.getWorld
import java.util.*
import kotlin.coroutines.CoroutineContext

Expand Down Expand Up @@ -112,8 +116,9 @@ object AsyncCommandHelper : CoroutineScope {
}
if (!request.needDelay()) {
request.call(AsyncCommandEvent.REQUEST_AFTER_DELAY)
// If request not a teleport request, then there are no necessary to consider timeout
if (request.getRequest().isTeleportRequest()) {
// If request is not acceptable, which means it do not need someone to confirm execute
// then there are no necessary to consider request timeout
if (request.getRequest().acceptable()) {
requests.add(request)
}
return
Expand All @@ -122,7 +127,7 @@ object AsyncCommandHelper : CoroutineScope {
request,
successCallback = {
it.call(AsyncCommandEvent.REQUEST_AFTER_DELAY)
if (it.getRequest().isTeleportRequest()) {
if (it.getRequest().acceptable()) {
requests.add(it)
}
},
Expand Down Expand Up @@ -173,22 +178,47 @@ object AsyncCommandHelper : CoroutineScope {
} != null
}

private fun teleportPlayer(from: ServerPlayerEntity, to: ServerPlayerEntity) =
from.teleport(to.serverWorld, to.x, to.y, to.z, to.yaw, to.pitch)
private fun ServerPlayerEntity.backTeleport() {
val lastDeathPos = TpaPlusPlus.dataService.getPlayerData(this@backTeleport).lastDeathPos
this@backTeleport.teleport(
TpaPlusPlus.server.getWorld(lastDeathPos.world.getWorld()),
lastDeathPos.x,
lastDeathPos.y,
lastDeathPos.z,
lastDeathPos.yaw,
lastDeathPos.pitch
)
lastDeathPos.backed = true
this@backTeleport.sendMessage(
Text.translatable("command.back.teleported").setStyle(TextColorPallet.primary)
)
}

private fun ServerPlayerEntity.teleport(target: ServerPlayerEntity) {
this.teleport(target.serverWorld, target.x, target.y, target.z, target.yaw, target.pitch)
}

fun teleport(asyncCommandData: AsyncCommandData) {
launch {
val asyncRequest = asyncCommandData.getRequest()
if (Config.getConfig()[CommonSpec.waitTimeBeforeTp] == 0.0) {
teleportPlayer(asyncRequest.from!!, asyncRequest.to!!)
if (asyncRequest.commandType == AsyncCommandType.BACK) {
asyncRequest.sender.backTeleport()
} else {
asyncRequest.from!!.teleport(asyncRequest.to!!)
}
return@launch
}
asyncCommandData.updateCurrentPos()
asyncCommandData.setCheckTarget(asyncCommandData.getRequest().from!!)
asyncWindupCheck(
asyncCommandData,
successCallback = {
teleportPlayer(asyncRequest.from!!, asyncRequest.to!!)
if (asyncRequest.commandType == AsyncCommandType.BACK) {
asyncRequest.sender.backTeleport()
} else {
asyncRequest.from!!.teleport(asyncRequest.to!!)
}
it.cancel()
},
errorCallback = {
Expand Down
23 changes: 11 additions & 12 deletions src/main/kotlin/net/superricky/tpaplusplus/async/AsyncRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import net.minecraft.server.network.ServerPlayerEntity
* A class which store command request
*/
class AsyncRequest(
/**
* Command type
* @see AsyncCommandType
*/
val commandType: AsyncCommandType,
/**
* Player who send request, must exist
*/
Expand All @@ -14,12 +19,7 @@ class AsyncRequest(
* Player who receive request
* For some command this is null
*/
val receiver: ServerPlayerEntity?,
/**
* Command type
* @see AsyncCommandType
*/
val commandType: AsyncCommandType,
val receiver: ServerPlayerEntity? = null,
/**
* Player who will be teleported
* For some command this is null
Expand All @@ -34,12 +34,6 @@ class AsyncRequest(
var delay: Double = commandType.handler.getDelayTime()
var cooldown: Double = commandType.handler.getCooldownTime()

/**
* Check whether this request can execute teleport
* @return True if this command can execute teleport else return False
*/
fun canBeTeleported(): Boolean = isTeleportRequest() && from != null && to != null

/**
* Check whether this is a teleport request
* @return True is it is else False
Expand All @@ -49,6 +43,11 @@ class AsyncRequest(
else -> false
}

/**
* Check whether this teleport request is acceptable
*/
fun acceptable(): Boolean = isTeleportRequest() && commandType != AsyncCommandType.BACK

override fun toString(): String =
"Request{sender=${sender.name}, receiver=${receiver?.name}, from=$from, to=$to, commandType=$commandType"
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ object AcceptCommand : AsyncCommand(), BuildableCommand {
receiver!!
AsyncCommandHelper.schedule(
AsyncCommandData(
AsyncRequest(sender, receiver, AsyncCommandType.ACCEPT),
AsyncRequest(AsyncCommandType.ACCEPT, sender, receiver),
LevelBoundVec3(sender.getDimension(), sender.pos),
AsyncCommandEventFactory.addListener(AsyncCommandEvent.REQUEST_AFTER_DELAY) {
if (AsyncCommandHelper.acceptRequest(sender, receiver) == AsyncCommandEvent.REQUEST_NOT_FOUND) {
Expand All @@ -75,7 +75,7 @@ object AcceptCommand : AsyncCommand(), BuildableCommand {
sender ?: return CommandResult.SENDER_NOT_EXIST.status
AsyncCommandHelper.schedule(
AsyncCommandData(
AsyncRequest(sender, null, AsyncCommandType.ACCEPT),
AsyncRequest(AsyncCommandType.ACCEPT, sender),
LevelBoundVec3(sender.getDimension(), sender.pos),
AsyncCommandEventFactory.addListener(AsyncCommandEvent.REQUEST_AFTER_DELAY) {
if (AsyncCommandHelper.acceptRequest(sender) == AsyncCommandEvent.REQUEST_NOT_FOUND) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package net.superricky.tpaplusplus.command.commands

import net.minecraft.server.command.CommandManager.literal
import net.superricky.tpaplusplus.async.AsyncCommand
import net.minecraft.text.Text
import net.superricky.tpaplusplus.TpaPlusPlus
import net.superricky.tpaplusplus.async.*
import net.superricky.tpaplusplus.command.BuildableCommand
import net.superricky.tpaplusplus.command.CommandResult
import net.superricky.tpaplusplus.config.Config
import net.superricky.tpaplusplus.config.command.CommandCooldownSpec
import net.superricky.tpaplusplus.config.command.CommandDelaySpec
import net.superricky.tpaplusplus.config.command.CommandDistanceSpec
import net.superricky.tpaplusplus.config.command.CommandNameSpec
import net.superricky.tpaplusplus.utility.LiteralNode
import net.superricky.tpaplusplus.utility.*

object BackCommand : AsyncCommand(), BuildableCommand {
init {
Expand All @@ -17,11 +20,50 @@ object BackCommand : AsyncCommand(), BuildableCommand {

override fun build(): LiteralNode =
literal(commandName)
.executes { backRequest(it) }
.build()

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

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

override fun getMinDistance(): Double = Config.getConfig()[CommandDistanceSpec.backDistance]

private fun backRequest(context: Context): Int {
val source = context.source
val sender = source.player
sender ?: return CommandResult.SENDER_NOT_EXIST.status
val playerData = TpaPlusPlus.dataService.getPlayerData(sender)
val lastDeathPos = playerData.lastDeathPos
if (lastDeathPos.backed) {
sender.sendMessage(
Text.translatable("command.back.death_not_found").setStyle(TextColorPallet.error)
)
return CommandResult.NORMAL.status
}
if (!LimitationHelper.checkDimensionLimitation(sender, lastDeathPos.world.getWorld())) {
sender.sendMessage(
Text.translatable(
"command.error.cross_dim",
sender.getDimension().value.toString().literal().setStyle(TextColorPallet.errorVariant),
lastDeathPos.world.getWorld().value.toString().literal().setStyle(TextColorPallet.errorVariant)
).setStyle(TextColorPallet.error)
)
return CommandResult.NORMAL.status
}
AsyncCommandHelper.schedule(
AsyncCommandData(
AsyncRequest(AsyncCommandType.BACK, sender, null, sender),
LevelBoundVec3(sender.getDimension(), sender.pos),
AsyncCommandEventFactory
.addListener(AsyncCommandEvent.REQUEST_AFTER_DELAY) {
sender.sendMessage(
Text.translatable("command.back.teleporting").setStyle(TextColorPallet.primary)
)
AsyncCommandHelper.teleport(it)
}
)
)
return CommandResult.NORMAL.status
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ import kotlinx.coroutines.launch
import net.minecraft.command.argument.EntityArgumentType
import net.minecraft.server.command.CommandManager.argument
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.AsyncCommandHelper
import net.superricky.tpaplusplus.async.AsyncCommandType
import net.superricky.tpaplusplus.async.*
import net.superricky.tpaplusplus.command.BuildableCommand
import net.superricky.tpaplusplus.command.CommandHelper.checkSenderReceiver
import net.superricky.tpaplusplus.command.CommandResult
Expand All @@ -18,10 +15,7 @@ import net.superricky.tpaplusplus.config.command.CommandCooldownSpec
import net.superricky.tpaplusplus.config.command.CommandDelaySpec
import net.superricky.tpaplusplus.config.command.CommandDistanceSpec
import net.superricky.tpaplusplus.config.command.CommandNameSpec
import net.superricky.tpaplusplus.utility.Context
import net.superricky.tpaplusplus.utility.LiteralNode
import net.superricky.tpaplusplus.utility.TextColorPallet
import net.superricky.tpaplusplus.utility.getColoredName
import net.superricky.tpaplusplus.utility.*

object BlockCommand : AsyncCommand(), BuildableCommand {
init {
Expand All @@ -42,45 +36,29 @@ object BlockCommand : AsyncCommand(), BuildableCommand {
override fun getMinDistance(): Double = Config.getConfig()[CommandDistanceSpec.blockDistance]

private fun blockPlayer(context: Context): Int {
val source = context.source
val (result, sender, target) = checkSenderReceiver(context)
val (result, sender, receiver) = checkSenderReceiver(context)
if (result != CommandResult.NORMAL) return result.status
sender!!
target!!
TpaPlusPlus.launch {
if (TpaPlusPlus.dataService.addBlockPlayer(sender.uuid, target.uuid)) {
source.sendFeedback(
{
Text.translatable(
"command.block.success",
target.getColoredName(TextColorPallet.secondary)
)
.setStyle(TextColorPallet.primary)
},
false
)
if (Config.getConfig()[CommonSpec.showBlockedMessage]) {
target.sendMessage(
Text.translatable(
"command.block.be_blocked",
sender.getColoredName(TextColorPallet.secondary)
).setStyle(TextColorPallet.primary)
)
}
} else {
source.sendFeedback(
{
Text.translatable(
"command.block.fail",
target.getColoredName(TextColorPallet.secondary)
)
.setStyle(TextColorPallet.primary)
},
false
)
}
AsyncCommandHelper.addCooldown(sender.uuid, AsyncCommandType.BLOCK)
}
receiver!!
AsyncCommandHelper.schedule(
AsyncCommandData(
AsyncRequest(AsyncCommandType.BLOCK, sender, receiver),
LevelBoundVec3(sender.getDimension(), sender.pos),
AsyncCommandEventFactory
.addListener(AsyncCommandEvent.REQUEST_AFTER_DELAY) {
TpaPlusPlus.launch {
if (TpaPlusPlus.dataService.addBlockPlayer(sender.uuid, receiver.uuid)) {
sender.sendMessageWithPlayerName("command.block.success", receiver)
if (Config.getConfig()[CommonSpec.showBlockedMessage]) {
receiver.sendMessageWithPlayerName("command.block.be_blocked", sender)
}
} else {
sender.sendMessageWithPlayerName("command.block.fail", receiver)
}
}
}
)
)
return CommandResult.NORMAL.status
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ object CancelCommand : AsyncCommand(), BuildableCommand {
receiver!!
AsyncCommandHelper.schedule(
AsyncCommandData(
AsyncRequest(sender, receiver, AsyncCommandType.CANCEL),
AsyncRequest(AsyncCommandType.CANCEL, sender, receiver),
LevelBoundVec3(sender.getDimension(), sender.pos),
AsyncCommandEventFactory.addListener(AsyncCommandEvent.REQUEST_AFTER_DELAY) {
if (AsyncCommandHelper.cancelRequest(sender, receiver) == AsyncCommandEvent.REQUEST_NOT_FOUND) {
Expand All @@ -63,7 +63,7 @@ object CancelCommand : AsyncCommand(), BuildableCommand {
sender ?: return CommandResult.SENDER_NOT_EXIST.status
AsyncCommandHelper.schedule(
AsyncCommandData(
AsyncRequest(sender, null, AsyncCommandType.CANCEL),
AsyncRequest(AsyncCommandType.CANCEL, sender),
LevelBoundVec3(sender.getDimension(), sender.pos),
AsyncCommandEventFactory.addListener(AsyncCommandEvent.REQUEST_AFTER_DELAY) {
if (AsyncCommandHelper.cancelRequest(sender) == AsyncCommandEvent.REQUEST_NOT_FOUND) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ object DenyCommand : AsyncCommand(), BuildableCommand {
receiver!!
AsyncCommandHelper.schedule(
AsyncCommandData(
AsyncRequest(sender, receiver, AsyncCommandType.DENY),
AsyncRequest(AsyncCommandType.DENY, sender, receiver),
LevelBoundVec3(sender.getDimension(), sender.pos),
AsyncCommandEventFactory.addListener(AsyncCommandEvent.REQUEST_AFTER_DELAY) {
if (AsyncCommandHelper.refuseRequest(sender, receiver) == AsyncCommandEvent.REQUEST_NOT_FOUND) {
Expand All @@ -63,7 +63,7 @@ object DenyCommand : AsyncCommand(), BuildableCommand {
sender ?: return CommandResult.SENDER_NOT_EXIST.status
AsyncCommandHelper.schedule(
AsyncCommandData(
AsyncRequest(sender, null, AsyncCommandType.DENY),
AsyncRequest(AsyncCommandType.DENY, sender),
LevelBoundVec3(sender.getDimension(), sender.pos),
AsyncCommandEventFactory.addListener(AsyncCommandEvent.REQUEST_AFTER_DELAY) {
if (AsyncCommandHelper.refuseRequest(sender) == AsyncCommandEvent.REQUEST_NOT_FOUND) {
Expand Down
Loading

0 comments on commit 9205c26

Please sign in to comment.