/src/Leet/LeetGamePvP/command/ArenaCommand.php
PHP | 222 lines | 152 code | 64 blank | 6 comment | 19 complexity | 917a72f20a015f4dcb9bad0ff7519def MD5 | raw file
- <?php
- namespace Leet\LeetGamePvP\command;
- use Leet\LeetGamePvP\data\Arena;
- use Leet\LeetGamePvP\LeetGamePvP;
- use pocketmine\command\Command;
- use pocketmine\command\CommandExecutor;
- use pocketmine\command\CommandSender;
- use pocketmine\Player;
- use pocketmine\utils\TextFormat;
- class ArenaCommand implements CommandExecutor {
- private $plugin;
- private $tempData;
- public function __construct(LeetGamePvP $plugin) {
- $this->plugin = $plugin;
- $this->tempData = [];
- }
- public function onCommand(CommandSender $sender, Command $command, $label, array $args) {
- if(!$sender->isOp() or !$sender instanceof Player) {
- $sender->sendMessage(TextFormat::RED.'Only OPs can do that.');
- return true;
- }
- if(!count($args) > 0) return false;
- if(!isset($this->tempData[$sender->getName()])) {
- $this->tempData[$sender->getName()] = [
- 'x1' => null,
- 'x2' => null,
- 'z1' => null,
- 'z2' => null,
- 'name' => null,
- 'y1' => null
- ];
- }
- switch(strtoupper($args[0])) {
- case 'P1':
- $this->tempData[$sender->getName()]['x1'] = $sender->getX();
- $this->tempData[$sender->getName()]['z1'] = $sender->getZ();
- $this->tempData[$sender->getName()]['y1'] = $sender->getY();
- $sender->sendMessage(TextFormat::GREEN.'Set point 1 to X: '.$this->tempData[$sender->getName()]['x1'].' Z: '.$this->tempData[$sender->getName()]['z1']);
- break;
- case 'P2':
- $this->tempData[$sender->getName()]['x2'] = $sender->getX();
- $this->tempData[$sender->getName()]['z2'] = $sender->getZ();
- $sender->sendMessage(TextFormat::GREEN.'Set point 2 to X: '.$this->tempData[$sender->getName()]['x2'].' Z: '.$this->tempData[$sender->getName()]['z2']);
- break;
- case 'CREATE':
- if(count($args) < 2) {
- $sender->sendMessage(TextFormat::RED.'You need to enter the name for the arena.');
- return true;
- }
- # Set the name of the arena.
- $this->tempData[$sender->getName()]['name'] = $args[1];
- # Return because some data is probably missing!
- if(!$this->plugin->getArenaManager()->createArena($this->tempData[$sender->getName()])) {
- $sender->sendMessage(TextFormat::RED.'Failed to create arena. Did you specify position 1, position 2 and the arena name?');
- return true;
- }
- # Attempt to load the newly created arena into memory.
- if(!$this->plugin->getArenaManager()->loadArena($this->tempData[$sender->getName()]['name'])) {
- $sender->sendMessage(TextFormat::RED.'Failed to load arena! Arena is null.');
- return true;
- }
- $sender->sendMessage(TextFormat::GREEN.'Successfully created and loaded the arena.');
- unset($this->tempData[$sender->getName()]);
- break;
- case 'DELETE':
- if(count($args) < 2) {
- $sender->sendMessage(TextFormat::RED.'You need to enter the name of the arena you wish to delete.');
- return true;
- }
- if(!$this->plugin->getArenaManager()->deleteArena($args[1])) {
- $sender->sendMessage(TextFormat::RED.'Failed to delete the arena!');
- return true;
- }
- $sender->sendMessage(TextFormat::GREEN.'Successfully deleted '.$args[1]);
- break;
- case 'SPAWN':
- if(count($args) < 2) {
- $sender->sendMessage(TextFormat::RED.'You need to specify an action. Valid actions are CREATE, LIST and DELETE');
- return true;
- }
- switch(strtoupper($args[1])) {
- case 'C':
- case 'CREATE':
- /** @var Arena $arenaData */
- $arenaData = false;
- foreach($this->plugin->getArenaManager()->getArenas() as $arena) {
- if($arena->insideArena($sender->getX(), $sender->getZ())) $arenaData = $arena;
- }
- # Check if the player is inside an arena.
- if($arenaData === false) {
- $sender->sendMessage(TextFormat::RED.'You can only create a spawn point while in an arena.');
- return true;
- }
- if(!$this->plugin->getArenaManager()->createSpawn($arenaData, [
- 'x' => $sender->getX(),
- 'y' => $sender->getY(),
- 'z' => $sender->getZ(),
- 'yaw' => $sender->getYaw(),
- 'pitch' => $sender->getPitch()
- ])) {
- $sender->sendMessage(TextFormat::RED.'Failed to create a spawn point!');
- return true;
- }
- $sender->sendMessage(TextFormat::GREEN.'Successfully created a spawn at X:'.$sender->getX().' Y:'.$sender->getY().' Z:'.$sender->getZ());
- break;
- case 'L':
- case 'LIST':
- $name = null;
- if(count($args) < 3) {
- foreach($this->plugin->getArenaManager()->getArenas() as $arena) {
- if($arena->insideArena($sender->getX(), $sender->getZ())) {
- $name = $arena->getName();
- break;
- }
- }
- } else {
- $name = $args[2];
- }
- # If name is null then no arena has been specified.
- if($name === null) {
- $sender->sendMessage(TextFormat::RED.'You either need to specify an arena or stand inside one.');
- return true;
- }
- $sender->sendMessage(TextFormat::YELLOW.'Listing all spawn points for '.$name);
- $invert = false;
- foreach($this->plugin->getArenaManager()->getArena($name)->getSpawns() as $index => $spawn) {
- $sender->sendMessage(($invert ? TextFormat::GRAY : TextFormat::WHITE).($index + 1).' X:'.$spawn['x'].' Y:'.$spawn['y1'].' Z:'.$spawn['z']);
- $invert = !$invert;
- }
- break;
- case 'D':
- case 'DELETE':
- if(count($args) < 4) {
- $sender->sendMessage(TextFormat::RED.'You have to specify an arena and the spawn index.');
- return true;
- }
- $arena = $this->plugin->getArenaManager()->getArena($args[2]);
- if($arena === null) {
- $sender->sendMessage(TextFormat::RED.'The specified arena '.$args[2].' does not exist.');
- return true;
- }
- if(!ctype_digit($args[3])) {
- $sender->sendMessage(TextFormat::RED.'A index can only be a number. Type /arena spawn list <name> to see spawns.');
- return true;
- }
- $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.');
- break;
- default:
- $sender->sendMessage(TextFormat::RED.'Invalid action! Valid actions are CREATE, LIST and DELETE.');
- return true;
- }
- break;
- default:
- $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.');
- }
- return true;
- }
- }