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

By: DerFrZocker <derrieple@gmail.com>
This commit is contained in:
CraftBukkit/Spigot 2024-04-26 09:16:58 +02:00
parent 5602bd9758
commit 6dc63e45f2

View file

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