Skip to content

Commit 358a842

Browse files
authored
Fix folder opening for most OS (#237)
1 parent cc54854 commit 358a842

File tree

8 files changed

+37
-15
lines changed

8 files changed

+37
-15
lines changed

src/main/kotlin/com/lambda/client/LambdaMod.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ class LambdaMod {
4646

4747
const val LAMBDA = "λ"
4848

49+
const val PLUGIN_PATH = "${DIRECTORY}plugins/"
50+
const val PACKET_LOG_PATH = "${DIRECTORY}packet-logs/"
51+
const val SONGS_PATH = "${DIRECTORY}songs/"
52+
4953
val LOG: Logger = LogManager.getLogger(NAME)
5054

5155
var ready: Boolean = false; private set

src/main/kotlin/com/lambda/client/command/commands/PluginCommand.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.lambda.client.command.commands
22

3+
import com.lambda.client.LambdaMod
34
import com.lambda.client.command.ClientCommand
45
import com.lambda.client.plugin.PluginLoader
56
import com.lambda.client.plugin.PluginManager
@@ -18,7 +19,7 @@ object PluginCommand : ClientCommand(
1819
string("jar name") { nameArg ->
1920
execute {
2021
val name = "${nameArg.value.removeSuffix(".jar")}.jar"
21-
val file = File("${PluginManager.pluginPath}$name")
22+
val file = File("${LambdaMod.PLUGIN_PATH}$name")
2223

2324
if (!file.exists()) {
2425
MessageSendHelper.sendErrorMessage("${formatValue(name)} is not a valid jar file name!")

src/main/kotlin/com/lambda/client/gui/clickgui/LambdaClickGui.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ object LambdaClickGui : AbstractLambdaGui<ModuleSettingWindow, AbstractModule>()
233233
URL(remotePluginButton.downloadUrl).openStream().use { inputStream ->
234234
Files.copy(
235235
inputStream,
236-
Paths.get("${PluginManager.pluginPath}/${remotePluginButton.fileName}"),
236+
Paths.get("${LambdaMod.PLUGIN_PATH}/${remotePluginButton.fileName}"),
237237
StandardCopyOption.REPLACE_EXISTING
238238
)
239239
}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
package com.lambda.client.gui.clickgui.component
22

3+
import com.lambda.client.LambdaMod
34
import com.lambda.client.gui.rgui.component.BooleanSlider
4-
import com.lambda.client.plugin.PluginManager
5+
import com.lambda.client.util.filesystem.FolderUtils
56
import com.lambda.client.util.math.Vec2f
6-
import java.awt.Desktop
7-
import java.io.File
87

98
object ImportPluginButton : BooleanSlider("Import...", 0.0, "Import plugins to Lambda") {
109
override fun onClick(mousePos: Vec2f, buttonId: Int) {
1110
super.onClick(mousePos, buttonId)
12-
if (buttonId == 0) Desktop.getDesktop().open(File(PluginManager.pluginPath))
11+
if (buttonId == 0) FolderUtils.openFolder(LambdaMod.PLUGIN_PATH)
1312
}
1413

1514
override fun onRelease(mousePos: Vec2f, buttonId: Int) {
1615
super.onRelease(mousePos, buttonId)
17-
if (buttonId == 1) Desktop.getDesktop().open(File(PluginManager.pluginPath))
16+
if (buttonId == 1) FolderUtils.openFolder(LambdaMod.PLUGIN_PATH)
1817
}
1918
}

src/main/kotlin/com/lambda/client/module/modules/misc/NoteBot.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ object NoteBot : Module(
106106

107107
private fun loadSong() {
108108
defaultScope.launch(Dispatchers.IO) {
109-
val path = "${LambdaMod.DIRECTORY}songs/$songName"
109+
val path = "${LambdaMod.SONGS_PATH}$songName"
110110

111111
try {
112112
parse(path).let {

src/main/kotlin/com/lambda/client/module/modules/player/PacketLogger.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import net.minecraft.network.Packet
2020
import net.minecraft.network.play.client.*
2121
import net.minecraft.network.play.server.*
2222
import net.minecraft.util.math.BlockPos
23+
import net.minecraft.util.text.TextFormatting
2324
import net.minecraftforge.fml.common.gameevent.TickEvent
2425
import java.io.File
2526
import java.io.FileWriter
@@ -48,7 +49,6 @@ object PacketLogger : Module(
4849
private var lastTick = 0L
4950
private val timer = TickTimer(TimeUnit.SECONDS)
5051

51-
private const val directory = "${LambdaMod.DIRECTORY}packetLogs"
5252
private var filename = ""
5353
private var lines = ArrayList<String>()
5454

@@ -76,7 +76,7 @@ object PacketLogger : Module(
7676
write()
7777

7878
runSafe {
79-
MessageSendHelper.sendChatMessage("$chatName Log saved at $directory/${filename}")
79+
MessageSendHelper.sendChatMessage("$chatName Log saved at ${TextFormatting.GREEN}${LambdaMod.PACKET_LOG_PATH}${filename}")
8080
}
8181
}
8282

@@ -735,11 +735,11 @@ object PacketLogger : Module(
735735

736736
defaultScope.launch(Dispatchers.IO) {
737737
try {
738-
with(File(directory)) {
738+
with(File(LambdaMod.PACKET_LOG_PATH)) {
739739
if (!exists()) mkdir()
740740
}
741741

742-
FileWriter("$directory/${filename}", true).buffered().use {
742+
FileWriter("${LambdaMod.PACKET_LOG_PATH}${filename}", true).buffered().use {
743743
for (line in lines) it.write(line)
744744
}
745745
} catch (e: Exception) {

src/main/kotlin/com/lambda/client/plugin/PluginManager.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ internal object PluginManager : AsyncLoader<List<PluginLoader>> {
1919
val loadedPlugins = NameableSet<Plugin>()
2020
val loadedPluginLoader = NameableSet<PluginLoader>()
2121

22-
const val pluginPath = "${LambdaMod.DIRECTORY}plugins/"
23-
2422
private val lambdaVersion = DefaultArtifactVersion(LambdaMod.VERSION)
2523

2624
override fun preLoad0() = checkPluginLoaders(getLoaders())
@@ -30,7 +28,7 @@ internal object PluginManager : AsyncLoader<List<PluginLoader>> {
3028
}
3129

3230
fun getLoaders(): List<PluginLoader> {
33-
val dir = File(pluginPath)
31+
val dir = File(LambdaMod.PLUGIN_PATH)
3432
if (!dir.exists()) dir.mkdir()
3533

3634
val files = dir.listFiles() ?: return emptyList()

src/main/kotlin/com/lambda/client/util/filesystem/FolderUtils.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.lambda.client.util.filesystem
22

3+
import java.awt.Desktop
34
import java.io.File
5+
import java.net.URL
46

57
object FolderUtils {
68
@JvmStatic
@@ -21,6 +23,24 @@ object FolderUtils {
2123
OperatingSystem.WINDOWS -> System.getenv("APPDATA") + File.separator + ".minecraft" + File.separator
2224
}
2325

26+
/**
27+
* Opens the given folder using the right library based on OS
28+
*/
29+
fun openFolder(path: String) {
30+
Thread {
31+
if (getOS() == OperatingSystem.WINDOWS) Desktop.getDesktop().open(File(path))
32+
else Runtime.getRuntime().exec(getURLOpenCommand(File(path).toURI().toURL()))
33+
}.start()
34+
}
35+
36+
private fun getURLOpenCommand(url: URL): Array<String> {
37+
var string: String = url.toString()
38+
if ("file" == url.protocol) {
39+
string = string.replace("file:", "file://")
40+
}
41+
return arrayOf("xdg-open", string)
42+
}
43+
2444
/**
2545
* @return current OperatingSystem
2646
*/

0 commit comments

Comments
 (0)