-
Notifications
You must be signed in to change notification settings - Fork 9
/
CPS.php
75 lines (59 loc) · 1.78 KB
/
CPS.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
<?php
declare(strict_types = 1);
namespace JackMD\CPS;
use Ifera\ScoreHud\event\PlayerTagUpdateEvent;
use Ifera\ScoreHud\scoreboard\ScoreTag;
use Ifera\ScoreHud\ScoreHud;
use pocketmine\Player;
use pocketmine\plugin\PluginBase;
use function is_null;
use function strval;
class CPS extends PluginBase{
/** @var ScoreHud|null */
private $scoreHud = null;
/** @var array */
private $clicks;
public function onEnable(){
$this->scoreHud = $this->getServer()->getPluginManager()->getPlugin("ScoreHud");
if(!is_null($this->scoreHud)){
$this->getServer()->getPluginManager()->registerEvents(new TagResolveListener($this), $this);
}
$this->getServer()->getPluginManager()->registerEvents(new EventListener($this), $this);
$this->getLogger()->info("CPS Plugin Enabled.");
}
/**
* @param Player $player
* @return int
*/
public function getClicks(Player $player): int{
if(!isset($this->clicks[$player->getLowerCaseName()])){
return 0;
}
$time = $this->clicks[$player->getLowerCaseName()][0];
$clicks = $this->clicks[$player->getLowerCaseName()][1];
if($time !== time()){
unset($this->clicks[$player->getLowerCaseName()]);
return 0;
}
return $clicks;
}
/**
* @param Player $player
*/
public function addClick(Player $player): void{
if(!isset($this->clicks[$player->getLowerCaseName()])){
$this->clicks[$player->getLowerCaseName()] = [time(), 0];
}
$time = $this->clicks[$player->getLowerCaseName()][0];
$clicks = $this->clicks[$player->getLowerCaseName()][1];
if($time !== time()){
$time = time();
$clicks = 0;
}
$clicks++;
$this->clicks[$player->getLowerCaseName()] = [$time, $clicks];
if(!is_null($this->scoreHud)){
(new PlayerTagUpdateEvent($player, new ScoreTag("cps.cps", strval($this->getClicks($player)))))->call();
}
}
}