From 890d378fdd53f3d7544a678e46398e8aa9196599 Mon Sep 17 00:00:00 2001 From: rina Date: Sat, 20 Jul 2024 11:51:30 +1000 Subject: [PATCH] review: fallback logic in CLI ping since this runs both ping() then status(), it can report precisely when one fails and the other succeeds. some kludgy logic to switch bedrock too. --- mcstatus/__main__.py | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/mcstatus/__main__.py b/mcstatus/__main__.py index 811a6aaf..e0c73302 100644 --- a/mcstatus/__main__.py +++ b/mcstatus/__main__.py @@ -5,6 +5,7 @@ import json as _json import argparse import socket +import warnings import dataclasses from typing import TYPE_CHECKING @@ -24,10 +25,33 @@ def _motd(motd: Motd) -> str: def ping(server: SupportedServers) -> int: - # this method supports both Java and Bedrock. - # only Java supports the `ping` packet, and even then not always: - # https://github.com/py-mine/mcstatus/issues/850 - print(f"{server.status().latency}") + notsup = "notsup" + + # handle java and bedrock differences, as well as non-conformant servers + # which require a 'status' packet. + + try: + ping_res = server.ping() if isinstance(server, JavaServer) else notsup + except Exception as e: + ping_res = e + + # at this point, ping_res is NOTSUP for Bedrock, otherwise it is either a float or an Exception. + + if isinstance(ping_res, (float, int)): + latency = ping_res + else: + latency = server.status().latency + + if ping_res != notsup: + addr = f"{server.address.host}:{server.address.port}" + warnings.warn( + f"contacting {addr} failed with a 'ping' packet but succeeded with a 'status' packet,\n " + f"this is likely a bug in the server-side implementation.\n " + f"for more details, see: https://mcstatus.readthedocs.io/en/stable/pages/faq/\n", + stacklevel=1, + ) + + print(f"{latency}") return 0