-
Notifications
You must be signed in to change notification settings - Fork 15
/
PurePerms.php
146 lines (125 loc) · 4.55 KB
/
PurePerms.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
<?php
#Plugin By:
/*
8888888 .d8888b. .d888 888 .d8888b. .d8888b. .d8888b.
888 d88P Y88b d88P" 888 d88P Y88b d88P Y88b d88P Y88b
888 888 888 888 888 888 888 .d88P
888 888 888 8888b. 88888b. 888 888d888 8888b. 888888 888888 888d888b. .d88P 8888"
888 888 888 "88b 888 "88b 888 888P" "88b 888 888 888P "Y88b .od888P" "Y8b.
888 Y88 88P .d888888 888 888 888 888 888 .d888888 888 888 888 888 d88P" 888 888
888 Y8bd8P 888 888 888 888 Y88b d88P 888 888 888 888 Y88b. Y88b d88P 888" Y88b d88P
8888888 Y88P "Y888888 888 888 "Y8888P" 888 "Y888888 888 "Y888 "Y8888P" 888888888 "Y8888P"
*/
declare(strict_types=1);
namespace IvanCraft623\RankSystem\migrator;
use pocketmine\utils\Config;
/**
* PurePerms Migrator!
*/
class PurePerms extends Migrator {
public function getName() : string {
return "PurePerms";
}
public function canMigrate() : bool {
return is_dir($this->dataPath . "PurePerms");
}
public function hasMigrated() : bool {
return file_exists($this->dataPath . "PurePerms" . DIRECTORY_SEPARATOR . $this->plugin->getName() . "_was_here");
}
public function setMigrated(bool $value = true) : void {
$file = $this->dataPath . "PurePerms" . DIRECTORY_SEPARATOR . $this->plugin->getName() . "_was_here";
if ($value) {
file_put_contents($file, "lol");
} else {
@unlink($file);
}
}
public function migrate() : bool {
if (!$this->canMigrate()) {
return false;
}
$pperms = $this->dataPath . "PurePerms";
$success = false;
$groupsFile = $pperms . DIRECTORY_SEPARATOR . "groups.yml";
// Look for groups data...
foreach ([
$pperms . DIRECTORY_SEPARATOR . "groups.yml",
$pperms . DIRECTORY_SEPARATOR . "ranks.yml"
] as $groupsFile) {
if (file_exists($groupsFile)) {
foreach ((new Config($groupsFile, Config::YAML))->getAll() as $groupName => $data) {
$group = (string) $groupName;
if (!$this->rankManager->exists($group)) {
$nameTag = [ //Hacks! >:D
"prefix" => "§8[" . $group . "] §r",
"nameColor" => "§f"
];
$chat = $nameTag;
$chat["chatFormat"] = "§f: §7";
$permissions = $data["permissions"] ?? [];
$inheritance = $data["inheritance"] ?? [];
$this->rankManager->create($group, $nameTag, $chat, $permissions, $inheritance);
}
if ($data["isDefault"] ?? false) {
$this->rankManager->setDefault($group);
}
}
$success = true;
}
}
// Look for players data on players directory...
$playersFolder = $pperms . DIRECTORY_SEPARATOR . "players" . DIRECTORY_SEPARATOR;
if ($handle = opendir($playersFolder)) {
while (false !== ($entry = readdir($handle))) {
if ($entry !== '.' && $entry !== '..') {
$playerData = new Config($playersFolder . $entry, Config::DETECT);
$data = $playerData->getAll();
if (isset($data["userName"]) && isset($data["group"])) {
$session = $this->sessionManager->get($data["userName"]);
$rank = $this->rankManager->getRank($data["group"]);
$permissions = $data["permissions"] ?? [];
$session->onInitialize(function() use ($session, $rank, $permissions) {
if ($rank !== null) {
$session->setRank($rank);
}
if (count($permissions) !== 0) {
foreach ($permissions as $permission) {
$session->setPermission($permission);
}
}
});
}
}
}
$success = true;
}
// Look for players data on JSON and YAML files...
foreach ([
$pperms . DIRECTORY_SEPARATOR . "players.yml" => Config::YAML,
$pperms . DIRECTORY_SEPARATOR . "players.json" => Config::JSON
] as $groupsFile => $format) {
if (file_exists($groupsFile)) {
foreach ((new Config($groupsFile, $format))->getAll() as $username => $data) {
if (isset($data["group"])) {
$session = $this->sessionManager->get((string) $username);
$rank = $this->rankManager->getRank($data["group"]);
$permissions = $data["permissions"] ?? [];
$session->onInitialize(function() use ($session, $rank, $permissions) {
if ($rank !== null) {
$session->setRank($rank);
}
if (count($permissions) !== 0) {
foreach ($permissions as $permission) {
$session->setPermission($permission);
}
}
});
}
}
$success = true;
}
}
$this->setMigrated();
return $success;
}
}