Skip to content

Commit fb397a7

Browse files
committed
feat: Throw caught exceptions when running in IDE
1 parent 1707cbe commit fb397a7

File tree

11 files changed

+71
-16
lines changed

11 files changed

+71
-16
lines changed

src/main/java/org/mcphackers/mcp/MCP.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.mcphackers.mcp.tasks.TaskStaged;
2323
import org.mcphackers.mcp.tasks.mode.TaskMode;
2424
import org.mcphackers.mcp.tasks.mode.TaskParameter;
25+
import org.mcphackers.mcp.tools.Util;
2526
import org.mcphackers.mcp.tools.project.LegacyProjectAdapter;
2627
import org.mcphackers.mcp.tools.source.Source;
2728
import org.mcphackers.mcp.tools.versions.DownloadData;
@@ -37,6 +38,7 @@ public abstract class MCP {
3738
private static final VersionParser VERSION_PARSER = new VersionParser();
3839
public Options options = new Options(this, Paths.get("options.cfg"));
3940
protected boolean isGUI = false;
41+
public static final boolean IS_RUNNING_IN_IDE = Boolean.getBoolean("retromcp.runInIDE");
4042

4143
protected MCP() {
4244
Update.attemptToDeleteUpdateJar();
@@ -126,6 +128,7 @@ public final boolean performTask(TaskMode mode, Side side, boolean completionMsg
126128
} catch (Throwable e1) {
127129
result1.set(Task.ERROR);
128130
e.set(e1);
131+
Util.throwExceptionInIDE(e1);
129132
}
130133
if (enableProgressBars) {
131134
setProgress(barIndex, TRANSLATOR.translateKey("task.stage.finished"), 100);

src/main/java/org/mcphackers/mcp/Options.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,31 +136,39 @@ public boolean getBooleanParameter(TaskParameter param) throws IllegalArgumentEx
136136
if (value instanceof Boolean) {
137137
return (Boolean) value;
138138
}
139-
throw new IllegalArgumentException("Type mismatch");
139+
IllegalArgumentException t = new IllegalArgumentException("Type mismatch");
140+
Util.throwExceptionInIDE(t);
141+
throw t;
140142
}
141143

142144
public String[] getStringArrayParameter(TaskParameter param) throws IllegalArgumentException {
143145
Object value = options.get(param);
144146
if (value == null || value instanceof String[]) {
145147
return (String[]) value;
146148
}
147-
throw new IllegalArgumentException("Type mismatch");
149+
IllegalArgumentException t = new IllegalArgumentException("Type mismatch");
150+
Util.throwExceptionInIDE(t);
151+
throw t;
148152
}
149153

150154
public String getStringParameter(TaskParameter param) throws IllegalArgumentException {
151155
Object value = options.get(param);
152156
if (value == null || value instanceof String) {
153157
return (String) value;
154158
}
155-
throw new IllegalArgumentException("Type mismatch");
159+
IllegalArgumentException t = new IllegalArgumentException("Type mismatch");
160+
Util.throwExceptionInIDE(t);
161+
throw t;
156162
}
157163

158164
public int getIntParameter(TaskParameter param) throws IllegalArgumentException {
159165
Object value = options.get(param);
160166
if (value instanceof Integer) {
161167
return (Integer) value;
162168
}
163-
throw new IllegalArgumentException("Type mismatch");
169+
IllegalArgumentException t = new IllegalArgumentException("Type mismatch");
170+
Util.throwExceptionInIDE(t);
171+
throw t;
164172
}
165173

166174
public boolean safeSetParameter(TaskParameter param, String value) {

src/main/java/org/mcphackers/mcp/tasks/TaskCreatePatch.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import codechicken.diffpatch.DiffOperation;
1010
import org.mcphackers.mcp.MCP;
1111
import org.mcphackers.mcp.MCPPaths;
12+
import org.mcphackers.mcp.tools.Util;
1213

1314
public class TaskCreatePatch extends TaskStaged {
1415
public TaskCreatePatch(Side side, MCP instance) {
@@ -24,10 +25,14 @@ protected Stage[] setStages() {
2425
Path patchesOut = MCPPaths.get(mcp, PATCHES, side);
2526
setProgress(getLocalizedStage("createpatch"));
2627
if (!Files.exists(srcPathPatched)) {
27-
throw new IOException("Patched " + side.name + " sources cannot be found!");
28+
IOException t = new IOException("Patched " + side.name + " sources cannot be found!");
29+
Util.throwExceptionInIDE(t);
30+
throw t;
2831
}
2932
if (!Files.exists(srcPathUnpatched)) {
30-
throw new IOException("Unpatched " + side.name + " sources cannot be found!");
33+
IOException t = new IOException("Unpatched " + side.name + " sources cannot be found!");
34+
Util.throwExceptionInIDE(t);
35+
throw t;
3136
}
3237
createDiffOperation(srcPathUnpatched, srcPathPatched, patchesOut);
3338
})

src/main/java/org/mcphackers/mcp/tasks/TaskDownloadUpdate.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ protected Stage[] setStages() {
6262
Util.runCommand(cmd);
6363
System.exit(0);
6464
} else {
65-
throw new IOException("Running from a folder! Aborting");
65+
IOException t = new IOException("Running from a folder! Aborting");
66+
Util.throwExceptionInIDE(t);
67+
throw t;
6668
}
6769
} else {
6870
log("Cancelling update!");

src/main/java/org/mcphackers/mcp/tasks/TaskMergeMappings.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.nio.file.Paths;
66

77
import org.mcphackers.mcp.MCP;
8+
import org.mcphackers.mcp.tools.Util;
89
import org.mcphackers.mcp.tools.mappings.MappingUtil;
910

1011
public class TaskMergeMappings extends TaskStaged {
@@ -28,7 +29,9 @@ protected Stage[] setStages() {
2829
MappingUtil.mergeMappings(clientMappings, mergedMappings);
2930
}
3031
} else {
31-
throw new RuntimeException("client.tiny/server.tiny could not be found in the current directory!");
32+
RuntimeException t = new RuntimeException("client.tiny/server.tiny could not be found in the current directory!");
33+
Util.throwExceptionInIDE(t);
34+
throw t;
3235
}
3336
})
3437
};

src/main/java/org/mcphackers/mcp/tasks/TaskRecompile.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ protected Stage[] setStages() {
5353
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
5454
String javaHome = mcp.getOptions().getStringParameter(TaskParameter.JAVA_HOME);
5555
if (javaHome.isEmpty() && compiler == null) {
56-
throw new RuntimeException("Could not find compiling API. Please install or use a Java Development Kit to run this program.");
56+
RuntimeException t = new RuntimeException("Could not find compiling API. Please install or use a Java Development Kit to run this program.");
57+
Util.throwExceptionInIDE(t);
58+
throw t;
5759
}
5860
Path binPath = MCPPaths.get(mcp, BIN, side);
5961
Path srcPath = MCPPaths.get(mcp, SOURCE, side);
@@ -210,15 +212,19 @@ public void recompile(MCP mcp, Side side, String javaHome, Iterable<File> src, I
210212
boolean isWindows = OS.getOs().equals(OS.windows);
211213
Path javac = Paths.get(javaHome).resolve("bin").resolve(isWindows ? "javac.exe" : "javac");
212214
if (!Files.exists(javac)) {
213-
throw new RuntimeException("Failed to find javac at " + javac.toAbsolutePath());
215+
RuntimeException t = new RuntimeException("Failed to find javac at " + javac.toAbsolutePath());
216+
Util.throwExceptionInIDE(t);
217+
throw t;
214218
}
215219

216220
Path binDir = MCPPaths.get(mcp, BIN, side);
217221
if (!Files.exists(binDir)) {
218222
try {
219223
Files.createDirectories(binDir);
220224
} catch (IOException e) {
221-
throw new RuntimeException("[" + side.getName() + "] Failed to create output directory for recompile!");
225+
RuntimeException t = new RuntimeException("[" + side.getName() + "] Failed to create output directory for recompile!");
226+
Util.throwExceptionInIDE(t);
227+
throw t;
222228
}
223229
}
224230

@@ -235,10 +241,14 @@ public void recompile(MCP mcp, Side side, String javaHome, Iterable<File> src, I
235241
try {
236242
int exitCode = Util.runCommand(cmd.toArray(new String[]{}), MCPPaths.get(mcp, PROJECT, side), true);
237243
if (exitCode != 0) {
238-
throw new RuntimeException("Failed to compile!");
244+
RuntimeException t = new RuntimeException("Failed to compile!");
245+
Util.throwExceptionInIDE(t);
246+
throw t;
239247
}
240248
} catch (IOException e) {
241-
throw new RuntimeException(e);
249+
RuntimeException t = new RuntimeException(e);
250+
Util.throwExceptionInIDE(t);
251+
throw t;
242252
}
243253
}
244254
}

src/main/java/org/mcphackers/mcp/tasks/TaskUpdateLibraries.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ protected Stage[] setStages() {
3939
VersionParser versionParser = mcp.getVersionParser();
4040
Version version = this.mcp.getCurrentVersion();
4141
if (version == null) {
42-
throw new RuntimeException("Current version is null!");
42+
RuntimeException t = new RuntimeException("Current version is null!");
43+
Util.throwExceptionInIDE(t);
44+
throw t;
4345
}
4446
VersionParser.VersionData data = versionParser.getVersion(version.id);
4547
InputStream versionStream;

src/main/java/org/mcphackers/mcp/tasks/TaskUpdateMD5.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ public void updateMD5(boolean reobf) throws IOException {
5656
final Path md5 = MCPPaths.get(mcp, reobf ? MD5_RO : MD5, side);
5757

5858
if (!Files.exists(binPath)) {
59-
throw new IOException(side.name + " classes not found!");
59+
IOException t = new IOException(side.name + " classes not found!");
60+
Util.throwExceptionInIDE(t);
61+
throw t;
6062
}
6163
try (BufferedWriter writer = Files.newBufferedWriter(md5)) {
6264
progress = 0;

src/main/java/org/mcphackers/mcp/tools/FileUtil.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ public static void extract(final Path zipFile, final Path destDir, Function<ZipE
120120
if (match.apply(entry)) {
121121
if (!entry.isDirectory()) {
122122
Files.deleteIfExists(toPath);
123+
124+
// Create parent directory if it does not exist
125+
if (!Files.exists(toPath.getParent())) {
126+
Files.createDirectories(toPath.getParent());
127+
}
128+
123129
Files.copy(zipInputStream, toPath);
124130
} else {
125131
createDirectories(toPath);

src/main/java/org/mcphackers/mcp/tools/Util.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,15 @@ public static int getJavaVersion(MCP mcp) {
304304
}
305305
return 8;
306306
}
307+
308+
@SuppressWarnings("unchecked")
309+
public static <T extends Throwable> void sneakyThrow(Throwable t) throws T {
310+
throw (T) t;
311+
}
312+
313+
public static void throwExceptionInIDE(Throwable t) {
314+
if (MCP.IS_RUNNING_IN_IDE) {
315+
sneakyThrow(t);
316+
}
317+
}
307318
}

0 commit comments

Comments
 (0)