mirror of
https://github.com/PaperMC/Paper.git
synced 2025-01-02 17:32:03 +01:00
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:
parent
5602bd9758
commit
6dc63e45f2
1 changed files with 9 additions and 7 deletions
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue