Skip to content

Commit d1ed078

Browse files
committed
Specify User Agent
1 parent 2089bf1 commit d1ed078

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.IOException;
66
import java.io.InputStream;
77
import java.net.URL;
8+
import java.net.URLConnection;
89
import java.nio.channels.Channels;
910
import java.nio.channels.FileChannel;
1011
import java.nio.channels.ReadableByteChannel;
@@ -30,6 +31,8 @@
3031
import java.util.zip.ZipInputStream;
3132
import java.util.zip.ZipOutputStream;
3233

34+
import org.mcphackers.mcp.MCP;
35+
3336
public abstract class FileUtil {
3437

3538
public static void delete(Path path) throws IOException {
@@ -131,13 +134,19 @@ public static void downloadFile(String url, Path output) throws IOException {
131134
}
132135

133136
public static void downloadFile(URL url, Path output) throws IOException {
134-
ReadableByteChannel channel = Channels.newChannel(url.openStream());
137+
ReadableByteChannel channel = Channels.newChannel(openURLStream(url));
135138
try (FileOutputStream stream = new FileOutputStream(output.toAbsolutePath().toString())) {
136139
FileChannel fileChannel = stream.getChannel();
137140
fileChannel.transferFrom(channel, 0, Long.MAX_VALUE);
138141
}
139142
}
140143

144+
public static InputStream openURLStream(URL url) throws IOException {
145+
URLConnection connection = url.openConnection();
146+
connection.setRequestProperty("User-Agent", "RetroMCP/" + MCP.VERSION);
147+
return connection.getInputStream();
148+
}
149+
141150
public static void deleteDirectoryIfExists(Path path) throws IOException {
142151
if (Files.isDirectory(path)) {
143152
deleteDirectory(path);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public DownloadData(Path libraries, Path gameDir, Path client, Path server, Vers
4747
Path assetIndex = gameDir.resolve("assets/indexes/" + version.assets + ".json");
4848
String assetIndexString;
4949
if (!Files.exists(assetIndex) || !version.assetIndex.sha1.equals(Util.getSHA1(assetIndex))) {
50-
assetIndexString = new String(Util.readAllBytes(new URL(version.assetIndex.url).openStream()));
50+
assetIndexString = new String(Util.readAllBytes(FileUtil.openURLStream(new URL(version.assetIndex.url))));
5151
Files.write(assetIndex, assetIndexString.getBytes());
5252
} else {
5353
assetIndexString = new String(Files.readAllBytes(assetIndex));

0 commit comments

Comments
 (0)