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 | | motd | Server motd |
| players_count | Current number of players on the server | | players_count | Current number of players on the server |
| maxplayers | Max number of players on the server | | maxplayers | Max number of players on the server |
| players_list | List of current players on server |
### As cli: ### As cli:
@ -84,4 +83,3 @@ MC_HOST=localhost MC_PORT=25565 mcaio name
| motd | Server motd | | motd | Server motd |
| pcount | Current number of players on the server | | pcount | Current number of players on the server |
| pmax | Max 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 return await server.players_count
async def get_players(server: AIOMCServer) -> tuple:
return tuple(await server.players_list)
async def action() -> None: async def action() -> None:
try: try:
HOST, PORT = get_env("MC_HOST"), int(get_env("MC_PORT")) HOST, PORT = get_env("MC_HOST"), int(get_env("MC_PORT"))
server = AIOMCServer(HOST, PORT) server = AIOMCServer(HOST, PORT)
match arg(): match arg():
case "name": case 'name':
out = get_name(server) out = get_name(server)
case "pmax": case 'pmax':
out = get_max(server) out = get_max(server)
case "pcount": case 'pcount':
out = get_count(server) out = get_count(server)
case "motd": case 'motd':
out = get_motd(server) out = get_motd(server)
case "players":
out = get_players(server)
case _: case _:
raise RuntimeError raise RuntimeError
print(await out) print(await out)

View File

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