-
Notifications
You must be signed in to change notification settings - Fork 8
/
ResidentManager.php
313 lines (306 loc) · 12.5 KB
/
ResidentManager.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
/*
__PocketMine Plugin__
name=ResidentManager
description=ResidentManager
version=0.5
author=shoghicp
class=ResidentManager
apiversion=6
*/
class ResidentManager implements Plugin
{
private $api, $path, $config, $login;
public function __construct(ServerAPI $api, $server = false)
{
$this->api = $api;
$this->login = array();
}
public function init()
{
$this->createConfig();
$this->api->addHandler('console.command', array($this, 'handle'), 24);
$this->api->addHandler('player.container.slot', array($this, 'handle'), 19);
$this->api->addHandler('player.pickup', array($this, 'handle'), 19);
$this->api->addHandler('player.block.break', array($this, 'handle'), 3);
$this->api->addHandler('player.block.place', array($this, 'handle'), 3);
$this->api->event('player.join', array($this, 'handle'));
$this->api->console->register('register', 'Register yourself on the server.', array($this, 'commandH'));
$this->api->console->register('login', 'Login to the server.', array($this, 'commandH'));
$this->api->console->register('logout', 'Logout from the server.', array($this, 'commandH'));
$this->api->console->register('changepassword', 'Change your password.', array($this, 'commandH'));
$this->api->console->register('unregister', 'Unregister from the server.', array($this, 'commandH'));
$this->api->console->register('purge', 'Delete players from database that haven\'t logged in for X days.', array($this, 'commandH'));
$this->api->console->register('resident', 'Show resident list.', array($this, 'commandH'));
$this->api->console->alias('passwd', 'changepassword');
$this->api->ban->cmdWhitelist("register");
$this->api->ban->cmdWhitelist("login");
}
public function __destruct()
{
}
public function handle($data, $event)
{
switch ($event) {
case "player.block.place":
$user = $data['player']->username;
if($user==="")break;
if ($this->login[$user]) {
break;
}
if (!isset($this->config[$user])) {
$data['player']->sendChat("[INFO] You are not a resident of this server! You must register.\n[INFO] Usage: /register <password>");
} else {
$data['player']->sendChat("[INFO] You have to log in to this server!\n[INFO] Usage: /login <password>");
}
return false;
case "player.block.break":
$user = $data['player']->username;
if($user==="")break;
if ($this->login[$user]) {
break;
}
if (!isset($this->config[$user])) {
$data['player']->sendChat("[INFO] You are not a resident of this server! You must register.\n[INFO] Usage: /register <password>");
} else {
$data['player']->sendChat("[INFO] You have to log in to this server!\n[INFO] Usage: /login <password>");
}
$this->api->console->run("give $user sign", "console", false);
return false;
case "console.command":
if (!($data['issuer'] instanceof Player)) {
break;
}
if (in_array($data['cmd'], array('register', 'login'))) {
break;
}
$user = $data['issuer']->username;
if($user==="")break;
if ($this->login[$user]) {
break;
}
if (!isset($this->config[$user])) {
$data['issuer']->sendChat("[INFO] You are not a resident of this server! You must register.\n[INFO] Usage: /register <password>");
} else {
$data['issuer']->sendChat("[INFO] You have to log in to this server!\n[INFO] Usage: /login <password>");
}
return false;
case "player.container.slot":
$user = $data['player']->username;
if($user==="")break;
if ($this->login[$user]) {
break;
}
if (!isset($this->config[$user])) {
$data['player']->sendChat("[INFO] You are not a resident of this server! You must register.\n[INFO] Usage: /register <password>");
} else {
$data['player']->sendChat("[INFO] You have to log in to this server!\n[INFO] Usage: /login <password>");
}
return false;
case "player.pickup":
$user = $data['player']->username;
if($user==="")break;
if ($this->login[$user]) {
break;
}
return false;
case "player.join":
$user = $data->username;
if($user==='' or $user == null) return false;
$this->api->chat->sendTo(false,"Dobro pozhalovat' na server 4pda\nAdministratsiya servera WW, Doverennyi OP Mause\nGlavnye OPy KlaiM200, Rockey_lol\nOstal'nye OP Warrior236236, Valeross55, kosder, stalkerbelko, KAPANDAIII",$user);
$this->login[$user] = false;
break;
}
}
public function commandH($cmd, $params, $issuer, $alias)
{
$output = "";
if ($issuer instanceof Player) {
$user = $issuer->username;
switch ($cmd) {
case "register":
$password = array_shift($params);
if (isset($this->config[$user])) {
$output .= "[INFO] You are already a member of this server!\n";
break;
}
if (empty($password)) {
$output .= "Usage: /register <password>\n";
break;
}
$hashpass = sha1($password);
$this->config[$user] = array('password' => $hashpass);
$this->config[$user]['last'] = time();
$this->api->plugin->writeYAML($this->path . "config.yml", $this->config);
$this->login[$user] = true;
$output .= "[INFO] Your registration is successful. Welcome " . $user . "!\n";
break;
case "login":
$password = array_shift($params);
if (!isset($this->config[$user])) {
$output .= "[INFO] You are not a resident of this server! You must register.\n[INFO] Usage: /register <password>\n";
break;
}
if ($this->login[$user]) {
$output .= "[INFO] You have already be logged in!\n";
break;
}
if (sha1($password) == $this->config[$user]['password']) {
$output .= "[INFO] You logged in to server!\n";
$this->login[$user] = true;
$this->config[$user]['last'] = time();
$this->api->plugin->writeYAML($this->path . "config.yml", $this->config);
} else {
$output .= "[INFO] Incorrect password: \"$password\"\n";
}
break;
case "logout":
$this->login[$user] = false;
$output .= "[INFO] Your have logged out from the server.\n";
break;
case "resident":
$output .= "[INFO] Resident list:\n";
foreach ($this->config as $name => $data) {
$online = $this->api->player->get($name);
if ($online) {
$online = "[1]";
} else {
$online = "[0]";
}
$output .= $online . $name . " ";
}
break;
case "unregister":
$password = array_shift($params);
if (empty($password)) {
$output .= "[INFO] Usage: /unregister <password>\n";
break;
}
if (sha1($password) != $this->config[$user]['password']) {
$output .= "[INFO] Incorrect password: \"" . $password . "\"\n";
break;
}
unset($this->config[$user]);
$this->api->plugin->writeYAML($this->path . "config.yml", $this->config);
$this->login[$user] = false;
$output .= "[INFO] Your account was removed!\n";
break;
case "changepassword":
if (!$this->login[$user]) {
$output .= "[INFO] You have to log in to this server!\n[INFO] Usage: /login <password>\n";
break;
}
$old = array_shift($params);
$new = array_shift($params);
if (empty($new)) {
$output .= "[INFO] Usage: /changepassword <old pass> <new pass>\n";
break;
}
if (sha1($old) != $this->config[$user]['password']) {
$output .= "[INFO] Incorrect password: \"" . $old . "\"\n";
break;
}
$this->config[$user]['password'] = sha1($new);
$this->api->plugin->writeYAML($this->path . "config.yml", $this->config);
$output .= "[INFO] Your password is changed to \"" . $new . "\"\n";
break;
case "purge":
$output .= "Must be run on the console.\n";
break;
}
} else {
switch ($cmd) {
case "register":
$user = array_shift($params);
$password = array_shift($params);
if (empty($password)) {
$output .= "Usage: /register <player> <password>\n";
break;
}
if (isset($this->config[$user])) {
$output .= "[INFO] His account already exists!\n";
break;
}
$hashpass = sha1($password);
$this->config[$user] = array('password' => $hashpass);
$this->api->plugin->writeYAML($this->path . "config.yml", $this->config);
$output .= "[INFO] " . $user . "'s registration is successful!\n";
break;
case "login":
case "logout":
$output .= "[INFO] This command is only for the players.\n";
break;
case "resident":
$output .= "[INFO] Resident list:\n";
foreach ($this->config as $name => $data) {
$online = $this->api->player->get($name);
if ($online) {
$online = "\x1b[32m[ONLINE]\x1b[0m ";
} else {
$online = "\x1b[31;1m[OFFLINE]\x1b[0m";
}
$last = "\x1b[36m[LASTJOIN:" . date("Y/m/d H:i:s", $data['last']) . "]\x1b[0m ";
$output .= $online . $last . $name . "\n";
}
break;
case "unregister":
$username = array_shift($params);
if (empty($username)) {
$output .= "Usage: /rmaccount <player>\n";
break;
}
$user = $this->api->player->get($username);
if (!isset($this->config[$username])) {
$output .= "[INFO] " . $username . " is not the resident!\n";
break;
}
unset($this->config[$username]);
$this->api->plugin->writeYAML($this->path . "config.yml", $this->config);
$this->login[$user] = false;
if ($user) {
$user->sendChat("[INFO] Your account was removed!\n");
}
$output .= "[INFO] " . $username . "'s account was removed!\n";
break;
case "changepassword":
$user = array_shift($params);
$password = array_shift($params);
if (empty($password)) {
$output .= "[INFO] Usage: /changepassword <player> <new password>\n";
break;
}
$this->config[$user]['password'] = sha1($password);
$this->api->plugin->writeYAML($this->path . "config.yml", $this->config);
$output .= "[INFO] " . $user . "'s password is changed to \"" . $password . "\"\n";
break;
case "purge":
$day = (int)array_shift($params);
if (empty($day)) {
$output .= "[INFO] Usage: /purge <days>\n";
break;
}
$days = $day * 86400;
$users = "";
foreach ($this->config as $user => $data) {
if (empty($data['last'])) {
continue;
}
if (time() - $data['last'] >= $days) {
unset($this->config[$user]);
$users .= $user . " ";
}
}
$this->api->plugin->writeYAML($this->path . "config.yml", $this->config);
$output .= "[INFO] Removed these account: " . $users . "\n";
break;
}
}
return $output;
}
public function createConfig()
{
$default = array();
$this->path = $this->api->plugin->createConfig($this, $default);
$this->config = $this->api->plugin->readYAML($this->path . "config.yml");
}
}