Skip to content

Commit 2edcd63

Browse files
committed
feat: use pattern matching to handle different command cases
1 parent 6df2691 commit 2edcd63

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

src/endstone_example/example_plugin.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from endstone.command import Command, CommandSender
22
from endstone.plugin import Plugin
3+
34
from endstone_example.python_command import PythonCommand, PythonCommandExecutor
45
from endstone_example.test_command import TestCommand
56

@@ -19,9 +20,18 @@ def on_disable(self) -> None:
1920

2021
def on_command(self, sender: CommandSender, command: Command, args: list[str]) -> bool:
2122
# 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]}!")
23+
match (command.name, args):
24+
case ("test", []):
25+
# handle /test
26+
sender.send_message("Test!")
27+
case ("test", [n]):
28+
# handle /test n
29+
sender.send_message(f"Test with number {n}!")
30+
case (_, []):
31+
# handle /* (wildcard)
32+
sender.send_message(f"/{command.name} is executed from Python!")
33+
case (_, *args):
34+
# handle /* args... (wildcard)
35+
sender.send_message(f"/{command.name} is executed from Python with arguments {args}!")
2636

2737
return True

0 commit comments

Comments
 (0)