Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example "Ping as Java and as Bedrock in one time" didn't work #500

Closed
minecraft-go-new opened this issue Mar 10, 2023 · 7 comments
Closed
Labels
meta: invalid This doesn't seem right type: question Request for clarification or further information

Comments

@minecraft-go-new
Copy link

Example "Ping as Java and as Bedrock in one time" didn't work
code:

import asyncio

from mcstatus import BedrockServer, JavaServer
from mcstatus.status_response import BedrockStatusResponse, JavaStatusResponse


async def status(host: str) -> JavaStatusResponse | BedrockStatusResponse:
    """Get status from server, which can be Java or Bedrock.

    The function will ping server as Java and as Bedrock in one time, and return the first response.
    """
    success_task = await handle_exceptions(
        *(
            await asyncio.wait(
                {
                    asyncio.create_task(handle_java(host), name="Get status as Java"),
                    asyncio.create_task(handle_bedrock(host), name="Get status as Bedrock"),
                },
                return_when=asyncio.FIRST_COMPLETED,
            )
        )
    )

    if success_task is None:
        raise ValueError("No tasks were successful. Is server offline?")

    return success_task.result()


async def handle_exceptions(done: set[asyncio.Task], pending: set[asyncio.Task]) -> asyncio.Task | None:
    """Handle exceptions from tasks.

    Also, cancel all pending tasks, if found correct one.
    """
    if len(done) == 0:
        raise ValueError("No tasks was given to `done` set.")

    for i, task in enumerate(done):
        if task.exception() is not None:
            if len(pending) == 0:
                continue

            if i == len(done) - 1:  # firstly check all items from `done` set, and then handle pending set
                return await handle_exceptions(*(await asyncio.wait(pending, return_when=asyncio.FIRST_COMPLETED)))
        else:
            for pending_task in pending:
                pending_task.cancel()
            return task


async def handle_java(host: str) -> JavaStatusResponse:
    """A wrapper around mcstatus, to compress it in one function."""
    return await (await JavaServer.async_lookup(host)).async_status()


async def handle_bedrock(host: str) -> BedrockStatusResponse:
    """A wrapper around mcstatus, to compress it in one function."""
    # note: `BedrockServer` doesn't have `async_lookup` method, see it's docstring
    return await BedrockServer.lookup(host).async_status()

def main() -> None:
    asyncio.run(status("mc.craftsquade.com"))


if __name__ == "__main__":
    main()

Output:

Traceback (most recent call last):
  File "D:\code\MinecraftServerInformer\main.py", line 4, in <module>
    from mcstatus.status_response import BedrockStatusResponse, JavaStatusResponse
ModuleNotFoundError: No module named 'mcstatus.status_response'

Process finished with exit code 1
@PerchunPak
Copy link
Member

PerchunPak commented Mar 10, 2023

We have not released #306 changes, so you can't use it's response classes. You should look at this version of example.

@kevinkjt2000
Copy link
Contributor

I could craft an alpha release if need be.

@PerchunPak
Copy link
Member

It's ready to be released. I don't see reasons to make it alpha/beta.

The only reason was documentation outdating, but it is fixed by #484.

@ItsDrike
Copy link
Member

The reason for putting it off was that we wanted the release to come alongside with #335

@PerchunPak
Copy link
Member

PerchunPak commented Mar 11, 2023

Oh, I forgot about this. I will work on #335 in some time.

But then yes, alpha/beta would be the best solution for now. Maybe even #335 should be added to the release, as there are only one conversation about tests and ToDos with testing on real world examples.

@kevinkjt2000
Copy link
Contributor

Alpha version 11.0.0a1 is available on PyPi now.

@PerchunPak
Copy link
Member

Can it be closed?

@ItsDrike ItsDrike added meta: invalid This doesn't seem right type: question Request for clarification or further information labels Apr 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
meta: invalid This doesn't seem right type: question Request for clarification or further information
Projects
None yet
Development

No branches or pull requests

4 participants