PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Leet/LeetGamePvP/command/ArenaCommand.php

https://gitlab.com/ProjectInfinity/LeetGamePvP
PHP | 222 lines | 152 code | 64 blank | 6 comment | 19 complexity | 917a72f20a015f4dcb9bad0ff7519def MD5 | raw file
  1. <?php
  2. namespace Leet\LeetGamePvP\command;
  3. use Leet\LeetGamePvP\data\Arena;
  4. use Leet\LeetGamePvP\LeetGamePvP;
  5. use pocketmine\command\Command;
  6. use pocketmine\command\CommandExecutor;
  7. use pocketmine\command\CommandSender;
  8. use pocketmine\Player;
  9. use pocketmine\utils\TextFormat;
  10. class ArenaCommand implements CommandExecutor {
  11. private $plugin;
  12. private $tempData;
  13. public function __construct(LeetGamePvP $plugin) {
  14. $this->plugin = $plugin;
  15. $this->tempData = [];
  16. }
  17. public function onCommand(CommandSender $sender, Command $command, $label, array $args) {
  18. if(!$sender->isOp() or !$sender instanceof Player) {
  19. $sender->sendMessage(TextFormat::RED.'Only OPs can do that.');
  20. return true;
  21. }
  22. if(!count($args) > 0) return false;
  23. if(!isset($this->tempData[$sender->getName()])) {
  24. $this->tempData[$sender->getName()] = [
  25. 'x1' => null,
  26. 'x2' => null,
  27. 'z1' => null,
  28. 'z2' => null,
  29. 'name' => null,
  30. 'y1' => null
  31. ];
  32. }
  33. switch(strtoupper($args[0])) {
  34. case 'P1':
  35. $this->tempData[$sender->getName()]['x1'] = $sender->getX();
  36. $this->tempData[$sender->getName()]['z1'] = $sender->getZ();
  37. $this->tempData[$sender->getName()]['y1'] = $sender->getY();
  38. $sender->sendMessage(TextFormat::GREEN.'Set point 1 to X: '.$this->tempData[$sender->getName()]['x1'].' Z: '.$this->tempData[$sender->getName()]['z1']);
  39. break;
  40. case 'P2':
  41. $this->tempData[$sender->getName()]['x2'] = $sender->getX();
  42. $this->tempData[$sender->getName()]['z2'] = $sender->getZ();
  43. $sender->sendMessage(TextFormat::GREEN.'Set point 2 to X: '.$this->tempData[$sender->getName()]['x2'].' Z: '.$this->tempData[$sender->getName()]['z2']);
  44. break;
  45. case 'CREATE':
  46. if(count($args) < 2) {
  47. $sender->sendMessage(TextFormat::RED.'You need to enter the name for the arena.');
  48. return true;
  49. }
  50. # Set the name of the arena.
  51. $this->tempData[$sender->getName()]['name'] = $args[1];
  52. # Return because some data is probably missing!
  53. if(!$this->plugin->getArenaManager()->createArena($this->tempData[$sender->getName()])) {
  54. $sender->sendMessage(TextFormat::RED.'Failed to create arena. Did you specify position 1, position 2 and the arena name?');
  55. return true;
  56. }
  57. # Attempt to load the newly created arena into memory.
  58. if(!$this->plugin->getArenaManager()->loadArena($this->tempData[$sender->getName()]['name'])) {
  59. $sender->sendMessage(TextFormat::RED.'Failed to load arena! Arena is null.');
  60. return true;
  61. }
  62. $sender->sendMessage(TextFormat::GREEN.'Successfully created and loaded the arena.');
  63. unset($this->tempData[$sender->getName()]);
  64. break;
  65. case 'DELETE':
  66. if(count($args) < 2) {
  67. $sender->sendMessage(TextFormat::RED.'You need to enter the name of the arena you wish to delete.');
  68. return true;
  69. }
  70. if(!$this->plugin->getArenaManager()->deleteArena($args[1])) {
  71. $sender->sendMessage(TextFormat::RED.'Failed to delete the arena!');
  72. return true;
  73. }
  74. $sender->sendMessage(TextFormat::GREEN.'Successfully deleted '.$args[1]);
  75. break;
  76. case 'SPAWN':
  77. if(count($args) < 2) {
  78. $sender->sendMessage(TextFormat::RED.'You need to specify an action. Valid actions are CREATE, LIST and DELETE');
  79. return true;
  80. }
  81. switch(strtoupper($args[1])) {
  82. case 'C':
  83. case 'CREATE':
  84. /** @var Arena $arenaData */
  85. $arenaData = false;
  86. foreach($this->plugin->getArenaManager()->getArenas() as $arena) {
  87. if($arena->insideArena($sender->getX(), $sender->getZ())) $arenaData = $arena;
  88. }
  89. # Check if the player is inside an arena.
  90. if($arenaData === false) {
  91. $sender->sendMessage(TextFormat::RED.'You can only create a spawn point while in an arena.');
  92. return true;
  93. }
  94. if(!$this->plugin->getArenaManager()->createSpawn($arenaData, [
  95. 'x' => $sender->getX(),
  96. 'y' => $sender->getY(),
  97. 'z' => $sender->getZ(),
  98. 'yaw' => $sender->getYaw(),
  99. 'pitch' => $sender->getPitch()
  100. ])) {
  101. $sender->sendMessage(TextFormat::RED.'Failed to create a spawn point!');
  102. return true;
  103. }
  104. $sender->sendMessage(TextFormat::GREEN.'Successfully created a spawn at X:'.$sender->getX().' Y:'.$sender->getY().' Z:'.$sender->getZ());
  105. break;
  106. case 'L':
  107. case 'LIST':
  108. $name = null;
  109. if(count($args) < 3) {
  110. foreach($this->plugin->getArenaManager()->getArenas() as $arena) {
  111. if($arena->insideArena($sender->getX(), $sender->getZ())) {
  112. $name = $arena->getName();
  113. break;
  114. }
  115. }
  116. } else {
  117. $name = $args[2];
  118. }
  119. # If name is null then no arena has been specified.
  120. if($name === null) {
  121. $sender->sendMessage(TextFormat::RED.'You either need to specify an arena or stand inside one.');
  122. return true;
  123. }
  124. $sender->sendMessage(TextFormat::YELLOW.'Listing all spawn points for '.$name);
  125. $invert = false;
  126. foreach($this->plugin->getArenaManager()->getArena($name)->getSpawns() as $index => $spawn) {
  127. $sender->sendMessage(($invert ? TextFormat::GRAY : TextFormat::WHITE).($index + 1).' X:'.$spawn['x'].' Y:'.$spawn['y1'].' Z:'.$spawn['z']);
  128. $invert = !$invert;
  129. }
  130. break;
  131. case 'D':
  132. case 'DELETE':
  133. if(count($args) < 4) {
  134. $sender->sendMessage(TextFormat::RED.'You have to specify an arena and the spawn index.');
  135. return true;
  136. }
  137. $arena = $this->plugin->getArenaManager()->getArena($args[2]);
  138. if($arena === null) {
  139. $sender->sendMessage(TextFormat::RED.'The specified arena '.$args[2].' does not exist.');
  140. return true;
  141. }
  142. if(!ctype_digit($args[3])) {
  143. $sender->sendMessage(TextFormat::RED.'A index can only be a number. Type /arena spawn list <name> to see spawns.');
  144. return true;
  145. }
  146. $this->plugin->getArenaManager()->deleteSpawn($arena, intval($args[3])) ? $sender->sendMessage(TextFormat::GREEN.'Successfully deleted spawn point.') : $sender->sendMessage(TextFormat::RED.'Failed to delete spawn point.');
  147. break;
  148. default:
  149. $sender->sendMessage(TextFormat::RED.'Invalid action! Valid actions are CREATE, LIST and DELETE.');
  150. return true;
  151. }
  152. break;
  153. default:
  154. $sender->sendMessage(TextFormat::RED.'Invalid argument. Use p1 for point 1, p2 for point 2, create to create the arena or delete to delete the portal.');
  155. }
  156. return true;
  157. }
  158. }