Skip to content

Commit

Permalink
refactor: Refactored the message sending logic to customize any messa…
Browse files Browse the repository at this point in the history
…ge you send (has not been tested yet)
  • Loading branch information
half-nothing committed Oct 9, 2024
1 parent 664ae94 commit 9dc279b
Show file tree
Hide file tree
Showing 21 changed files with 209 additions and 198 deletions.
11 changes: 5 additions & 6 deletions src/main/kotlin/net/superricky/tpaplusplus/TpaPlusPlus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import net.superricky.tpaplusplus.command.CommandRegister
import net.superricky.tpaplusplus.config.config.AdvancedSpec
import net.superricky.tpaplusplus.config.config.CommonSpec
import net.superricky.tpaplusplus.config.config.Config
import net.superricky.tpaplusplus.config.config.Config.get
import net.superricky.tpaplusplus.config.language.LanguageConfig
import net.superricky.tpaplusplus.config.language.SystemSpec
import net.superricky.tpaplusplus.database.DataManager
import net.superricky.tpaplusplus.database.service.DataService
import java.nio.file.Files
Expand Down Expand Up @@ -83,13 +83,12 @@ object TpaPlusPlus : ModInitializer, CoroutineScope {

logger.info("Loading lang file...")
try {
LanguageConfig.loadLangFile(Config.getConfig()[CommonSpec.language])
LanguageConfig.loadLangFile(CommonSpec.language.get())
logger.info("Language file loaded.")
} catch (e: Exception) {
logger.error("Error while loading lang file", e)
return
}
logger.info(LanguageConfig.getConfig()[SystemSpec.version])

logger.info("Register commands...")
CommandRegistrationEvent.EVENT.register { dispatcher, _, _ ->
Expand Down Expand Up @@ -117,13 +116,13 @@ object TpaPlusPlus : ModInitializer, CoroutineScope {
PlayerEvent.PLAYER_JOIN.register(PlayerEventListener::joinEvent)
EntityEvent.LIVING_DEATH.register(PlayerEventListener::deathEvent)

if (Config.getConfig()[AdvancedSpec.unblockingTickLoop]) {
if (AdvancedSpec.unblockingTickLoop.get()) {
logger.info("Using non blocking tick loop")
logger.info(
"Initializing non blocking tick loop with rate of " +
"${Config.getConfig()[AdvancedSpec.asyncLoopRate]} per second"
"${AdvancedSpec.asyncLoopRate.get()} per second"
)
AsyncCommandHelper.startTickLoop(Config.getConfig()[AdvancedSpec.asyncLoopRate])
AsyncCommandHelper.startTickLoop(AdvancedSpec.asyncLoopRate.get())
} else {
logger.info("Using synchronous tick loop")
logger.info("Registering server post tick event")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ 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.config.CommonSpec
import net.superricky.tpaplusplus.config.config.Config
import net.superricky.tpaplusplus.config.config.Config.get
import net.superricky.tpaplusplus.config.language.LanguageConfig.getMutableText
import net.superricky.tpaplusplus.config.language.TeleportSpec
import net.superricky.tpaplusplus.config.language.WindupSpec
import net.superricky.tpaplusplus.utility.*

class AsyncCommandData(
Expand All @@ -14,7 +16,7 @@ class AsyncCommandData(
private val asyncCommandEventFactory: AsyncCommandEventFactory
) {
private var canceled: AtomicBoolean = atomic(false)
private var timeout = Config.getConfig()[CommonSpec.tpaTimeout].translateSecondToTick()
private var timeout = CommonSpec.tpaTimeout.get().translateSecondToTick()
private var checkTarget = asyncRequest.sender

init {
Expand All @@ -27,8 +29,7 @@ class AsyncCommandData(
}
.addListener(AsyncCommandEvent.REQUEST_OUT_DISTANCE) {
asyncRequest.sender.sendMessage(
Text.translatable(
"command.windup.error.out_distance",
WindupSpec.outDistance.getMutableText(
asyncRequest.commandType.handler.getCommandName()
).setStyle(TextColorPallet.error)
)
Expand All @@ -46,7 +47,7 @@ class AsyncCommandData(
asyncCommandEventFactory
.addListener(AsyncCommandEvent.TELEPORT_OUT_DISTANCE) {
asyncRequest.from?.sendMessage(
Text.translatable("command.teleport.out_distance").setStyle(TextColorPallet.error)
TeleportSpec.outDistance.getMutableText().setStyle(TextColorPallet.error)
)
}
.addListener(AsyncCommandEvent.TELEPORT_UPDATE_MESSAGE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ 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.config.CommonSpec
import net.superricky.tpaplusplus.config.config.Config
import net.superricky.tpaplusplus.config.config.Config.get
import net.superricky.tpaplusplus.config.language.LanguageConfig.getMutableText
import net.superricky.tpaplusplus.config.language.command.BackSpec
import net.superricky.tpaplusplus.utility.TextColorPallet
import net.superricky.tpaplusplus.utility.getWorld
import java.util.*
Expand Down Expand Up @@ -190,7 +191,7 @@ object AsyncCommandHelper : CoroutineScope {
)
lastDeathPos.backed = true
this@backTeleport.sendMessage(
Text.translatable("command.back.teleported").setStyle(TextColorPallet.primary)
BackSpec.teleported.getMutableText().setStyle(TextColorPallet.primary)
)
}

Expand All @@ -201,7 +202,7 @@ object AsyncCommandHelper : CoroutineScope {
fun teleport(asyncCommandData: AsyncCommandData) {
launch {
val asyncRequest = asyncCommandData.getRequest()
if (Config.getConfig()[CommonSpec.waitTimeBeforeTp] == 0.0) {
if (CommonSpec.waitTimeBeforeTp.get() == 0.0) {
if (asyncRequest.commandType == AsyncCommandType.BACK) {
asyncRequest.sender.backTeleport()
} else {
Expand All @@ -228,7 +229,7 @@ object AsyncCommandHelper : CoroutineScope {
progressCallback = {
it.call(AsyncCommandEvent.TELEPORT_UPDATE_MESSAGE)
},
Config.getConfig()[CommonSpec.waitTimeBeforeTp]
CommonSpec.waitTimeBeforeTp.get()
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import net.minecraft.text.Text
import net.superricky.tpaplusplus.TpaPlusPlus
import net.superricky.tpaplusplus.async.AsyncCommandHelper
import net.superricky.tpaplusplus.async.AsyncCommandType
import net.superricky.tpaplusplus.config.language.LanguageConfig.getMutableText
import net.superricky.tpaplusplus.config.language.error.ErrorRequestSpec
import net.superricky.tpaplusplus.config.language.error.ErrorSpec
import net.superricky.tpaplusplus.utility.Context
import net.superricky.tpaplusplus.utility.TextColorPallet
import net.superricky.tpaplusplus.utility.sendMessageWithPlayerName
Expand All @@ -27,17 +30,17 @@ object CommandHelper {
val result = checkSenderReceiver(sender, receiver)
return when (result) {
CommandResult.SELF_CHECK_ERROR -> {
source.sendError(Text.translatable("command.error.self"))
source.sendError(ErrorSpec.selfCommand.getMutableText())
Triple(CommandResult.SELF_CHECK_ERROR, null, null)
}

CommandResult.SENDER_NOT_EXIST -> {
source.sendError(Text.translatable("command.error.sender.not_exist"))
source.sendError(ErrorSpec.senderNotExist.getMutableText())
Triple(CommandResult.SENDER_NOT_EXIST, null, null)
}

CommandResult.RECEIVER_NOT_EXIST -> {
source.sendError(Text.translatable("command.error.target.not_exist"))
source.sendError(ErrorSpec.receiverNotExist.getMutableText())
Triple(CommandResult.RECEIVER_NOT_EXIST, null, null)
}

Expand All @@ -58,11 +61,11 @@ object CommandHelper {

fun checkBlocked(sender: ServerPlayerEntity, receiver: ServerPlayerEntity): Boolean {
if (TpaPlusPlus.dataService.checkPlayerBlocked(receiver.uuid, sender.uuid)) {
sender.sendMessageWithPlayerName("command.error.request.blocked.sender", receiver)
sender.sendMessageWithPlayerName(ErrorRequestSpec.blockedSender, receiver)
return true
}
if (TpaPlusPlus.dataService.checkPlayerBlocked(sender.uuid, receiver.uuid)) {
sender.sendMessageWithPlayerName("command.error.request.blocked.receiver", receiver)
sender.sendMessageWithPlayerName(ErrorRequestSpec.blockedReceiver, receiver)
return true
}
return false
Expand All @@ -71,12 +74,13 @@ object CommandHelper {
fun checkToggled(sender: ServerPlayerEntity, receiver: ServerPlayerEntity): Boolean {
if (TpaPlusPlus.dataService.checkPlayerToggle(sender.uuid)) {
sender.sendMessage(
Text.translatable("command.error.request.toggled.sender").setStyle(TextColorPallet.error)
ErrorRequestSpec.toggledSender.getMutableText()
.setStyle(TextColorPallet.error)
)
return true
}
if (TpaPlusPlus.dataService.checkPlayerToggle(receiver.uuid)) {
sender.sendMessageWithPlayerName("command.error.request.toggled.receiver", receiver)
sender.sendMessageWithPlayerName(ErrorRequestSpec.toggledReceiver, receiver)
return true
}
return false
Expand All @@ -88,7 +92,7 @@ object CommandHelper {
commandType: AsyncCommandType
): Boolean {
if (AsyncCommandHelper.checkRequestExist(sender, receiver, commandType)) {
sender.sendMessageWithPlayerName("command.error.request.exist", receiver)
sender.sendMessageWithPlayerName(ErrorRequestSpec.requestExist, receiver)
return true
}
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import net.superricky.tpaplusplus.GlobalConst
import net.superricky.tpaplusplus.GlobalConst.logger
import net.superricky.tpaplusplus.command.commands.*
import net.superricky.tpaplusplus.command.subcommands.RootCommand
import net.superricky.tpaplusplus.config.config.Config
import net.superricky.tpaplusplus.config.config.Config.get
import net.superricky.tpaplusplus.config.config.command.CommandEnableSpec
import net.superricky.tpaplusplus.config.config.command.CommandNameSpec
import net.superricky.tpaplusplus.utility.Dispatcher

object CommandRegister {
Expand All @@ -16,54 +15,52 @@ object CommandRegister {
logger.info("Register command /${GlobalConst.MOD_ID}...")
dispatcher.root.addChild(rootNode)

val config = Config.getConfig()

if (config[CommandEnableSpec.backEnable]) {
logger.info("Register command /${config[CommandNameSpec.backCommand]}...")
if (CommandEnableSpec.backEnable.get()) {
logger.info("Register command /${BackCommand.getCommandName()}...")
dispatcher.root.addChild(BackCommand.build())
}

if (config[CommandEnableSpec.tpaunblockEnable]) {
logger.info("Register command /${config[CommandNameSpec.tpaunblockCommand]}...")
if (CommandEnableSpec.tpaunblockEnable.get()) {
logger.info("Register command /${UnblockCommand.getCommandName()}...")
dispatcher.root.addChild(UnblockCommand.build())
}

if (config[CommandEnableSpec.tpablockEnable]) {
logger.info("Register command /${config[CommandNameSpec.tpablockCommand]}...")
if (CommandEnableSpec.tpablockEnable.get()) {
logger.info("Register command /${BlockCommand.getCommandName()}...")
dispatcher.root.addChild(BlockCommand.build())
}

if (config[CommandEnableSpec.tpatoggleEnable]) {
logger.info("Register command /${config[CommandNameSpec.tpatoggleCommand]}...")
if (CommandEnableSpec.tpatoggleEnable.get()) {
logger.info("Register command /${ToggleCommand.getCommandName()}...")
dispatcher.root.addChild(ToggleCommand.build())
}

if (config[CommandEnableSpec.tpaEnable]) {
logger.info("Register command /${config[CommandNameSpec.tpaCommand]}...")
if (CommandEnableSpec.tpaEnable.get()) {
logger.info("Register command /${TpaCommand.getCommandName()}...")
dispatcher.root.addChild(TpaCommand.build())
}

if (config[CommandEnableSpec.tpahereEnable]) {
logger.info("Register command /${config[CommandNameSpec.tpahereCommand]}...")
if (CommandEnableSpec.tpahereEnable.get()) {
logger.info("Register command /${TpaHereCommand.getCommandName()}...")
dispatcher.root.addChild(TpaHereCommand.build())
}

if (!config[CommandEnableSpec.tpaEnable] && !config[CommandEnableSpec.tpahereEnable]) {
if (!CommandEnableSpec.tpaEnable.get() && !CommandEnableSpec.tpahereEnable.get()) {
return
}

if (config[CommandEnableSpec.tpacceptEnable]) {
logger.info("Register command /${config[CommandNameSpec.tpacceptCommand]}...")
if (CommandEnableSpec.tpacceptEnable.get()) {
logger.info("Register command /${AcceptCommand.getCommandName()}...")
dispatcher.root.addChild(AcceptCommand.build())
}

if (config[CommandEnableSpec.tpadenyEnable]) {
logger.info("Register command /${config[CommandNameSpec.tpadenyCommand]}...")
if (CommandEnableSpec.tpadenyEnable.get()) {
logger.info("Register command /${DenyCommand.getCommandName()}...")
dispatcher.root.addChild(DenyCommand.build())
}

if (config[CommandEnableSpec.tpacancelEnable]) {
logger.info("Register command /${config[CommandNameSpec.tpacancelCommand]}...")
if (CommandEnableSpec.tpacancelEnable.get()) {
logger.info("Register command /${CancelCommand.getCommandName()}...")
dispatcher.root.addChild(CancelCommand.build())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import net.superricky.tpaplusplus.command.BuildableCommand
import net.superricky.tpaplusplus.command.CommandHelper
import net.superricky.tpaplusplus.command.CommandHelper.checkSenderReceiver
import net.superricky.tpaplusplus.command.CommandResult
import net.superricky.tpaplusplus.config.config.Config
import net.superricky.tpaplusplus.config.config.Config.get
import net.superricky.tpaplusplus.config.config.command.CommandCooldownSpec
import net.superricky.tpaplusplus.config.config.command.CommandDelaySpec
import net.superricky.tpaplusplus.config.config.command.CommandDistanceSpec
Expand All @@ -20,7 +20,7 @@ import net.superricky.tpaplusplus.utility.getDimension

object AcceptCommand : AsyncCommand(), BuildableCommand {
init {
commandName = Config.getConfig()[CommandNameSpec.tpacceptCommand]
commandName = CommandNameSpec.tpacceptCommand.get()
}

override fun build(): LiteralNode =
Expand All @@ -32,11 +32,11 @@ object AcceptCommand : AsyncCommand(), BuildableCommand {
.executes { acceptCommand(it) }
.build()

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

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

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

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

import net.minecraft.server.command.CommandManager.literal
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.Config
import net.superricky.tpaplusplus.config.config.Config.get
import net.superricky.tpaplusplus.config.config.command.CommandCooldownSpec
import net.superricky.tpaplusplus.config.config.command.CommandDelaySpec
import net.superricky.tpaplusplus.config.config.command.CommandDistanceSpec
import net.superricky.tpaplusplus.config.config.command.CommandNameSpec
import net.superricky.tpaplusplus.config.language.LanguageConfig.getMutableText
import net.superricky.tpaplusplus.config.language.command.BackSpec
import net.superricky.tpaplusplus.config.language.error.ErrorSpec
import net.superricky.tpaplusplus.utility.*

object BackCommand : AsyncCommand(), BuildableCommand {
init {
commandName = Config.getConfig()[CommandNameSpec.backCommand]
commandName = CommandNameSpec.backCommand.get()
}

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

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

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

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

private fun backRequest(context: Context): Int {
val source = context.source
Expand All @@ -37,14 +39,13 @@ object BackCommand : AsyncCommand(), BuildableCommand {
val lastDeathPos = playerData.lastDeathPos
if (lastDeathPos.backed) {
sender.sendMessage(
Text.translatable("command.back.death_not_found").setStyle(TextColorPallet.error)
BackSpec.deathNotFound.getMutableText().setStyle(TextColorPallet.error)
)
return CommandResult.NORMAL.status
}
if (!LimitationHelper.checkDimensionLimitation(sender, lastDeathPos.world.getWorld())) {
sender.sendMessage(
Text.translatable(
"command.error.cross_dim",
ErrorSpec.crossDim.getMutableText(
sender.getDimension().value.toString().literal().setStyle(TextColorPallet.errorVariant),
lastDeathPos.world.getWorld().value.toString().literal().setStyle(TextColorPallet.errorVariant)
).setStyle(TextColorPallet.error)
Expand All @@ -58,7 +59,7 @@ object BackCommand : AsyncCommand(), BuildableCommand {
AsyncCommandEventFactory
.addListener(AsyncCommandEvent.REQUEST_AFTER_DELAY) {
sender.sendMessage(
Text.translatable("command.back.teleporting").setStyle(TextColorPallet.primary)
BackSpec.teleporting.getMutableText().setStyle(TextColorPallet.primary)
)
AsyncCommandHelper.teleport(it)
}
Expand Down
Loading

0 comments on commit 9dc279b

Please sign in to comment.