Skip to content

Commit 68b18c4

Browse files
committed
add {hooks_config_path}
1 parent 42b3d85 commit 68b18c4

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ tasks:
9191
name: motd # 声明task的名字,别有空格
9292
task_type: shell_command # 任务类型
9393
command: date # 要执行的指令
94-
command_file: /home/aaa/.../script.txt(脚本内容路径,如果此路径有效,插件将从command_file中读取command并执行,执行的指令即文件的所有内容(文件扩展名随意写,插件并非直接执行此文件,而是将文件内容读到内存处理后再执行)。command_file和command只用写一个,command_file如果写了command项就会被忽略)
94+
command_file: {hooks_config_path}/scripts/script.txt(脚本内容路径,如果此路径有效,插件将从command_file中读取command并执行,执行的指令即文件的所有内容(文件扩展名随意写,插件并非直接执行此文件,而是将文件内容读到内存处理后再执行)。command_file和command只用写一个,command_file如果写了command项就会被忽略)。{hooks_config_path}会被替换为hooks插件的配置文件目录,即server.get_data_folder()
9595
hooks: # 要挂载到的hook,必须是数组
9696
- on_server_started
9797
- on_mcdr_started
@@ -135,7 +135,7 @@ tasks:
135135

136136
`python_code`
137137
- 直接编写python代码即可,无需定义函数啥的
138-
- mcdr的所有实例、属性以及函数全都可以随意调用,在`exec()`时已经传入所有`globals()``locals()`
138+
- mcdr的所有实例、属性以及函数全都可以随意调用,在`exec()`时已经传入所有对象,例如`server`,你可以直接`server.is_server_running()`这样调用
139139
- 没有手动补参
140140

141141
### “获取”函数值

hooks/__init__.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ def execute_task(self, server: PluginServerInterface, hook: str, var_dict: dict
9090
command.write(self.command)
9191

9292
os.system(command.getvalue())
93+
# mc command
9394
elif self.task_type == TaskType.server_command:
9495
# 替换参数
9596
command = self.command
@@ -98,6 +99,7 @@ def execute_task(self, server: PluginServerInterface, hook: str, var_dict: dict
9899
command = command.replace('{$' + key + '}', str(var_dict.get(key)))
99100

100101
server.execute(command)
102+
# mcdr command
101103
elif self.task_type == TaskType.mcdr_command:
102104
# 替换参数
103105
command = self.command
@@ -106,7 +108,8 @@ def execute_task(self, server: PluginServerInterface, hook: str, var_dict: dict
106108
command = command.replace('{$' + key + '}', str(var_dict.get(key)))
107109

108110
server.execute_command(command)
109-
111+
112+
# python code
110113
elif self.task_type == TaskType.python_code:
111114
if obj_dict is not None:
112115
exec(self.command, obj_dict, {})
@@ -343,9 +346,15 @@ def parse_and_apply_scripts(script: str, server: PluginServerInterface):
343346
content: dict[str, Union[str, Union[list, dict]]] = yaml.load(f.read(), Loader=yaml.Loader)
344347

345348
for task in content.get('tasks').values():
349+
cmd_file_path = str(task.get('command_file')).replace('{hooks_config_path}', server.get_data_folder())
350+
351+
if not os.path.isfile(cmd_file_path):
352+
server.logger.warning(f'Script path for task {task.get("name")} is invalid! {task.get("command_file")}')
353+
346354
if (task.get('command_file') is not None) and (len(task.get('command_file')) > 0) and \
347-
(os.path.isfile(task.get('command_file'))):
348-
with open(task.get('command_file'), 'r') as command_file:
355+
(os.path.isfile(cmd_file_path)):
356+
with open(cmd_file_path, 'r') \
357+
as command_file:
349358
command_file_content = command_file.read()
350359
# 创建task
351360
create_task(task.get('task_type'), command_file_content, task.get('name'),

0 commit comments

Comments
 (0)