PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/SkygridRex/skywars/ArenaPlayerLocation.php

https://gitlab.com/Skull3x/SimpleSkyWars
PHP | 128 lines | 106 code | 11 blank | 11 comment | 16 complexity | bab4057ead85850ce7ece743db03f4e4 MD5 | raw file
  1. <?php
  2. namespace SkygridRex\skywars;
  3. use pocketmine\math\Vector3 as Vector3;
  4. use pocketmine\level\Position;
  5. use pocketmine\Server;
  6. use pocketmine\Player;
  7. use pocketmine\utils\Config;
  8. use pocketmine\block\Chest;
  9. use pocketmine\item\Item;
  10. class ArenaPlayerLocation {
  11. public $pgin;
  12. public $name;
  13. public $spawnLocation;
  14. public $buttonLocation;
  15. public $buttonWorlName;
  16. public $kit;
  17. public $arenaName;
  18. public $type = "defaullt";
  19. public function __construct($pg, $name, $pos, $arena) {
  20. $this->pgin = $pg;
  21. $this->name = $name;
  22. $this->spawnLocation = $pos;
  23. $this->arenaName = $arena;
  24. }
  25. public function save() {
  26. $path = $this->pgin->getDataFolder () . "arena/playerlocation/";
  27. if (! file_exists ( $path )) {
  28. // @mkdir ($this->pgin->getDataFolder());
  29. @mkdir ( $path );
  30. }
  31. $data = new Config ( $path . $this->arenaName . "_" . "$this->name.yml", Config::YAML );
  32. // this should not happen
  33. $data->set ( "name", $this->name );
  34. $data->set ( "arenaName", $this->arenaName );
  35. $data->set ( "type", $this->type );
  36. $data->set ( "button_worldName", $this->buttonWorlName );
  37. if ($this->buttonLocation != null) {
  38. $data->set ( "buttonX", $this->buttonLocation->x );
  39. $data->set ( "buttonY", $this->buttonLocation->y );
  40. $data->set ( "buttonZ", $this->buttonLocation->z );
  41. }
  42. if ($this->spawnLocation != null) {
  43. $data->set ( "spawnX", $this->spawnLocation->x );
  44. $data->set ( "spawnY", $this->spawnLocation->y );
  45. $data->set ( "spawnZ", $this->spawnLocation->z );
  46. }
  47. $data->save ();
  48. $this->log ( " saved - " . $path . "$this->name.yml" );
  49. }
  50. public function testSave() {
  51. $this->name = "player1";
  52. $this->arenaName = "test world";
  53. $this->spawnLocation = new Vector3 ( 128, 128, 128 );
  54. $this->save ();
  55. $this->log ( " saved player location ");
  56. }
  57. public function delete() {
  58. $path = $this->pgin->getDataFolder () . "arena/playerlocation/";
  59. $name = $this->name;
  60. @unlink ( $path . "$name.yml" );
  61. }
  62. public static function loadPlayerLocation($plugin) {
  63. $path = $plugin->getDataFolder () . "arena/playerlocation/";
  64. if (! file_exists ( $path )) {
  65. @mkdir ( $this->pgin->getDataFolder () );
  66. @mkdir ( $path );
  67. // nothing to load
  68. return;
  69. }
  70. $plugin->getLogger()->info ( "loading player Location on " . $path );
  71. $playerLocations = [ ];
  72. $handler = opendir ( $path );
  73. while ( ($filename = readdir ( $handler )) !== false ) {
  74. //$plugin->getLogger()->info ( "file - " . $filename );
  75. //skip sub folders
  76. if (is_dir($filename)) {
  77. continue;
  78. }
  79. if ($filename != "." && $filename != "..") {
  80. $plugin->getLogger()->info ( "file - " . $filename );
  81. $data = new Config ( $path . $filename, Config::YAML );
  82. $xname = $data->get ( "name" );
  83. $plugin->getLogger()->info($xname);
  84. // load levels
  85. //Server::getInstance ()->loadLevel ( $xname );
  86. //if (($pLevel = Server::getInstance ()->getLevelByName ( $xname )) === null)
  87. // continue;
  88. $name = str_replace ( ".yml", "", $filename );
  89. $spawnLocation = null;
  90. if ($data->get ( "spawnX" ) != null) {
  91. $spawnLocation = new Position ( $data->get ( "spawnX" ), $data->get ( "spawnY" ), $data->get ( "spawnZ" ));
  92. }
  93. $buttonLocation = null;
  94. if ($data->get ( "buttonX" ) != null) {
  95. $buttonLocation = new Position ( $data->get ( "buttonX" ), $data->get ( "buttonY" ), $data->get ( "buttonZ" ));
  96. }
  97. $buttonWorldName = $data->get ( "button_worldName" );
  98. $arenaName = $data->get ( "arenaName" );
  99. $type = $data->get ( "type" );
  100. //save
  101. $key=$data->get ( "spawnX" )." ".$data->get ( "spawnY" )." ".$data->get ( "spawnZ" );
  102. //$plugin->getLogger()->info($key);
  103. $arenaLocation = new ArenaPlayerLocation ( $plugin, $name, $spawnLocation, $arenaName );
  104. $arenaLocation->type = $type;
  105. $arenaLocation->buttonWorlName = $buttonWorldName;
  106. $arenaLocation->buttonLocation = $buttonLocation;
  107. $playerLocations [$name] = $arenaLocation;
  108. $plugin->getLogger()->info ( "arena player locations: " . count ( $playerLocations ) );
  109. }
  110. }
  111. closedir ( $handler );
  112. return $playerLocations;
  113. }
  114. private function log($msg) {
  115. $this->pgin->getLogger ()->info ( $msg );
  116. }
  117. }