Skip to content

Commit

Permalink
refactor: clean up examples
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Jul 28, 2024
1 parent 9f7def6 commit ae233b4
Showing 1 changed file with 4 additions and 23 deletions.
27 changes: 4 additions & 23 deletions src/endstone_example/example_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@


class ExamplePlugin(Plugin):
name = "PythonExamplePlugin"
version = "0.4.0"
prefix = "PythonExamplePlugin"
api_version = "0.4"
description = "Python example plugin for Endstone servers"
authors = ["Endstone Developers <[email protected]>"]
website = "https://github.com/EndstoneMC/python-example-plugin"
load = "POSTWORLD"

commands = {
Expand All @@ -24,15 +20,6 @@ class ExamplePlugin(Plugin):
"aliases": ["py"],
"permissions": ["python_example.command.python"],
},
"test": {
"description": "This is a test command from python",
"usages": [
"/test",
"/test [value: int]",
"/test [value: float]",
],
"permissions": ["python_example.command.test"],
},
"kickme": {
"description": "Ask the server to kick you with a custom message",
"usages": ["/kickme [reason: message]"],
Expand Down Expand Up @@ -72,22 +59,16 @@ def on_enable(self) -> None:
self.get_command("python").executor = PythonCommandExecutor()

self.register_events(self) # register event listeners defined directly in Plugin class
self._listener = ExampleListener(self)
self.register_events(self._listener) # you can also register event listeners in a separate class
self.register_events(ExampleListener(self)) # you can also register event listeners in a separate class

self.server.scheduler.run_task_timer(self, self.log_time, 0, 20 * 1) # every second
self.server.scheduler.run_task(self, self.log_time, delay=0, period=20 * 1) # every second

def on_disable(self) -> None:
self.logger.info("on_disable is called!")

def on_command(self, sender: CommandSender, command: Command, args: list[str]) -> bool:
# You can also handle commands here instead of setting an executor in on_enable if you prefer
match command.name:
case "test":
if len(args) > 0:
sender.send_message(f"Test with number: {args[0]}!")
else:
sender.send_message("Test!!")
case "kickme":
player = sender.as_player()
if player is None:
Expand All @@ -108,7 +89,7 @@ def on_server_load(self, event: ServerLoadEvent):
@event_handler(priority=EventPriority.HIGH)
def on_server_load_2(self, event: ServerLoadEvent):
# this will be called after on_server_load because of a higher priority
self.server.dispatch_command(self.server.command_sender, "say Hello world!")
self.logger.info(f"{event.event_name} is passed to on_server_load2")

def log_time(self):
now = datetime.datetime.now().strftime("%c")
Expand Down

0 comments on commit ae233b4

Please sign in to comment.