Compare commits

..

No commits in common. "c56bc396fe39c14a9c1390fb897057d246459ea9" and "be81204cd95c8b922edfafc2276e6a1cebbd826b" have entirely different histories.

3 changed files with 7 additions and 36 deletions

View File

@ -68,7 +68,6 @@ print(name)
| motd | Server motd |
| players_count | Current number of players on the server |
| maxplayers | Max number of players on the server |
| players_list | List of current players on server |
### As cli:
@ -84,4 +83,3 @@ MC_HOST=localhost MC_PORT=25565 mcaio name
| motd | Server motd |
| pcount | Current number of players on the server |
| pmax | Max number of players on the server |
| players | List of current players on server |

View File

@ -33,25 +33,19 @@ async def get_max(server: AIOMCServer) -> int:
return await server.players_count
async def get_players(server: AIOMCServer) -> tuple:
return tuple(await server.players_list)
async def action() -> None:
try:
HOST, PORT = get_env("MC_HOST"), int(get_env("MC_PORT"))
server = AIOMCServer(HOST, PORT)
match arg():
case "name":
case 'name':
out = get_name(server)
case "pmax":
case 'pmax':
out = get_max(server)
case "pcount":
case 'pcount':
out = get_count(server)
case "motd":
case 'motd':
out = get_motd(server)
case "players":
out = get_players(server)
case _:
raise RuntimeError
print(await out)

View File

@ -1,6 +1,5 @@
from asyncio import open_connection as aiocon, StreamReader, StreamWriter
from abc import ABC, abstractmethod
from collections.abc import Iterable
from struct import pack as struct_pack
from json import loads as jl
@ -60,11 +59,6 @@ class IMCServer(ABC):
async def motd(self) -> str:
raise NotImplementedError
@property
@abstractmethod
async def players_list(self) -> Iterable:
raise NotImplementedError
class AIOMCServer(IMCServer):
@ -72,15 +66,6 @@ class AIOMCServer(IMCServer):
_max: int
_count: int
_motd: str
_data: bytes
_players: Iterable
def __init__(self, host: str, port: int) -> None:
super().__init__(host, port)
self._data = self._pack_data(
b"\x00\x00" + self._pack_data(self.host.encode('utf8')) + self.
_pack_port(self.port) + b"\x01")
self._players = ()
@staticmethod
async def _unpack_varint(s):
@ -115,7 +100,9 @@ class AIOMCServer(IMCServer):
async def _get_data(self) -> dict:
async with AIOConnection(self.host, self.port) as socket:
await socket.send(self._data)
await socket.send(self._pack_data(
b"\x00\x00" + self._pack_data(self.host.encode('utf8')) + self.
_pack_port(self.port) + b"\x01"))
await socket.send(self._pack_data("\x00"))
await self._unpack_varint(socket)
await self._unpack_varint(socket)
@ -132,9 +119,6 @@ class AIOMCServer(IMCServer):
players = data["players"]
self._count = int(players["online"])
self._max = int(players["max"])
if "sample" in tuple(players.keys()):
self._players = tuple(map(lambda player: player["name"],
players["sample"]))
@property
async def players_count(self) -> int:
@ -155,8 +139,3 @@ class AIOMCServer(IMCServer):
async def motd(self) -> str:
await self.update()
return self._motd
@property
async def players_list(self) -> Iterable:
await self.update()
return self._players