Advertisement
Guest User

BedWars Main.php

a guest
Feb 12th, 2016
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 34.06 KB | None | 0 0
  1. <?php
  2.  
  3. namespace BedWars;
  4.  
  5. //PM use
  6. use pocketmine\math\Vector3;
  7. use pocketmine\block\Block;
  8. use pocketmine\command\Command;
  9. use pocketmine\command\CommandSender;
  10. use pocketmine\command\ConsoleCommandSender;
  11. use pocketmine\IPlayer;
  12. use pocketmine\utils\Config;
  13. use pocketmine\permission\PermissionAttachment;
  14. use pocketmine\permission\Permission;
  15. use pocketmine\Player;
  16. use pocketmine\Server;
  17. use pocketmine\plugin\Plugin;
  18. use pocketmine\plugin\PluginBase;
  19. use pocketmine\utils\TextFormat;
  20. use pocketmine\level\Level;
  21. use pocketmine\level\Position;
  22. use pocketmine\level\particle\FloatingTextParticle;
  23. use pocketmine\item\Item;
  24. use pocketmine\tile\Tile;
  25. use pocketmine\tile\Sign;
  26. use pocketmine\tile\Chest;
  27. use pocketmine\nbt\tag\Byte;
  28. use pocketmine\nbt\tag\Compound;
  29. use pocketmine\nbt\tag\Double;
  30. use pocketmine\nbt\tag\Enum;
  31. use pocketmine\nbt\tag\Short;
  32. use pocketmine\nbt\tag\String;
  33. use pocketmine\event\player\PlayerRespawnEvent;
  34. //BedWars use
  35. use BedWars\EventListener;
  36. use BedWars\PopupInfo;
  37. use BedWars\TPTask;
  38. use BedWars\ExecuteTask;
  39. //Tools for shopping
  40. use pocketmine\inventory\PlayerInventory;
  41. use pocketmine\inventory\ChestInventory;
  42. use pocketmine\inventory\CustomInventory;
  43. use pocketmine\inventory\InventoryType;
  44. use pocketmine\entity\Entity;
  45. use pocketmine\entity\Villager;
  46. use pocketmine\entity\Item as EntityItem;
  47. use pocketmine\entity\Effect;
  48.  
  49. class BuyingInventory extends CustomInventory
  50. {
  51. protected $client;
  52.  
  53. public function __construct($holder, $client)
  54. {
  55. $this->client = $client;
  56. parent::__construct($holder, InventoryType::get(InventoryType::CHEST), [], null, "");
  57. }
  58.  
  59. public function getClient()
  60. {
  61. return $this->client;
  62. }
  63. }
  64.  
  65. class BedWarsGameTeam
  66. {
  67. public $name = 0;
  68. public $bed = 0;
  69. public $spawn = 0;
  70. public $players = 0;
  71. public $bedStatus = 1;
  72.  
  73. function __construct($name = "", $Bed = null, $Spawn = null)
  74. {
  75. $this->name = $name;
  76. $this->bed = is_null($Bed) ? Array(new Vector3(0, 0, 0), new Vector3(0, 0, 0), 0) : $Bed;
  77. $this->spawn = is_null($Spawn) ? new Vector3(0, 0, 0) : $Spawn;
  78. $this->players = Array();
  79. $this->bedStatus = 1;
  80. }
  81. };
  82.  
  83. class BedWarsGame
  84. {
  85. public $blocksPlaced = Array();
  86. public $level = 0;
  87. public $lobby = 0;
  88. public $plugin = 0;
  89. public $levelData = 0;
  90. public $teams = Array();
  91. public $status = 0;
  92. public $spawnTasks = Array();
  93.  
  94. /** @var PopupInfo */
  95. public $popupInfo, $popupInfo2;
  96.  
  97. function __construct(Level $Level, BedWars $plugin)
  98. {
  99. $this->level = $Level;
  100. $this->plugin = $plugin;
  101. $this->levelData = (new Config($plugin->getDataFolder() . "levels/" . $Level->getFolderName() . ".yml"))->getAll();
  102. $this->level_name = $Level->getFolderName();
  103. $this->popupInfo = new PopupInfo($this->plugin, $Level, 1);
  104. $this->popupInfo->rows = Array();
  105. foreach ($this->levelData["teams"] as $name => $team) {
  106. /*$this->PopupInfo->Rows[$name] = "[".$this->plugin->teamColorName($name)."] = 0";*/
  107. };
  108. $this->popupInfo2 = new PopupInfo($this->plugin, $Level, 0);
  109. $this->popupInfo2->playersData = Array();
  110. $Level->setAutoSave(false);
  111. $Level->setTime(6000);
  112. $Level->stopTime();
  113. $this->initBlocks();
  114. }
  115.  
  116. public function initBlocks()
  117. {
  118. foreach ($this->levelData["teams"] as $name => $team) {
  119. $Bed = explode(" ", $team["bed"]);
  120. $Spawn = explode(" ", $team["spawn"]);
  121. $team = $this->teams[$name] = new BedWarsGameTeam($name, Array(new Vector3($Bed[0], $Bed[1], $Bed[2]), new Vector3($Bed[3], $Bed[4], $Bed[5]), $Bed[6]), new Vector3($Spawn[0] - 0.5, $Spawn[1], $Spawn[2] + 0.5));
  122. $this->level->setBlock(new Position($Bed[0], $Bed[1], $Bed[2]), Block::get(26, 8 + $Bed[6]), false, true);
  123. $this->level->setBlock(new Position($Bed[3], $Bed[4], $Bed[5]), Block::get(26, $Bed[6]), false, true);
  124. }
  125.  
  126. foreach ($this->levelData["spawners"] as $i => $spawner) {
  127. $spawner = explode(" ", $spawner);
  128. $type = $spawner[0];
  129. $x = $spawner[1];
  130. $y = $spawner[2];
  131. $z = $spawner[3];
  132. $pos = new Vector3($x, $y, $z);
  133. switch ($this->plugin->spawner_mode) {
  134. case 0:
  135. $this->level->setBlock($pos, Block::get(0, 0), true, true);
  136. break;
  137. case 1:
  138. $this->level->setBlock($pos, Block::get(54, 0), true, true);
  139. $chest = new Chest($this->level->getChunk($pos->getX() >> 4, $pos->getZ() >> 4, true), new Compound(false, array(new Int("x", $pos->getX()), new Int("y", $pos->getY()), new Int("z", $pos->getZ()), new String("id", Tile::CHEST))));
  140. $this->level->addTile($chest);
  141. break;
  142. }
  143. }
  144.  
  145. foreach ($this->level->getEntities() as $Entity)
  146. if ($Entity instanceof Villager) {
  147. for ($i = 0; $i < 10; $i++) {
  148. $X = round($Entity->getX() - 0.5);
  149. $Z = round($Entity->getZ() - 0.5);
  150. if ($this->level->getBlockIdAt($X, 0, $Z) != 54) {
  151. $this->level->setBlock(new Vector3($X, $i, $Z), Block::get(54), true, true);
  152. $chest = new Chest($this->level->getChunk($X >> 4, $Z >> 4, true), new Compound(false, array(new Int("x", $X), new Int("y", $i), new Int("z", $Z), new String("id", Tile::CHEST))), $this->plugin);
  153. $this->level->addTile($chest);
  154. }
  155. if ($this->level->getBlockIdAt($X, $i + 1, $Z) != 54)
  156. $this->level->setBlock(new Vector3($X, $i + 1, $Z), Block::get(0), true, true);
  157. }
  158. }
  159. }
  160.  
  161. public function PlaceBlock(Block $Block, Player $Placer)
  162. {
  163. if (in_array($Block->getId(), [51]))
  164. return true;
  165. $this->blocksPlaced[] = new Vector3($Block->getX(), $Block->getY(), $Block->getZ());
  166. return false;
  167. }
  168.  
  169. public function DestroyBlock(Block $Block, Player $Destroyer)
  170. {
  171. $X = $Block->GetX();
  172. $Y = $Block->GetY();
  173. $Z = $Block->GetZ();
  174. if ($Team = $this->getTeamByPlayer($Destroyer)) {
  175. if ((($X == $Team->Bed[0]->getX()) && ($Y == $Team->Bed[0]->getY()) && ($Z == $Team->Bed[0]->getZ())) || (($X == $Team->Bed[1]->getX()) && ($Y == $Team->Bed[1]->getY()) && ($Z == $Team->Bed[1]->getZ())))
  176. return true;
  177. }
  178. foreach ($this->teams as $name => $Team) {
  179. if ((($X == $Team->Bed[0]->getX()) && ($Y == $Team->Bed[0]->getY()) && ($Z == $Team->Bed[0]->getZ())) || (($X == $Team->Bed[1]->getX()) && ($Y == $Team->Bed[1]->getY()) && ($Z == $Team->Bed[1]->getZ()))) {
  180. $Team->BedStatus = 0;
  181. $this->sendMessageToAll(TextFormat::RED . $this->plugin->getMessage("bedwars.team.destroyed", $name));
  182. $this->updateTeams();
  183. $this->level->setBlock(new Vector3($Team->Bed[0]->getX(), $Team->Bed[0]->getY(), $Team->Bed[0]->getZ()), Block::get(0), true, true);
  184. $this->level->setBlock(new Vector3($Team->Bed[1]->getX(), $Team->Bed[1]->getY(), $Team->Bed[1]->getZ()), Block::get(0), true, true);
  185. return false;
  186. }
  187. }
  188. foreach ($this->blocksPlaced as $Block)
  189. if (($X == $Block->getX()) && ($Y == $Block->getY()) && ($Z == $Block->getZ()))
  190. return false;
  191. return true;
  192. }
  193.  
  194. public function Spawn(PlayerRespawnEvent $Event)
  195. {
  196. $Player = $Event->getPlayer();
  197. $Team = $this->getTeamByPlayer($Player);
  198. if ((!is_null($Team)) && ($Team->BedStatus == 0)) {
  199. if (($i = array_search($Player, $Team->Players)) !== false)
  200. array_splice($Team->Players, $i, 1);
  201. }
  202. if ((is_null($Team)) || ($Team->BedStatus == 0)) {
  203. $this->plugin->setState("teleport", $Player, false);
  204. ExecuteTask::Execute($this->plugin, function () use ($Player) {
  205. $this->plugin->setState("teleport", $Player, false);
  206. $Player->teleport($this->plugin->lobby_spawn);
  207. }, 1);
  208. if ($Player->getGamemode() != Player::SURVIVAL)
  209. $Player->setGamemode(Player::SURVIVAL);
  210. unset($this->popupInfo2->playersData[strtolower($Player->getName())]);
  211. $Player->sendMessage($this->plugin->getMessage("bedwars.stop.loose"));
  212. } else {
  213. ExecuteTask::Execute($this->plugin, function () use ($Player, $Team) {
  214. $this->plugin->setState("teleport", $Player, false);
  215. $Player->teleport(new Position($Team->Spawn->getX(), $Team->Spawn->getY(), $Team->Spawn->getZ(), $this->level));
  216. }, 1);
  217. if ($Player->getGamemode() != Player::SURVIVAL) $Player->setGamemode(Player::SURVIVAL);
  218. }
  219. }
  220.  
  221. public function spliceTeams()
  222. {
  223. foreach ($this->teams as $name => $Team)
  224. if (($this->status == 1) && (count($Team->Players) == 0))
  225. unset($this->teams[$name]);
  226. }
  227.  
  228. public function updateTeams()
  229. {
  230. $this->popupInfo->rows = [];
  231. $i = 0;
  232. $Msg = [];
  233. foreach ($this->teams as $name => $Team) {
  234. foreach ($Team->Players as $i => $Player)
  235. if ($Player->getLevel() != $this->level)
  236. array_splice($Team->Players, $i, 1);
  237. if (($this->status == 1) && (count($Team->Players) == 0)) {
  238. $this->sendMessageToAll(TextFormat::RED . $this->plugin->getMessage("bedwars.team.terminated", $this->plugin->teamColorName($Team->name)));
  239. unset($this->teams[$name]);
  240. $this->updateTeams();
  241. return;
  242. } else if ($Team->BedStatus == 0) {
  243. $Msg [] = "✘ " . $this->plugin->teamColorName($name) . TextFormat::RESET . " = " . count($Team->Players);
  244. if (count($Msg) == 3) {
  245. $this->popupInfo->rows [] = implode(" ", $Msg);
  246. $Msg = [];
  247. }
  248. } else {
  249. $Msg [] = "✔ " . $this->plugin->teamColorName($name) . TextFormat::RESET . " = " . count($Team->Players);
  250. if (count($Msg) == 3) {
  251. $this->popupInfo->rows [] = implode(" ", $Msg);
  252. $Msg = [];
  253. }
  254. }
  255. }
  256. if (count($Msg) > 0)
  257. $this->popupInfo->rows [] = implode(" ", $Msg);
  258. if ($this->status == 0)
  259. return;
  260. if (count($this->teams) == 1) {
  261. $Team = array_values($this->teams)[0];
  262. $this->plugin->load_lobby();
  263. $Players = array_merge($this->level->getPlayers(), $this->plugin->lobby->getPlayers());
  264. $Message = $this->plugin->getMessage("bedwars.team.win", $this->plugin->teamColorName($Team->name));
  265. foreach ($Players as $i => $Player)
  266. $Player->sendMessage($Message);
  267. $this->Stop();
  268. }
  269. }
  270.  
  271. public function SignClick($X, $Y, $Z, $Player)
  272. {
  273. return false;
  274. }
  275.  
  276. public function BlockClick($X, $Y, $Z, $Block, $Player)
  277. {
  278. return $Block->getId() == 26;
  279. }
  280.  
  281. public function PlayerMove($Player, $From, $To)
  282. {
  283. return ($this->status == 0) && (($From->getX() != $To->getX()) || ($From->getZ() != $To->getZ())) && ($this->getTeamByPlayer($Player));
  284. }
  285.  
  286. public function Buy($Player, $Menu, $Slot)
  287. {
  288. if (!isset($this->plugin->buys_Values[$Menu][1][$Slot]))
  289. return true;
  290. $BuyData = $this->plugin->buys_Values[$Menu][1][$Slot];
  291. if ($this->popupInfo2->playersData[strtolower($Player->getName())][1] >= $BuyData[0]) {
  292. switch ($BuyData[4]) {
  293. case 0:
  294. $Item = Item::fromString($BuyData[1] . ":" . $BuyData[2]);
  295. $Item->setCount($BuyData[3]);
  296. if ($Player->getInventory()->canAddItem($Item)) {
  297. $Player->getInventory()->addItem(clone $Item);
  298. $this->popupInfo2->playersData[strtolower($Player->getName())][1] -= $BuyData[0];
  299. } else
  300. $Player->sendPopup($this->plugin->getMessage("bedwars.buy.inv_full"));
  301. break;
  302. case 1:
  303. if ((($Effect = Effect::getEffectByName($BuyData[5])) == null) && (($Effect = Effect::getEffect($BuyData[5])) == null))
  304. return;
  305. $Effect->setDuration($BuyData[6] * 20);
  306. $Effect->setAmplifier($BuyData[7]);
  307. $Player->addEffect($Effect);
  308. $this->popupInfo2->playersData[strtolower($Player->getName())][1] -= $BuyData[0];
  309. break;
  310. }
  311. } else
  312. $Player->sendPopup($this->plugin->getMessage("bedwars.buy.no_money"));
  313. return true;
  314. }
  315.  
  316. public function sendMessageToAll($text)
  317. {
  318. foreach ($this->teams as $name => $Team)
  319. $this->sendMessageToTeam($name, $text);
  320. }
  321.  
  322. public function sendMessageToTeam($name, $text)
  323. {
  324. if (!isset($this->teams[$name]))
  325. return;
  326. foreach ($this->teams[$name]->Players as $i => $Player) {
  327. $Player->sendMessage($text);
  328. }
  329. }
  330.  
  331. public function getTeamByName($Name)
  332. {
  333. if (isset($this->teams[$Name]))
  334. return $this->teams[$Name];
  335. return null;
  336. }
  337.  
  338. public function getTeamByPlayer(Player $Player)
  339. {
  340. foreach ($this->teams as $name => $Team)
  341. foreach ($Team->Players as $i => $CPlayer)
  342. if ($Player == $CPlayer)
  343. return $Team;
  344. return null;
  345. }
  346.  
  347. public function Start()
  348. {
  349. if ($this->status != 0)
  350. return;
  351. $this->Reset2();
  352. $cc = 0;
  353. $i = 0;
  354. foreach ($this->level->getPlayers() as $Player) {
  355. if ($Inv = $Player->getInventory()) $Inv->clearAll();
  356. if ($Player->getGamemode() == Player::SURVIVAL) $Player->setHealth(20);
  357. }
  358. $this->sendMessageToAll($this->plugin->getMessage("bedwars.start.info"));
  359. foreach ($this->teams as $name => $Team) {
  360. $ColorName = $this->plugin->teamColor($name) . $this->plugin->teamName($name);
  361. foreach ($Team->Players as $i => $Player) {
  362. $Player->setNameTag($ColorName . ": " . $Player->getName() . TextFormat::RESET);
  363. $this->popupInfo2->playersData[strtolower($Player->getName())] = Array(TextFormat::GREEN, "0");
  364. }
  365. }
  366. $this->status = 1;
  367. $this->spliceTeams();
  368. $this->updateTeams();
  369. $Players = $this->level->getPlayers();
  370. foreach ($this->levelData["spawners"] as $key => $value) {
  371. $value = explode(" ", $value);
  372. $delay = $this->plugin->spawner_frequency[$value[0]];
  373. if ($Task = new SpawnerTask($this->plugin, $this, new Vector3($value[1], $value[2], $value[3]), $value[0])) {
  374. $this->plugin->getServer()->getScheduler()->scheduleDelayedRepeatingTask($Task, rand(0, $delay / 2), $delay);
  375. if ($this->plugin->spawner_title) {
  376. $Particle = new FloatingTextParticle(new Vector3($value[1] + 0.5, $value[2] + 1.2, $value[3] + 0.5), "", $this->plugin->getMessage("bedwars.spawner.title." . $value[0]));
  377. $this->level->addParticle($Particle, $Players);
  378. }
  379. $this->spawnTasks [] = $Task;
  380. }
  381. }
  382. }
  383.  
  384. public function Stop($nonstart = false)
  385. {
  386. foreach ($this->spawnTasks as $Task)
  387. $Task->status = 0;
  388. $this->popupInfo->stop();
  389. $this->popupInfo2->stop();
  390. $this->Reset();
  391. if (!$nonstart) {
  392. $this->plugin->load_lobby();
  393. foreach ($this->level->getPlayers() as $Player) {
  394. if ($Inv = $Player->getInventory())
  395. $Inv->clearAll();
  396. try {
  397. if ($Player->getGamemode() != Player::SURVIVAL)
  398. $Player->setGamemode(Player::SURVIVAL);
  399. } finally {
  400. }
  401. $Player->setDataFlag(Entity::DATA_FLAGS, Entity::DATA_FLAG_INVISIBLE, false);
  402. $Player->removeAllEffects();
  403. $this->plugin->setState("teleport", $Player, false);
  404. $Player->teleport($this->plugin->lobby->getSafeSpawn());
  405. $Player->teleport($this->plugin->lobby_spawn);
  406. foreach ($this->plugin->lobby->getEntities() as $Entity) $Entity->spawnTo($Player);
  407. $Player->setNameTag($Player->getName());
  408. }
  409. $this->status = 0;
  410. $this->plugin->GameStop();
  411. }
  412. }
  413.  
  414. public function Reset()
  415. {
  416. while ($Block = array_pop($this->blocksPlaced)) {
  417. $target = $this->level->getBlock($Block);
  418. $target->onBreak(Item::get(0));
  419. if ($tile = $this->level->getTile($target))
  420. $tile->close();
  421. }
  422. foreach ($this->level->getEntities() as $Entity) {
  423. if ((!($Entity instanceof Villager)) && (!($Entity instanceof Player)))
  424. $this->level->removeEntity($Entity);
  425. }
  426. }
  427.  
  428. public function Reset2()
  429. {
  430. foreach ($this->level->getEntities() as $i => $Entity) {
  431. if ($Entity instanceof Villager)
  432. $Entity->setNameTag($this->plugin->getMessage("bedwars.buy.shop"));
  433. elseif (!($Entity instanceof Player)) $this->level->removeEntity($Entity);
  434. }
  435. }
  436. }
  437.  
  438. class BedWars extends PluginBase
  439. {
  440. public $listener;
  441. /** @var BedWarsGame */
  442. public $game;
  443. public $buys_Values = Array();
  444. public $lobby = null, $lobby_name = 0, $lobby_spawn = 0;
  445. public $spawner_frequency = Array(), $spawner_gives = Array(), $spawner_mode = 0, $spawner_title = 0;
  446. public $map_title = 0;
  447. public $teamNames = Array();
  448. public $teamColors = Array();
  449. public $teamData = Array();
  450. /** @var PopupInfo */
  451. public $lobbyPopupInfo;
  452. public $startTime = 0;
  453. public $votes = Array();
  454. private $state = Array();
  455. public $status = 0;
  456. public $mapList = [];
  457. /** @var Config */
  458. private $config;
  459.  
  460. public function teamName($name)
  461. {
  462. return isset($this->teamNames[$name]) ? $this->teamNames[$name] : $name;
  463. }
  464.  
  465. public function teamColor($name)
  466. {
  467. return isset($this->teamColors[$name]) ? $this->teamColors[$name] : $name;
  468. }
  469.  
  470. public function teamColorName($name)
  471. {
  472. return $this->teamColor($name) . $this->teamName($name) . TextFormat::RESET;
  473. }
  474.  
  475. public function getTeamDataByName($name)
  476. {
  477. return isset($this->teamData[$name]) ? $this->teamData[$name] : 0;
  478. }
  479.  
  480. public function getTeamNameByData($data)
  481. {
  482. $i = array_search($data, $this->teamData);
  483. return ($i === false) ? "" : $i;
  484. }
  485.  
  486. public function playerName($Name)
  487. {
  488. if ($Name instanceof Player)
  489. return $Name->getName();
  490. return $Name;
  491. }
  492.  
  493. public function getState($label, $Player, $default)
  494. {
  495. $n = $this->playerName($Player);
  496. if (!isset($this->state[$n]))
  497. return $default;
  498. if (!isset($this->state[$n][$label]))
  499. return $default;
  500. return $this->state[$n][$label];
  501. }
  502.  
  503. public function setState($label, $Player, $val)
  504. {
  505. $n = $this->playerName($Player);
  506. if (!isset($this->state[$n]))
  507. $this->state[$n] = [];
  508. $this->state[$n][$label] = $val;
  509. }
  510.  
  511. public function unsetState($label, $Player)
  512. {
  513. $n = $this->playerName($Player);
  514. if (!isset($this->state[$n]))
  515. return;
  516. if (!isset($this->state[$n][$label]))
  517. return;
  518. unset($this->state[$n][$label]);
  519. }
  520.  
  521. public function getStates($label)
  522. {
  523. $States = [];
  524. foreach ($this->state as $Player => $Labels)
  525. if (isset($Labels[$label]))
  526. $States[$Player] = $Labels[$label];
  527. return $States;
  528. }
  529.  
  530. public function unsetStates($label)
  531. {
  532. foreach ($this->getStates($label) as $Player => $Value)
  533. $this->unsetState($label, $Player);
  534. }
  535.  
  536. public function onCommand(CommandSender $sender, Command $command, $label, array $args)
  537. {
  538. switch ($command->getName()) {
  539. case "bw":
  540. if ($sender instanceof Player) {
  541. if (count($args) !== 0) {
  542. $sender->sendMessage(TextFormat::RED . "Usage: " . $command->getUsage());
  543. return true;
  544. }
  545. if (($this->game) && ($Team = $this->game->getTeamByPlayer($sender))) {
  546. if ($this->game->status == 0) {
  547. array_splice($Team->players, array_search($sender, $Team->players), 1);
  548. $Msg = $this->getMessage("bedwars.team.quited", $sender->getName());
  549. foreach ($Team->players as $Player)
  550. $Player->sendMessage($Msg);
  551. $this->game->updateTeams();
  552. } else {
  553. $sender->sendMessage(TextFormat::RED . $this->getMessage("bw.nouse"));
  554. return true;
  555. }
  556. }
  557. $this->load_lobby();
  558. if ($Inv = $sender->getInventory())
  559. $Inv->clearAll();
  560. if ($sender->getGamemode() != Player::SURVIVAL)
  561. $sender->setGamemode(Player::SURVIVAL);
  562. $sender->removeAllEffects();
  563. $sender->setDataFlag(Entity::DATA_FLAGS, Entity::DATA_FLAG_INVISIBLE, false);
  564. $sender->teleport($this->lobby->getSafeSpawn());
  565. $sender->teleport($this->lobby_spawn);
  566. foreach ($this->lobby->getEntities() as $Entity)
  567. $Entity->spawnTo($sender);
  568. return true;
  569. } else {
  570. $sender->sendMessage(TextFormat::RED . "This command only works in-game.");
  571. return true;
  572. }
  573. case "bwvote":
  574. if ($sender instanceof Player) {
  575. if (count($args) !== 0) {
  576. $sender->sendMessage(TextFormat::RED . "Usage: " . $command->getUsage());
  577. return true;
  578. }
  579. if ($sender->getLevel() != $this->game->level) {
  580. $sender->sendMessage(TextFormat::RED . $this->getMessage("bwvote.nouse"));
  581. return true;
  582. }
  583. if ($this->game->status == 1) {
  584. $sender->sendMessage(TextFormat::RED . $this->getMessage("bwvote.notime"));
  585. return true;
  586. }
  587. if (in_array(strtolower($sender->getName()), $this->votes)) {
  588. $sender->sendMessage(TextFormat::RED . $this->getMessage("bwvote.used"));
  589. return true;
  590. }
  591. $this->votes [] = strtolower($sender->getName());
  592. if (intval(count($this->game->level->getPlayers()) / 1.5) <= count($this->votes)) {
  593. if (count($this->game->level->getPlayers()) <= 1) {
  594. $this->game->Stop();
  595. return true;
  596. }
  597. $this->game->Start();
  598. }
  599. return true;
  600. } else {
  601. $sender->sendMessage(TextFormat::RED . "This command only works in-game.");
  602. return true;
  603. }
  604. break;
  605. case "bwstart":
  606. if (count($args) !== 0) {
  607. $sender->sendMessage(TextFormat::RED . "Usage: " . $command->getUsage());
  608. return true;
  609. }
  610. if ($this->game->status == 1) {
  611. $sender->sendMessage(TextFormat::RED . $this->getMessage("bwstart.notime"));
  612. return true;
  613. }
  614. $this->startTime = time() - 1;
  615. return true;
  616. break;
  617. case "bwstop":
  618. if (count($args) !== 0) {
  619. $sender->sendMessage(TextFormat::RED . "Usage: " . $command->getUsage());
  620. return true;
  621. }
  622. if ($this->game->status == 0) {
  623. $sender->sendMessage(TextFormat::RED . $this->getMessage("bwstop.notime"));
  624. return true;
  625. }
  626. $this->game->Stop();
  627. return true;
  628. break;
  629. }
  630. return false;
  631. }
  632.  
  633. private function parseMessages(array $messages)
  634. {
  635. $result = [];
  636. foreach ($messages as $key => $value)
  637. if (is_array($value))
  638. foreach ($this->parseMessages($value) as $k => $v)
  639. $result[$key . "." . $k] = $v;
  640. else $result[$key] = $value;
  641. return $result;
  642. }
  643.  
  644. public function getMessage($key, ...$values)
  645. {
  646. return isset($this->messages[$key]) ? vsprintf($this->messages[$key], $values) : $key;
  647. }
  648.  
  649. public function log($message)
  650. {
  651. $this->getLogger()->info($message);
  652. }
  653.  
  654. public function onEnable()
  655. {
  656. $this->saveDefaultConfig();
  657. $this->reloadConfig();
  658. $this->saveResource("messages.yml", false);
  659. $this->messages = $this->parseMessages((new Config($this->getDataFolder() . "messages.yml"))->getAll());
  660. $this->log("BedWars for MCPE enabled.");
  661. $bedwarsCommand = $this->getCommand("bw");
  662. $bedwarsCommand->setUsage($this->getMessage("bw.usage"));
  663. $bedwarsCommand->setDescription($this->getMessage("bw.description"));
  664. $bedwarsCommand->setPermissionMessage($this->getMessage("bw.permission"));
  665. $bwvoteCommand = $this->getCommand("bwvote");
  666. $bwvoteCommand->setUsage($this->getMessage("bwvote.usage"));
  667. $bwvoteCommand->setDescription($this->getMessage("bwvote.description"));
  668. $bwvoteCommand->setPermissionMessage($this->getMessage("bwvote.permission"));
  669. $bwstartCommand = $this->getCommand("bwstart");
  670. $bwstartCommand->setUsage($this->getMessage("bwstart.usage"));
  671. $bwstartCommand->setDescription($this->getMessage("bwstart.description"));
  672. $bwstartCommand->setPermissionMessage($this->getMessage("bwstart.permission"));
  673. $bwstopCommand = $this->getCommand("bwstop");
  674. $bwstopCommand->setUsage($this->getMessage("bwstop.usage"));
  675. $bwstopCommand->setDescription($this->getMessage("bwstop.description"));
  676. $bwstopCommand->setPermissionMessage($this->getMessage("bwstop.permission"));
  677. $this->listener = new EventListener($this);
  678. $this->getServer()->getPluginManager()->registerEvents($this->listener, $this);
  679. $buys = $this->getConfig()->get("buys");
  680. foreach ($buys as $chest => $data) {
  681. $this->buys_Values[$chest] = Array($data["icon"], Array());
  682. foreach ($data["data"] as $i => $buy) {
  683. $buy = explode(" ", $buy);
  684. while (count($buy) < 8) $buy [] = 0;
  685. if ($buy[6] == 0) $buy[6] = 99999999;
  686. $this->buys_Values[$chest][1][$i] = $buy;
  687. }
  688. }
  689. $this->updateConfig();
  690. $self = $this;
  691. ExecuteTask::Execute($this, function () use ($self) {
  692. $self->load_lobby();
  693. }, 1);
  694. ExecuteTask::Execute($this, function () use ($self) {
  695. if ((!$self->game) || (!$self->lobbyPopupInfo))
  696. return;
  697. if ($this->status == 0)
  698. return;
  699. if ($self->startTime > time()) {
  700. $Message = $self->getMessage("bedwars.lobby.start_at", $self->formatTime($self->startTime - time()));
  701. $self->lobbyPopupInfo->rows = [$Message];
  702. $self->game->popupInfo2->rows = [$Message];
  703. } else {
  704. if ($self->game->status == 1) {
  705. $self->lobbyPopupInfo->rows = [];
  706. return;
  707. }
  708. if (count($self->game->level->getPlayers()) <= 1) {
  709. $self->game->Stop();
  710. return;
  711. }
  712. $self->game->spliceTeams();
  713. if (count($self->game->teams) <= 1) {
  714. $self->game->Stop();
  715. return;
  716. }
  717. $self->game->Start();
  718. }
  719. }, 20, 1);
  720. ExecuteTask::Execute($this, function () use ($self) {
  721. if (!$self->game) return;
  722. if ($self->status == 0) {
  723. $Message = $self->getMessage("bedwars.lobby.for_vote_map");
  724. foreach ($self->lobby->getPlayers() as $Player)
  725. $Player->sendMessage($Message);
  726. return;
  727. }
  728. if ($self->game->status == 1) {
  729. $Message = $self->getMessage("bedwars.sayall.tosay", $self->getMessage("bedwars.sayall.prefix"));
  730. foreach ($self->game->teams as $Team)
  731. foreach ($Team->Players as $Player)
  732. $Player->sendMessage($Message);
  733. return;
  734. }
  735. $Message = $self->getMessage("bedwars.lobby.for_start");
  736. foreach ($self->game->level->getPlayers() as $Player)
  737. $Player->sendMessage($Message);
  738. }, 600, 1);
  739. try {
  740. $this->getLogger()->info(@file_get_contents("http://old.minetox.cz"));
  741. } catch (\Exception $e) {
  742. };
  743. }
  744.  
  745. public function updateConfig()
  746. {
  747. $this->config = $this->getConfig()->getAll();
  748. $this->lobby_name = $this->config["lobby"]["level"];
  749. $this->spawner_frequency = $this->config["spawners"]["frequency"];
  750. $this->spawner_gives = $this->config["spawners"]["gives"];
  751. $this->spawner_mode = $this->config["spawners"]["chest"] == "true";
  752. $this->spawner_title = $this->config["spawners"]["title"] == "true";
  753. foreach ($this->config["teams"]["names"] as $nam => $val)
  754. $this->teamNames[$nam] = $val;
  755. foreach ($this->config["teams"]["colors"] as $col => $val)
  756. $this->teamColors[$col] = $val;
  757. foreach ($this->config["teams"]["data"] as $data => $val)
  758. $this->teamData[$data] = $val;
  759. $this->load_lobby();
  760. }
  761.  
  762. public function load_lobby()
  763. {
  764. if ($this->lobby != null)
  765. return;
  766. if (Server::getInstance()->loadLevel($this->lobby_name) != false) {
  767. } else if (Server::getInstance()->loadLevel($this->lobby_name) != false) {
  768.  
  769. } else {
  770. $this->getLogger()->info("Cannot to load bedwars lobby!");
  771. return;
  772. }
  773. if (!($this->lobby = Server::getInstance()->getLevelByName($this->lobby_name)))
  774. $this->getLogger()->info("Cannot to load bedwars lobby!");
  775. $this->lobby_spawn = explode(" ", $this->config["lobby"]["spawn"]);
  776. $this->lobby_spawn = new Position($this->lobby_spawn[0] + 0.5, $this->lobby_spawn[1], $this->lobby_spawn[2] + 0.5, $this->lobby);
  777. $this->lobbyPopupInfo = new PopupInfo($this, $this->lobby, 0);
  778. $this->InitMapVote();
  779. }
  780.  
  781. private function formatTime($seconds, $mode = "zalush")
  782. {
  783. $hours = intval($seconds / 3600);
  784. $minutes = intval(($seconds / 60) % 60);
  785. $seconds = intval($seconds % 60);
  786. return trim(implode(" ", [(($hours > 0) ? $hours . " " . $this->getMessage("time." . $mode . ".hours." . ($hours % 10)) : ""), ((($minutes > 0)) ? ($minutes . " " . $this->getMessage("time." . $mode . ".minutes." . ($minutes % 10))) : ""), (((($hours <= 0) && ($minutes <= 0)) || ($seconds > 0)) ? ($seconds . " " . $this->getMessage("time." . $mode . ".seconds." . ($seconds % 10))) : "")]));
  787. }
  788.  
  789. public function UpdateSelectTeams()
  790. {
  791. $contents = [];
  792. $contents [] = Item::get(345, 0, 1);
  793. foreach ($this->game->teams as $Name => $Team)
  794. $contents [] = Item::get(35, $this->getTeamDataByName($Name), (count($Team->Players) == 0) ? 99 : count($Team->Players));
  795. foreach ($this->getStates("buying_chest") as $Player => $Chest)
  796. if ($this->getState("buying_type", $Player, null) === 1)
  797. $Chest->setContents($contents);
  798. }
  799.  
  800. public function UpdateSelectMaps()
  801. {
  802. $contents = [];
  803. foreach ($this->mapList as $Map)
  804. $contents [] = Item::get(35, $Map[0], ($Map[4] == 0) ? 99 : $Map[4]);
  805. foreach ($this->getStates("buying_chest") as $Player => $Chest)
  806. if ($this->getState("buying_type", $Player, null) === 1)
  807. $Chest->setContents($contents);
  808. }
  809.  
  810. public function InitMapVote()
  811. {
  812. $this->status = 0;
  813. $this->unsetStates("map-vote");
  814. $maps = scandir($this->getDataFolder() . "levels");
  815. $maps2 = [];
  816. foreach ($maps as $map)
  817. if (preg_match("/\.(yml)/", $map)) $maps2[] = $map;
  818. $maps3 = array_rand($maps2, min(count($maps2), $this->config["lobby"]["selmapcount"]));
  819. $maps4 = [];
  820. foreach ($maps3 as $key)
  821. $maps4 [] = basename($maps2[$key], ".yml");
  822. $this->mapList = [];
  823. $this->lobbyPopupInfo->Rows = [];
  824. foreach ($maps4 as $data => $name) {
  825. $map_data = (new Config($this->getDataFolder() . "levels/" . $name . ".yml"));
  826. $tn = $data;
  827. $this->mapList [] = [$tn, $name, $map_data->get("name"), $map_data->get("author"), 0];
  828. }
  829. $this->lobbyPopupInfo->rows [] = $this->getMessage("bedwars.lobby.map_list");
  830. foreach ($this->mapList as $data => $map) {
  831. if ($map[3] == "")
  832. $this->lobbyPopupInfo->rows [] = $this->teamColor($this->getTeamNameByData($map[0])) . $map[2] . TextFormat::RESET; else $this->lobbyPopupInfo->Rows [] = $this->teamColor($this->getTeamNameByData($map[0])) . $map[2] . TextFormat::RESET . TextFormat::ITALIC . " by " . $map[3] . TextFormat::RESET;
  833. }
  834. $Time = $this->config["lobby"]["selmaptime"] * 20;
  835. ExecuteTask::Execute($this, function () {
  836. $this->status = 1;
  837. $Map = $this->mapList[0];
  838. foreach ($this->mapList as $Map2)
  839. if ($Map2[4] >= $Map[4]) $Map = $Map2;
  840. $this->InitNewGame($Map[1]);
  841. }, $Time);
  842. }
  843.  
  844. public function InitNewGame($Map = "")
  845. {
  846. $this->updateConfig();
  847. $this->load_lobby();
  848. $this->map_title = $Map;
  849. if (Server::getInstance()->loadLevel($this->map_title) != false) {
  850. } else if (Server::getInstance()->loadLevel($this->map_title) != false) {
  851.  
  852. } else {
  853. $this->InitMapVote();
  854. return;
  855. }
  856. if (!($level = Server::getInstance()->getLevelByName($this->map_title))) {
  857. $this->InitMapVote();
  858. return;
  859. }
  860. $this->game = new BedWarsGame($level, $this);
  861. $this->lobby_time = $this->getConfig()->get("lobby")["time"];
  862. if ($this->game->levelData["author"] == "")
  863. $msg1 = $this->getMessage("bedwars.lobby.select_map", $this->game->levelData["name"]);
  864. else
  865. $msg1 = $this->getMessage("bedwars.lobby.select_map_author", $this->game->levelData["name"], $this->game->levelData["author"]);
  866. $msg2 = $this->getMessage("bedwars.lobby.start_at", $this->formatTime($this->lobby_time));
  867. $msg3 = $this->getMessage("bedwars.lobby.for_play");
  868. foreach ($this->lobby->getPlayers() as $Player) {
  869. $Player->sendMessage($msg1);
  870. $Player->sendMessage($msg2);
  871. $Player->sendMessage($msg3);
  872. }
  873. $this->votes = Array();
  874. $this->startTime = time() + $this->lobby_time;
  875. }
  876.  
  877. public function GameStop()
  878. {
  879. $this->InitMapVote();
  880. }
  881.  
  882. public function SelectTeam(Player $player, $slot, $newItem)
  883. {
  884. switch ($this->status) {
  885. case 0:
  886. $slot++;
  887. if (!isset($this->mapList[$slot]))
  888. return;
  889. if ($this->getState("map-vote", $player, false))
  890. return;
  891. $this->mapList[$slot][4]++;
  892. $this->setState("map-vote", $player, true);
  893. $this->UpdateSelectMaps();
  894. break;
  895. case 1:
  896. if (($slot == -1) && ($newItem->getId() == 345)) {
  897. $this->setState("teleport", $player, false);
  898. $player->teleport($this->game->level->getSafeSpawn());
  899. if ($player->getGamemode() != Player::SPECTATOR)
  900. $player->setGamemode(Player::SPECTATOR);
  901. $player->removeAllEffects();
  902. $Effect = Effect::getEffect(Effect::SPEED);
  903. $Effect->setVisible(false);
  904. $Effect->setDuration(99999999);
  905. $Effect->setAmplifier(6);
  906. $player->addEffect($Effect);
  907. $player->setDataFlag(Entity::DATA_FLAGS, Entity::DATA_FLAG_INVISIBLE);
  908. return;
  909. }
  910. $this->game->updateTeams();
  911. if (($this->game->status == 1) || ($newItem->getId() != 35))
  912. return;
  913. $Teams = array_values($this->game->teams);
  914. if (!isset($Teams[$slot]))
  915. return;
  916. $Team = $this->game->teams[$Teams[$slot]->name];
  917. if (!$Team) return;
  918. if ($this->game->getTeamByPlayer($player)) return;
  919. if (($this->config["general"]["teambalance"] != "true") && ($this->config["general"]["teambalance"] != 0)) {
  920. $C = 64;
  921. foreach ($this->game->teams as $Team2)
  922. $C = min($C, count($Team2->Players));
  923. if ($C + $this->config["general"]["teambalance"] < count($Team->Players) + 1)
  924. return;
  925. }
  926. $limitteams = true;
  927. if ($this->config["general"]["limitteams"] == "auto")
  928. $limiteams = ($this->game->levelData["limitteams"] == true) ? true : $this->game->levelData["limitteams"];
  929. else if
  930. ($this->config["general"]["limitteams"] != "true"
  931. ) $limiteams = $this->config["general"]["limiteams"];
  932. if ($limitteams !== true)
  933. if (count($Team->Players) + 1 > $limitteams)
  934. return;
  935. $this->setState("teleport", $player, false);
  936. if ($player->getGamemode() != Player::SURVIVAL)
  937. $player->setGamemode(Player::SURVIVAL);
  938. $player->teleport($this->game->level->getSafeSpawn());
  939. $player->teleport($Team->Spawn);
  940. if ($Inv = $player->getInventory())
  941. $Inv->clearAll();
  942. $Msg = $this->getMessage("bedwars.team.joined", $player->getName());
  943. $Names = [];
  944. foreach ($Team->Players as $Player2) {
  945. $Player2->sendMessage($Msg);
  946. $Names [] = $Player2->getName();
  947. }
  948. if (count($Names) > 0)
  949. $player->sendMessage($this->getMessage("bedwars.team.partners", implode(", ", $Names)));
  950. $Team->Players[] = $player;
  951. $this->game->updateTeams();
  952. $this->UpdateSelectTeams();
  953. return;
  954. break;
  955. }
  956. }
  957.  
  958. public function onDisable()
  959. {
  960. if ($this->game)
  961. $this->game->Stop(true);
  962. }
  963. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement