Skip to content

Commit

Permalink
feat: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Jun 15, 2024
1 parent 213630f commit b1cce60
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
7 changes: 3 additions & 4 deletions src/endstone_test/event_listener.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import datetime
from functools import partial
from uuid import UUID

from endstone import ColorFormat, Server
from endstone.event import event_handler, PlayerJoinEvent
Expand All @@ -13,16 +12,16 @@ def __init__(self, plugin: Plugin):

@event_handler
def on_player_join(self, event: PlayerJoinEvent) -> None:
def send_welcome_message(player_name: str, player_id: UUID) -> None:
def send_welcome_message(player_name: str) -> None:
self.server.broadcast_message(ColorFormat.YELLOW + f"{player_name} joined the game.")

self.server.scheduler.run_task_later(
self._plugin, partial(send_welcome_message, event.player.name, event.player.unique_id), delay=20
self._plugin, partial(send_welcome_message, event.player.name), delay=20
)

assert event.player in self.server.online_players
assert self.server.get_player(event.player.name) is event.player
# assert self.server.get_player(event.player.unique_id) is event.player # TODO(fixme): this will crash
assert self.server.get_player(event.player.unique_id) is event.player

self._plugin.logger.info("===========================")
self._plugin.logger.info(f"Name: {event.player.name}")
Expand Down
9 changes: 7 additions & 2 deletions src/endstone_test/plugin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import datetime
import uuid

from endstone import __minecraft_version__
from endstone.plugin import Plugin

Expand Down Expand Up @@ -42,8 +45,10 @@ def on_enable(self) -> None:
self.server.max_players = 100
assert self.server.max_players == 100

self.server.broadcast_message("Hello!")
assert self.server.get_player("non-existent") is None
assert self.server.get_player(uuid.uuid4()) is None

self.server.broadcast_message("Hello!")
self.server.scheduler.run_task_timer(self, self.send_debug_message, delay=0, period=10)

def on_disable(self) -> None:
Expand All @@ -52,7 +57,7 @@ def on_disable(self) -> None:
def send_debug_message(self):
for player in self.server.online_players:
player.send_tip(
f"Level: {player.level.name}, Time: {player.level.time}\n"
f"Level: {player.level.name}, Time: {player.level.time}, Ping: {player.ping / datetime.timedelta(milliseconds=1)}\n"
f"Location: {player.location}\n"
f"Velocity: {player.velocity}\n"
f"Dimension: {player.location.dimension.name}\n"
Expand Down

0 comments on commit b1cce60

Please sign in to comment.