Skip to content

Commit 623eaae

Browse files
committed
refactor: drop server-specific fields, use per-download post action
1 parent fdd04c6 commit 623eaae

1 file changed

Lines changed: 24 additions & 11 deletions

File tree

src/main/java/org/mcphackers/mcp/tools/versions/DownloadData.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ public class DownloadData {
2828
public int totalSize;
2929
protected List<Download> downloadQueue = new ArrayList<>();
3030
protected AssetIndex assets;
31-
private Path serverZip;
32-
private Path serverJar;
3331

3432
public DownloadData(MCP mcp, Version version) {
3533
this(mcp, MCPPaths.get(mcp, MCPPaths.LIB), MCPPaths.get(mcp, MCPPaths.JARS), MCPPaths.get(mcp, MCPPaths.JAR_ORIGINAL, Side.CLIENT), MCPPaths.get(mcp, MCPPaths.JAR_ORIGINAL, Side.SERVER), version);
@@ -43,12 +41,14 @@ public DownloadData(MCP mcp, Path libraries, Path gameDir, Path client, Path ser
4341
Artifact serverArtifact = version.downloads.artifacts.get("server");
4442
if (mcp.getOptions().getSide().includesServer() && serverArtifact != null) {
4543
Path serverOut = server;
44+
IORunnable onComplete = null;
4645
if (serverArtifact.url.endsWith(".zip")) {
47-
serverOut = server.getParent().resolve("minecraft_server.zip");
48-
this.serverZip = serverOut;
49-
this.serverJar = server;
46+
Path serverZip = server.getParent().resolve("minecraft_server.zip");
47+
Path serverJar = server;
48+
serverOut = serverZip;
49+
onComplete = () -> extractServerZip(serverZip, serverJar);
5050
}
51-
queueDownload(serverArtifact, serverOut);
51+
queueDownload(serverArtifact, serverOut, onComplete);
5252
}
5353
for (DependDownload dependencyDownload : version.libraries) {
5454
if (Rule.apply(dependencyDownload.rules)) {
@@ -126,11 +126,15 @@ public void setAssets(AssetIndex assets) {
126126
}
127127

128128
public void queueDownload(IDownload dl, Path baseDir) {
129+
queueDownload(dl, baseDir, null);
130+
}
131+
132+
public void queueDownload(IDownload dl, Path baseDir, IORunnable onComplete) {
129133
if (dl == null) {
130134
return;
131135
}
132136
totalSize += dl.downloadSize();
133-
downloadQueue.add(new Download(dl, baseDir));
137+
downloadQueue.add(new Download(dl, baseDir, onComplete));
134138
}
135139

136140
public void performDownload(DownloadListener listener) throws IOException {
@@ -146,12 +150,14 @@ public void performDownload(DownloadListener listener) throws IOException {
146150
if (parent != null) Files.createDirectories(parent);
147151
FileUtil.downloadFile(download.downloadURL(), file);
148152
}
153+
if (dl.onComplete != null) {
154+
dl.onComplete.run();
155+
}
149156
}
150-
extractServerZip();
151157
}
152158

153-
private void extractServerZip() throws IOException {
154-
if (serverZip == null || !Files.exists(serverZip)) {
159+
private static void extractServerZip(Path serverZip, Path serverJar) throws IOException {
160+
if (!Files.exists(serverZip)) {
155161
return;
156162
}
157163
FileUtil.extractByExtension(serverZip, serverZip.getParent(), ".jar");
@@ -164,10 +170,17 @@ private void extractServerZip() throws IOException {
164170
private static class Download {
165171
IDownload download;
166172
Path dir;
173+
IORunnable onComplete;
167174

168-
public Download(IDownload dl, Path path) {
175+
public Download(IDownload dl, Path path, IORunnable onComplete) {
169176
download = dl;
170177
dir = path;
178+
this.onComplete = onComplete;
171179
}
172180
}
181+
182+
@FunctionalInterface
183+
private interface IORunnable {
184+
void run() throws IOException;
185+
}
173186
}

0 commit comments

Comments
 (0)