13
0
geforkt von Mirrors/Paper

Open a direct connection instead of pinging mojang server to check if it is reachable

By: DerFrZocker <derrieple@gmail.com>
Dieser Commit ist enthalten in:
CraftBukkit/Spigot 2024-04-26 09:16:58 +02:00
Ursprung 5602bd9758
Commit 6dc63e45f2

Datei anzeigen

@ -1,7 +1,7 @@
package org.bukkit.support.condition; package org.bukkit.support.condition;
import com.mojang.authlib.yggdrasil.YggdrasilEnvironment; import com.mojang.authlib.yggdrasil.YggdrasilEnvironment;
import java.net.InetAddress; import java.net.HttpURLConnection;
import java.net.URI; import java.net.URI;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.extension.ConditionEvaluationResult; import org.junit.jupiter.api.extension.ConditionEvaluationResult;
@ -12,17 +12,19 @@ public class EnableIfMojangServerAvailableCondition implements ExecutionConditio
@Override @Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext extensionContext) { public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext extensionContext) {
HttpURLConnection url = null;
try { try {
URI url = new URI(YggdrasilEnvironment.PROD.getEnvironment().servicesHost()); url = (HttpURLConnection) new URI(YggdrasilEnvironment.PROD.getEnvironment().servicesHost()).toURL().openConnection();
InetAddress address = InetAddress.getByName(url.getHost()); url.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(1));
url.connect();
if (!address.isReachable((int) TimeUnit.SECONDS.toMillis(1))) {
return ConditionEvaluationResult.disabled("Mojang server is not available");
}
return ConditionEvaluationResult.enabled("Mojang server available"); return ConditionEvaluationResult.enabled("Mojang server available");
} catch (Exception e) { } catch (Exception e) {
return ConditionEvaluationResult.disabled(e.getMessage()); return ConditionEvaluationResult.disabled(e.getMessage());
} finally {
if (url != null) {
url.disconnect();
}
} }
} }
} }