Skip to content
This repository was archived by the owner on Jul 11, 2018. It is now read-only.

Commit 140a530

Browse files
committed
Update Main.php
1 parent fb37ee9 commit 140a530

File tree

1 file changed

+113
-68
lines changed

1 file changed

+113
-68
lines changed

BasicHUD/src/aliuly/hud/Main.php

+113-68
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
use pocketmine\scheduler\PluginTask;
88
use pocketmine\event\Listener;
99
use pocketmine\event\player\PlayerQuitEvent;
10+
use pocketmine\event\player\PlayerCommandPreprocessEvent;
11+
use pocketmine\event\server\RemoteServerCommandEvent;
12+
use pocketmine\event\server\ServerCommandEvent;
1013
use pocketmine\permission\Permission;
1114
use pocketmine\command\CommandExecutor;
1215
use pocketmine\command\CommandSender;
@@ -51,20 +54,22 @@ public function onRun($currentTick){
5154
foreach ($plugin->getServer()->getOnlinePlayers() as $pl) {
5255
if (!$pl->hasPermission("basichud.user")) continue;
5356
$msg = $plugin->getMessage($pl);
54-
if ($msg != "") $pl->sendPopup($msg);
57+
if ($msg !== null) $pl->sendPopup($msg);
5558
}
5659
}
5760

5861
}
5962

