|
5 | 5 | import java.nio.file.Files;
|
6 | 6 | import java.nio.file.Path;
|
7 | 7 | import java.util.ArrayList;
|
| 8 | +import java.util.Arrays; |
8 | 9 | import java.util.Iterator;
|
9 | 10 | import java.util.List;
|
10 | 11 | import java.util.ListIterator;
|
@@ -109,44 +110,45 @@ public static String walkUp(int offset, String path) throws Exception {
|
109 | 110 | }
|
110 | 111 |
|
111 | 112 | /**
|
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 | + * |
113 | 116 | * @return ServerSync jar file
|
114 | 117 | */
|
115 | 118 | 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()); |
120 | 120 | }
|
121 | 121 |
|
122 |
| - /**ath.substring(0, lastIndex) |
| 122 | + /** |
123 | 123 | * Tries to guess Minecraft directory intelligently.
|
| 124 | + * |
124 | 125 | * @return Minecraft directory location as {@link Path} object
|
125 | 126 | */
|
126 | 127 | public static String getMinecraftDirectory() {
|
127 | 128 | File jarFile = getServerSyncFile();
|
128 | 129 | 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); |
148 | 136 | }
|
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; |
150 | 152 | }
|
151 | 153 |
|
152 | 154 | private static List<String> getPathParts(String path) {
|
|
0 commit comments