Skip to content

Commit 71785e0

Browse files
Add ;openfolder command (#233)
* Add openfolder command * Make it (probably) work for windows Why getOS wasn't public irritates me * Refactor of FolderUtils * Fix mistake Co-authored-by: Constructor <[email protected]>
1 parent bfa2bd7 commit 71785e0

File tree

12 files changed

+107
-34
lines changed

12 files changed

+107
-34
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ 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-
5349
val LOG: Logger = LogManager.getLogger(NAME)
5450

5551
var ready: Boolean = false; private set

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ object LicenseCommand : ClientCommand(
99
) {
1010
init {
1111
execute {
12-
MessageSendHelper.sendChatMessage("You can view Lambda's &7client&f License (LGPLv3) at &9https://lambda-client.org/license")
12+
MessageSendHelper.sendChatMessage("You can view Lambda's &7client&f License (LGPLv3) at &9https://github.com/lambda-client/lambda/blob/master/LICENSE.md")
1313
}
1414
}
1515
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.lambda.client.command.commands
2+
3+
import com.lambda.client.command.ClientCommand
4+
import com.lambda.client.util.FolderUtils
5+
6+
object OpenFolderCommand : ClientCommand(
7+
name = "openfolder",
8+
alias = arrayOf("of", "open"),
9+
description = "Open any Lambda folder"
10+
) {
11+
init {
12+
literal("lambda") {
13+
execute {
14+
FolderUtils.openFolder(FolderUtils.lambdaFolder)
15+
}
16+
}
17+
18+
literal("plugins") {
19+
execute {
20+
FolderUtils.openFolder(FolderUtils.pluginFolder)
21+
}
22+
}
23+
24+
literal("packetLogs") {
25+
execute {
26+
FolderUtils.openFolder(FolderUtils.packetLogFolder)
27+
}
28+
}
29+
30+
literal("songs") {
31+
execute {
32+
FolderUtils.openFolder(FolderUtils.songFolder)
33+
}
34+
}
35+
36+
literal("screenshots") {
37+
execute {
38+
FolderUtils.openFolder(FolderUtils.screenshotFolder)
39+
}
40+
}
41+
42+
literal("logs") {
43+
execute {
44+
FolderUtils.openFolder(FolderUtils.logFolder)
45+
}
46+
}
47+
48+
execute {
49+
FolderUtils.openFolder(FolderUtils.lambdaFolder)
50+
}
51+
}
52+
}

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

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

3-
import com.lambda.client.LambdaMod
43
import com.lambda.client.command.ClientCommand
54
import com.lambda.client.plugin.PluginLoader
65
import com.lambda.client.plugin.PluginManager
76
import com.lambda.client.plugin.api.Plugin
87
import com.lambda.client.util.ConfigUtils
8+
import com.lambda.client.util.FolderUtils
99
import com.lambda.client.util.text.MessageSendHelper
1010
import com.lambda.client.util.text.formatValue
1111
import java.io.File
@@ -19,7 +19,7 @@ object PluginCommand : ClientCommand(
1919
string("jar name") { nameArg ->
2020
execute {
2121
val name = "${nameArg.value.removeSuffix(".jar")}.jar"
22-
val file = File("${LambdaMod.PLUGIN_PATH}$name")
22+
val file = File("${FolderUtils.pluginFolder}$name")
2323

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

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ object SearchCommand : ClientCommand(
2020
val blockName = blockArg.value.registryName.toString()
2121
addBlock(blockName)
2222
}
23-
2423
}
2524

2625
execute("Add a block to search list") {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import com.lambda.client.module.ModuleManager
1212
import com.lambda.client.module.modules.client.ClickGUI
1313
import com.lambda.client.plugin.PluginManager
1414
import com.lambda.client.setting.ConfigManager
15+
import com.lambda.client.util.FolderUtils
1516
import com.lambda.client.util.math.Vec2f
1617
import com.lambda.client.util.text.MessageSendHelper
1718
import com.lambda.client.util.threads.defaultScope
@@ -233,7 +234,7 @@ object LambdaClickGui : AbstractLambdaGui<ModuleSettingWindow, AbstractModule>()
233234
URL(remotePluginButton.downloadUrl).openStream().use { inputStream ->
234235
Files.copy(
235236
inputStream,
236-
Paths.get("${LambdaMod.PLUGIN_PATH}/${remotePluginButton.fileName}"),
237+
Paths.get("${FolderUtils.pluginFolder}/${remotePluginButton.fileName}"),
237238
StandardCopyOption.REPLACE_EXISTING
238239
)
239240
}
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
package com.lambda.client.gui.clickgui.component
22

3-
import com.lambda.client.LambdaMod
43
import com.lambda.client.gui.rgui.component.BooleanSlider
5-
import com.lambda.client.util.filesystem.FolderUtils
4+
import com.lambda.client.util.FolderUtils
65
import com.lambda.client.util.math.Vec2f
76

87
object ImportPluginButton : BooleanSlider("Import...", 0.0, "Import plugins to Lambda") {
98
override fun onClick(mousePos: Vec2f, buttonId: Int) {
109
super.onClick(mousePos, buttonId)
11-
if (buttonId == 0) FolderUtils.openFolder(LambdaMod.PLUGIN_PATH)
10+
if (buttonId == 0) FolderUtils.openFolder(FolderUtils.pluginFolder)
1211
}
1312

1413
override fun onRelease(mousePos: Vec2f, buttonId: Int) {
1514
super.onRelease(mousePos, buttonId)
16-
if (buttonId == 1) FolderUtils.openFolder(LambdaMod.PLUGIN_PATH)
15+
if (buttonId == 1) FolderUtils.openFolder(FolderUtils.pluginFolder)
1716
}
1817
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.lambda.client.module.modules.misc
22

3-
import com.lambda.client.LambdaMod
43
import com.lambda.client.event.SafeClientEvent
54
import com.lambda.client.event.events.PacketEvent
65
import com.lambda.client.event.events.RenderWorldEvent
76
import com.lambda.client.module.Category
87
import com.lambda.client.module.Module
98
import com.lambda.client.util.TickTimer
109
import com.lambda.client.util.TimeUnit
10+
import com.lambda.client.util.FolderUtils
1111
import com.lambda.client.util.text.MessageSendHelper
1212
import com.lambda.client.util.threads.defaultScope
1313
import com.lambda.client.util.threads.runSafe
@@ -106,7 +106,7 @@ object NoteBot : Module(
106106

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

111111
try {
112112
parse(path).let {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.lambda.client.module.Category
88
import com.lambda.client.module.Module
99
import com.lambda.client.util.TickTimer
1010
import com.lambda.client.util.TimeUnit
11+
import com.lambda.client.util.FolderUtils
1112
import com.lambda.client.util.text.MessageSendHelper
1213
import com.lambda.client.util.threads.defaultScope
1314
import com.lambda.client.util.threads.runSafe
@@ -76,7 +77,7 @@ object PacketLogger : Module(
7677
write()
7778

7879
runSafe {
79-
MessageSendHelper.sendChatMessage("$chatName Log saved at ${TextFormatting.GREEN}${LambdaMod.PACKET_LOG_PATH}${filename}")
80+
MessageSendHelper.sendChatMessage("$chatName Log saved at ${TextFormatting.GREEN}${FolderUtils.packetLogFolder}${filename}")
8081
}
8182
}
8283

@@ -735,11 +736,11 @@ object PacketLogger : Module(
735736

736737
defaultScope.launch(Dispatchers.IO) {
737738
try {
738-
with(File(LambdaMod.PACKET_LOG_PATH)) {
739+
with(File(FolderUtils.packetLogFolder)) {
739740
if (!exists()) mkdir()
740741
}
741742

742-
FileWriter("${LambdaMod.PACKET_LOG_PATH}${filename}", true).buffered().use {
743+
FileWriter("${FolderUtils.packetLogFolder}${filename}", true).buffered().use {
743744
for (line in lines) it.write(line)
744745
}
745746
} catch (e: Exception) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.lambda.client.LambdaMod
55
import com.lambda.client.gui.clickgui.LambdaClickGui
66
import com.lambda.client.gui.clickgui.component.PluginButton
77
import com.lambda.client.plugin.api.Plugin
8+
import com.lambda.client.util.FolderUtils
89
import com.lambda.client.util.text.MessageSendHelper
910
import com.lambda.commons.collections.NameableSet
1011
import kotlinx.coroutines.Deferred
@@ -28,7 +29,7 @@ internal object PluginManager : AsyncLoader<List<PluginLoader>> {
2829
}
2930

3031
fun getLoaders(): List<PluginLoader> {
31-
val dir = File(LambdaMod.PLUGIN_PATH)
32+
val dir = File(FolderUtils.pluginFolder)
3233
if (!dir.exists()) dir.mkdir()
3334

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

src/main/kotlin/com/lambda/client/util/ConfigUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ object ConfigUtils {
6969
}
7070
}
7171

72-
// TODO: Introduce a version helper for LambdaMod.BUILD_NUMBER for version-specific configs. This should be theoritically fine for now
72+
// TODO: Introduce a version helper for LambdaMod.BUILD_NUMBER for version-specific configs. This should be theoretically fine for now
7373
fun moveAllLegacyConfigs() {
7474
moveLegacyConfig("lambda/generic.json", "lambda/generic.bak", GenericConfig)
7575
moveLegacyConfig("lambda/modules/default.json", "lambda/modules/default.bak", ModuleConfig)

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

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
package com.lambda.client.util.filesystem
1+
package com.lambda.client.util
22

3+
import com.lambda.client.LambdaMod
34
import java.awt.Desktop
45
import java.io.File
56
import java.net.URL
67

78
object FolderUtils {
9+
@JvmStatic
10+
val minecraftFolder: String
11+
get() = "${File("").absolutePath}${File.separator}"
12+
813
@JvmStatic
914
val versionsFolder
1015
get() = "${minecraftFolder}versions${File.separator}"
@@ -13,23 +18,42 @@ object FolderUtils {
1318
val modsFolder
1419
get() = "${minecraftFolder}mods${File.separator}"
1520

16-
/**
17-
* The Minecraft folder specific to the current operating system
18-
*/
19-
private val minecraftFolder: String
20-
get() = when (getOS()) {
21-
OperatingSystem.UNIX -> System.getProperty("user.home") + "/.minecraft/"
22-
OperatingSystem.OSX -> System.getProperty("user.home") + "/Library/Application Support/minecraft/"
23-
OperatingSystem.WINDOWS -> System.getenv("APPDATA") + File.separator + ".minecraft" + File.separator
24-
}
21+
@JvmStatic
22+
val logFolder
23+
get() = "${minecraftFolder}logs${File.separator}"
24+
25+
@JvmStatic
26+
val screenshotFolder
27+
get() = "${minecraftFolder}screenshots${File.separator}"
28+
29+
@JvmStatic
30+
val lambdaFolder
31+
get() = "$minecraftFolder${LambdaMod.DIRECTORY}${File.separator}"
32+
33+
@JvmStatic
34+
val pluginFolder
35+
get() = "${lambdaFolder}plugins${File.separator}"
36+
37+
@JvmStatic
38+
val packetLogFolder
39+
get() = "${lambdaFolder}packet-logs${File.separator}"
40+
41+
@JvmStatic
42+
val songFolder
43+
get() = "${lambdaFolder}songs${File.separator}"
2544

2645
/**
27-
* Opens the given folder using the right library based on OS
46+
* Opens the given path using the right library based on OS
2847
*/
2948
fun openFolder(path: String) {
3049
Thread {
31-
if (getOS() == OperatingSystem.WINDOWS) Desktop.getDesktop().open(File(path))
32-
else Runtime.getRuntime().exec(getURLOpenCommand(File(path).toURI().toURL()))
50+
val file = File(path)
51+
if (!file.exists()) file.mkdir()
52+
if (getOS() == OperatingSystem.WINDOWS) {
53+
Desktop.getDesktop().open(file)
54+
} else {
55+
Runtime.getRuntime().exec(getURLOpenCommand(file.toURI().toURL()))
56+
}
3357
}.start()
3458
}
3559

@@ -62,7 +86,7 @@ object FolderUtils {
6286
}
6387
}
6488

65-
private enum class OperatingSystem {
89+
enum class OperatingSystem {
6690
UNIX, OSX, WINDOWS
6791
}
6892
}

0 commit comments

Comments
 (0)