6063
class Main extends PluginBase implements Listener,CommandExecutor {
61-
protected $_getMessage;
62-
protected $_getVars;
64+
protected $_getMessage; // Message function (to disabled)
65+
protected $_getVars; // Customize variables
6366

64-
protected $format;
65-
protected $sendPopup;
66-
protected $disabled;
67-
protected $perms;
67+
protected $format; // HUD format
68+
protected $sendPopup; // Message to popup through API
69+
protected $disabled; // HUD disabled by command
70+
protected $perms; // Attachable permissions
71+
protected $consts; // These are constant variables...
72+
protected $perms_cache; // Permissions cache
6873

6974
static public function pickFormatter($format) {
7075
if (strpos($format,"<?php") !== false|| strpos($format,"<?=") !== false) {
@@ -122,6 +127,7 @@ private function changePermission($player,$perm,$bool) {
122127
}
123128
$attach = $this->perms[$n];
124129
$attach->setPermission($perm,$bool);
130+
if (isset($this->perms_cache[$n])) unset($this->perms_cache[$n])
125131
}
126132

127133
public function getMessage($player) {
@@ -130,18 +136,71 @@ public function getMessage($player) {
130136
}
131137

132138
public function getVars($player) {
133-
$vars = [
139+
$vars = $this->consts;
140+
foreach ([
141+
"{tps}"] => $this->getServer()->getTicksPerSecond(),
142+
"{player}"] => $player->getName(),
143+
"{world}" => $player->getLevel()->getName(),
144+
"{x}" => (int)$player->getX(),
145+
"{y}" => (int)$player->getY(),
146+
"{z}" => (int)$player->getZ(),
147+
"{yaw}" => (int)$player->getYaw(),
148+
"{pitch}" => (int)$player->getPitch(),
149+
"{bearing}" => self::bearing($player->getYaw()),
150+
] as $a => $b) {
151+
$vars[$a] = $b;
152+
}
153+
$fn = $this->_getVars;
154+
$fn($this,$vars,$player);
155+
return $vars;
156+
}
157+
158+
public function defaultGetMessage($player) {
159+
$n = strtolower($player->getName());
160+
if (isset($this->sendPopup[$n])) {
161+
// An API user wants to post a Popup...
162+
list($msg,$timer) = $this->sendPopup[$n];
163+
if (microtime(true) < $timer) return $msg;
164+
unset($this->sendPopup[$n]);
165+
}
166+
if (isset($this->disabled[$n])) return null;
167+
168+
// Manage custom groups
169+
if (is_array($this->format[0])) {
170+
if (!isset($this->perms_cache[$n])) {
171+
$i = 0;
172+
foreach ($this->format as $rr) {
173+
list($rank,$fmt,$formatter) = $rr;
174+
if ($player->hasPermission("basichud.rank.".$rank)) {
175+
$this->perms_cache[$n] = $i;
176+
break;
177+
}
178+
++$i;
179+
}
180+
} else {
181+
list($rank,$fmt,$formatter) = $this->format[$rank = $this->perms_cache[$n]];
182+
}
183+
} else {
184+
list($fmt,$formatter) = $this->format;
185+
}
186+
$txt = $formatter::formatString($this,$fmt,$player);
187+
return $txt;
188+
}
189+
190+
public function onEnable(){
191+
$this->disabled = [];
192+
$this->sendPopup = [];
193+
$this->perms = [];
194+
195+
if (!is_dir($this->getDataFolder())) mkdir($this->getDataFolder());
196+
/* Save default resources */
197+
$this->saveResource("message-example.php",true);
198+
$this->saveResource("vars-example.php",true);
199+
200+
// These are constants that should be pre calculated
201+
$this->consts = [
134202
"{BasicHUD}" => $this->getDescription()->getFullName(),
135203
"{MOTD}" => $this->getServer()->getMotd(),
136-
"{tps}" => $this->getServer()->getTicksPerSecond(),
137-
"{player}" => $player->getName(),
138-
"{world}" => $player->getLevel()->getName(),
139-
"{x}" => (int)$player->getX(),
140-
"{y}" => (int)$player->getY(),
141-
"{z}" => (int)$player->getZ(),
142-
"{yaw}" => (int)$player->getYaw(),
143-
"{pitch}" => (int)$player->getPitch(),
144-
"{bearing}" => self::bearing($player->getYaw()),
145204
"{10SPACE}" => str_repeat(" ",10),
146205
"{20SPACE}" => str_repeat(" ",20),
147206
"{30SPACE}" => str_repeat(" ",30),
@@ -171,63 +230,20 @@ public function getVars($player) {
171230
"{ITALIC}" => TextFormat::ITALIC,
172231
"{RESET}" => TextFormat::RESET,
173232
];
174-
if ($this->_getVars !== null) {
175-
$fn = $this->_getVars;
176-
$fn($this,$vars,$player);
177-
}
178-
return $vars;
179-
}
180-
181-
public function sendPopup($player,$msg,$length=3) {
182-
if ($this->isEnabled()) {
183-
if ($player->hasPermission("basichud.user")) {
184-
$n = strtolower($player->getName());
185-
$this->sendPopup[$n] = [ $msg, microtime(true)+$length ];
186-
$msg = $this->getMessage($player);
187-
}
188-
}
189-
$player->sendPopup($msg);
190-
}
191-
192-
public function defaultGetMessage($player) {
193-
$n = strtolower($player->getName());
194-
if (isset($this->sendPopup[$n])) {
195-
list($msg,$timer) = $this->sendPopup[$n];
196-
if (microtime(true) < $timer) return $msg;
197-
unset($this->sendPopup[$n]);
198-
}
199-
if (isset($this->disabled[$n])) return "";
200233

201-
// Manage custom groups
202-
if (is_array($this->format[0])) {
203-
foreach ($this->format as $rr) {
204-
list($rank,$fmt,$formatter) = $rr;
205-
if ($player->hasPermission("basichud.rank.".$rank)) break;
206-
}
207-
} else {
208-
list($fmt,$formatter) = $this->format;
209-
}
210-
$txt = $formatter::formatString($this,$fmt,$player);
211-
return $txt;
212-
}
213-
214-
public function onEnable(){
215-
$this->disabled = [];
216-
$this->sendPopup = [];
217-
$this->perms = [];
218-
if (!is_dir($this->getDataFolder())) mkdir($this->getDataFolder());
219-
/* Save default resources */
220-
$this->saveResource("message-example.php",true);
221-
$this->saveResource("vars-example.php",true);
222234

223235
$defaults = [
224236
"version" => $this->getDescription()->getVersion(),
225-
"ticks" => 15,
237+
"ticks" => 10,
226238
"format" => "{GREEN}{BasicHUD} {WHITE}{world} ({x},{y},{z}) {bearing}",
227239
];
240+
228241
$cf = (new Config($this->getDataFolder()."config.yml",
229242
Config::YAML,$defaults))->getAll();
243+
230244
if (is_array($cf["format"])) {
245+
// Multiple format specified...
246+
// save them and also register the appropriate permissions
231247
$this->format = [];
232248
foreach ($cf["format"] as $rank=>$fmt) {
233249
$this->format[] = [ $rank, $fmt, self::pickFormatter($fmt) ];
@@ -236,6 +252,7 @@ public function onEnable(){
236252
$this->getServer()->getPluginManager()->addPermission($p);
237253
}
238254
} else {
255+
// Single format only
239256
$this->format = [ $cf["format"], self::pickFormatter($cf["format"]) ];
240257
}
241258
$code = '$this->_getMessage = function($plugin,$player){';
@@ -254,14 +271,30 @@ public function onEnable(){
254271
//echo $code."\n";//##DEBUG
255272
eval($code);
256273
} else {
257-
$this->_getVars = null;
274+
// Empty function (this means we do not need to test _getVars)
275+
$this->_getVars = function(){};
258276
}
259277
$this->getServer()->getScheduler()->scheduleRepeatingTask(new PopupTask($this), $cf["ticks"]);
260278
$this->getServer()->getPluginManager()->registerEvents($this, $this);
261279
}
262280

281+
// We clear the permissions cache in the event of a command
282+
// next time we schedule to fetch the HUD message it will be recomputed
283+
private function onCmdEvent() {
284+
$this->perms_cache = []
285+
}
286+
public function onPlayerCmd(PlayerCommandPreprocessEvent $ev) {
287+
$this->onCmdEvent();
288+
}
289+
public function onRconCmd(RemoteServerCommandEvent $ev) {
290+
$this->onCmdEvent();
291+
}
292+
public function onConsoleCmd(ServerCommandEvent $ev) {
293+
$this->onCmdEvent();
294+
}
263295
public function onQuit(PlayerQuitEvent $ev) {
264296
$n = strtolower($ev->getPlayer()->getName());
297+
if (isset($this->perms_cache[$n])) unset($this->perms_cache[$n])
265298
if (isset($this->sendPopup[$n])) unset($this->sendPopup[$n]);
266299
if (isset($this->disabled[$n])) unset($this->disabled[$n]);
267300
if (isset($this->perms[$n])) {
@@ -319,10 +352,8 @@ public function onCommand(CommandSender $sender, Command $cmd, $label, array $ar
319352
list($rn,,) = $rr2;
320353
if ($rank == $rn) {
321354
$this->changePermission($sender,"basichud.rank.".$rn,true);
322-
$sender->addAttachment($this,"basichud.rank.".$rn,true);
323355
} else {
324356
$this->changePermission($sender,"basichud.rank.".$rn,false);
325-
$sender->addAttachment($this,"basichud.rank.".$rn,false);
326357
}
327358
}
328359
$sender->sendMessage("Switching to format $rank");
@@ -344,4 +375,18 @@ public function onCommand(CommandSender $sender, Command $cmd, $label, array $ar
344375
$sender->sendMessage("Turning off HUD");
345376
return true;
346377
}
378+
379+
/**
380+
* @API
381+
*/
382+
public function sendPopup($player,$msg,$length=3) {
383+
if ($this->isEnabled()) {
384+
if ($player->hasPermission("basichud.user")) {
385+
$n = strtolower($player->getName());
386+
$this->sendPopup[$n] = [ $msg, microtime(true)+$length ];
387+
$msg = $this->getMessage($player);
388+
}
389+
}
390+
$player->sendPopup($msg);
391+
}
347392
}

0 commit comments

Comments
 (0)