-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRubberBand.php
90 lines (79 loc) · 2.33 KB
/
RubberBand.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
<?php
/**
* ____ _ _ ___
* | _ \ _ __ ___ ___ ___ _ __ | |_| |/ (_)_ __ ___
* | |_) | '__/ _ \/ __|/ _ \ '_ \| __| ' /| | '_ ` _ \
* | __/| | | __/\__ \ __/ | | | |_| . \| | | | | | |
* |_| |_| \___||___/\___|_| |_|\__|_|\_\_|_| |_| |_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the MIT License. see <https://opensource.org/licenses/MIT>.
*
*
* @author PresentKim ([email protected])
* @link https://github.com/PresentKim
* @license https://opensource.org/licenses/MIT MIT License
*
* @name RubberBand
* @main kim\present\singleton\RubberBand
* @version 1.0.0
* @api 4.0.0
* @description Set max players to player count + 1
*
* (\ /)
* ( . .) ♥
* c(")(")
*/
declare(strict_types=1);
namespace kim\present\singleton {
use pocketmine\event\Listener;
use pocketmine\event\player\{PlayerJoinEvent, PlayerQuitEvent};
use pocketmine\plugin\PluginBase;
use pocketmine\Server;
class RubberBand extends PluginBase implements Listener{
/** @var \ReflectionProperty */
private $reflectionProperty;
/**
* Called when the plugin is loaded, before calling onEnable()
*
* @throws \ReflectionException
*/
public function onLoad() : void{
$reflectionClass = new \ReflectionClass(Server::class);
$this->reflectionProperty = $reflectionClass->getProperty("maxPlayers");
$this->reflectionProperty->setAccessible(true);
}
/**
* Called when the plugin is enabled
*/
public function onEnable() : void{
$this->recalculateMaxPlayers(1);
$this->getServer()->getPluginManager()->registerEvents($this, $this);
}
/**
* @priority HIGHEST
*
* @param PlayerJoinEvent $event
*/
public function onPlayerJoinEvent(PlayerJoinEvent $event) : void{
$this->recalculateMaxPlayers(1);
}
/**
* @priority HIGHEST
*
* @param PlayerQuitEvent $event
*/
public function onPlayerQuitEvent(PlayerQuitEvent $event) : void{
$this->recalculateMaxPlayers(0);
}
/**
* Set `Server::$maxPlayers` to `{PLAYER_COUNT} + $addition`
*
* @param int $addition
*/
public function recalculateMaxPlayers(int $addition) : void{
$server = $this->getServer();
$this->reflectionProperty->setValue($server, count($server->getOnlinePlayers()) + $addition);
}
}
}