Skip to content

Commit

Permalink
feat: add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Jun 14, 2024
1 parent a9281fe commit 72d5270
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/endstone_test/event_listener.py
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions src/endstone_test/plugin.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
from endstone import __minecraft_version__
from endstone.plugin import Plugin

from endstone_test.event_listener import EventListener


class TestPlugin(Plugin):
name = "EndstoneTest"
api_version = "0.4"

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!")

0 comments on commit 72d5270

Please sign in to comment.