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

Commit 84f98c7

Browse files
committed
More flexible
1 parent b512a6f commit 84f98c7

File tree

3 files changed

+179
-16
lines changed

3 files changed

+179
-16
lines changed

BasicHUD/README.md

+50
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ BasicHUD
1717
This plugin lets you configure a basic Heads-Up Display (HUD) for
1818
players.
1919

20+
### Basic Usage
21+
22+
* **/hud** _[on|off|format]_
23+
* If none specified, will show the current mode info.
24+
* If _on_ is specified, HUD will be turned on.
25+
* If _off_ is specified, HUD will be turned off.
26+
* If _format_ is specified, that needs to be configured in
27+
`config.yml` and that format will be used.
28+
2029
### Configuration
2130

2231
In the `config.yml` you can configure the following:
@@ -81,6 +90,43 @@ inactive until you log-in. If you are using something other than
8190
`SimpleAuth` you can copy the `message-example.php` to `message.php`
8291
and do whatever checks you need to do.
8392

93+
### Multi-Format options
94+
95+
BasicHUD supports multiple formats. These can be configured through
96+
the `config.yml`. So instead of **format** only having **one** format
97+
configured, you can configure multiple, like this example:
98+
99+
````YAML
100+
[CODE]
101+
format:
102+
lv3: '{GREEN}{BasicHUD} {YELLOW}Lv3 {WHITE}{world} ({x},{y},{z}) {bearing} {RED}EUR:{money} Pts:{score}'
103+
lv2: '{GREEN}{BasicHUD} {GREEN}Lv2 {WHITE}{world} ({x},{y},{z}) {bearing} {RED}EUR:{money} Pts:{score}'
104+
lv1: '{GREEN}{BasicHUD} {BLUE}Lv1 {WHITE}{world} ({x},{y},{z}) {bearing} {RED}EUR:{money} Pts:{score}'
105+
lv0: '{GREEN}{BasicHUD} {GRAY}Lv0 {WHITE}{world} ({x},{y},{z}) {bearing} {RED}EUR:{money} Pts:{score}'
106+
[/CODE]
107+
````
108+
109+
In this example, four formats are defined. To select the format,
110+
**BasicHUD** will test permissions in order until the player has the
111+
permission:
112+
113+
* basichud.rank.selector
114+
115+
So if the player wants to use format _lv2_, permission
116+
_basichud.rank.lv2_ is required. For multiple matches, the first
117+
match is used. If none matches, the last format is used.
118+
119+
Switching formats is not saved. So on join the player always gets the
120+
default format. If you want HUD format choices to be saved you need a
121+
permissions plugin.
122+
123+
### Permission Nodes
124+
125+
* basichud.cmd: Allow players to access HUD command
126+
* basichud.cmd.toggle: Allow players to toggle HUD on/off
127+
* basichud.cmd.switch: Allow players to switch formats
128+
* basichud.rank.*: If multiple formats, these selects them.
129+
84130
# API
85131

