Skip to content

Commit 0e74d24

Browse files
committed
NoIssue: Fix forge loaded root directory discovery
1 parent 1555021 commit 0e74d24

File tree

2 files changed

+37
-30
lines changed

2 files changed

+37
-30
lines changed

src/main/java/com/superzanti/serversync/filemanager/FileManager.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@ public class FileManager {
2020
public final Path logsDirectory;
2121

2222
public FileManager() {
23-
// TODO could probably DRY this out a bit
24-
modFilesDirectory = new PathBuilder(PathUtils.getMinecraftDirectory()).add("mods").buildPath();
25-
configurationFilesDirectory = new PathBuilder(PathUtils.getMinecraftDirectory()).add("config").buildPath();
26-
logsDirectory = new PathBuilder(PathUtils.getMinecraftDirectory()).add("logs").buildPath();
23+
String root = PathUtils.getMinecraftDirectory();
24+
25+
if (root == null) {
26+
root = "";
27+
}
28+
29+
modFilesDirectory = new PathBuilder(root).add("mods").buildPath();
30+
configurationFilesDirectory = new PathBuilder(root).add("config").buildPath();
31+
logsDirectory = new PathBuilder(root).add("logs").buildPath();
2732
}
2833

2934
public ArrayList<SyncFile> getModFiles(String directory, List<String> fileMatchPatterns,

src/main/java/com/superzanti/serversync/util/PathUtils.java

+28-26
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.nio.file.Files;
66
import java.nio.file.Path;
77
import java.util.ArrayList;
8+
import java.util.Arrays;
89
import java.util.Iterator;
910
import java.util.List;
1011
import java.util.ListIterator;
@@ -109,44 +110,45 @@ public static String walkUp(int offset, String path) throws Exception {
109110
}
110111

111112
/**
112-
* Uses Java reflection magic and ServerSync's {@linkplain Main} class to get jar file as {@linkplain File} object.
113+
* Uses Java reflection magic and ServerSync's {@linkplain Main} class to get
114+
* jar file as {@linkplain File} object.
115+
*
113116
* @return ServerSync jar file
114117
*/
115118
public static File getServerSyncFile() {
116-
return new java.io.File(Main.class.getProtectionDomain()
117-
.getCodeSource()
118-
.getLocation()
119-
.getPath());
119+
return new java.io.File(Main.class.getProtectionDomain().getCodeSource().getLocation().getPath());
120120
}
121121

122-
/**ath.substring(0, lastIndex)
122+
/**
123123
* Tries to guess Minecraft directory intelligently.
124+
*
124125
* @return Minecraft directory location as {@link Path} object
125126
*/
126127
public static String getMinecraftDirectory() {
127128
File jarFile = getServerSyncFile();
128129
String jarFilePath = jarFile.getAbsolutePath();
129-
String jarFileName = jarFile.getName();
130-
int lastIndex = -1;
131-
String[] directories = jarFilePath.replace("\\", "/").split("/");
132-
int dirsLen = directories.length - 1;
133-
if (directories[dirsLen].equals("mods")) {
134-
// Length - ServerSync jar filename - "mods/"
135-
// this covers mods/ServerSync.jar case
136-
lastIndex = jarFilePath.length() - jarFileName.length() - 5;
137-
} else if (directories[dirsLen].contains(".") && directories[dirsLen - 1].equals("mods")) {
138-
// Length - ServerSync jar filename - "/" - length of directory parenting jar file - "/"
139-
// this covers mods/1.12.2/ServerSync.jar case
140-
lastIndex = jarFilePath.length() - jarFileName.length() - 2 - directories[dirsLen - 1].length();
141-
// } else if (directories[dirsLen].equals(".minecraft")) {
142-
// // Length - jar filename - "/"
143-
// // this covers .minecraft/ServerSync.jar case
144-
// lastIndex = jarFilePath.length() - jarFileName.length() - 1;
145-
} else {
146-
// According to repository wiki, ServerSync must be placed in Minecraft directory
147-
lastIndex = jarFilePath.length() - jarFileName.length() - 1;
130+
131+
List<String> parts = Arrays.asList(jarFilePath.split("[\\\\/]"));
132+
133+
if (parts.contains("file:")) {
134+
// Shift past the file declaration when loaded in a forge environment
135+
parts = parts.subList(parts.indexOf("file:") + 1, parts.size() - 1);
148136
}
149-
return jarFilePath.substring(0, lastIndex);
137+
138+
if (parts.contains("mods")) {
139+
// ASSUMPTION: We are most likely in the mods directory of a minecraft directory
140+
List<String> root = parts.subList(0, parts.indexOf("mods"));
141+
PathBuilder builder = new PathBuilder();
142+
root.forEach(builder::add);
143+
144+
return builder.toString();
145+
}
146+
147+
// ASSUMPTION: As users are instructed to put ServerSync in the Minecraft
148+
// directory we can assume that the current directory is where serversync is
149+
// supposed to be, as we are asking for the Minecraft directory it should be
150+
// handled elsewhere when the directory can not be found
151+
return null;
150152
}
151153

152154
private static List<String> getPathParts(String path) {

0 commit comments

Comments
 (0)