1
0
mirror of https://github.com/GeyserMC/Geyser.git synced 2025-12-19 14:59:27 +00:00

Merge remote-tracking branch 'upstream/master'

This commit is contained in:
onebeastchris
2025-10-26 19:02:42 +01:00
3 changed files with 34 additions and 13 deletions

View File

@@ -216,7 +216,11 @@ public class GeyserImpl implements GeyserApi, EventRegistrar {
public void initialize() { public void initialize() {
// Setup encryption early so we don't start if we can't auth // Setup encryption early so we don't start if we can't auth
EncryptionUtils.getMojangPublicKey(); try {
EncryptionUtils.getMojangPublicKey();
} catch (Throwable e) {
throw new RuntimeException("Cannot setup authentication! Are you offline? ", e);
}
long startupTime = System.currentTimeMillis(); long startupTime = System.currentTimeMillis();

View File

@@ -74,7 +74,6 @@ import java.util.function.Function;
*/ */
// Lots of unchecked casting happens here. It should all be handled properly. // Lots of unchecked casting happens here. It should all be handled properly.
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
// TODO only log some things once (like was done in vault translator)
public final class ItemStackParser { public final class ItemStackParser {
private static final Map<DataComponentType<?>, DataComponentParser<?, ?>> PARSERS = new Reference2ObjectOpenHashMap<>(); private static final Map<DataComponentType<?>, DataComponentParser<?, ?>> PARSERS = new Reference2ObjectOpenHashMap<>();

View File

@@ -76,7 +76,7 @@ public class WebUtils {
con.setRequestProperty("User-Agent", getUserAgent()); // Otherwise Java 8 fails on checking updates con.setRequestProperty("User-Agent", getUserAgent()); // Otherwise Java 8 fails on checking updates
con.setConnectTimeout(10000); con.setConnectTimeout(10000);
con.setReadTimeout(10000); con.setReadTimeout(10000);
checkResponseCode(con);
return connectionToString(con); return connectionToString(con);
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
throw new IllegalStateException("Unable to resolve requested url (%s)! Are you offline?".formatted(reqURL), e); throw new IllegalStateException("Unable to resolve requested url (%s)! Are you offline?".formatted(reqURL), e);
@@ -94,6 +94,7 @@ public class WebUtils {
con.setRequestProperty("User-Agent", getUserAgent()); con.setRequestProperty("User-Agent", getUserAgent());
con.setConnectTimeout(10000); con.setConnectTimeout(10000);
con.setReadTimeout(10000); con.setReadTimeout(10000);
checkResponseCode(con);
return GeyserImpl.JSON_MAPPER.readTree(con.getInputStream()); return GeyserImpl.JSON_MAPPER.readTree(con.getInputStream());
} }
@@ -107,6 +108,7 @@ public class WebUtils {
try { try {
HttpURLConnection con = (HttpURLConnection) new URL(reqURL).openConnection(); HttpURLConnection con = (HttpURLConnection) new URL(reqURL).openConnection();
con.setRequestProperty("User-Agent", getUserAgent()); con.setRequestProperty("User-Agent", getUserAgent());
checkResponseCode(con);
InputStream in = con.getInputStream(); InputStream in = con.getInputStream();
Files.copy(in, Paths.get(fileLocation), StandardCopyOption.REPLACE_EXISTING); Files.copy(in, Paths.get(fileLocation), StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) { } catch (Exception e) {
@@ -270,24 +272,38 @@ public class WebUtils {
} }
/** /**
* Get the string output from the passed {@link HttpURLConnection} * Gets the string output from the passed {@link HttpURLConnection},
* * or logs the error message.
* @param con The connection to get the string from
* @return The body of the returned page
* @throws IOException If the request fails
*/ */
private static String connectionToString(HttpURLConnection con) throws IOException { private static String connectionToString(HttpURLConnection con) throws IOException {
checkResponseCode(con);
return inputStreamToString(con.getInputStream(), con::disconnect);
}
/**
* Throws an exception if there is an error stream to avoid further issues
*/
private static void checkResponseCode(HttpURLConnection con) throws IOException {
// Send the request (we dont use this but its required for getErrorStream() to work) // Send the request (we dont use this but its required for getErrorStream() to work)
con.getResponseCode(); con.getResponseCode();
// Read the error message if there is one if not just read normally // Read the error message if there is one if not just read normally
InputStream inputStream = con.getErrorStream(); InputStream errorStream = con.getErrorStream();
if (inputStream == null) { if (errorStream != null) {
inputStream = con.getInputStream(); throw new IOException(inputStreamToString(errorStream, null));
} }
}
/**
* Get the string output from the passed {@link InputStream}
*
* @param stream The input stream to get the string from
* @return The body of the returned page
* @throws IOException If the request fails
*/
private static String inputStreamToString(InputStream stream, @Nullable Runnable onFinish) throws IOException {
StringBuilder content = new StringBuilder(); StringBuilder content = new StringBuilder();
try (BufferedReader in = new BufferedReader(new InputStreamReader(inputStream))) { try (BufferedReader in = new BufferedReader(new InputStreamReader(stream))) {
String inputLine; String inputLine;
while ((inputLine = in.readLine()) != null) { while ((inputLine = in.readLine()) != null) {
@@ -295,7 +311,9 @@ public class WebUtils {
content.append("\n"); content.append("\n");
} }
con.disconnect(); if (onFinish != null) {
onFinish.run();
}
} }
return content.toString(); return content.toString();