86132
Since **BasicHUD** takes over the built-in _sendPopup_ functionality,
@@ -100,6 +146,10 @@ if (($hud = $this->getServer()->getPluginManager()->getPlugin("BasicHUD")) !== n
100146

101147
# Changes
102148

149+
* 1.0.2: added features
150+
* command to turn on|off|change HUD
151+
* supports multiple HUD formats which can be selected based on
152+
permissions.
103153
* 1.0.1: minor update
104154
* Added additional variables
105155
* Improved examples

BasicHUD/plugin.yml

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
name: BasicHUD
2-
version: 1.0.1
2+
version: 1.0.2
33
main: aliuly\hud\Main
44
api: 1.12.0
55
load: POSTWORLD
66

77
description: A configurable heads up display
88
author: aliuly
99

10+
commands:
11+
hud:
12+
description: Control HUD display
13+
usage: "/hud [on|off|format]"
14+
permission: basichud.cmd
15+
16+
permissions:
17+
basichud.cmd:
18+
default: true
19+
description: Allow players to access HUD command
20+
basichud.cmd.toggle:
21+
default: true
22+
description: Allow players to toggle HUD on/off
23+
basichud.cmd.switch:
24+
default: op
25+
description: Allow players to switch formats

BasicHUD/src/aliuly/hud/Main.php

+112-15
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
use pocketmine\scheduler\PluginTask;
88
use pocketmine\event\Listener;
99
use pocketmine\event\player\PlayerQuitEvent;
10+
use pocketmine\permission\Permission;
11+
use pocketmine\command\CommandExecutor;
12+
use pocketmine\command\CommandSender;
13+
use pocketmine\command\Command;
14+
1015

1116
interface Formatter {
1217
static public function formatString(Main $plugin,$format,Player $player);
@@ -51,13 +56,23 @@ public function onRun($currentTick){
5156

5257
}
5358

54-
class Main extends PluginBase implements Listener {
59+
class Main extends PluginBase implements Listener,CommandExecutor {
5560
protected $_getMessage;
5661
protected $_getVars;
5762

5863
protected $format;
59-
protected $formatter;
60-
public $sendPopup;
64+
protected $sendPopup;
65+
protected $disabled;
66+
67+
static public function pickFormatter($format) {
68+
if (strpos($format,"<?php") !== false|| strpos($format,"<?=") !== false) {
69+
return __NAMESPACE__."\\PhpFormat";
70+
}
71+
if (strpos($format,"{") !== false && strpos($format,"}")) {
72+
return __NAMESPACE__."\\StrtrFormat";
73+
}
74+
return __NAMESPACE__."\\FixedFormat";
75+
}
6176

6277
static public function bearing($deg) {
6378
// Determine bearing
@@ -138,8 +153,6 @@ public function getVars($player) {
138153
"{ITALIC}" => TextFormat::ITALIC,
139154
"{RESET}" => TextFormat::RESET,
140155
];
141-
142-
143156
if ($this->_getVars !== null) {
144157
$fn = $this->_getVars;
145158
$fn($this,$vars,$player);
@@ -163,13 +176,23 @@ public function defaultGetMessage($player) {
163176
if (microtime(true) < $timer) return $msg;
164177
unset($this->sendPopup[$n]);
165178
}
179+
if (isset($this->disabled[$n])) return "";
166180

167-
$fmt = $this->formatter;
168-
$txt = $fmt::formatString($this,$this->format,$player);
181+
// Manage custom groups
182+
if (is_array($this->format[0])) {
183+
foreach ($this->format as $rr) {
184+
list($rank,$fmt,$formatter) = $rr;
185+
if ($player->hasPermission("basichud.rank.".$rank)) break;
186+
}
187+
} else {
188+
list($fmt,$formatter) = $this->format;
189+
}
190+
$txt = $formatter::formatString($this,$fmt,$player);
169191
return $txt;
170192
}
171193

172194
public function onEnable(){
195+
$this->disabled = [];
173196
$this->sendPopup = [];
174197
if (!is_dir($this->getDataFolder())) mkdir($this->getDataFolder());
175198
/* Save default resources */
@@ -183,15 +206,16 @@ public function onEnable(){
183206
];
184207
$cf = (new Config($this->getDataFolder()."config.yml",
185208
Config::YAML,$defaults))->getAll();
186-
$this->format = $cf["format"];
187-
if (strpos($this->format,"<?php") !== false
188-
|| strpos($this->format,"<?=") !== false) {
189-
$this->formatter = __NAMESPACE__."\\PhpFormat";
190-
} elseif (strpos($this->format,"{") !== false &&
191-
strpos($this->format,"}")) {
192-
$this->formatter = __NAMESPACE__."\\StrtrFormat";
209+
if (is_array($cf["format"])) {
210+
$this->format = [];
211+
foreach ($cf["format"] as $rank=>$fmt) {
212+
$this->format[] = [ $rank, $fmt, self::pickFormatter($fmt) ];
213+
$p = new Permission("basichud.rank.".$rank,
214+
"BasicHUD format ".$rank, false);
215+
$this->getServer()->getPluginManager()->addPermission($p);
216+
}
193217
} else {
194-
$this->formatter = __NAMESPACE__."\\FixedFormat";
218+
$this->format = [ $cf["format"], self::pickFormatter($cf["format"]) ];
195219
}
196220
$code = '$this->_getMessage = function($plugin,$player){';
197221
if (file_exists($this->getDataFolder()."message.php")) {
@@ -218,5 +242,78 @@ public function onEnable(){
218242
public function onQuit(PlayerQuitEvent $ev) {
219243
$n = strtolower($ev->getPlayer()->getName());
220244
if (isset($this->sendPopup[$n])) unset($this->sendPopup[$n]);
245+
if (isset($this->disabled[$n])) unset($this->disabled[$n]);
246+
}
247+
248+
public function onCommand(CommandSender $sender, Command $cmd, $label, array $args) {
249+
if ($cmd->getName() != "hud") return false;
250+
if (!($sender instanceof Player)) {
251+
$sender->sendMessage("This command can only be used in-game");
252+
return true;
253+
}
254+
$n = strtolower($sender->getName());
255+
if (count($args) == 0) {
256+
if (isset($this->disabled[$n])) {
257+
$sender->sendMessage("HUD is OFF");
258+
return true;
259+
}
260+
if (is_array($this->format[0])) {
261+
foreach ($this->format as $rr) {
262+
list($rank,,) = $rr;
263+
if ($sender->hasPermission("basichud.rank.".$rank)) break;
264+
}
265+
$sender->sendMessage("HUD using format $rank");
266+
$fl = [];
267+
foreach ($this->format as $rr) {
268+
list($rank,,) = $rr;
269+
$fl[] = $rank;
270+
}
271+
if ($sender->hasPermission("basichud.cmd.switch")) {
272+
$sender->sendMessage("Available formats: ".
273+
implode(", ",$fl));
274+
}
275+
return true;
276+
}
277+
$sender->sendMessage("HUD is ON");
278+
return true;
279+
}
280+
if (count($args) != 1) return false;
281+
$mode = strtolower(array_shift($args));
282+
if (is_array($this->format[0])) {
283+
// Check if the input matches any of the ranks...
284+
foreach ($this->format as $rr1) {
285+
list($rank,,) = $rr1;
286+
if (strtolower($rank) == $mode) {
287+
// OK, user wants to switch to this format...
288+
if (!$sender->hasPermission("basichud.cmd.switch")) {
289+
$sender->sendMessage("You are not allowed to do that");
290+
return true;
291+
}
292+
foreach ($this->format as $rr2) {
293+
list($rn,,) = $rr2;
294+
if ($rank == $rn) {
295+
$sender->addAttachment($this,"basichud.rank.".$rn,true);
296+
} else {
297+
$sender->addAttachment($this,"basichud.rank.".$rn,false);
298+
}
299+
}
300+
$sender->sendMessage("Switching to format $rank");
301+
return true;
302+
}
303+
}
304+
}
305+
if (!$sender->hasPermission("basichud.cmd.toggle")) {
306+
$sender->sendMessage("You are not allowed to do that");
307+
return true;
308+
}
309+
$mode = filter_var($mode,FILTER_VALIDATE_BOOLEAN);
310+
if ($mode) {
311+
if (isset($this->disabled[$n])) unset($this->disabled[$n]);
312+
$sender->sendMessage("Turning on HUD");
313+
return true;
314+
}
315+
$this->disabled[$n] = $n;
316+
$sender->sendMessage("Turning off HUD");
317+
return true;
221318
}
222319
}

0 commit comments

Comments
 (0)