7
7
use pocketmine \scheduler \PluginTask ;
8
8
use pocketmine \event \Listener ;
9
9
use pocketmine \event \player \PlayerQuitEvent ;
10
+ use pocketmine \event \player \PlayerCommandPreprocessEvent ;
11
+ use pocketmine \event \server \RemoteServerCommandEvent ;
12
+ use pocketmine \event \server \ServerCommandEvent ;
10
13
use pocketmine \permission \Permission ;
11
14
use pocketmine \command \CommandExecutor ;
12
15
use pocketmine \command \CommandSender ;
@@ -51,20 +54,22 @@ public function onRun($currentTick){
51
54
foreach ($ plugin ->getServer ()->getOnlinePlayers () as $ pl ) {
52
55
if (!$ pl ->hasPermission ("basichud.user " )) continue ;
53
56
$ msg = $ plugin ->getMessage ($ pl );
54
- if ($ msg != "" ) $ pl ->sendPopup ($ msg );
57
+ if ($ msg !== null ) $ pl ->sendPopup ($ msg );
55
58
}
56
59
}
57
60
58
61
}
59
62
60
63
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
63
66
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
68
73
69
74
static public function pickFormatter ($ format ) {
70
75
if (strpos ($ format ,"<?php " ) !== false || strpos ($ format ,"<?= " ) !== false ) {
@@ -122,6 +127,7 @@ private function changePermission($player,$perm,$bool) {
122
127
}
123
128
$ attach = $ this ->perms [$ n ];
124
129
$ attach ->setPermission ($ perm ,$ bool );
130
+ if (isset ($ this ->perms_cache [$ n ])) unset($ this ->perms_cache [$ n ])
125
131
}
126
132
127
133
public function getMessage ($ player ) {
@@ -130,18 +136,71 @@ public function getMessage($player) {
130
136
}
131
137
132
138
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 = [
134
202
"{BasicHUD} " => $ this ->getDescription ()->getFullName (),
135
203
"{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 ()),
145
204
"{10SPACE} " => str_repeat (" " ,10 ),
146
205
"{20SPACE} " => str_repeat (" " ,20 ),
147
206
"{30SPACE} " => str_repeat (" " ,30 ),
@@ -171,63 +230,20 @@ public function getVars($player) {
171
230
"{ITALIC} " => TextFormat::ITALIC ,
172
231
"{RESET} " => TextFormat::RESET ,
173
232
];
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 "" ;
200
233
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 );
222
234
223
235
$ defaults = [
224
236
"version " => $ this ->getDescription ()->getVersion (),
225
- "ticks " => 15 ,
237
+ "ticks " => 10 ,
226
238
"format " => "{GREEN}{BasicHUD} {WHITE}{world} ({x},{y},{z}) {bearing} " ,
227
239
];
240
+
228
241
$ cf = (new Config ($ this ->getDataFolder ()."config.yml " ,
229
242
Config::YAML ,$ defaults ))->getAll ();
243
+
230
244
if (is_array ($ cf ["format " ])) {
245
+ // Multiple format specified...
246
+ // save them and also register the appropriate permissions
231
247
$ this ->format = [];
232
248
foreach ($ cf ["format " ] as $ rank =>$ fmt ) {
233
249
$ this ->format [] = [ $ rank , $ fmt , self ::pickFormatter ($ fmt ) ];
@@ -236,6 +252,7 @@ public function onEnable(){
236
252
$ this ->getServer ()->getPluginManager ()->addPermission ($ p );
237
253
}
238
254
} else {
255
+ // Single format only
239
256
$ this ->format = [ $ cf ["format " ], self ::pickFormatter ($ cf ["format " ]) ];
240
257
}
241
258
$ code = '$this->_getMessage = function($plugin,$player){ ' ;
@@ -254,14 +271,30 @@ public function onEnable(){
254
271
//echo $code."\n";//##DEBUG
255
272
eval ($ code );
256
273
} else {
257
- $ this ->_getVars = null ;
274
+ // Empty function (this means we do not need to test _getVars)
275
+ $ this ->_getVars = function (){};
258
276
}
259
277
$ this ->getServer ()->getScheduler ()->scheduleRepeatingTask (new PopupTask ($ this ), $ cf ["ticks " ]);
260
278
$ this ->getServer ()->getPluginManager ()->registerEvents ($ this , $ this );
261
279
}
262
280
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
+ }
263
295
public function onQuit (PlayerQuitEvent $ ev ) {
264
296
$ n = strtolower ($ ev ->getPlayer ()->getName ());
297
+ if (isset ($ this ->perms_cache [$ n ])) unset($ this ->perms_cache [$ n ])
265
298
if (isset ($ this ->sendPopup [$ n ])) unset($ this ->sendPopup [$ n ]);
266
299
if (isset ($ this ->disabled [$ n ])) unset($ this ->disabled [$ n ]);
267
300
if (isset ($ this ->perms [$ n ])) {
@@ -319,10 +352,8 @@ public function onCommand(CommandSender $sender, Command $cmd, $label, array $ar
319
352
list ($ rn ,,) = $ rr2 ;
320
353
if ($ rank == $ rn ) {
321
354
$ this ->changePermission ($ sender ,"basichud.rank. " .$ rn ,true );
322
- $ sender ->addAttachment ($ this ,"basichud.rank. " .$ rn ,true );
323
355
} else {
324
356
$ this ->changePermission ($ sender ,"basichud.rank. " .$ rn ,false );
325
- $ sender ->addAttachment ($ this ,"basichud.rank. " .$ rn ,false );
326
357
}
327
358
}
328
359
$ sender ->sendMessage ("Switching to format $ rank " );
@@ -344,4 +375,18 @@ public function onCommand(CommandSender $sender, Command $cmd, $label, array $ar
344
375
$ sender ->sendMessage ("Turning off HUD " );
345
376
return true ;
346
377
}
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
+ }
347
392
}
0 commit comments