From 75a4a1d4f430d74bd65d7b9beddf767586fbdf17 Mon Sep 17 00:00:00 2001 From: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:11:38 +1100 Subject: Backend: Change SimpleCommand to Kotlin (#564) Backend: Change SimpleCommand to Kotlin #564 --- .../hannibal2/skyhanni/config/commands/Commands.kt | 35 ++++++------ .../skyhanni/config/commands/SimpleCommand.java | 66 ---------------------- .../skyhanni/config/commands/SimpleCommand.kt | 55 ++++++++++++++++++ 3 files changed, 73 insertions(+), 83 deletions(-) delete mode 100644 src/main/java/at/hannibal2/skyhanni/config/commands/SimpleCommand.java create mode 100644 src/main/java/at/hannibal2/skyhanni/config/commands/SimpleCommand.kt (limited to 'src/main/java') diff --git a/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt b/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt index 6c00abb66..439e6cd0f 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt +++ b/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt @@ -2,7 +2,6 @@ package at.hannibal2.skyhanni.config.commands import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.config.ConfigGuiManager -import at.hannibal2.skyhanni.config.commands.SimpleCommand.ProcessCommandRunnable import at.hannibal2.skyhanni.data.ChatManager import at.hannibal2.skyhanni.data.GuiEditManager import at.hannibal2.skyhanni.data.PartyAPI @@ -49,6 +48,7 @@ import net.minecraft.client.Minecraft import net.minecraft.command.ICommandSender import net.minecraft.event.ClickEvent import net.minecraft.event.HoverEvent +import net.minecraft.util.BlockPos import net.minecraft.util.ChatComponentText import net.minecraftforge.client.ClientCommandHandler @@ -292,7 +292,6 @@ object Commands { // "shsendtranslation", // "Respond with a translation of the message that the user clicks" // ) { Translator.toEnglish(it) } - registerCommand("shwords", "Opens the config list for modifying visual words") { openVisualWords() } } private fun commandHelp(args: Array) { @@ -357,11 +356,10 @@ object Commands { config.outdatedItems.clear() } - private fun registerCommand( - name: String, - description: String, - function: (Array) -> Unit - ) = registerCommand0(name, description, function) + private fun registerCommand(name: String, description: String, function: (Array) -> Unit) { + ClientCommandHandler.instance.registerCommand(SimpleCommand(name, createCommand(function))) + commands.add(CommandInfo(name, description, currentCategory)) + } private fun registerCommand0( name: String, @@ -369,19 +367,22 @@ object Commands { function: (Array) -> Unit, autoComplete: ((Array) -> List) = { listOf() } ) { - ClientCommandHandler.instance.registerCommand( - SimpleCommand( - name, - createCommand(function) - ) { _, b, _ -> autoComplete(b) } + val command = SimpleCommand( + name, + createCommand(function), + object : SimpleCommand.TabCompleteRunnable { + override fun tabComplete(sender: ICommandSender?, args: Array?, pos: BlockPos?): List { + return autoComplete(args ?: emptyArray()) + } + } ) + ClientCommandHandler.instance.registerCommand(command) commands.add(CommandInfo(name, description, currentCategory)) } - private fun createCommand(function: (Array) -> Unit) = - object : ProcessCommandRunnable() { - override fun processCommand(sender: ICommandSender?, args: Array) { - function(args.asList().toTypedArray()) - } + private fun createCommand(function: (Array) -> Unit) = object : SimpleCommand.ProcessCommandRunnable() { + override fun processCommand(sender: ICommandSender?, args: Array?) { + if (args != null) function(args.asList().toTypedArray()) } + } } diff --git a/src/main/java/at/hannibal2/skyhanni/config/commands/SimpleCommand.java b/src/main/java/at/hannibal2/skyhanni/config/commands/SimpleCommand.java deleted file mode 100644 index 0bb0bd664..000000000 --- a/src/main/java/at/hannibal2/skyhanni/config/commands/SimpleCommand.java +++ /dev/null @@ -1,66 +0,0 @@ -package at.hannibal2.skyhanni.config.commands; - -import at.hannibal2.skyhanni.test.command.ErrorManager; -import net.minecraft.command.CommandBase; -import net.minecraft.command.ICommandSender; -import net.minecraft.util.BlockPos; - -import java.util.List; - -/** - * @author Moulberry - **/ -public class SimpleCommand extends CommandBase { - - private final String commandName; - private final ProcessCommandRunnable runnable; - private TabCompleteRunnable tabRunnable; - - public SimpleCommand(String commandName, ProcessCommandRunnable runnable) { - this.commandName = commandName; - this.runnable = runnable; - } - - public SimpleCommand(String commandName, ProcessCommandRunnable runnable, TabCompleteRunnable tabRunnable) { - this.commandName = commandName; - this.runnable = runnable; - this.tabRunnable = tabRunnable; - } - - public abstract static class ProcessCommandRunnable { - - public abstract void processCommand(ICommandSender sender, String[] args); - } - - public interface TabCompleteRunnable { - - List tabComplete(ICommandSender sender, String[] args, BlockPos pos); - } - - public boolean canCommandSenderUseCommand(ICommandSender sender) { - return true; - } - - public String getCommandName() { - return commandName; - } - - public String getCommandUsage(ICommandSender sender) { - return "/" + commandName; - } - - @Override - public void processCommand(ICommandSender sender, String[] args) { - try { - runnable.processCommand(sender, args); - } catch (Throwable e) { - ErrorManager.INSTANCE.logError(e, "Error while running command /" + commandName); - } - } - - @Override - public List addTabCompletionOptions(ICommandSender sender, String[] args, BlockPos pos) { - if (tabRunnable != null) return tabRunnable.tabComplete(sender, args, pos); - return null; - } -} diff --git a/src/main/java/at/hannibal2/skyhanni/config/commands/SimpleCommand.kt b/src/main/java/at/hannibal2/skyhanni/config/commands/SimpleCommand.kt new file mode 100644 index 000000000..59c6044c3 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/config/commands/SimpleCommand.kt @@ -0,0 +1,55 @@ +package at.hannibal2.skyhanni.config.commands + +import at.hannibal2.skyhanni.test.command.ErrorManager +import net.minecraft.command.CommandBase +import net.minecraft.command.ICommandSender +import net.minecraft.util.BlockPos + +class SimpleCommand : CommandBase { + private val commandName: String + private val runnable: ProcessCommandRunnable + private var tabRunnable: TabCompleteRunnable? = null + + constructor(commandName: String, runnable: ProcessCommandRunnable) { + this.commandName = commandName + this.runnable = runnable + } + + constructor(commandName: String, runnable: ProcessCommandRunnable, tabRunnable: TabCompleteRunnable?) { + this.commandName = commandName + this.runnable = runnable + this.tabRunnable = tabRunnable + } + + abstract class ProcessCommandRunnable { + abstract fun processCommand(sender: ICommandSender?, args: Array?) + } + + interface TabCompleteRunnable { + fun tabComplete(sender: ICommandSender?, args: Array?, pos: BlockPos?): List + } + + override fun canCommandSenderUseCommand(sender: ICommandSender): Boolean { + return true + } + + override fun getCommandName(): String { + return commandName + } + + override fun getCommandUsage(sender: ICommandSender): String { + return "/$commandName" + } + + override fun processCommand(sender: ICommandSender, args: Array) { + try { + runnable.processCommand(sender, args) + } catch (e: Throwable) { + ErrorManager.logError(e, "Error while running command /$commandName") + } + } + + override fun addTabCompletionOptions(sender: ICommandSender, args: Array, pos: BlockPos): List? { + return if (tabRunnable != null) tabRunnable!!.tabComplete(sender, args, pos) else null + } +} \ No newline at end of file -- cgit