Skip to content

Commit

Permalink
Add FAQ to docs
Browse files Browse the repository at this point in the history
  • Loading branch information
PerchunPak committed Mar 20, 2023
1 parent 64be967 commit a7c9e7c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Content
:maxdepth: 1
:caption: Pages

pages/faq.rst
pages/contributing.rst
examples/examples.rst

Expand Down
76 changes: 76 additions & 0 deletions docs/pages/faq.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
Frequently Asked Questions
==========================


Why doesn't :class:`~mcstatus.server.BedrockServer` have an async :meth:`~mcstatus.server.MCServer.lookup` method?
------------------------------------------------------------------------------------------------------------------

With Java servers, to find the server, we sometimes end up performing an SRV
DNS lookup. This means making a request to your DNS server and waiting for an
answer, making that lookup a blocking operation (during which other things can
be done).

.. note::
An SRV record allows the server to have an address like: ``hypixel.net``,
that points to a some specified IP/Host and port, depending on this record.

That way, even if the server is hosted on a non-standard port (other than
25565, say 8855), you won't need to use ``myserver.com:8855``, since the
port number will simply be stored in the SRV record, so people can still
connect simply with ``myserver.com``.

On top of that, it also allows to specify a different IP/Host, which means
you don't need to use the same server to run both the website, and the
minecraft server. Instead, the SRV record can simply point to a different
IP/Host address (like ``mc.hypixel.net``, or ``209.222.114.112``).

However with Bedrock servers, no such lookups are required (Bedrock doesn't
support SRV records), and so there is no blocking I/O operation being made,
that would justify having an async version.

In fact, all that the bedrock lookup does is parsing the ``host:port`` address,
and obtaining the ``host`` and ``port`` parts out of it (with some error
handling, and support for default ports).


Incorrect encoding
------------------

In Query protocol, Minecraft uses ISO 8859-1 for encoding all text (like MOTD,
server name, etc.). This can cause problems with non-latin characters. To fix
such error, you can re-encode text into UTF-8.

.. code-block:: python
>>> query = JavaServer.lookup("my-server-ip.com").query()
>>> query.motd
'Ð\x9fÑ\x80ивÑ\x96Ñ\x82!'
>>> query.motd.encode("iso-8859-1").decode("utf-8")
'Привіт!'
:attr:`query.motd <mcstatus.querier.QueryResponse.motd>` here can be anything,
that contains incorrect encoding.


How to get server image?
------------------------

Bedrock servers do not provide icons, so you can't get server image from them.
For Java servers, you can use :attr:`status.icon <mcstatus.status_response.JavaStatusResponse.icon>`
attribute. It will return `Base64 <https://en.wikipedia.org/wiki/Base64>`_
encoded PNG image, so you can decode it in that way:

.. code-block:: python
import base64
with open("server-icon.png", "wb") as server_icon_file:
server_favicon_file.write(base64.decodebytes(status.icon))
.. note::
Most modern browsers support simply pasting the raw Base64 image into the
URL bar, which will open it as an image preview, allowing you to take a
quick look at it without having to use file saving from Python.

See `How to display Base64 image <https://stackoverflow.com/questions/8499633>`_
and `Base64 Images: Support table <https://caniuse.com/atob-btoa>`_.
5 changes: 4 additions & 1 deletion mcstatus/status_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ class JavaStatusResponse(BaseStatusResponse):
players: JavaStatusPlayers
version: JavaStatusVersion
icon: str | None
"""The icon of the server. In `Base64 <https://en.wikipedia.org/wiki/Base64>`_ encoded PNG image format."""
"""The icon of the server. In `Base64 <https://en.wikipedia.org/wiki/Base64>`_ encoded PNG image format.
.. seealso:: :ref:`pages/faq:how to get server image?`
"""

@classmethod
def build(cls, raw: RawJavaResponse, latency: float = 0) -> Self:
Expand Down

0 comments on commit a7c9e7c

Please sign in to comment.