Skip to content

Commit 6df2691

Browse files
committed
feat: add example commands
1 parent bb57ad0 commit 6df2691

3 files changed

Lines changed: 61 additions & 0 deletions

File tree

src/endstone_example/example_plugin.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
from endstone.command import Command, CommandSender
12
from endstone.plugin import Plugin
3+
from endstone_example.python_command import PythonCommand, PythonCommandExecutor
4+
from endstone_example.test_command import TestCommand
25

36

47
class ExamplePlugin(Plugin):
@@ -7,6 +10,18 @@ def on_load(self) -> None:
710

811
def on_enable(self) -> None:
912
self.logger.info("on_enable is called!")
13+
# note we pass in PythonCommand instead of PythonCommand() - use a class rather than an instance
14+
self.register_command(PythonCommand).executor = PythonCommandExecutor()
15+
self.register_command(TestCommand)
1016

1117
def on_disable(self) -> None:
1218
self.logger.info("on_disable is called!")
19+
20+
def on_command(self, sender: CommandSender, command: Command, args: list[str]) -> bool:
21+
# You can also handle commands here instead of setting an executor in on_enable if you prefer
22+
if not args:
23+
sender.send_message(f"/{command.name} is executed from Python!")
24+
else:
25+
sender.send_message(f"/{command.name} is executed from Python with argument {args[0]}!")
26+
27+
return True
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from endstone.command import Command, CommandSender, CommandExecutor
2+
3+
# Zen of python - https://peps.python.org/pep-0020/
4+
zen_of_python = """The Zen of Python, by Tim Peters
5+
6+
Beautiful is better than ugly.
7+
Explicit is better than implicit.
8+
Simple is better than complex.
9+
Complex is better than complicated.
10+
Flat is better than nested.
11+
Sparse is better than dense.
12+
Readability counts.
13+
Special cases aren't special enough to break the rules.
14+
Although practicality beats purity.
15+
Errors should never pass silently.
16+
Unless explicitly silenced.
17+
In the face of ambiguity, refuse the temptation to guess.
18+
There should be one-- and preferably only one --obvious way to do it.
19+
Although that way may not be obvious at first unless you're Dutch.
20+
Now is better than never.
21+
Although never is often better than *right* now.
22+
If the implementation is hard to explain, it's a bad idea.
23+
If the implementation is easy to explain, it may be a good idea.
24+
Namespaces are one honking great idea -- let's do more of those!"""
25+
26+
27+
class PythonCommand(Command):
28+
def __init__(self):
29+
Command.__init__(self, "python")
30+
self.description = "Zen of python"
31+
self.usages = ["/python"]
32+
self.aliases = ["py"]
33+
34+
35+
class PythonCommandExecutor(CommandExecutor):
36+
def on_command(self, sender: CommandSender, command: Command, args: list[str]) -> bool:
37+
sender.send_message(zen_of_python)
38+
return True
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from endstone.command import Command
2+
3+
4+
class TestCommand(Command):
5+
def __init__(self):
6+
Command.__init__(self, "test")
7+
self.description = "This is a test command from python"
8+
self.usages = ["/test", "/test [value: int]"]

0 commit comments

Comments
 (0)