-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmonitor.py
More file actions
842 lines (781 loc) · 31.1 KB
/
Copy pathmonitor.py
File metadata and controls
842 lines (781 loc) · 31.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
from datetime import datetime
import os
import logging
import math
import time
from typing import List, Any, Tuple, Union
import copy
# ujson比json快 所以我就是要用ujson
try:
import ujson
except ImportError:
import json as ujson
from mcdreforged.api.rtext import RAction, RTextList, RText
from mcdreforged.api.decorator import new_thread
from mcdreforged.plugin.server_interface import ServerInterface
from mcdreforged.api.types import Info
PLUGIN_METADATA = {
'id': 'monitor_reforged',
'version': '1.0.5',
'name': 'MonitorR',
'description': 'Adapted from Monitor, a more complete monitoring plug-in.',
'author': 'XiaoHuiHui',
'link': 'https://github.com/XiaoHuiHui233/MonitorR',
'dependencies': {
'minecraft_data_api': '>=1.1.0',
}
}
DEFAULT_CONFIG = {
'interval': 15,
'permissions': {
'add': 3,
'del': 3,
'list': 1,
'range': 3,
'reload': 3
},
'point': {
'radius': 200,
'contain_y': False,
},
'range': {
'contain_y': False,
}
}
# hard code
DIMENSIONS = {
'0': 'minecraft:overworld',
'-1': 'minecraft:the_nether',
'1': 'minecraft:the_end',
'overworld': 'minecraft:overworld',
'the_nether': 'minecraft:the_nether',
'the_end': 'minecraft:the_end',
'nether': 'minecraft:the_nether',
'end': 'minecraft:the_end',
'minecraft:overworld': 'minecraft:overworld',
'minecraft:the_nether': 'minecraft:the_nether',
'minecraft:the_end': 'minecraft:the_end'
}
HELP_MSG = [
RText('----------- §aMCDR 监控插件帮助信息 §f-----------'),
RTextList(
RText('§b!!mr add '),
RText('§f<§aname§f> ').set_hover_text(
RText(
'§aname§f参数表示保护点的唯一标识符\n'
'参数类型为§e字符串§f,必填\n'
'注意:\n'
' 此参数不能输入§dhere§f和能被解析为§e§l数字§r的§e字符串§f\n'
' 这会导致可能的§c解析错误§f并报错'
)
),
RText('<§ax§f> ').set_hover_text(
RText(
'§ax§f参数表示保护点的x坐标\n'
'参数类型为§e数字§f,必填\n'
'注意:\n'
' 此参数会被强制转化为§e§l整数§r,即使输入是§e浮点数§f\n'
' 若§c无法解析输入§f(通常是输入中带有§c§l非法字符§r)会报错'
)
),
RText('<§ay§f> ').set_hover_text(
RText(
'§ay§f参数表示保护点的y坐标\n'
'参数类型为§e数字§f,必填\n'
'注意:\n'
' 此参数会被强制转化为§e§l整数§r,即使输入是§e浮点数§f\n'
' 若§c无法解析输入§f(通常是输入中带有§c§l非法字符§r)会报错'
)
),
RText('<§az§f> ').set_hover_text(
RText(
'§az§f参数表示保护点的z坐标\n'
'参数类型为§e数字§f,必填\n'
'注意:\n'
' 此参数会被强制转化为§e§l整数§r,即使输入是§e浮点数§f\n'
' 若§c无法解析输入§f(通常是输入中带有§c§l非法字符§r)会报错'
)
),
RText('[§aworld§f] ').set_hover_text(
RText(
'§aworld§f参数表示保护点所处的世界名称\n'
'参数类型为§e数字§f或§e字符串§f,选填\n'
'参数默认为§d主世界§f,但可以在配置里更改默认\n'
'支持的输入有:\n'
' §d0§f、§d-1§f、§d1§f、§doverworld§f、§dthe_nether§f、§dthe_end§f、§dnether§f、\n'
' §dend§f、§dminecraft:overworld§f、§dminecraft:the_nether§f、\n'
' §dminecraft:the_end§f\n'
' 若不属于§c上述范围§f的输入会报错'
)
),
RText('[§aradius§f] ').set_hover_text(
RText(
'§aradius§f参数表示保护点周围的保护半径\n'
'这表示保护范围是一个向上取整后的圆\n'
'参数类型为§e数字§f,选填\n'
'参数默认为§d200§f,表示半径§d200格§f的区域\n'
'可以在配置里更改默认\n'
'注意:\n'
' 此参数接受任何§e浮点数§f\n'
' 但是进行距离计算的输入坐标仍然是§e整数§f坐标\n'
' 距离计算的结果存在开平方,因此可能是§e浮点数§f\n'
' 若§c无法解析输入§f(通常是输入中带有§c§l非法字符§r)会报错'
)
),
RText('[§acontain_y§f]').set_hover_text(
RText(
'§acontain_y§f参数表示是否计算y轴\n'
'若为是,则保护区实为§d球体§f,否则为§d圆柱§f\n'
'参数类型为§e不区分大小写的§l布尔表达式§r§f,选填\n'
'参数默认为§dFalse§f,即不参与计算\n'
'注意:\n'
' 支持的输入仅有不区分大小写的§dtrue§f或者§dfalse§f\n'
' 若不属于§c上述范围§f的输入会报错'
)
),
),
RText(' - §d添加一个保护的坐标点'),
RTextList(
RText('§b!!mr add '),
RText('§f<§aname§f> ').set_hover_text(
RText(
'§aname§f参数表示保护点的唯一标识符\n'
'参数类型为§e字符串§f,必填\n'
'注意:\n'
' 此参数不能输入§dhere§f和能被解析为§e§l数字§r的§e字符串§f\n'
' 这会导致可能的§c解析错误§f并报错'
)
),
RText('§bhere '),
RText('[§aworld§f] ').set_hover_text(
RText(
'§aworld§f参数表示保护点所处的世界名称\n'
'即§d坐标§f仍然是§d玩家坐标§f,但可以设置到§d§l别的世界§r\n'
'参数类型为§e数字§f或§e字符串§f,选填\n'
'参数默认为§d玩家所在的世界§f\n'
'支持的输入有:\n'
' §d0§f、§d-1§f、§d1§f、§doverworld§f、§dthe_nether§f、§dthe_end§f、§dnether§f、\n'
' §dend§f、§dminecraft:overworld§f、§dminecraft:the_nether§f、\n'
' §dminecraft:the_end§f\n'
' 若不属于§c上述范围§f的输入会报错'
)
),
RText('[§aradius§f] ').set_hover_text(
RText(
'§aradius§f参数表示保护点周围的保护半径\n'
'这表示保护范围是一个向上取整后的圆\n'
'参数类型为§e数字§f,选填\n'
'参数默认为§d200§f,表示半径§d200格§f的区域\n'
'可以在配置里更改默认\n'
'注意:\n'
' 此参数接受任何§e浮点数§f\n'
' 但是进行距离计算的输入坐标仍然是§e整数§f坐标\n'
' 距离计算的结果存在开平方,因此可能是§e浮点数§f\n'
' 若§c无法解析输入§f(通常是输入中带有§c§l非法字符§r)会报错'
)
),
RText('[§acontain_y§f]').set_hover_text(
RText(
'§acontain_y§f参数表示是否计算y轴\n'
'若为是,则保护区实为§d球体§f,否则为§d圆柱§f\n'
'参数类型为§e不区分大小写的§l布尔表达式§r§f,选填\n'
'参数默认为§dFalse§f,即不参与计算\n'
'注意:\n'
' 支持的输入仅有不区分大小写的§dtrue§f或者§dfalse§f\n'
' 若不属于§c上述范围§f的输入会报错'
)
),
),
RText(' - §d添加一个保护的坐标点在玩家所在的位置'),
RTextList(
RText('§b!!mr range '),
RText('§f<§aname§f> ').set_hover_text(
RText(
'§aname§f参数表示保护区的唯一标识符\n'
'参数类型为§e字符串§f,必填\n'
'注意:\n'
' 此参数不能输入§dhere§f和能被解析为§e§l数字§r的§e字符串§f\n'
' 这会导致可能的§c解析错误§f并报错'
)
),
RText('<§apoint1§f> ').set_hover_text(
RText(
'§apoint1§f参数表示保护区的角点1\n'
'参数类型为§e字符串§f或§e§l数字三元组§r,必填\n'
'此参数可接受的输入为:\n'
' §dhere§f、§d已存在的§l保护点§r§d的名称§f\n'
' 以及§d(x, y, z)坐标§f\n'
' 关于坐标的输入要求等同于§badd§f子命令\n'
'注意:\n'
' 若使用§d已存在的§l保护点§r§d的名称§f作为参数\n'
' 则必须保证两角点处于§d同一世界§f'
)
),
RText('<§apoint2§f> ').set_hover_text(
RText(
'§apoint2§f参数表示保护区的角点2\n'
'参数类型为§e字符串§f或§e§l数字三元组§r,必填\n'
'此参数可接受的输入为:\n'
' §dhere§f、§d已存在的§l保护点§r§d的名称§f\n'
' 以及§d(x, y, z)坐标§f\n'
' 关于坐标的输入要求等同于§badd§f子命令\n'
'注意:\n'
' 若使用§d已存在的§l保护点§r§d的名称§f作为参数\n'
' 则必须保证两角点处于§d同一世界§f'
)
),
RText('[§aworld§f] ').set_hover_text(
RText(
'§aworld§f参数表示保护区所处的世界名称\n'
'参数类型为§e数字§f或§e字符串§f,选填\n'
'参数默认会针对两个§e角点§f的输入进行所处§e世界§f的推断\n'
'但注意,若存在歧义,则§c§l直接报错§r,并不会被本参数覆盖\n'
'支持的输入有:\n'
' §d0§f、§d-1§f、§d1§f、§doverworld§f、§dthe_nether§f、§dthe_end§f、§dnether§f、\n'
' §dend§f、§dminecraft:overworld§f、§dminecraft:the_nether§f、\n'
' §dminecraft:the_end§f\n'
' 若不属于§c上述范围§f的输入会报错'
)
),
RText('[§acontain_y§f]').set_hover_text(
RText(
'§acontain_y§f参数表示是否计算y轴\n'
'若为是,则保护区会计算角点的§dy坐标§f差值\n'
'否则会考虑§d0§f到§d255§f的全部高度\n'
'参数类型为§e不区分大小写的§l布尔表达式§r§f,选填\n'
'参数默认为§dFalse§f,即不参与计算\n'
'注意:\n'
' 支持的输入仅有不区分大小写的§dtrue§f或者§dfalse§f\n'
' 若不属于§c上述范围§f的输入会报错'
)
),
),
RText(' - §d添加一个两点围成的矩形(长方体)作为保护区'),
RTextList(
RText('§b!!mr del '),
RText('§f<§aname§f> ').set_hover_text(
RText(
'§aname§f参数表示保护点/区的唯一标识符\n'
'参数类型为§e字符串§f,必填\n'
'若输入不存在的唯一标识符则报错并返回'
)
),
RText(' - §d删除一个保护的坐标点/区')
),
RTextList(
RText('§b!!mr list §f - §d显示所有已有保护的坐标点/区'),
RText('§f §l[§a§l√§f§l]§r').set_hover_text(
RText('§e点我执行此命令')
).set_click_event(
RAction.run_command,
'!!mr list'
)
),
RTextList(
RText('§b!!mr reload§f - §d重载插件'),
RText('§f §l[§a§l√§f§l]§r').set_hover_text(
RText('§e点我执行此命令')
).set_click_event(
RAction.run_command,
'!!mr reload'
)
),
RText('----------------------------------------------'),
]
config_folder = f"./config/{PLUGIN_METADATA['id']}"
log_folder = f'{config_folder}/logs'
log_file = f'{log_folder}/log.json'
site_file = f'{config_folder}/site.json'
config_file = f'{config_folder}/config.json'
bots = set()
players = set()
monitor = None
record_fp = None
sites = {}
config = DEFAULT_CONFIG
logger = logging.getLogger("MonitorR")
running = False
class ParseError(Exception):
"""一个表示命令解析失败的异常.
"""
pass
def check_config() -> None:
for key in DEFAULT_CONFIG:
if key not in config:
logger.warn(f'Config配置项缺失:{key},已默认补全。')
config[key] = DEFAULT_CONFIG[key]
elif isinstance(DEFAULT_CONFIG[key], dict):
for key1 in DEFAULT_CONFIG[key]:
if key1 not in config[key]:
logger.warn(f'Config配置项缺失:{key}.{key1},已默认补全。')
config[key][key1] = DEFAULT_CONFIG[key][key1]
def save_config() -> None:
if not os.path.exists(config_folder):
os.makedirs(config_folder)
with open(config_file, 'w', encoding='utf-8') as wf:
ujson.dump(
config,
wf,
ensure_ascii=False,
indent=4
)
def load_config() -> None:
global config
if not os.path.exists(config_file):
logger.warn('Config文件不存在!自动生成覆盖!')
save_config()
return
try:
with open(config_file, 'r', encoding='utf-8') as rf:
config = ujson.load(rf)
except:
logger.warn('Config文件格式错误!自动生成覆盖!')
save_config()
check_config()
def split_log() -> None:
global record_fp
if os.path.exists(log_file):
time = datetime.now().strftime('%Y_%m_%d_%H_%M_%S')
os.rename(log_file, f'{log_folder}/{time}.json')
if not os.path.exists(config_folder):
os.makedirs(config_folder)
if not os.path.exists(log_folder):
os.makedirs(log_folder)
record_fp = open(log_file, 'w+', encoding='utf-8')
def save_sites() -> None:
if not os.path.exists(config_folder):
os.makedirs(config_folder)
with open(site_file, 'w', encoding='utf-8') as wf:
ujson.dump(
sites,
wf,
ensure_ascii=False,
indent=4
)
def load_sites() -> None:
global sites
if not os.path.exists(site_file):
logger.warn('Sites文件不存在!自动生成覆盖!')
save_sites()
return
try:
with open(site_file, 'r', encoding='utf-8') as rf:
sites = ujson.load(rf)
except:
logger.warn('Sites文件格式错误!自动生成覆盖!')
save_sites()
def on_help(server: ServerInterface, info: Info) -> None:
for rtext in HELP_MSG:
server.reply(info, rtext)
def permission_check(server: ServerInterface, info: Info, subcmd: str) -> bool:
if info.is_player:
if server.get_permission_level(info) < config['permissions'][subcmd]:
server.reply(info, RText(f'§f[§aMonitorR§f][§cWARN§f] §c你没有足够的权限执行此命令!'))
return False
return True
def on_reload(server: ServerInterface, info: Info, args: List[str]) -> None:
if not permission_check(server, info, 'reload'):
return
try:
load_config()
load_sites()
server.reply(info, RText(f'§f[§aMonitorR§f][§2INFO§f] §2重载成功!'))
except Exception as err:
server.reply(info, RText(f'§f[§aMonitorR§f][§cERROR§f] §c发生错误!').set_hover_text(f'{repr(err)}'))
raise err
def name_check(name: str) -> None:
if name in sites:
raise ParseError(f'已有名称为{name}的保护点/区,拒绝添加!')
if name == 'here':
raise ParseError(f'here是命令解析中的关键字,不能被设为名称!')
else:
try:
int(name)
except:
pass
else:
raise ParseError(f'名称不能是纯数字,这会导致解析出现二义性!')
def here_to_pos(server: ServerInterface, info: Info) -> Union[Tuple[int, int, int, str], None]:
if not info.is_player:
server.reply(info, RText(f'§f[§aMonitorR§f][§cWARN§f] §e非玩家不能使用§bhere§e作为参数。'))
return None
try:
data_api = server.get_plugin_instance('minecraft_data_api')
if data_api is None:
raise ImportError("找不到MinecraftDataAPI!")
except:
logger.error('获取DataAPI时发生错误!')
server.reply(info, RText(f'§f[§aMonitorR§f][§cERROR§f] §c前置插件加载失败!'))
return None
else:
pos = data_api.get_player_coordinate(info.player)
dim = data_api.get_player_dimension(info.player)
return (int(pos[0]), int(pos[1]), int(pos[2]), DIMENSIONS[str(dim)])
def on_add(server: ServerInterface, info: Info, args: List[str]) -> None:
if not permission_check(server, info, 'add'):
return
try:
name = args[0]
if name in sites:
raise ParseError(f'已有名称为{name}的保护点/区,拒绝添加!')
if name == 'here':
raise ParseError(f'here是命令解析中的关键字,不能被设为名称!')
else:
try:
int(name)
except:
pass
else:
raise ParseError(f'名称不能是纯数字,这会导致解析出现二义性!')
if args[1] == 'here':
pos = here_to_pos(server, info)
if pos is None:
return
x, y, z, world = pos
idx = 2
else:
x = int(float(args[1]))
y = int(float(args[2]))
z = int(float(args[3]))
idx = 4
world = 'minecraft:overworld'
if len(args) > idx:
world = DIMENSIONS[args[idx]]
idx += 1
radius = config['point']['radius']
if len(args) > idx:
radius = float(args[idx])
idx += 1
contain_y = config['point']['contain_y']
if len(args) > idx:
if args[idx].lower() == 'true':
contain_y = True
elif args[idx].lower() == 'false':
contain_y = False
else:
raise ParseError(
f'不支持的逻辑输入!你输入了{args[idx]},'
'但是仅支持输入不区分大小写的true和false。'
)
except IndexError as err:
raise ParseError(f'参数数量错误!详情:{repr(err)}')
except ValueError as err:
raise ParseError(f'无法解析的数值!详情:{repr(err)}')
except KeyError as err:
raise ParseError(f'不支持的世界类型输入!详情:{repr(err)}')
else:
sites[name] = {
'type': 'point',
'name': name,
'x': x,
'y': y,
'z': z,
'world': world,
'radius': radius,
'contain_y': contain_y
}
try:
save_sites()
except Exception as err:
raise ParseError(f'保存文件时发生错误,详情:{repr(err)}')
server.reply(info, RText(f'§f[§aMonitorR§f][§2INFO§f] §2执行成功!'))
def on_del(server: ServerInterface, info: Info, args: List[str]) -> None:
if not permission_check(server, info, 'del'):
return
try:
name = args[0]
sites.pop(name)
except IndexError as err:
raise ParseError(f'参数数量错误!详情:{repr(err)}')
except KeyError as err:
raise ParseError(f'没有名称为{name}的保护点/区。')
else:
try:
save_sites()
except Exception as err:
raise ParseError(f'保存文件时发生错误,详情:{repr(err)}')
server.reply(info, RText(f'§f[§aMonitorR§f][§2INFO§f] §2执行成功!'))
def point_to_pos(server: ServerInterface, info: Info, name: str) -> Union[Tuple[int, int, int, str], None]:
if sites[name]['type'] != 'point':
server.reply(info, RTextList(
RText('§f[§aMonitorR§f][§cWARN§f] '),
RText(
f'§e使用名称作为点的输入时必须保证类型是§b保护点§e而不是§b保护区§e!'
).set_hover_text(
f"您提供的输入名称为{name},其类型为{sites[name]['type']},而我们需要point"
)
))
return None
return (sites[name]['x'], sites[name]['y'], sites[name]['z'], sites[name]['world'])
def on_range(server: ServerInterface, info: Info, args: List[str]) -> None:
if not permission_check(server, info, 'range'):
return
try:
name = args[0]
name_check(name)
if 'here' in args[1:]:
pos = here_to_pos(server, info)
if pos is None:
return
x_h, y_h, z_h, world_h = pos
if args[1] == 'here':
x1 = x_h
y1 = y_h
z1 = z_h
world_1 = world_h
idx = 2
elif args[1] in sites:
pos = point_to_pos(server, info, args[1])
if pos is None:
return
x1, y1, z1, world_1 = pos
idx = 2
else:
x1 = int(float(args[1]))
y1 = int(float(args[2]))
z1 = int(float(args[3]))
world_1 = 'minecraft:overworld'
idx = 4
if args[idx] == 'here':
x2 = x_h
y2 = y_h
z2 = z_h
world_2 = world_h
idx += 1
elif args[idx] in sites:
pos = point_to_pos(server, info, args[idx])
if pos is None:
return
x2, y2, z2, world_2 = pos
idx += 1
else:
x2 = int(float(args[idx]))
idx += 1
y2 = int(float(args[idx]))
idx += 1
z2 = int(float(args[idx]))
idx += 1
world_2 = 'minecraft:overworld'
if world_1 != world_2:
server.reply(info, RText(f'§e请保证构成区域的两角点处于同一世界!'))
return
world = world_1
if len(args) > idx:
world = DIMENSIONS[args[idx]]
idx += 1
contain_y = config['range']['contain_y']
if len(args) > idx:
if args[idx].lower() == 'true':
contain_y = True
elif args[idx].lower() == 'false':
contain_y = False
else:
raise ParseError(
f'不支持的逻辑输入!你输入了{args[idx]},'
'但是仅支持输入不区分大小写的true和false。'
)
except IndexError as err:
raise ParseError(f'参数数量错误!详情:{repr(err)}')
except ValueError as err:
raise ParseError(f'无法解析的数值!详情:{repr(err)}')
except KeyError as err:
raise ParseError(f'不支持的世界类型输入!详情:{repr(err)}')
else:
sites[name] = {
'type': 'range',
'name': name,
'x1': min(x1, x2),
'y1': min(y1, y2),
'z1': min(z1, z2),
'x2': max(x1, x2),
'y2': max(y1, y2),
'z2': max(z1, z2),
'world': world,
'contain_y': contain_y
}
try:
save_sites()
except Exception as err:
raise ParseError(f'保存文件时发生错误,详情:{repr(err)}')
server.reply(info, RText(f'§f[§aMonitorR§f][§2INFO§f] §2执行成功!'))
def on_list(server: ServerInterface, info: Info, args: List[str]) -> None:
if not permission_check(server, info, 'list'):
return
server.reply(info, RText('§f保护点/区的列表:'))
for name in sites:
txt = RTextList(RText('§f - '))
if sites[name]['type'] == 'point':
txt.append(RText(f'§b{name}').set_hover_text(
RText(
f"§e类型§f: §b{sites[name]['type']}\n"
f"§e中心点§f: (§b{sites[name]['x']}§f, §b{sites[name]['y']}§f, §b{sites[name]['z']}§f)\n"
f"§e世界§f: §b{sites[name]['world']}\n"
f"§e半径§f: §b{sites[name]['radius']}\n"
f"§e是否计算y轴§f: §b{'是' if sites[name]['contain_y'] else '否'}"
)
))
elif sites[name]['type'] == 'range':
txt.append(RText(f'§b{name}').set_hover_text(
RText(
f"§e类型§f: §b{sites[name]['type']}\n"
f"§e角点1§f: (§b{sites[name]['x1']}§f, §b{sites[name]['y1']}§f, §b{sites[name]['z1']}§f)\n"
f"§e角点2§f: (§b{sites[name]['x2']}§f, §b{sites[name]['y2']}§f, §b{sites[name]['z2']}§f)\n"
f"§e世界§f: §b{sites[name]['world']}\n"
f"§e是否计算y轴§f: §b{'是' if sites[name]['contain_y'] else '否'}"
)
))
else:
txt.append(RText(f'§b{name}').set_hover_text(
RText(
f"§e类型§f: §b{sites[name]['type']}\n"
)
))
txt.append(
RText('§f §l[§c§lX§f§l]§r').set_hover_text(
RText('§e点我删除此项')
).set_click_event(
RAction.run_command,
f'!!mr del {name}'
))
server.reply(info, txt)
commands = {
'help': on_help,
'add': on_add,
'del': on_del,
'range': on_range,
'list': on_list,
'reload': on_reload
}
def check_pos(server: ServerInterface, player: str, x: int, y: int, z: int, dim: str) -> bool:
for name in sites:
if dim != sites[name]['world']:
continue
status = False
if sites[name]['type'] == 'point':
if sites[name]['contain_y']:
delta_x = sites[name]['x'] - x
delta_y = sites[name]['y'] - y
delta_z = sites[name]['z'] - z
dis_2 = delta_x * delta_x + delta_y * delta_y + delta_z * delta_z
dis = math.sqrt(dis_2)
else:
delta_x = sites[name]['x'] - x
delta_z = sites[name]['z'] - z
dis_2 = delta_x * delta_x + delta_z * delta_z
dis = math.sqrt(dis_2)
status = sites[name]['radius'] >= dis
elif sites[name]['type'] == 'range':
if sites[name]['contain_y']:
status = x >= sites[name]['x1'] and x <= sites[name]['x2'] \
and y >= sites[name]['y1'] and y <= sites[name]['y2'] \
and z >= sites[name]['z1'] and z <= sites[name]['z2']
else:
status = x >= sites[name]['x1'] and x <= sites[name]['x2'] \
and z >= sites[name]['z1'] and z <= sites[name]['z2']
if status:
server.say(RText(
f'§f[§aMonitorR§f][§cWARN§f] §c§l危§r§f!§b{player}§f在§e{name}§f游荡!!!'
))
record_fp.write(ujson.dumps({
'type': 'warning',
'timestamp': datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
'player': str(player),
'danger_zone': name
}) + '\n')
# 监控
@new_thread('MonitorR')
def monitor(server: ServerInterface) -> None:
data_api = server.get_plugin_instance('minecraft_data_api')
remain_time = 0.0
while running:
remain_time = 15.0 - remain_time
if remain_time < 0.0:
remain_time = 0.0
time.sleep(remain_time)
if not running:
break
remain_time = 0.0
clone_players = copy.deepcopy(players)
for player in clone_players:
if player not in players:
continue
try:
pos = data_api.get_player_coordinate(player)
x = int(pos[0])
y = int(pos[1])
z = int(pos[2])
dim = data_api.get_player_dimension(player)
dim = DIMENSIONS[str(dim)]
record_fp.write(ujson.dumps({
'type': 'timer',
'timestamp': datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
'player': str(player),
'x': x,
'y': y,
'z': z,
'world': dim
}) + '\n')
check_pos(server, player, x, y, z, dim)
except:
continue
time.sleep(0.2)
if not running:
break
remain_time += 0.2
record_fp.flush()
def on_load(server: ServerInterface, old: Any) -> None:
server.register_help_message('!!mr', '重置版监控插件')
global bots, players, running
if old is not None:
bots = old.bots
players = old.players
load_config()
load_sites()
split_log()
running = True
monitor(server)
def on_unload(server: ServerInterface) -> None:
global running
running = False
global record_fp
if record_fp is not None:
record_fp.close()
record_fp = None
@new_thread("MonitorR")
def on_user_info(server: ServerInterface, info: Info) -> None:
# 真的不是我不喜欢MCDR新的命令树解析
# 把简单东西复杂化了啊.....
# 而且做成js的异步延迟回调的格式, 这种代码风格好难看啊!
text = info.content
if not text.startswith('!!mr'):
return
args = text.split(' ')
if len(args) <= 1:
on_help(server, info)
else:
subcmd = args[1]
try:
commands[subcmd](server, info, args[2:])
except KeyError as err:
server.reply(info, RText('§f[§aMonitorR§f][§cWARN§f] §c子命令错误!'))
on_help(server, info)
except ParseError as err:
server.reply(info, RText('§f[§aMonitorR§f][§cWARN§f] §c参数错误!').set_hover_text(f'{err}'))
def on_player_joined(server: ServerInterface, player: str, info: Info) -> None:
global bots, players
if '[local] logged in with entity id' in info.content:
bots.add(player)
else:
players.add(player)
def on_player_left(server: ServerInterface, player: str) -> None:
global bots, players
if player in bots:
bots.remove(player)
if player in players:
players.remove(player)
def on_mcdr_stop(server: ServerInterface) -> None:
on_unload(server)
def on_server_stop(server: ServerInterface, return_code: int) -> None:
global bots, players
bots = set()
players = set()