From 7ed1be6712e7413a1768708c760a27db921d7245 Mon Sep 17 00:00:00 2001 From: JacobHerd Date: Mon, 31 Jan 2022 21:54:24 -0500 Subject: [PATCH 1/4] Add openfolder command --- .../command/commands/OpenFolderCommand.kt | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/main/kotlin/com/lambda/client/command/commands/OpenFolderCommand.kt diff --git a/src/main/kotlin/com/lambda/client/command/commands/OpenFolderCommand.kt b/src/main/kotlin/com/lambda/client/command/commands/OpenFolderCommand.kt new file mode 100644 index 000000000..a38bfd07f --- /dev/null +++ b/src/main/kotlin/com/lambda/client/command/commands/OpenFolderCommand.kt @@ -0,0 +1,28 @@ +package com.lambda.client.command.commands + +import com.lambda.client.command.ClientCommand +import java.io.File +import java.net.URL + +object OpenFolderCommand : ClientCommand( + name = "openfolder", + description = "Open your lambda client folder" +) { + init { + execute { + val dotMCFile = File("") + val path = File("${dotMCFile.absolutePath}/lambda") + + // Because main thread comedy + Thread { Runtime.getRuntime().exec(getURLOpenCommand(path.toURI().toURL())) }.start() + } + } + + private fun getURLOpenCommand(url: URL): Array { + var string: String = url.toString() + if ("file" == url.protocol) { + string = string.replace("file:", "file://") + } + return arrayOf("xdg-open", string) + } +} \ No newline at end of file From c5fef92b17c686f54abe236cfa4689e83af0d24a Mon Sep 17 00:00:00 2001 From: JacobHerd Date: Mon, 31 Jan 2022 22:14:18 -0500 Subject: [PATCH 2/4] Make it (probably) work for windows Why getOS wasn't public irritates me --- .../client/command/commands/OpenFolderCommand.kt | 10 +++++++++- .../com/lambda/client/util/filesystem/FolderUtils.kt | 4 ++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/com/lambda/client/command/commands/OpenFolderCommand.kt b/src/main/kotlin/com/lambda/client/command/commands/OpenFolderCommand.kt index a38bfd07f..acb7112d8 100644 --- a/src/main/kotlin/com/lambda/client/command/commands/OpenFolderCommand.kt +++ b/src/main/kotlin/com/lambda/client/command/commands/OpenFolderCommand.kt @@ -1,6 +1,10 @@ package com.lambda.client.command.commands import com.lambda.client.command.ClientCommand +import com.lambda.client.util.filesystem.FolderUtils +import com.lambda.client.util.filesystem.FolderUtils.OperatingSystem +import java.awt.Desktop + import java.io.File import java.net.URL @@ -12,9 +16,13 @@ object OpenFolderCommand : ClientCommand( execute { val dotMCFile = File("") val path = File("${dotMCFile.absolutePath}/lambda") + val os = FolderUtils.getOS() // Because main thread comedy - Thread { Runtime.getRuntime().exec(getURLOpenCommand(path.toURI().toURL())) }.start() + Thread { + if (os == OperatingSystem.WINDOWS) Desktop.getDesktop().open(path) + else Runtime.getRuntime().exec(getURLOpenCommand(path.toURI().toURL())) + }.start() } } diff --git a/src/main/kotlin/com/lambda/client/util/filesystem/FolderUtils.kt b/src/main/kotlin/com/lambda/client/util/filesystem/FolderUtils.kt index 26df3ac99..ebf6f463a 100644 --- a/src/main/kotlin/com/lambda/client/util/filesystem/FolderUtils.kt +++ b/src/main/kotlin/com/lambda/client/util/filesystem/FolderUtils.kt @@ -24,7 +24,7 @@ object FolderUtils { /** * @return current OperatingSystem */ - private fun getOS(): OperatingSystem { + public fun getOS(): OperatingSystem { val osName = System.getProperty("os.name").lowercase() return when { osName.contains("nux") -> { @@ -42,7 +42,7 @@ object FolderUtils { } } - private enum class OperatingSystem { + enum class OperatingSystem { UNIX, OSX, WINDOWS } } \ No newline at end of file From 9daa69dfb90a3974f735d7c4d30f76aca94abbdf Mon Sep 17 00:00:00 2001 From: Constructor Date: Sat, 12 Feb 2022 00:21:28 +0100 Subject: [PATCH 3/4] Refactor of FolderUtils --- .../kotlin/com/lambda/client/LambdaMod.kt | 4 -- .../client/command/commands/LicenseCommand.kt | 2 +- .../command/commands/OpenFolderCommand.kt | 62 ++++++++++++------- .../client/command/commands/PluginCommand.kt | 4 +- .../client/command/commands/SearchCommand.kt | 1 - .../client/gui/clickgui/LambdaClickGui.kt | 3 +- .../clickgui/component/ImportPluginButton.kt | 7 +-- .../client/module/modules/misc/NoteBot.kt | 4 +- .../module/modules/player/PacketLogger.kt | 7 ++- .../com/lambda/client/plugin/PluginManager.kt | 3 +- .../com/lambda/client/util/ConfigUtils.kt | 2 +- .../util/{filesystem => }/FolderUtils.kt | 52 +++++++++++----- 12 files changed, 94 insertions(+), 57 deletions(-) rename src/main/kotlin/com/lambda/client/util/{filesystem => }/FolderUtils.kt (52%) diff --git a/src/main/kotlin/com/lambda/client/LambdaMod.kt b/src/main/kotlin/com/lambda/client/LambdaMod.kt index 3db31a672..46da0bbb8 100644 --- a/src/main/kotlin/com/lambda/client/LambdaMod.kt +++ b/src/main/kotlin/com/lambda/client/LambdaMod.kt @@ -46,10 +46,6 @@ class LambdaMod { const val LAMBDA = "λ" - const val PLUGIN_PATH = "${DIRECTORY}plugins/" - const val PACKET_LOG_PATH = "${DIRECTORY}packet-logs/" - const val SONGS_PATH = "${DIRECTORY}songs/" - val LOG: Logger = LogManager.getLogger(NAME) var ready: Boolean = false; private set diff --git a/src/main/kotlin/com/lambda/client/command/commands/LicenseCommand.kt b/src/main/kotlin/com/lambda/client/command/commands/LicenseCommand.kt index f54dfcbd0..a84736199 100644 --- a/src/main/kotlin/com/lambda/client/command/commands/LicenseCommand.kt +++ b/src/main/kotlin/com/lambda/client/command/commands/LicenseCommand.kt @@ -9,7 +9,7 @@ object LicenseCommand : ClientCommand( ) { init { execute { - MessageSendHelper.sendChatMessage("You can view Lambda's &7client&f License (LGPLv3) at &9https://lambda-client.org/license") + MessageSendHelper.sendChatMessage("You can view Lambda's &7client&f License (LGPLv3) at &9https://github.com/lambda-client/lambda/blob/master/LICENSE.md") } } } \ No newline at end of file diff --git a/src/main/kotlin/com/lambda/client/command/commands/OpenFolderCommand.kt b/src/main/kotlin/com/lambda/client/command/commands/OpenFolderCommand.kt index acb7112d8..629f47352 100644 --- a/src/main/kotlin/com/lambda/client/command/commands/OpenFolderCommand.kt +++ b/src/main/kotlin/com/lambda/client/command/commands/OpenFolderCommand.kt @@ -1,36 +1,52 @@ package com.lambda.client.command.commands import com.lambda.client.command.ClientCommand -import com.lambda.client.util.filesystem.FolderUtils -import com.lambda.client.util.filesystem.FolderUtils.OperatingSystem -import java.awt.Desktop - -import java.io.File -import java.net.URL +import com.lambda.client.util.FolderUtils object OpenFolderCommand : ClientCommand( name = "openfolder", - description = "Open your lambda client folder" + alias = arrayOf("of", "open"), + description = "Open any Lambda folder" ) { init { - execute { - val dotMCFile = File("") - val path = File("${dotMCFile.absolutePath}/lambda") - val os = FolderUtils.getOS() - - // Because main thread comedy - Thread { - if (os == OperatingSystem.WINDOWS) Desktop.getDesktop().open(path) - else Runtime.getRuntime().exec(getURLOpenCommand(path.toURI().toURL())) - }.start() + literal("lambda") { + execute { + FolderUtils.openFolder(FolderUtils.songFolder) + } + } + + literal("plugins") { + execute { + FolderUtils.openFolder(FolderUtils.pluginFolder) + } + } + + literal("packetLogs") { + execute { + FolderUtils.openFolder(FolderUtils.packetLogFolder) + } } - } - private fun getURLOpenCommand(url: URL): Array { - var string: String = url.toString() - if ("file" == url.protocol) { - string = string.replace("file:", "file://") + literal("songs") { + execute { + FolderUtils.openFolder(FolderUtils.songFolder) + } + } + + literal("screenshots") { + execute { + FolderUtils.openFolder(FolderUtils.screenshotFolder) + } + } + + literal("logs") { + execute { + FolderUtils.openFolder(FolderUtils.logFolder) + } + } + + execute { + FolderUtils.openFolder(FolderUtils.lambdaFolder) } - return arrayOf("xdg-open", string) } } \ No newline at end of file diff --git a/src/main/kotlin/com/lambda/client/command/commands/PluginCommand.kt b/src/main/kotlin/com/lambda/client/command/commands/PluginCommand.kt index cde713170..1745606fe 100644 --- a/src/main/kotlin/com/lambda/client/command/commands/PluginCommand.kt +++ b/src/main/kotlin/com/lambda/client/command/commands/PluginCommand.kt @@ -1,11 +1,11 @@ package com.lambda.client.command.commands -import com.lambda.client.LambdaMod import com.lambda.client.command.ClientCommand import com.lambda.client.plugin.PluginLoader import com.lambda.client.plugin.PluginManager import com.lambda.client.plugin.api.Plugin import com.lambda.client.util.ConfigUtils +import com.lambda.client.util.FolderUtils import com.lambda.client.util.text.MessageSendHelper import com.lambda.client.util.text.formatValue import java.io.File @@ -19,7 +19,7 @@ object PluginCommand : ClientCommand( string("jar name") { nameArg -> execute { val name = "${nameArg.value.removeSuffix(".jar")}.jar" - val file = File("${LambdaMod.PLUGIN_PATH}$name") + val file = File("${FolderUtils.pluginFolder}$name") if (!file.exists()) { MessageSendHelper.sendErrorMessage("${formatValue(name)} is not a valid jar file name!") diff --git a/src/main/kotlin/com/lambda/client/command/commands/SearchCommand.kt b/src/main/kotlin/com/lambda/client/command/commands/SearchCommand.kt index 0061b6664..dcde86644 100644 --- a/src/main/kotlin/com/lambda/client/command/commands/SearchCommand.kt +++ b/src/main/kotlin/com/lambda/client/command/commands/SearchCommand.kt @@ -20,7 +20,6 @@ object SearchCommand : ClientCommand( val blockName = blockArg.value.registryName.toString() addBlock(blockName) } - } execute("Add a block to search list") { diff --git a/src/main/kotlin/com/lambda/client/gui/clickgui/LambdaClickGui.kt b/src/main/kotlin/com/lambda/client/gui/clickgui/LambdaClickGui.kt index 9a9ef07a4..89b576ffb 100644 --- a/src/main/kotlin/com/lambda/client/gui/clickgui/LambdaClickGui.kt +++ b/src/main/kotlin/com/lambda/client/gui/clickgui/LambdaClickGui.kt @@ -12,6 +12,7 @@ import com.lambda.client.module.ModuleManager import com.lambda.client.module.modules.client.ClickGUI import com.lambda.client.plugin.PluginManager import com.lambda.client.setting.ConfigManager +import com.lambda.client.util.FolderUtils import com.lambda.client.util.math.Vec2f import com.lambda.client.util.text.MessageSendHelper import com.lambda.client.util.threads.defaultScope @@ -233,7 +234,7 @@ object LambdaClickGui : AbstractLambdaGui() URL(remotePluginButton.downloadUrl).openStream().use { inputStream -> Files.copy( inputStream, - Paths.get("${LambdaMod.PLUGIN_PATH}/${remotePluginButton.fileName}"), + Paths.get("${FolderUtils.pluginFolder}/${remotePluginButton.fileName}"), StandardCopyOption.REPLACE_EXISTING ) } diff --git a/src/main/kotlin/com/lambda/client/gui/clickgui/component/ImportPluginButton.kt b/src/main/kotlin/com/lambda/client/gui/clickgui/component/ImportPluginButton.kt index c5aaf0919..b9cf57517 100644 --- a/src/main/kotlin/com/lambda/client/gui/clickgui/component/ImportPluginButton.kt +++ b/src/main/kotlin/com/lambda/client/gui/clickgui/component/ImportPluginButton.kt @@ -1,18 +1,17 @@ package com.lambda.client.gui.clickgui.component -import com.lambda.client.LambdaMod import com.lambda.client.gui.rgui.component.BooleanSlider -import com.lambda.client.util.filesystem.FolderUtils +import com.lambda.client.util.FolderUtils import com.lambda.client.util.math.Vec2f object ImportPluginButton : BooleanSlider("Import...", 0.0, "Import plugins to Lambda") { override fun onClick(mousePos: Vec2f, buttonId: Int) { super.onClick(mousePos, buttonId) - if (buttonId == 0) FolderUtils.openFolder(LambdaMod.PLUGIN_PATH) + if (buttonId == 0) FolderUtils.openFolder(FolderUtils.pluginFolder) } override fun onRelease(mousePos: Vec2f, buttonId: Int) { super.onRelease(mousePos, buttonId) - if (buttonId == 1) FolderUtils.openFolder(LambdaMod.PLUGIN_PATH) + if (buttonId == 1) FolderUtils.openFolder(FolderUtils.pluginFolder) } } \ No newline at end of file diff --git a/src/main/kotlin/com/lambda/client/module/modules/misc/NoteBot.kt b/src/main/kotlin/com/lambda/client/module/modules/misc/NoteBot.kt index 07f14f552..1e68c176d 100644 --- a/src/main/kotlin/com/lambda/client/module/modules/misc/NoteBot.kt +++ b/src/main/kotlin/com/lambda/client/module/modules/misc/NoteBot.kt @@ -1,6 +1,5 @@ package com.lambda.client.module.modules.misc -import com.lambda.client.LambdaMod import com.lambda.client.event.SafeClientEvent import com.lambda.client.event.events.PacketEvent import com.lambda.client.event.events.RenderWorldEvent @@ -8,6 +7,7 @@ import com.lambda.client.module.Category import com.lambda.client.module.Module import com.lambda.client.util.TickTimer import com.lambda.client.util.TimeUnit +import com.lambda.client.util.FolderUtils import com.lambda.client.util.text.MessageSendHelper import com.lambda.client.util.threads.defaultScope import com.lambda.client.util.threads.runSafe @@ -106,7 +106,7 @@ object NoteBot : Module( private fun loadSong() { defaultScope.launch(Dispatchers.IO) { - val path = "${LambdaMod.SONGS_PATH}$songName" + val path = "${FolderUtils.songFolder}$songName" try { parse(path).let { diff --git a/src/main/kotlin/com/lambda/client/module/modules/player/PacketLogger.kt b/src/main/kotlin/com/lambda/client/module/modules/player/PacketLogger.kt index 39b34de70..ff4e58052 100644 --- a/src/main/kotlin/com/lambda/client/module/modules/player/PacketLogger.kt +++ b/src/main/kotlin/com/lambda/client/module/modules/player/PacketLogger.kt @@ -8,6 +8,7 @@ import com.lambda.client.module.Category import com.lambda.client.module.Module import com.lambda.client.util.TickTimer import com.lambda.client.util.TimeUnit +import com.lambda.client.util.FolderUtils import com.lambda.client.util.text.MessageSendHelper import com.lambda.client.util.threads.defaultScope import com.lambda.client.util.threads.runSafe @@ -76,7 +77,7 @@ object PacketLogger : Module( write() runSafe { - MessageSendHelper.sendChatMessage("$chatName Log saved at ${TextFormatting.GREEN}${LambdaMod.PACKET_LOG_PATH}${filename}") + MessageSendHelper.sendChatMessage("$chatName Log saved at ${TextFormatting.GREEN}${FolderUtils.packetLogFolder}${filename}") } } @@ -735,11 +736,11 @@ object PacketLogger : Module( defaultScope.launch(Dispatchers.IO) { try { - with(File(LambdaMod.PACKET_LOG_PATH)) { + with(File(FolderUtils.packetLogFolder)) { if (!exists()) mkdir() } - FileWriter("${LambdaMod.PACKET_LOG_PATH}${filename}", true).buffered().use { + FileWriter("${FolderUtils.packetLogFolder}${filename}", true).buffered().use { for (line in lines) it.write(line) } } catch (e: Exception) { diff --git a/src/main/kotlin/com/lambda/client/plugin/PluginManager.kt b/src/main/kotlin/com/lambda/client/plugin/PluginManager.kt index 444452582..14a305adf 100644 --- a/src/main/kotlin/com/lambda/client/plugin/PluginManager.kt +++ b/src/main/kotlin/com/lambda/client/plugin/PluginManager.kt @@ -5,6 +5,7 @@ import com.lambda.client.LambdaMod import com.lambda.client.gui.clickgui.LambdaClickGui import com.lambda.client.gui.clickgui.component.PluginButton import com.lambda.client.plugin.api.Plugin +import com.lambda.client.util.FolderUtils import com.lambda.client.util.text.MessageSendHelper import com.lambda.commons.collections.NameableSet import kotlinx.coroutines.Deferred @@ -28,7 +29,7 @@ internal object PluginManager : AsyncLoader> { } fun getLoaders(): List { - val dir = File(LambdaMod.PLUGIN_PATH) + val dir = File(FolderUtils.pluginFolder) if (!dir.exists()) dir.mkdir() val files = dir.listFiles() ?: return emptyList() diff --git a/src/main/kotlin/com/lambda/client/util/ConfigUtils.kt b/src/main/kotlin/com/lambda/client/util/ConfigUtils.kt index 392088d39..4af2a25ef 100644 --- a/src/main/kotlin/com/lambda/client/util/ConfigUtils.kt +++ b/src/main/kotlin/com/lambda/client/util/ConfigUtils.kt @@ -69,7 +69,7 @@ object ConfigUtils { } } - // TODO: Introduce a version helper for LambdaMod.BUILD_NUMBER for version-specific configs. This should be theoritically fine for now + // TODO: Introduce a version helper for LambdaMod.BUILD_NUMBER for version-specific configs. This should be theoretically fine for now fun moveAllLegacyConfigs() { moveLegacyConfig("lambda/generic.json", "lambda/generic.bak", GenericConfig) moveLegacyConfig("lambda/modules/default.json", "lambda/modules/default.bak", ModuleConfig) diff --git a/src/main/kotlin/com/lambda/client/util/filesystem/FolderUtils.kt b/src/main/kotlin/com/lambda/client/util/FolderUtils.kt similarity index 52% rename from src/main/kotlin/com/lambda/client/util/filesystem/FolderUtils.kt rename to src/main/kotlin/com/lambda/client/util/FolderUtils.kt index 4718e9b12..3cbea595e 100644 --- a/src/main/kotlin/com/lambda/client/util/filesystem/FolderUtils.kt +++ b/src/main/kotlin/com/lambda/client/util/FolderUtils.kt @@ -1,10 +1,15 @@ -package com.lambda.client.util.filesystem +package com.lambda.client.util +import com.lambda.client.LambdaMod import java.awt.Desktop import java.io.File import java.net.URL object FolderUtils { + @JvmStatic + val minecraftFolder: String + get() = "${File("").absolutePath}${File.separator}" + @JvmStatic val versionsFolder get() = "${minecraftFolder}versions${File.separator}" @@ -13,23 +18,42 @@ object FolderUtils { val modsFolder get() = "${minecraftFolder}mods${File.separator}" - /** - * The Minecraft folder specific to the current operating system - */ - private val minecraftFolder: String - get() = when (getOS()) { - OperatingSystem.UNIX -> System.getProperty("user.home") + "/.minecraft/" - OperatingSystem.OSX -> System.getProperty("user.home") + "/Library/Application Support/minecraft/" - OperatingSystem.WINDOWS -> System.getenv("APPDATA") + File.separator + ".minecraft" + File.separator - } + @JvmStatic + val logFolder + get() = "${minecraftFolder}logs${File.separator}" + + @JvmStatic + val screenshotFolder + get() = "${minecraftFolder}screenshots${File.separator}" + + @JvmStatic + val lambdaFolder + get() = "$minecraftFolder${LambdaMod.DIRECTORY}${File.separator}" + + @JvmStatic + val pluginFolder + get() = "${lambdaFolder}plugins${File.separator}" + + @JvmStatic + val packetLogFolder + get() = "${lambdaFolder}packet-logs${File.separator}" + + @JvmStatic + val songFolder + get() = "${lambdaFolder}songs${File.separator}" /** - * Opens the given folder using the right library based on OS + * Opens the given path using the right library based on OS */ fun openFolder(path: String) { Thread { - if (getOS() == OperatingSystem.WINDOWS) Desktop.getDesktop().open(File(path)) - else Runtime.getRuntime().exec(getURLOpenCommand(File(path).toURI().toURL())) + val file = File(path) + if (!file.exists()) file.mkdir() + if (getOS() == OperatingSystem.WINDOWS) { + Desktop.getDesktop().open(file) + } else { + Runtime.getRuntime().exec(getURLOpenCommand(file.toURI().toURL())) + } }.start() } @@ -44,7 +68,7 @@ object FolderUtils { /** * @return current OperatingSystem */ - public fun getOS(): OperatingSystem { + private fun getOS(): OperatingSystem { val osName = System.getProperty("os.name").lowercase() return when { osName.contains("nux") -> { From eb443e9bd3fa14b63209d06db0cc98dc77d8b39c Mon Sep 17 00:00:00 2001 From: Constructor Date: Sat, 12 Feb 2022 02:26:26 +0100 Subject: [PATCH 4/4] Fix mistake --- .../com/lambda/client/command/commands/OpenFolderCommand.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/com/lambda/client/command/commands/OpenFolderCommand.kt b/src/main/kotlin/com/lambda/client/command/commands/OpenFolderCommand.kt index 629f47352..030b71f35 100644 --- a/src/main/kotlin/com/lambda/client/command/commands/OpenFolderCommand.kt +++ b/src/main/kotlin/com/lambda/client/command/commands/OpenFolderCommand.kt @@ -11,7 +11,7 @@ object OpenFolderCommand : ClientCommand( init { literal("lambda") { execute { - FolderUtils.openFolder(FolderUtils.songFolder) + FolderUtils.openFolder(FolderUtils.lambdaFolder) } }