/
Manager.php
733 lines (583 loc) · 14.1 KB
/
Manager.php
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
<?php namespace sex\guard;
/**
* _ _ _ _ ____
* | | | |_ __ (_)_ _____ _ ______ __ _| |/ ___\_ _______ __
* | | | | '_ \| | \ / / _ \ '_/ __// _' | | / | '_/ _ \ \ / /
* | |__| | | | | |\ \/ / __/ | \__ \ (_) | | \___| || __/\ \/\/ /
* \____/|_| |_|_| \__/ \___|_| /___/\__,_|_|\____/_| \___/ \_/\_/
*
* @author sex_KAMAZ
* @link http://universalcrew.ru
*
*/
use sex\guard\data\Region;
use sex\guard\listener\BlockGuard;
use sex\guard\listener\EntityGuard;
use sex\guard\listener\PlayerGuard;
use sex\guard\command\OldGuardCommand;
use sex\guard\command\NewGuardCommand;
use sex\guard\event\region\RegionCreateEvent;
use sex\guard\event\region\RegionRemoveEvent;
use pocketmine\permission\Permission;
use pocketmine\plugin\PluginBase;
use pocketmine\level\Position;
use pocketmine\utils\Config;
use pocketmine\Player;
/**
* @todo throw exceptions in production isn't a good practice.
* add config autoupdater.
*/
use Exception;
/**
* @todo implement 'manager -> provider' pattern.
* convert arrays to containers.
*/
class Manager extends PluginBase
{
const CONFIGURATION_SIGN = '5bfa52e5eed0d57dee1f33cd435eb988';
const DEFAULT_FLAG = [
'interact' => TRUE,
'teleport' => TRUE,
'combust' => FALSE,
'explode' => FALSE,
'change' => FALSE,
'bucket' => FALSE,
'damage' => TRUE,
'chest' => FALSE,
'frame' => FALSE,
'place' => FALSE,
'break' => FALSE,
'sleep' => FALSE,
'decay' => TRUE,
'drop' => TRUE,
'chat' => TRUE,
'pvp' => FALSE,
'mob' => TRUE
];
/**
* @var Manager
*/
private static $instance = null;
/**
* @return Manager
*/
static function getInstance( ): Manager
{
return self::$instance;
}
/**
* @var Config
*/
private $message, $region, $config, $group;
/**
* @todo better store $data in provider.
*
* @var Region[][]
*/
private $data = [];
/**
* @var Position[]
*/
public $position = [];
/**
* @var PluginBase[]
*/
public $extension = [];
/**
* @var Config
*/
public $sign;
function onLoad( )
{
$this->loadInstance();
}
function onEnable( )
{
$this->initPermission();
$this->initProvider();
if( $this->getValue('sign', 'config') !== self::CONFIGURATION_SIGN )
{
throw new Exception("Configuration error: использование старой версии конфига. Пожалуйста, удалите старый конфиг (/plugins/sexGuard/config.yml) и перезагрузите сервер.");
}
$this->initListener();
$this->initCommand();
$this->initExtension();
}
/**
* @todo data stores can be deleted by user.
*/
function onDisable( )
{
$this->region->save();
$this->sign->save();
}
/**
* _
* __ _ ____ (_)
* / _' | _ \| |
* | (_) | (_) | |
* \__,_| __/|_|
* |_|
*
*
* @param Position $min
* @param Position $max
*
* @return Region[]
*/
function getOverride( Position $min, Position $max ): array
{
$level = $min->getLevel()->getName();
if( $level != $max->getLevel()->getName() )
{
return [];
}
if( !isset($this->data[$level]) )
{
return [];
}
$data = $this->data[$level];
if( count($data) == 0 )
{
unset($data); return [];
}
$arr = [];
foreach( $data as $rg )
{
if(
$rg->getMin('x') <= $max->getX() and $min->getX() <= $rg->getMax('x') and
$rg->getMin('y') <= $max->getY() and $min->getY() <= $rg->getMax('y') and
$rg->getMin('z') <= $max->getZ() and $min->getZ() <= $rg->getMax('z')
) {
$arr[] = $rg;
}
}
unset($data); return $arr;
}
/**
* @param Position $pos
*
* @return Region | NULL
*/
function getRegion( Position $pos )
{
$level = $pos->getLevel()->getName();
if( !isset($this->data[$level]) )
{
return NULL;
}
$data = $this->data[$level];
if( count($data) == 0 )
{
unset($data); return NULL;
}
$x = $pos->getFloorX();
$y = $pos->getFloorY();
$z = $pos->getFloorZ();
end($data);
for( $i = key($data); $i >= 0; $i-- ) // sqlite sucks.
{
if( !isset($data[$i]) )
{
continue;
}
$rg = $data[$i];
if(
$rg->getMin('x') <= $x and $x <= $rg->getMax('x') and
$rg->getMin('y') <= $y and $y <= $rg->getMax('y') and
$rg->getMin('z') <= $z and $z <= $rg->getMax('z')
) {
unset($data); return $rg;
}
}
unset($data); return NULL;
}
/**
* @param string $name
*
* @return Region | NULL
*/
function getRegionByName( string $name )
{
$name = strtolower($name);
foreach( $this->getServer()->getLevels() as $level )
{
$level = $level->getName();
if( !isset($this->data[$level]) )
{
continue;
}
$data = $this->data[$level];
if( count($data) == 0 )
{
unset($data); continue;
}
foreach( $data as $rg )
{
if( $rg->getRegionName() != $name )
{
continue;
}
unset($data); return $rg;
}
}
unset($data); return NULL;
}
/**
* @param string $nick
* @param string $name
* @param Position $min
* @param Position $max
*/
function createRegion( string $nick, string $name, Position $min, Position $max )
{
$level = $min->getLevel()->getName();
$nick = strtolower($nick);
$name = strtolower($name);
if( $this->getValue('full_height', 'config') === TRUE )
{
$min_y = 0;
$max_y = 256;
}
$data = [
'owner' => $nick,
'member' => [],
'level' => $level,
'min' => [
'x' => $min->getX(),
'y' => $min_y ?? $min->getY(),
'z' => $min->getZ()
],
'max' => [
'x' => $max->getX(),
'y' => $max_y ?? $max->getY(),
'z' => $max->getZ()
],
'flag' => $this->getValue('default_flag', 'config')
];
$region = new Region($name, $data);
$event = new RegionCreateEvent($this, $region);
$this->getServer()->getPluginManager()->callEvent($event);
if( $event->isCancelled() )
{
return;
}
$this->data[$level][] = $region;
unset($this->position[0][$nick]);
unset($this->position[1][$nick]);
$this->saveRegion($region);
}
/**
* @param string $name
*
* @return bool
*/
function removeRegion( string $name ): bool
{
$name = strtolower($name);
foreach( $this->getServer()->getLevels() as $level )
{
$level = $level->getName();
if( !isset($this->data[$level]) )
{
continue;
}
$data = $this->data[$level];
if( count($data) == 0 )
{
unset($data); continue;
}
foreach( $data as $key => $rg )
{
if( $rg->getRegionName() != $name )
{
continue;
}
$event = new RegionRemoveEvent($this, $rg);
$this->getServer()->getPluginManager()->callEvent($event);
if( $event->isCancelled() )
{
return FALSE;
}
unset($this->data[$level][$key]);
$this->data[$level] = array_values($this->data[$level]);
$this->region->remove($name);
$this->region->save(TRUE);
unset($data); return TRUE;
}
}
unset($data); return FALSE;
}
/**
* @param string $nick
* @param bool $include_member
*
* @return Region[]
*/
function getRegionList( string $nick, bool $include_member = false ): array
{
$nick = strtolower($nick);
$arr = [];
foreach( $this->getServer()->getLevels() as $level )
{
$level = $level->getName();
if( !isset($this->data[$level]) )
{
continue;
}
$data = $this->data[$level];
if( count($data) == 0 )
{
unset($data); continue;
}
foreach( $data as $rg )
{
if( $rg->getOwner() == $nick )
{
$arr[] = $rg;
continue;
}
if( !$include_member )
{
continue;
}
if( !in_array($nick, $rg->getMemberList()) )
{
continue;
}
$arr[] = $rg;
}
}
unset($data); return $arr;
}
/**
* @todo rewrite this piece of shit.
*
* @param string $type
* @param string $key
*
* @return string | int | NULL
*/
function getValue( string $key, string $type = 'message' )
{
$type = strtolower($type);
$key = mb_strtolower($key);
$error = "Configuration error: пункт '$key' не найден в $type.yml. Пожалуйста, удалите старый конфиг (/plugins/sexGuard/$type.yml) и перезагрузите сервер.";
if( $type == 'config' )
{
$value = $this->config->get($key, 'жопа');
if( $value === 'жопа' )
{
throw new Exception($error);
}
}
elseif( $type == 'group' )
{
$value = $this->group->get($key);
$value = !$value ? $this->group->get('default') : $value;
if( !$value )
{
$this->getLogger()->error($error);
$value = [
'max_size' => 5000,
'max_count' => 4,
'ignored_flag' => [],
'ignored_region' => []
];
}
}
else
{
$value = $this->message->get($key);
if( $value === FALSE )
{
$this->getLogger()->error($error);
$value = "§l§c- §fGUARD §c- Произошла внутренняя ошибка§r";
}
}
return $value;
}
/**
* @param Region $region
*/
function saveRegion( Region $region )
{
$this->region->set($region->getRegionName(), $region->toData());
$this->region->save(TRUE);
}
/**
* @param Position $pos1
* @param Position $pos2
*
* @return int
*/
function calculateSize( Position $pos1, Position $pos2 ): int
{
$x = [ min($pos1->getX(), $pos2->getX()), max($pos1->getX(), $pos2->getX()) ];
$y = [ min($pos1->getY(), $pos2->getY()), max($pos1->getY(), $pos2->getY()) ];
$z = [ min($pos1->getZ(), $pos2->getZ()), max($pos1->getZ(), $pos2->getZ()) ];
if( $this->getValue('full_height', 'config') === TRUE )
{
$y = [ 0, 1 ];
}
return ($x[1] - $x[0]) * ($y[1] - $y[0]) * ($z[1] - $z[0]);
}
/**
* @return string[]
*/
function getAllowedFlag( ): array
{
$list = array_map('strtolower', $this->getValue('allowed_flag', 'config'));
foreach( $list as $flag )
{
if( isset(self::DEFAULT_FLAG[$flag]) )
{
continue;
}
unset($list[$flag]);
}
return $list;
}
/**
* @param Player $player
* @param string $message
*/
function sendWarning( Player $player, string $message )
{
if( empty($message) )
{
return;
}
switch( $this->getValue('warn_type', 'config') )
{
case 0: $player->sendPopup($message); break;
case 1: $player->sendMessage($message); break;
default: $player->sendTip($message); break;
}
}
/**
* @todo better manage permissions for size control.
*
* @param Player $player
*
* @return int[]
*/
function getGroupValue( Player $player ): array
{
if( isset($this->extension['pureperms']) )
{
$group = $this->extension['pureperms']->getUserDataMgr()->getGroup($player)->getName();
}
elseif( isset($this->extension['universalgroup']) )
{
$group = $this->extension['universalgroup']->getGroup($player->getName())->getId();
}
elseif( isset($this->extension['sexgroup']) )
{
$group = $this->extension['sexgroup']->getPlayerGroup($player->getName())->getId();
}
return $this->getValue($group ?? 'default', 'group');
}
/**
* _ _
* | | ___ __ _ __| |
* | |/ _ \ / _' |/ _' |
* | | (_) | (_) | (_) |
* |_|\___/ \__,_|\__,_|
*
*/
private function loadInstance( )
{
self::$instance = $this;
}
private function initPermission( )
{
$list = [
new Permission('sexguard.noflag', 'Игнорирование флагов внутри регионов', Permission::DEFAULT_OP),
new Permission('sexguard.all', 'Доступ ко всем функциям sexGuard', Permission::DEFAULT_OP)
];
foreach( $list as $permission )
{
$this->getServer()->getPluginManager()->addPermission($permission);
}
}
private function initProvider( )
{
$folder = $this->getDataFolder();
if( !is_dir($folder) )
{
@mkdir($folder);
}
$this->saveResource('group.yml');
$this->saveResource('config.yml');
$this->saveResource('message.yml');
$this->group = new Config($folder. 'group.yml');
$this->config = new Config($folder. 'config.yml');
$this->message = new Config($folder. 'message.yml');
$this->sign = new Config($folder. 'sign.json');
$this->region = new Config($folder. 'region.json');
$this->sign->reload();
$this->region->reload();
foreach( $this->region->getAll() as $name => $data )
{
/**
* @todo check data on load.
*/
$rg = new Region($name, $data);
$level = $rg->getLevelName();
$this->data[$level][] = $rg;
}
}
private function initListener( )
{
$listener = [
new PlayerGuard($this),
new BlockGuard($this),
new EntityGuard($this)
];
foreach( $listener as $class )
{
$this->getServer()->getPluginManager()->registerEvents($class, $this);
}
}
private function initCommand( )
{
try
{
$command = new OldGuardCommand($this);
}
catch( Exception $exception )
{
$command = new NewGuardCommand($this);
}
$map = $this->getServer()->getCommandMap();
$replace = $map->getCommand($command->getName());
if( isset($replace) )
{
$replace->setLabel('');
$replace->unregister($map);
}
$map->register($this->getName(), $command);
}
private function initExtension( )
{
$list = [
'PurePerms',
'EconomyAPI',
'UniversalGroup',
'UniversalMoney',
'SexGroup'
];
foreach( $list as $extension )
{
if( $this->getValue('allow_'. strtolower($extension), 'config') === TRUE )
{
$plugin = $this->getServer()->getPluginManager()->getPlugin($extension);
if( isset($plugin) )
{
$this->extension[strtolower($extension)] = $plugin;
}
}
}
}
}