7
7
use pocketmine \scheduler \PluginTask ;
8
8
use pocketmine \event \Listener ;
9
9
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
+
10
15
11
16
interface Formatter {
12
17
static public function formatString (Main $ plugin ,$ format ,Player $ player );
@@ -51,13 +56,23 @@ public function onRun($currentTick){
51
56
52
57
}
53
58
54
- class Main extends PluginBase implements Listener {
59
+ class Main extends PluginBase implements Listener,CommandExecutor {
55
60
protected $ _getMessage ;
56
61
protected $ _getVars ;
57
62
58
63
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
+ }
61
76
62
77
static public function bearing ($ deg ) {
63
78
// Determine bearing
@@ -138,8 +153,6 @@ public function getVars($player) {
138
153
"{ITALIC} " => TextFormat::ITALIC ,
139
154
"{RESET} " => TextFormat::RESET ,
140
155
];
141
-
142
-
143
156
if ($ this ->_getVars !== null ) {
144
157
$ fn = $ this ->_getVars ;
145
158
$ fn ($ this ,$ vars ,$ player );
@@ -163,13 +176,23 @@ public function defaultGetMessage($player) {
163
176
if (microtime (true ) < $ timer ) return $ msg ;
164
177
unset($ this ->sendPopup [$ n ]);
165
178
}
179
+ if (isset ($ this ->disabled [$ n ])) return "" ;
166
180
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 );
169
191
return $ txt ;
170
192
}
171
193
172
194
public function onEnable (){
195
+ $ this ->disabled = [];
173
196
$ this ->sendPopup = [];
174
197
if (!is_dir ($ this ->getDataFolder ())) mkdir ($ this ->getDataFolder ());
175
198
/* Save default resources */
@@ -183,15 +206,16 @@ public function onEnable(){
183
206
];
184
207
$ cf = (new Config ($ this ->getDataFolder ()."config.yml " ,
185
208
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
+ }
193
217
} else {
194
- $ this ->formatter = __NAMESPACE__ . "\\ FixedFormat " ;
218
+ $ this ->format = [ $ cf [ " format " ], self :: pickFormatter ( $ cf [ " format " ]) ] ;
195
219
}
196
220
$ code = '$this->_getMessage = function($plugin,$player){ ' ;
197
221
if (file_exists ($ this ->getDataFolder ()."message.php " )) {
@@ -218,5 +242,78 @@ public function onEnable(){
218
242
public function onQuit (PlayerQuitEvent $ ev ) {
219
243
$ n = strtolower ($ ev ->getPlayer ()->getName ());
220
244
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 ;
221
318
}
222
319
}
0 commit comments