From f161f652e783a8ddcf44bd253863bc0b526b43c7 Mon Sep 17 00:00:00 2001 From: Constructor Date: Fri, 11 Feb 2022 22:50:12 +0100 Subject: [PATCH] Fix folder opening for most OS --- .../kotlin/com/lambda/client/LambdaMod.kt | 4 ++++ .../client/command/commands/PluginCommand.kt | 3 ++- .../client/gui/clickgui/LambdaClickGui.kt | 2 +- .../clickgui/component/ImportPluginButton.kt | 9 ++++----- .../client/module/modules/misc/NoteBot.kt | 2 +- .../module/modules/player/PacketLogger.kt | 8 ++++---- .../com/lambda/client/plugin/PluginManager.kt | 4 +--- .../client/util/filesystem/FolderUtils.kt | 20 +++++++++++++++++++ 8 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/main/kotlin/com/lambda/client/LambdaMod.kt b/src/main/kotlin/com/lambda/client/LambdaMod.kt index 46da0bbb8..3db31a672 100644 --- a/src/main/kotlin/com/lambda/client/LambdaMod.kt +++ b/src/main/kotlin/com/lambda/client/LambdaMod.kt @@ -46,6 +46,10 @@ 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/PluginCommand.kt b/src/main/kotlin/com/lambda/client/command/commands/PluginCommand.kt index a4954f19b..cde713170 100644 --- a/src/main/kotlin/com/lambda/client/command/commands/PluginCommand.kt +++ b/src/main/kotlin/com/lambda/client/command/commands/PluginCommand.kt @@ -1,5 +1,6 @@ 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 @@ -18,7 +19,7 @@ object PluginCommand : ClientCommand( string("jar name") { nameArg -> execute { val name = "${nameArg.value.removeSuffix(".jar")}.jar" - val file = File("${PluginManager.pluginPath}$name") + val file = File("${LambdaMod.PLUGIN_PATH}$name") if (!file.exists()) { MessageSendHelper.sendErrorMessage("${formatValue(name)} is not a valid jar file name!") 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 b4cea4a1d..9a9ef07a4 100644 --- a/src/main/kotlin/com/lambda/client/gui/clickgui/LambdaClickGui.kt +++ b/src/main/kotlin/com/lambda/client/gui/clickgui/LambdaClickGui.kt @@ -233,7 +233,7 @@ object LambdaClickGui : AbstractLambdaGui() URL(remotePluginButton.downloadUrl).openStream().use { inputStream -> Files.copy( inputStream, - Paths.get("${PluginManager.pluginPath}/${remotePluginButton.fileName}"), + Paths.get("${LambdaMod.PLUGIN_PATH}/${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 cd68661c1..c5aaf0919 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,19 +1,18 @@ package com.lambda.client.gui.clickgui.component +import com.lambda.client.LambdaMod import com.lambda.client.gui.rgui.component.BooleanSlider -import com.lambda.client.plugin.PluginManager +import com.lambda.client.util.filesystem.FolderUtils import com.lambda.client.util.math.Vec2f -import java.awt.Desktop -import java.io.File object ImportPluginButton : BooleanSlider("Import...", 0.0, "Import plugins to Lambda") { override fun onClick(mousePos: Vec2f, buttonId: Int) { super.onClick(mousePos, buttonId) - if (buttonId == 0) Desktop.getDesktop().open(File(PluginManager.pluginPath)) + if (buttonId == 0) FolderUtils.openFolder(LambdaMod.PLUGIN_PATH) } override fun onRelease(mousePos: Vec2f, buttonId: Int) { super.onRelease(mousePos, buttonId) - if (buttonId == 1) Desktop.getDesktop().open(File(PluginManager.pluginPath)) + if (buttonId == 1) FolderUtils.openFolder(LambdaMod.PLUGIN_PATH) } } \ 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 cff2a99c6..07f14f552 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 @@ -106,7 +106,7 @@ object NoteBot : Module( private fun loadSong() { defaultScope.launch(Dispatchers.IO) { - val path = "${LambdaMod.DIRECTORY}songs/$songName" + val path = "${LambdaMod.SONGS_PATH}$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 a30de2a01..39b34de70 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 @@ -20,6 +20,7 @@ import net.minecraft.network.Packet import net.minecraft.network.play.client.* import net.minecraft.network.play.server.* import net.minecraft.util.math.BlockPos +import net.minecraft.util.text.TextFormatting import net.minecraftforge.fml.common.gameevent.TickEvent import java.io.File import java.io.FileWriter @@ -48,7 +49,6 @@ object PacketLogger : Module( private var lastTick = 0L private val timer = TickTimer(TimeUnit.SECONDS) - private const val directory = "${LambdaMod.DIRECTORY}packetLogs" private var filename = "" private var lines = ArrayList() @@ -76,7 +76,7 @@ object PacketLogger : Module( write() runSafe { - MessageSendHelper.sendChatMessage("$chatName Log saved at $directory/${filename}") + MessageSendHelper.sendChatMessage("$chatName Log saved at ${TextFormatting.GREEN}${LambdaMod.PACKET_LOG_PATH}${filename}") } } @@ -735,11 +735,11 @@ object PacketLogger : Module( defaultScope.launch(Dispatchers.IO) { try { - with(File(directory)) { + with(File(LambdaMod.PACKET_LOG_PATH)) { if (!exists()) mkdir() } - FileWriter("$directory/${filename}", true).buffered().use { + FileWriter("${LambdaMod.PACKET_LOG_PATH}${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 e1af34749..444452582 100644 --- a/src/main/kotlin/com/lambda/client/plugin/PluginManager.kt +++ b/src/main/kotlin/com/lambda/client/plugin/PluginManager.kt @@ -19,8 +19,6 @@ internal object PluginManager : AsyncLoader> { val loadedPlugins = NameableSet() val loadedPluginLoader = NameableSet() - const val pluginPath = "${LambdaMod.DIRECTORY}plugins/" - private val lambdaVersion = DefaultArtifactVersion(LambdaMod.VERSION) override fun preLoad0() = checkPluginLoaders(getLoaders()) @@ -30,7 +28,7 @@ internal object PluginManager : AsyncLoader> { } fun getLoaders(): List { - val dir = File(pluginPath) + val dir = File(LambdaMod.PLUGIN_PATH) if (!dir.exists()) dir.mkdir() val files = dir.listFiles() ?: return emptyList() 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..9460cd039 100644 --- a/src/main/kotlin/com/lambda/client/util/filesystem/FolderUtils.kt +++ b/src/main/kotlin/com/lambda/client/util/filesystem/FolderUtils.kt @@ -1,6 +1,8 @@ package com.lambda.client.util.filesystem +import java.awt.Desktop import java.io.File +import java.net.URL object FolderUtils { @JvmStatic @@ -21,6 +23,24 @@ object FolderUtils { OperatingSystem.WINDOWS -> System.getenv("APPDATA") + File.separator + ".minecraft" + File.separator } + /** + * Opens the given folder 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())) + }.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) + } + /** * @return current OperatingSystem */