From 72d5270f8843580e2ac74926baa1bd97ef78c24c Mon Sep 17 00:00:00 2001 From: Vincent Date: Fri, 14 Jun 2024 14:09:02 +0100 Subject: [PATCH] feat: add some tests --- src/endstone_test/event_listener.py | 28 ++++++++++++++++++++++++++++ src/endstone_test/plugin.py | 13 +++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 src/endstone_test/event_listener.py diff --git a/src/endstone_test/event_listener.py b/src/endstone_test/event_listener.py new file mode 100644 index 0000000..1d5804c --- /dev/null +++ b/src/endstone_test/event_listener.py @@ -0,0 +1,28 @@ +import uuid +from functools import partial + +from endstone import Server +from endstone.event import event_handler, PlayerJoinEvent +from endstone.plugin import Plugin + + +class EventListener: + def __init__(self, plugin: Plugin): + self._plugin = plugin + + @event_handler + def on_player_join(self, event: PlayerJoinEvent) -> None: + def send_welcome_message(player_name: str, player_id: uuid.UUID) -> None: + self.server.broadcast_message(f"Welcome {player_name} to the server.") + self.server.dispatch_command(self.server.command_sender, f"say {player_name}, uuid: {player_id}") + + self.server.scheduler.run_task_later(self._plugin, + partial(send_welcome_message, event.player.name, event.player.unique_id), + delay=20) + + assert self.server.get_player(event.player.name) is event.player + # assert self.server.get_player(event.player.unique_id) is event.player # TODO: this will crash + + @property + def server(self) -> Server: + return self._plugin.server diff --git a/src/endstone_test/plugin.py b/src/endstone_test/plugin.py index 606a9d1..fe13eac 100644 --- a/src/endstone_test/plugin.py +++ b/src/endstone_test/plugin.py @@ -1,5 +1,8 @@ +from endstone import __minecraft_version__ from endstone.plugin import Plugin +from endstone_test.event_listener import EventListener + class TestPlugin(Plugin): name = "EndstoneTest" @@ -7,9 +10,19 @@ class TestPlugin(Plugin): def on_load(self) -> None: self.logger.info("on_load is called!") + assert len(self.server.levels) == 0 + assert self.server.minecraft_version == f"v{__minecraft_version__}" def on_enable(self) -> None: + self._listener = EventListener(self) + self.register_events(self._listener) + self.logger.info("on_enable is called!") + assert len(self.server.levels) == 1 + assert self.server.max_players == 10 + self.server.max_players = 100 + assert self.server.max_players == 100 + def on_disable(self) -> None: self.logger.info("on_disable is called!")