Skip to content

Commit 080c112

Browse files
authored
Change the storage locations on macOS and Linux (#24)
* Change Storage location to Application Support on macOS Give XDG_DATA_HOME the right fallback ~/.local/share * remove unused imports
1 parent aa5ea26 commit 080c112

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

src/main/java/i18nupdatemod/I18nUpdateMod.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
import java.nio.file.Paths;
1818
import java.util.ArrayList;
1919
import java.util.List;
20+
import java.util.Objects;
2021
import java.util.stream.Collectors;
22+
import java.util.stream.Stream;
2123

2224
public class I18nUpdateMod {
2325
public static final String MOD_ID = "i18nupdatemod";
@@ -97,20 +99,26 @@ private static String getResourcePackDescription(List<GameAssetDetail.AssetDownl
9799
}
98100

99101
public static String getLocalStoragePos(Path minecraftPath) {
100-
String userHome = System.getProperty("user.home");
101-
Path oldPath = Paths.get(userHome, "." + MOD_ID);
102+
Path userHome = Paths.get(System.getProperty("user.home"));
103+
Path oldPath = userHome.resolve("." + MOD_ID);
102104
if (Files.exists(oldPath)) {
103-
return userHome;
105+
return userHome.toString();
104106
}
105107

108+
// https://developer.apple.com/documentation/foundation/url/3988452-applicationsupportdirectory#discussion
109+
String macAppSupport = System.getProperty("os.name").contains("OS X") ?
110+
userHome.resolve("Library/Application Support").toString() : null;
106111
String localAppData = System.getenv("LocalAppData");
112+
113+
// XDG_DATA_HOME fallbacks to ~/.local/share
114+
// https://specifications.freedesktop.org/basedir-spec/latest/#variables
107115
String xdgDataHome = System.getenv("XDG_DATA_HOME");
108-
if (localAppData != null) {
109-
return localAppData;
110-
} else if (xdgDataHome != null) {
111-
return xdgDataHome;
112-
} else {
113-
return minecraftPath.toString();
116+
if (xdgDataHome == null) {
117+
xdgDataHome = userHome.resolve(".local/share").toString();
114118
}
119+
120+
return Stream.of(localAppData, macAppSupport).filter(
121+
Objects::nonNull
122+
).findFirst().orElse(xdgDataHome);
115123
}
116124
}

0 commit comments

Comments
 (0)