|
1 | | -import datetime |
2 | | - |
3 | | -from endstone.command import Command, CommandSender |
4 | | -from endstone.event import EventPriority, ServerLoadEvent, event_handler |
| 1 | +from endstone import Player |
| 2 | +from endstone.command import Command, CommandSender, ConsoleCommandSender |
5 | 3 | from endstone.plugin import Plugin |
6 | 4 |
|
7 | | -from endstone_example.command import PythonCommandExecutor |
8 | 5 | from endstone_example.listener import ExampleListener |
9 | 6 |
|
10 | 7 |
|
11 | 8 | class ExamplePlugin(Plugin): |
12 | | - prefix = "PythonExamplePlugin" |
| 9 | + prefix = "ExamplePlugin" |
13 | 10 | api_version = "0.11" |
14 | | - load = "POSTWORLD" |
15 | 11 |
|
16 | 12 | commands = { |
17 | | - "python": { |
18 | | - "description": "Zen of python", |
19 | | - "usages": ["/python"], |
20 | | - "aliases": ["py"], |
21 | | - "permissions": ["python_example.command.python"], |
22 | | - }, |
23 | | - "kickme": { |
24 | | - "description": "Ask the server to kick you with a custom message", |
25 | | - "usages": ["/kickme [reason: message]"], |
26 | | - "permissions": ["python_example.command.kickme"], |
| 13 | + "hello": { |
| 14 | + "description": "Send a greeting", |
| 15 | + "usages": ["/hello"], |
| 16 | + "permissions": ["example.command.hello"], |
27 | 17 | }, |
28 | 18 | } |
29 | 19 |
|
30 | 20 | permissions = { |
31 | | - "python_example.command": { |
32 | | - "description": "Allow users to use all commands provided by this plugin.", |
33 | | - "default": True, |
34 | | - "children": { |
35 | | - "python_example.command.python": True, |
36 | | - "python_example.command.kickme": True, |
37 | | - }, |
38 | | - }, |
39 | | - "python_example.command.python": { |
40 | | - "description": "Allow users to use the /python command.", |
41 | | - "default": "op", |
42 | | - }, |
43 | | - "python_example.command.kickme": { |
44 | | - "description": "Allow users to use the /kickme command.", |
| 21 | + "example.command.hello": { |
| 22 | + "description": "Allow users to use the /hello command.", |
45 | 23 | "default": True, |
46 | 24 | }, |
47 | 25 | } |
48 | 26 |
|
49 | | - def on_load(self) -> None: |
50 | | - self.logger.info("on_load is called!") |
51 | | - |
52 | 27 | def on_enable(self) -> None: |
53 | | - self.logger.info("on_enable is called!") |
54 | | - self.get_command("python").executor = PythonCommandExecutor() |
55 | | - |
56 | | - self.register_events(self) # register event listeners defined directly in Plugin class |
57 | | - self.register_events(ExampleListener(self)) # you can also register event listeners in a separate class |
58 | | - |
59 | | - self.server.scheduler.run_task(self, self.log_time, delay=0, period=20 * 1) # every second |
| 28 | + self.save_default_config() |
| 29 | + self.register_events(ExampleListener(self)) |
| 30 | + self.logger.info("ExamplePlugin enabled!") |
60 | 31 |
|
61 | 32 | def on_disable(self) -> None: |
62 | | - self.logger.info("on_disable is called!") |
| 33 | + self.logger.info("ExamplePlugin disabled!") |
63 | 34 |
|
64 | 35 | def on_command(self, sender: CommandSender, command: Command, args: list[str]) -> bool: |
65 | | - # You can also handle commands here instead of setting an executor in on_enable if you prefer |
66 | 36 | match command.name: |
67 | | - case "kickme": |
68 | | - player = sender.as_player() |
69 | | - if player is None: |
70 | | - sender.send_error_message("You must be a player to execute this command.") |
71 | | - return False |
72 | | - |
73 | | - if len(args) > 0: |
74 | | - player.kick(args[0]) |
| 37 | + case "hello": |
| 38 | + greeting = self.config["greeting"] # from config.toml |
| 39 | + |
| 40 | + # Use isinstance to check the sender type |
| 41 | + if isinstance(sender, Player): |
| 42 | + sender.send_message(f"{greeting}, {sender.name}!") |
| 43 | + elif isinstance(sender, ConsoleCommandSender): |
| 44 | + self.logger.info(f"{greeting} from the console!") |
75 | 45 | else: |
76 | | - player.kick("You asked for it!") |
| 46 | + sender.send_message(f"{greeting}!") |
77 | 47 |
|
78 | 48 | return True |
79 | | - |
80 | | - @event_handler |
81 | | - def on_server_load(self, event: ServerLoadEvent) -> None: |
82 | | - self.logger.info(f"{event.event_name} is passed to on_server_load") |
83 | | - |
84 | | - @event_handler(priority=EventPriority.HIGH) |
85 | | - def on_server_load_2(self, event: ServerLoadEvent) -> None: |
86 | | - # this will be called after on_server_load because of a higher priority |
87 | | - self.logger.info(f"{event.event_name} is passed to on_server_load2") |
88 | | - |
89 | | - def log_time(self) -> None: |
90 | | - now = datetime.datetime.now().strftime("%c") |
91 | | - for player in self.server.online_players: |
92 | | - player.send_popup(now) |
0 commit comments