PageRenderTime 56ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/SkyBlockPE/SkyBlockPE-master/src/thebigsmileXD/SkyBlock/SkyBlock.php

https://gitlab.com/Skull3x/WorkingInProgress-Plugins-Sourcecode-For-Dev
PHP | 413 lines | 233 code | 41 blank | 139 comment | 21 complexity | da2c5f7fcff38e70d2e1ba0896e19ca4 MD5 | raw file
  1. <?php
  2. namespace thebigsmileXD\SkyBlock;
  3. use thebigsmileXD\SkyBlock\provider\EconomySProvider;
  4. use thebigsmileXD\SkyBlock\provider\PocketMoneyProvider;
  5. use thebigsmileXD\SkyBlock\task\ClearPlotTask;
  6. use pocketmine\event\block\BlockBreakEvent;
  7. use pocketmine\event\block\BlockEvent;
  8. use pocketmine\event\block\BlockPlaceEvent;
  9. use pocketmine\event\block\BlockUpdateEvent;
  10. use pocketmine\event\level\LevelLoadEvent;
  11. use pocketmine\event\level\LevelUnloadEvent;
  12. use pocketmine\event\Listener;
  13. use pocketmine\level\generator\biome\Biome;
  14. use pocketmine\level\Position;
  15. use pocketmine\plugin\PluginBase;
  16. use pocketmine\level\generator\Generator;
  17. use pocketmine\event\EventPriority;
  18. use pocketmine\plugin\MethodEventExecutor;
  19. use pocketmine\Player;
  20. use thebigsmileXD\SkyBlock\provider\DataProvider;
  21. use pocketmine\utils\Config;
  22. use pocketmine\utils\TextFormat;
  23. use pocketmine\level\Level;
  24. use thebigsmileXD\SkyBlock\provider\SQLiteDataProvider;
  25. use thebigsmileXD\SkyBlock\provider\EconomyProvider;
  26. class SkyBlock extends PluginBase implements Listener{
  27. /** @var thebigsmileXD\SkyBlock */
  28. private static $instance;
  29. /** @var PlotLevelSettings[] */
  30. private $levels = [];
  31. /** @var DataProvider */
  32. private $dataProvider;
  33. /** @var EconomyProvider */
  34. private $economyProvider;
  35. /**
  36. * @api
  37. *
  38. * @return thebigsmileXD\SkyBlock
  39. */
  40. public static function getInstance(){
  41. return self::$instance;
  42. }
  43. /**
  44. * Returns the DataProvider that is being used
  45. *
  46. * @api
  47. *
  48. * @return DataProvider
  49. */
  50. public function getProvider(){
  51. return $this->dataProvider;
  52. }
  53. /**
  54. * Returns the EconomyProvider that is being used
  55. *
  56. * @api
  57. *
  58. * @return EconomyProvider
  59. */
  60. public function getEconomyProvider(){
  61. return $this->economyProvider;
  62. }
  63. /**
  64. * Returns a PlotLevelSettings object which contains all the settings of a level
  65. *
  66. * @api
  67. *
  68. * @param string $levelName
  69. * @return PlotLevelSettings|null
  70. */
  71. public function getLevelSettings($levelName){
  72. if(isset($this->levels[$levelName])){
  73. return $this->levels[$levelName];
  74. }
  75. return null;
  76. }
  77. /**
  78. * Checks if a plot level is loaded
  79. *
  80. * @api
  81. *
  82. * @param string $levelName
  83. * @return bool
  84. */
  85. public function isLevelLoaded($levelName){
  86. return isset($this->levels[$levelName]);
  87. }
  88. /**
  89. * Generate a new plot level with optional settings
  90. *
  91. * @api
  92. *
  93. * @param string $levelName
  94. * @param array $settings
  95. * @return bool
  96. */
  97. public function generateLevel($levelName, $settings = []){
  98. if($this->getServer()->isLevelGenerated($levelName) === true){
  99. return false;
  100. }
  101. if(empty($settings)){
  102. $settings = $this->getConfig()->get("DefaultWorld");
  103. }
  104. $settings = ["preset" => json_encode($settings)];
  105. return $this->getServer()->generateLevel($levelName, null, SkyBlockGenerator::class, $settings);
  106. }
  107. /**
  108. * Saves provided plot if changed
  109. *
  110. * @api
  111. *
  112. * @param Plot $plot
  113. * @return bool
  114. */
  115. public function savePlot(Plot $plot){
  116. return $this->dataProvider->savePlot($plot);
  117. }
  118. /**
  119. * Get all the plots a player owns (in a certain level if $levelName is provided)
  120. *
  121. * @api
  122. *
  123. * @param string $username
  124. * @param string $levelName
  125. * @return Plot[]
  126. */
  127. public function getPlotsOfPlayer($username, $levelName = ""){
  128. return $this->dataProvider->getPlotsByOwner($username, $levelName);
  129. }
  130. /**
  131. * Get the next free plot in a level
  132. *
  133. * @api
  134. *
  135. * @param string $levelName
  136. * @param int $limitXZ
  137. * @return Plot|null
  138. */
  139. public function getNextFreePlot($levelName, $limitXZ = 20){
  140. return $this->dataProvider->getNextFreePlot($levelName, $limitXZ);
  141. }
  142. /**
  143. * Finds the plot at a certain position or null if there is no plot at that position
  144. *
  145. * @api
  146. *
  147. * @param Position $position
  148. * @return Plot|null
  149. */
  150. public function getPlotByPosition(Position $position){
  151. $x = $position->x;
  152. $z = $position->z;
  153. $levelName = $position->level->getName();
  154. $plotLevel = $this->getLevelSettings($levelName);
  155. if($plotLevel === null){
  156. return null;
  157. }
  158. $chunks = $plotLevel->chunks;
  159. $roadWidth = 0;
  160. $totalSize = $chunks + $roadWidth;
  161. if($x >= 0){
  162. $X = floor($x / $totalSize);
  163. $difX = $x % $totalSize;
  164. }
  165. else{
  166. $X = ceil(($x - $chunks + 1) / $totalSize);
  167. $difX = abs(($x - $chunks + 1) % $totalSize);
  168. }
  169. if($z >= 0){
  170. $Z = floor($z / $totalSize);
  171. $difZ = $z % $totalSize;
  172. }
  173. else{
  174. $Z = ceil(($z - $chunks + 1) / $totalSize);
  175. $difZ = abs(($z - $chunks + 1) % $totalSize);
  176. }
  177. if(($difX > $chunks - 1) or ($difZ > $chunks - 1)){
  178. return null;
  179. }
  180. return $this->dataProvider->getPlot($levelName, $X, $Z);
  181. }
  182. /**
  183. * Get the begin position of a plot
  184. *
  185. * @api
  186. *
  187. * @param Plot $plot
  188. * @return Position|null
  189. */
  190. public function getPlotPosition(Plot $plot){
  191. $plotLevel = $this->getLevelSettings($plot->levelName);
  192. if($plotLevel === null){
  193. return null;
  194. }
  195. $chunks = $plotLevel->chunks;
  196. $roadWidth = 0;
  197. $totalSize = $chunks * 16 + $roadWidth;
  198. $x = $totalSize * $plot->X;
  199. $z = $totalSize * $plot->Z;
  200. $level = $this->getServer()->getLevelByName($plot->levelName);
  201. return new Position($x, 40, $z, $level);
  202. }
  203. /**
  204. * Teleport a player to a plot
  205. *
  206. * @api
  207. *
  208. * @param Player $player
  209. * @param Plot $plot
  210. * @return bool
  211. */
  212. public function teleportPlayerToPlot(Player $player, Plot $plot){
  213. $plotLevel = $this->getLevelSettings($plot->levelName);
  214. if($plotLevel === null){
  215. return false;
  216. }
  217. $pos = $this->getPlotPosition($plot);
  218. $chunks = $plotLevel->chunks;
  219. $pos->x += floor($chunks / 2);
  220. $pos->z -= 1;
  221. $pos->y += 1;
  222. $player->teleport($pos);
  223. return true;
  224. }
  225. /**
  226. * Reset all the blocks inside a plot
  227. *
  228. * @api
  229. *
  230. * @param Plot $plot
  231. * @param Player $issuer
  232. * @param int $maxBlocksPerTick
  233. * @return bool
  234. */
  235. public function clearPlot(Plot $plot, Player $issuer = null, $maxBlocksPerTick = 256){
  236. if(!$this->isLevelLoaded($plot->levelName)){
  237. return false;
  238. }
  239. $task = new ClearPlotTask($this, $plot, $issuer, $maxBlocksPerTick);
  240. $task->onRun(0);
  241. return true;
  242. }
  243. /**
  244. * Delete the plot data
  245. *
  246. * @param Plot $plot
  247. * @return bool
  248. */
  249. public function disposePlot(Plot $plot){
  250. return $this->dataProvider->deletePlot($plot);
  251. }
  252. /**
  253. * Clear and dispose a plot
  254. *
  255. * @param Plot $plot
  256. * @return bool
  257. */
  258. public function resetPlot(Plot $plot){
  259. if($this->disposePlot($plot)){
  260. return $this->clearPlot($plot);
  261. }
  262. return false;
  263. }
  264. /**
  265. * Changes the biome of a plot
  266. *
  267. * @api
  268. *
  269. * @param Plot $plot
  270. * @param Biome $biome
  271. * @return bool
  272. */
  273. public function setPlotBiome(Plot $plot, Biome $biome){
  274. $plotLevel = $this->getLevelSettings($plot->levelName);
  275. if($plotLevel === null){
  276. return false;
  277. }
  278. $level = $this->getServer()->getLevelByName($plot->levelName);
  279. $pos = $this->getPlotPosition($plot);
  280. $chunks = $plotLevel->chunks;
  281. $xMax = $pos->x + $chunks;
  282. $zMax = $pos->z + $chunks;
  283. $chunkIndexes = [];
  284. for($x = $pos->x; $x < $xMax; $x++){
  285. for($z = $pos->z; $z < $zMax; $z++){
  286. $index = Level::chunkHash($x >> 4, $z >> 4);
  287. if(!in_array($index, $chunkIndexes)){
  288. $chunkIndexes[] = $index;
  289. }
  290. $color = $biome->getColor();
  291. $R = $color >> 16;
  292. $G = ($color >> 8) & 0xff;
  293. $B = $color & 0xff;
  294. $level->setBiomeColor($x, $z, $R, $G, $B);
  295. }
  296. }
  297. foreach($chunkIndexes as $index){
  298. Level::getXZ($index, $X, $Z);
  299. $chunk = $level->getChunk($X, $Z);
  300. foreach($level->getChunkPlayers($X, $Z) as $player){
  301. $player->onChunkChanged($chunk);
  302. }
  303. }
  304. $plot->biome = $biome->getName();
  305. $this->dataProvider->savePlot($plot);
  306. return true;
  307. }
  308. /**
  309. * Returns the PlotLevelSettings of all the loaded levels
  310. *
  311. * @api
  312. *
  313. * @return string[]
  314. */
  315. public function getPlotLevels(){
  316. return $this->levels;
  317. }
  318. /* -------------------------- Non-API part -------------------------- */
  319. public function onEnable(){
  320. self::$instance = $this;
  321. $folder = $this->getDataFolder();
  322. if(!is_dir($folder)){
  323. mkdir($folder);
  324. }
  325. if(!is_dir($folder . "worlds")){
  326. mkdir($folder . "worlds");
  327. }
  328. Generator::addGenerator(SkyBlockGenerator::class, "skyblock");
  329. $this->saveDefaultConfig();
  330. $this->reloadConfig();
  331. $this->getLogger()->info(TextFormat::GREEN . "Loading the SkyBlock Framework!");
  332. // $this->getLogger()->warning(TextFormat::YELLOW."It seems that you are running the development build of thebigsmileXD\SkyBlock! Thats cool, but it CAN be very, very buggy! Just be careful when using this plugin and report any issues to".TextFormat::GOLD." http://github.com/wiez/thebigsmileXD\SkyBlock/issues");
  333. $this->getServer()->getPluginManager()->registerEvents(new EventListener($this), $this);
  334. $this->getServer()->getCommandMap()->register(Commands::class, new Commands($this));
  335. $cacheSize = $this->getConfig()->get("PlotCacheSize");
  336. switch(strtolower($this->getConfig()->get("DataProvider"))){
  337. case "sqlite":
  338. default:
  339. $this->dataProvider = new SQLiteDataProvider($this, $cacheSize);
  340. break;
  341. }
  342. if($this->getConfig()->get("UseEconomy") == true){
  343. if($this->getServer()->getPluginManager()->getPlugin("EconomyAPI") !== null){
  344. $this->economyProvider = new EconomySProvider();
  345. }
  346. elseif(($plugin = $this->getServer()->getPluginManager()->getPlugin("PocketMoney")) !== null){
  347. $this->economyProvider = new PocketMoneyProvider($plugin);
  348. }
  349. else{
  350. $this->economyProvider = null;
  351. }
  352. }
  353. else{
  354. $this->economyProvider = null;
  355. }
  356. }
  357. public function addLevelSettings($levelName, PlotLevelSettings $settings){
  358. $this->levels[$levelName] = $settings;
  359. }
  360. public function unloadLevelSettings($levelName){
  361. if(isset($this->levels[$levelName])){
  362. unset($this->levels[$levelName]);
  363. return true;
  364. }
  365. return false;
  366. }
  367. public function onDisable(){
  368. $this->dataProvider->close();
  369. $this->getLogger()->info(TextFormat::GREEN . "Saving islands");
  370. $this->getLogger()->info(TextFormat::BLUE . "Disabled the SkyBlock framework!");
  371. }
  372. }