Skip to content

Commit

Permalink
review: fallback logic in CLI ping
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
katrinafyi committed Jul 20, 2024
1 parent faf208b commit 890d378
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions mcstatus/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import json as _json
import argparse
import socket
import warnings
import dataclasses
from typing import TYPE_CHECKING

Expand All @@ -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


Expand Down

0 comments on commit 890d378

Please sign in to comment.