Skip to content

Commit

Permalink
feat(command): Add command (Not yet implemented, just register them)
Browse files Browse the repository at this point in the history
  • Loading branch information
half-nothing committed Sep 13, 2024
1 parent 1fe42dd commit af5ff5a
Show file tree
Hide file tree
Showing 22 changed files with 311 additions and 46 deletions.
2 changes: 0 additions & 2 deletions detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ comments:
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/jsTest/**', '**/iosTest/**']
DeprecatedBlockTag:
active: true
UndocumentedPublicFunction:
active: true

complexity:
active: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package net.superricky.tpaplusplus
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger

object Const {
object GlobalConst {
const val MOD_ID = "tpaplusplus"
const val CONFIG_FOLDER_PATH = MOD_ID
const val CONFIG_FILE_NAME = "$MOD_ID.toml"
const val CONFIG_FILE_PATH = "$CONFIG_FOLDER_PATH/$CONFIG_FILE_NAME"
const val GITHUB_URL = "https://github.com/SuperRicky14/TpaPlusPlus"
const val MODRINTH_URL = "https://modrinth.com/mod/tpa++"
const val COURSE_FORGE_URL = "https://www.curseforge.com/minecraft/mc-mods/tpaplusplus"
val logger: Logger = LogManager.getLogger(MOD_ID)
}
44 changes: 16 additions & 28 deletions src/main/kotlin/net/superricky/tpaplusplus/TpaPlusPlus.kt
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
package net.superricky.tpaplusplus

import kotlinx.coroutines.*
import dev.architectury.event.events.common.CommandRegistrationEvent
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import net.fabricmc.api.ModInitializer
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents
import net.fabricmc.loader.api.FabricLoader
import net.fabricmc.loader.api.Version
import net.minecraft.server.MinecraftServer
import net.minecraft.text.Text
import net.superricky.tpaplusplus.Const.CONFIG_FILE_NAME
import net.superricky.tpaplusplus.Const.CONFIG_FILE_PATH
import net.superricky.tpaplusplus.Const.CONFIG_FOLDER_PATH
import net.superricky.tpaplusplus.Const.MOD_ID
import net.superricky.tpaplusplus.Const.logger
import net.superricky.tpaplusplus.GlobalConst.CONFIG_FILE_NAME
import net.superricky.tpaplusplus.GlobalConst.CONFIG_FILE_PATH
import net.superricky.tpaplusplus.GlobalConst.CONFIG_FOLDER_PATH
import net.superricky.tpaplusplus.GlobalConst.MOD_ID
import net.superricky.tpaplusplus.GlobalConst.logger
import net.superricky.tpaplusplus.command.CommandRegister
import net.superricky.tpaplusplus.config.Config
import net.superricky.tpaplusplus.utility.TextColorPallet
import java.nio.file.Files
import kotlin.coroutines.CoroutineContext

object TpaPlusPlus : ModInitializer, CoroutineScope {
private lateinit var server: MinecraftServer
override val coroutineContext: CoroutineContext = Dispatchers.IO
private lateinit var job: Job
val version: Version = FabricLoader.getInstance().getModContainer(MOD_ID).get().metadata.version

override fun onInitialize() {
val version = FabricLoader.getInstance().getModContainer(MOD_ID).get().metadata.version
logger.info("Initializing TPA++ ${version.friendlyString}")

if (!Files.isDirectory(FabricLoader.getInstance().configDir.resolve(CONFIG_FOLDER_PATH))) {
Expand All @@ -45,34 +46,21 @@ object TpaPlusPlus : ModInitializer, CoroutineScope {
logger.error("Error while loading config file", e)
return
}
CommandRegistrationEvent.EVENT.register { dispatcher, _, _ ->
CommandRegister.registerCommands(
dispatcher
)
}
ServerLifecycleEvents.SERVER_STARTING.register(::serverStarting)
ServerLifecycleEvents.SERVER_STOPPED.register(::serverStopped)
}

@Suppress("MagicNumber")
private fun sendTest() {
job = TpaPlusPlus.launch {
delay(5000)
logger.info("TPA plus test")
server.playerManager.playerList.forEach { player ->
run {
player.sendMessage(Text.translatable("test").setStyle(TextColorPallet.primary))
}
}
sendTest()
}
}

private fun serverStarting(server: MinecraftServer) {
logger.info("Starting TPA++ server")
this.server = server
job = TpaPlusPlus.launch {
sendTest()
}
}

private fun serverStopped(ignored: MinecraftServer) {
logger.info("Shutting down TPA++")
job.cancel()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.superricky.tpaplusplus.command

import net.superricky.tpaplusplus.utility.LiteralNode

interface BuildableCommand {
fun build(): LiteralNode
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package net.superricky.tpaplusplus.command

import net.superricky.tpaplusplus.command.commands.*
import net.superricky.tpaplusplus.command.subcommands.RootCommand
import net.superricky.tpaplusplus.utility.Dispatcher

object CommandRegister {
fun registerCommands(dispatcher: Dispatcher) {
val rootNode = RootCommand.build()

dispatcher.root.addChild(rootNode)

dispatcher.root.addChild(AcceptCommand.build())
dispatcher.root.addChild(BackCommand.build())
dispatcher.root.addChild(CancelCommand.build())
dispatcher.root.addChild(DenyCommand.build())
dispatcher.root.addChild(ToggleCommand.build())
dispatcher.root.addChild(TpaCommand.build())
dispatcher.root.addChild(TpaHereCommand.build())
dispatcher.root.addChild(UnblockCommand.build())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package net.superricky.tpaplusplus.command.commands

import net.minecraft.server.command.CommandManager.literal
import net.superricky.tpaplusplus.command.BuildableCommand
import net.superricky.tpaplusplus.config.Config
import net.superricky.tpaplusplus.config.command.CommandNameSpec
import net.superricky.tpaplusplus.utility.LiteralNode

object AcceptCommand : BuildableCommand {
override fun build(): LiteralNode =
literal(Config.getConfig()[CommandNameSpec.tpaacceptCommand])
.build()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package net.superricky.tpaplusplus.command.commands

import net.minecraft.server.command.CommandManager.literal
import net.superricky.tpaplusplus.command.BuildableCommand
import net.superricky.tpaplusplus.config.Config
import net.superricky.tpaplusplus.config.command.CommandNameSpec
import net.superricky.tpaplusplus.utility.LiteralNode

object BackCommand : BuildableCommand {
override fun build(): LiteralNode =
literal(Config.getConfig()[CommandNameSpec.backCommand])
.build()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package net.superricky.tpaplusplus.command.commands

import net.minecraft.server.command.CommandManager.literal
import net.superricky.tpaplusplus.command.BuildableCommand
import net.superricky.tpaplusplus.config.Config
import net.superricky.tpaplusplus.config.command.CommandNameSpec
import net.superricky.tpaplusplus.utility.LiteralNode

object BlockCommand : BuildableCommand {
override fun build(): LiteralNode =
literal(Config.getConfig()[CommandNameSpec.tpablockCommand])
.build()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package net.superricky.tpaplusplus.command.commands

import net.minecraft.server.command.CommandManager.literal
import net.superricky.tpaplusplus.command.BuildableCommand
import net.superricky.tpaplusplus.config.Config
import net.superricky.tpaplusplus.config.command.CommandNameSpec
import net.superricky.tpaplusplus.utility.LiteralNode

object CancelCommand : BuildableCommand {
override fun build(): LiteralNode =
literal(Config.getConfig()[CommandNameSpec.tpacancelCommand])
.build()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package net.superricky.tpaplusplus.command.commands

import net.minecraft.server.command.CommandManager.literal
import net.superricky.tpaplusplus.command.BuildableCommand
import net.superricky.tpaplusplus.config.Config
import net.superricky.tpaplusplus.config.command.CommandNameSpec
import net.superricky.tpaplusplus.utility.LiteralNode

object DenyCommand : BuildableCommand {
override fun build(): LiteralNode =
literal(Config.getConfig()[CommandNameSpec.tpadenyCommand])
.build()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package net.superricky.tpaplusplus.command.commands

import net.minecraft.server.command.CommandManager.literal
import net.superricky.tpaplusplus.command.BuildableCommand
import net.superricky.tpaplusplus.config.Config
import net.superricky.tpaplusplus.config.command.CommandNameSpec
import net.superricky.tpaplusplus.utility.LiteralNode

object ToggleCommand : BuildableCommand {
override fun build(): LiteralNode =
literal(Config.getConfig()[CommandNameSpec.tpatoggleCommand])
.build()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package net.superricky.tpaplusplus.command.commands

import net.minecraft.server.command.CommandManager.literal
import net.superricky.tpaplusplus.command.BuildableCommand
import net.superricky.tpaplusplus.config.Config
import net.superricky.tpaplusplus.config.command.CommandNameSpec
import net.superricky.tpaplusplus.utility.LiteralNode

object TpaCommand : BuildableCommand {
override fun build(): LiteralNode =
literal(Config.getConfig()[CommandNameSpec.tpaCommand])
.build()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package net.superricky.tpaplusplus.command.commands

import net.minecraft.server.command.CommandManager.literal
import net.superricky.tpaplusplus.command.BuildableCommand
import net.superricky.tpaplusplus.config.Config
import net.superricky.tpaplusplus.config.command.CommandNameSpec
import net.superricky.tpaplusplus.utility.LiteralNode

object TpaHereCommand : BuildableCommand {
override fun build(): LiteralNode =
literal(Config.getConfig()[CommandNameSpec.tpahereCommand])
.build()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package net.superricky.tpaplusplus.command.commands

import net.minecraft.server.command.CommandManager.literal
import net.superricky.tpaplusplus.command.BuildableCommand
import net.superricky.tpaplusplus.config.Config
import net.superricky.tpaplusplus.config.command.CommandNameSpec
import net.superricky.tpaplusplus.utility.LiteralNode

object UnblockCommand : BuildableCommand {
override fun build(): LiteralNode =
literal(Config.getConfig()[CommandNameSpec.tpaunblockCommand])
.build()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package net.superricky.tpaplusplus.command.subcommands

import net.minecraft.server.command.CommandManager.literal
import net.minecraft.text.ClickEvent
import net.minecraft.text.Text
import net.superricky.tpaplusplus.GlobalConst
import net.superricky.tpaplusplus.TpaPlusPlus
import net.superricky.tpaplusplus.command.BuildableCommand
import net.superricky.tpaplusplus.utility.*

object RootCommand : BuildableCommand {
override fun build(): LiteralNode =
literal("tpaplusplus")
.executes { showMetaData(it) }
.build()

@Suppress("LongMethod")
private fun showMetaData(context: Context): Int {
val source = context.source
source.sendFeedback(
{
Text.translatable(
"system.version",
TpaPlusPlus.version.friendlyString.literal()
.setStyle(TextColorPallet.secondaryVariant)
).setStyle(TextColorPallet.primaryVariant)
},
false
)
source.sendFeedback(
{
Text.translatable(
"system.github",
"system.github.view".translate()
.setStyle(TextColorPallet.secondaryVariant)
.styled {
it.withClickEvent(
ClickEvent(ClickEvent.Action.OPEN_URL, GlobalConst.GITHUB_URL)
)
}
).setStyle(TextColorPallet.primaryVariant)
},
false
)
source.sendFeedback(
{
Text.translatable(
"system.modrinth",
"system.modrinth.view".translate()
.setStyle(TextColorPallet.secondaryVariant)
.styled {
it.withClickEvent(
ClickEvent(ClickEvent.Action.OPEN_URL, GlobalConst.MODRINTH_URL)
)
}
)
.setStyle(TextColorPallet.primaryVariant)
},
false
)
source.sendFeedback(
{
Text.translatable(
"system.courseforge",
"system.courseforge.view".translate()
.setStyle(TextColorPallet.secondaryVariant)
.styled {
it.withClickEvent(
ClickEvent(ClickEvent.Action.OPEN_URL, GlobalConst.COURSE_FORGE_URL)
)
}
)
.setStyle(TextColorPallet.primaryVariant)
},
false
)
return 1
}
}
21 changes: 17 additions & 4 deletions src/main/kotlin/net/superricky/tpaplusplus/config/Config.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package net.superricky.tpaplusplus.config

import com.uchuhimo.konf.Config
import com.uchuhimo.konf.RequiredItem
import com.uchuhimo.konf.source.toml
import net.fabricmc.loader.api.FabricLoader
import net.superricky.tpaplusplus.Const
import net.superricky.tpaplusplus.GlobalConst
import net.superricky.tpaplusplus.config.command.*

object Config {
Expand All @@ -18,9 +19,9 @@ object Config {
addSpec(CommandDistanceSpec)
addSpec(CommandLimitationsSpec)
}
.from.toml.resource(Const.CONFIG_FILE_NAME)
.from.toml.watchFile(FabricLoader.getInstance().configDir.resolve(Const.CONFIG_FILE_PATH).toFile())
.from.env()
.from.toml.resource(GlobalConst.CONFIG_FILE_NAME)
.from.toml.watchFile(FabricLoader.getInstance().configDir.resolve(GlobalConst.CONFIG_FILE_PATH).toFile())
.from.env()

/**
* @return Config instance
Expand All @@ -35,5 +36,17 @@ object Config {
*/
fun loadAndVerifyConfig() {
config.validateRequired()
@Suppress("UNCHECKED_CAST")
// CommandNameSpec are all RequiredItem<String> type, so this cast is safe
CommandNameSpec.items.forEach { replaceCommand(it as? RequiredItem<String> ?: return@forEach) }
}

/**
* Replace / before command
*/
private fun replaceCommand(item: RequiredItem<String>) {
if (config[item].startsWith("/")) {
config[item] = config[item].replace("/", "")
}
}
}
10 changes: 10 additions & 0 deletions src/main/kotlin/net/superricky/tpaplusplus/utility/Aliases.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package net.superricky.tpaplusplus.utility

import com.mojang.brigadier.CommandDispatcher
import com.mojang.brigadier.context.CommandContext
import com.mojang.brigadier.tree.LiteralCommandNode
import net.minecraft.server.command.ServerCommandSource

typealias Dispatcher = CommandDispatcher<ServerCommandSource>
typealias LiteralNode = LiteralCommandNode<ServerCommandSource>
typealias Context = CommandContext<ServerCommandSource>
Loading

0 comments on commit af5ff5a

Please sign in to comment.