/NumericRanks/src/NumericRanks/NumericRanks.php

https://gitlab.com/Skull3x/Small-ZC-Plugins · PHP · 243 lines · 137 code · 48 blank · 58 comment · 7 complexity · e2962bff08ac2a32ca6fb977e4298623 MD5 · raw file

  1. <?php
  2. namespace NumericRanks;
  3. use NumericRanks\data\Rank;
  4. use NumericRanks\data\User;
  5. use NumericRanks\provider\MysqlProvider;
  6. use NumericRanks\provider\NumRanksProvider;
  7. use NumericRanks\provider\YamlProvider;
  8. use pocketmine\IPlayer;
  9. use pocketmine\permission\Permission;
  10. use pocketmine\Player;
  11. use pocketmine\plugin\PluginBase;
  12. use pocketmine\utils\Config;
  13. /*
  14. NumericRanks v1.0.0 by PEMapModder & 64FF00 :3
  15. ## ## ## ## ## ## ######## ######## #### ###### ######## ### ## ## ## ## ###### ####
  16. ### ## ## ## ### ### ## ## ## ## ## ## ## ## ## ## ### ## ## ## ## ## ####
  17. #### ## ## ## #### #### ## ## ## ## ## ## ## ## ## #### ## ## ## ## ####
  18. ## ## ## ## ## ## ### ## ###### ######## ## ## ######## ## ## ## ## ## ##### ###### ##
  19. ## #### ## ## ## ## ## ## ## ## ## ## ## ######### ## #### ## ## ##
  20. ## ### ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ### ## ## ## ## ####
  21. ## ## ####### ## ## ######## ## ## #### ###### ## ## ## ## ## ## ## ## ###### ####
  22. */
  23. class NumericRanks extends PluginBase{
  24. /** @var array */
  25. private $attachments = [];
  26. /** @var NumRanksProvider */
  27. private $provider;
  28. /** @var Config */
  29. private $permsConfig;
  30. public function onEnable(){
  31. $this->saveDefaultConfig();
  32. $this->permsConfig = new Config($this->getDataFolder() . "perms.yml", Config::YAML);
  33. $dataProvider = $this->getConfig()->getNested("dataProvider.name", "yaml");
  34. switch($dataProvider){
  35. case "mysql":
  36. $this->getLogger()->notice("Enabling MySQL provider...");
  37. try{
  38. $this->provider = new MysqlProvider($this);
  39. break;
  40. }catch(\Exception $e){
  41. $this->getLogger()->critical("Could not connect to MySQL server: " . $e->getMessage());
  42. $this->getLogger()->notice("Changing to YAML provider");
  43. }
  44. case "yaml":
  45. case "yml":
  46. $this->getLogger()->notice("Enabling YAML provider...");
  47. $this->provider = new YamlProvider($this);
  48. break;
  49. }
  50. $this->updatePerms();
  51. $this->getServer()->getPluginManager()->registerEvents(new PlayerListener($this), $this);
  52. $this->getLogger()->info("Enabled " . $this->getDescription()->getFullName() . " with " . (new \ReflectionClass($this->provider))->getShortName() . " as data provider.");
  53. }
  54. public function onDisable(){
  55. if($this->provider instanceof NumRanksProvider){
  56. $this->provider->close();
  57. }
  58. }
  59. /*
  60. ### ######## ####
  61. ## ## ## ## ##
  62. ## ## ## ## ##
  63. ## ## ######## ##
  64. ######### ## ##
  65. ## ## ## ##
  66. ## ## ## ####
  67. */
  68. /**
  69. * @param Player $player
  70. *
  71. * @return mixed
  72. */
  73. public function getAttachment(Player $player){
  74. if(!isset($this->attachments[$player->getName()])){
  75. $this->attachments[$player->getName()] = $player->addAttachment($this);
  76. }
  77. return $this->attachments[$player->getName()];
  78. }
  79. /**
  80. * @return mixed
  81. */
  82. public function getDefaultRank(){
  83. $defaultRanks = [];
  84. foreach($this->getRanks() as $rank){
  85. if($rank->isDefault()){
  86. array_push($defaultRanks, $rank);
  87. }
  88. }
  89. switch(count($defaultRanks)){
  90. case 1:
  91. $defaultRank = $defaultRanks[0];
  92. break;
  93. default:
  94. if(count($defaultRanks) > 1){
  95. throw new \RuntimeException("More than one default rank was declared in config.yml");
  96. }else{
  97. throw new \RuntimeException("No default group was found in config.yml");
  98. }
  99. break;
  100. }
  101. return $defaultRank;
  102. }
  103. /**
  104. * @return mixed
  105. */
  106. public function getProvider(){
  107. return $this->provider;
  108. }
  109. /**
  110. * @param $rankName
  111. *
  112. * @return Rank
  113. */
  114. public function getRank($rankName){
  115. $rank = new Rank($this, $rankName);
  116. if($rank->getData() == null){
  117. throw new \RuntimeException("Rank $rankName does NOT exist");
  118. }
  119. return $rank;
  120. }
  121. /**
  122. * @return Rank[]
  123. */
  124. public function getRanks(){
  125. $ranks = [];
  126. foreach(array_keys($this->getConfig()->getNested("ranks")) as $rankName){
  127. $ranks[] = new Rank($this, $rankName);
  128. }
  129. return $ranks;
  130. }
  131. /**
  132. * @return mixed
  133. */
  134. public function getRegisteredPermissions(){
  135. return $this->permsConfig->getAll();
  136. }
  137. /**
  138. * @param IPlayer $player
  139. *
  140. * @return User
  141. */
  142. public function getUser(IPlayer $player){
  143. return new User($this, $player);
  144. }
  145. public function reload(){
  146. $this->reloadConfig();
  147. }
  148. /**
  149. * @param Player $player
  150. */
  151. public function removeAttachment(Player $player){
  152. $attachment = $this->getAttachment($player);
  153. $player->removeAttachment($attachment);
  154. unset($this->attachments[$player->getName()]);
  155. }
  156. public function removeAttachments(){
  157. $this->attachments = [];
  158. }
  159. /**
  160. * @param Player $player
  161. */
  162. public function setPermissions(Player $player){
  163. $attachment = $this->getAttachment($player);
  164. $attachment->clearPermissions();
  165. $perms = $this->getUser($player)->getPermissions();
  166. ksort($perms);
  167. $attachment->setPermissions($perms);
  168. }
  169. public function updatePerms(){
  170. foreach($this->getServer()->getPluginManager()->getPermissions() as $perm){
  171. if(!$this->permsConfig->exists($perm->getName(), true)){
  172. $this->permsConfig->set(strtolower($perm->getName()), $this->getDefaultIndex($perm->getDefault()));
  173. }
  174. }
  175. $all = $this->permsConfig->getAll();
  176. asort($all, SORT_FLAG_CASE | SORT_NATURAL);
  177. $this->permsConfig->setAll($all);
  178. }
  179. private function getDefaultIndex($default){
  180. switch($default){
  181. case Permission::DEFAULT_TRUE:
  182. return $this->getConfig()->getNested("defaultPermissionIndex.true", 0);
  183. case Permission::DEFAULT_NOT_OP:
  184. return $this->getConfig()->getNested("defaultPermissionIndex.true", 0);
  185. case Permission::DEFAULT_OP:
  186. return $this->getConfig()->getNested("defaultPermissionIndex.true", 150);
  187. case Permission::DEFAULT_FALSE:
  188. return $this->getConfig()->getNested("defaultPermissionIndex.false", 999);
  189. }
  190. throw new \UnexpectedValueException("Unexpected permission default: $default");
  191. }
  192. }