From a61b3f9e2b8b2eeb9d556b5f477d2a479b9f7643 Mon Sep 17 00:00:00 2001 From: nea Date: Sat, 19 Mar 2022 23:17:20 +0100 Subject: dungeon map data collection --- .../src/main/kotlin/moe/nea89/sbdata/Commands.kt | 87 ++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 ingame/src/main/kotlin/moe/nea89/sbdata/Commands.kt (limited to 'ingame/src/main/kotlin/moe/nea89/sbdata/Commands.kt') diff --git a/ingame/src/main/kotlin/moe/nea89/sbdata/Commands.kt b/ingame/src/main/kotlin/moe/nea89/sbdata/Commands.kt new file mode 100644 index 0000000..e4eff17 --- /dev/null +++ b/ingame/src/main/kotlin/moe/nea89/sbdata/Commands.kt @@ -0,0 +1,87 @@ +package moe.nea89.sbdata + +import net.minecraft.command.CommandBase +import net.minecraft.command.ICommandSender +import net.minecraft.util.BlockPos +import net.minecraft.util.ChatComponentText +import net.minecraft.util.EnumChatFormatting +import net.minecraft.util.IChatComponent + +object Commands : CommandBase() { + + fun mkPrefix(text: String?) = "${EnumChatFormatting.AQUA} SBDATA ${text?.let { "$it " } ?: ""}| " + + data class CommandContext( + val subcommand: String, + val sender: ICommandSender, + val allArgs: List, + val args: List, + ) { + val prefix = mkPrefix(subcommand) + fun reply(vararg lines: String) { + replyJust(ChatComponentText("").apply { + lines.forEach { + appendSibling(ChatComponentText(prefix)) + appendText(it + "\n") + } + }) + } + + fun replyJust(component: IChatComponent) { + sender.addChatMessage(component) + } + + fun reply(vararg lines: IChatComponent) { + replyJust(ChatComponentText("").apply { + lines.forEach { + appendSibling(ChatComponentText(prefix)) + appendSibling(it) + appendText("\n") + } + }) + } + } + + val subcommands = mutableMapOf Unit>() + + override fun getRequiredPermissionLevel(): Int = 0 + override fun getCommandName(): String = "sbdata" + + override fun getCommandUsage(p0: ICommandSender): String = + "SubCommands: ${subcommands.keys.joinToString(separator = ", ")}" + + override fun getCommandAliases(): List = listOf("sbd") + + override fun canCommandSenderUseCommand(p0: ICommandSender): Boolean = true + + override fun processCommand(p0: ICommandSender, p1: Array) { + if (p1.isEmpty()) { + p0.addChatMessage(ChatComponentText(mkPrefix(null)).appendText("Please provide a subcommand!")) + } else { + val subcommandName = p1[0] + val subcommand = subcommands[subcommandName] + if (subcommand == null) { + p0.addChatMessage(ChatComponentText(mkPrefix(null)).appendText("Unknown subcommand $subcommandName!")) + } else { + subcommand(CommandContext(subcommandName, p0, p1.toList(), p1.slice(1 until p1.size))) + } + } + } + + override fun addTabCompletionOptions( + p0: ICommandSender?, + p1: Array?, + p2: BlockPos? + ): MutableList { + return mutableListOf() + } + + + fun addSubCommand(name: String, exec: CommandContext.() -> Unit) { + if (name in subcommands) { + println("Duplicate subcommand registered in sbdata: $name") + } + subcommands[name] = exec + } + +} -- cgit