Make minigames easilier !
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TNTRun.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. use Ad5001\GameManager\Game;
  3. use pocketmine\Player;
  4. use pocketmine\item\Item;
  5. use pocketmine\block\Block;
  6. use pocketmine\math\Vector3;
  7. use pocketmine\level\Level;
  8. use pocketmine\nbt\tag\CompoundTag;
  9. use pocketmine\nbt\tag\ListTag;
  10. use pocketmine\nbt\tag\StringTag;
  11. class TNTRun extends Game {
  12. public function __construct(string $name, Level $level) {
  13. parent::__construct($name, $level);
  14. $this->getLogger()->info("TNTRun enabled ! Thanks for choosing TNTRun and remeber to leave a 'review' on projects.ad5001.ga/GameManager/TNTRun/ if you like it :)");
  15. $text = '# Welcome to TNTRun config !
  16. # Configure here the max stuffs about the TNTRun game.
  17. # Example:
  18. #world:
  19. # maxplayers: 8
  20. # minplayers: 2
  21. # center: [x, z]
  22. # TNTBlock: 13
  23. # WinCommand: give {winner} diamond
  24. # # This is optional but if you want super customisation...
  25. # # WinPHPCode: "$this->getServer()->broadcastMessage($this->winner . \' won the game !\');"
  26. ';
  27. if(!file_exists($this->getDataFolder() . "/config.yml")) {
  28. file_put_contents($this->getDataFolder()."/config.yml", $text);
  29. }
  30. if(!is_array($this->getConfig()->get($this->getLevel()->getName()))) {
  31. $this->getConfig()->set($this->getLevel()->getName(), ["minplayers" => 2, "maxplayers" => 8, "center" =>[0, 0], "TNTBlock" => 13, "WinCommand" => "give {winner} diamond"]);
  32. }
  33. $this->doubleJump = [];
  34. $this->registerCommand("tntrun", "Main TNTRun command !", "/tntrun <subcommand> [parameter]", [], "gamemanager.command.op");
  35. }
  36. public function onGameStart() {
  37. $this->layers = [];
  38. for($y = 0; $y <= 128; $y++) {
  39. if($this->getLevel()->getBlock(new Vector3($this->getConfig()->get($this->getLevel()->getName())["center"][0], $y, $this->getConfig()->get($this->getLevel()->getName())["center"][1]))->getId() == $this->getConfig()->get($this->getLevel()->getName())["TNTBlock"]) {
  40. $this->layers[] = $y;
  41. }
  42. }
  43. foreach($this->getLevel()->getPlayers() as $player) {
  44. $player->sendMessage("§l§o§6[§r§l§bTNTRun§o§6]§r§f Game started !");
  45. $this->tpSafePlayer($player);
  46. $player->setGamemode(0);
  47. $i = Item::get(Item::FEATHER, 0, 10);
  48. $i->setCompoundTag(\pocketmine\nbt\NBT::parseJSON('{display:{Name:"§rDouble jump"},doubleJump:"true"}'));
  49. $player->getInventory()->addItem($i);
  50. $this->doubleJump[$player->getName()] = 10;
  51. }
  52. }
  53. public function onInteract(\pocketmine\event\player\PlayerInteractEvent $event) {
  54. if($event->getPlayer()->getInventory()->getItemInHand()->getId() == Item::FEATHER and $event->getPlayer()->getInventory()->getItemInHand()->hasCompoundTag()) {
  55. // echo $event->getPlayer()->getInventory()->getItemInHand()->getCompoundTag();
  56. if(strpos($event->getPlayer()->getInventory()->getItemInHand()->getCompoundTag(), "doubleJump")) {
  57. $yaw = $event->getPlayer()->yaw;
  58. if (0 <= $yaw and $yaw < 22.5) {
  59. $this->doubleJump($event->getPlayer(), 0, 0, -15040, -0.125);
  60. } elseif (22.5 <= $yaw and $yaw < 67.5) {
  61. $this->doubleJump($event->getPlayer(), 0, 15040, -15040, -0.125);
  62. } elseif (67.5 <= $yaw and $yaw < 112.5) {
  63. $this->doubleJump($event->getPlayer(), 0, 15040, 0, -0.125);
  64. } elseif (112.5 <= $yaw and $yaw < 157.5) {
  65. $this->doubleJump($event->getPlayer(), 0, 15040, 15040, -0.125);
  66. } elseif (157.5 <= $yaw and $yaw < 202.5) {
  67. $this->doubleJump($event->getPlayer(), 0, 0, 15040, -0.125);
  68. } elseif (202.5 <= $yaw and $yaw < 247.5) {
  69. $this->doubleJump($event->getPlayer(), 0, -15040, 15040, -0.125);
  70. } elseif (247.5 <= $yaw and $yaw < 292.5) {
  71. $this->doubleJump($event->getPlayer(), 0, -15040, 0, -0.125);
  72. } elseif (292.5 <= $yaw and $yaw < 337.5) {
  73. $this->doubleJump($event->getPlayer(), 0, -15040, -15040, -0.125);
  74. } elseif (337.5 <= $yaw and $yaw < 360) {
  75. $this->doubleJump($event->getPlayer(), 0, 0, -15040, -0.125);
  76. }
  77. }
  78. }
  79. }
  80. public function doubleJump(Player $player, $damage, $x, $z, $base) {
  81. $f = sqrt(-$x * -$x + -$z * -$z);
  82. if($f <= 0){
  83. return;
  84. }
  85. $f = 1 / $f;
  86. $motion = new Vector3($player->motionX, $player->motionY, $player->motionZ);
  87. $motion->x /= 2;
  88. $motion->y /= 2;
  89. $motion->z /= 2;
  90. $motion->x += -$x * $f * -$base;
  91. $motion->y = 0.75;
  92. // echo $motion->y;
  93. $motion->z += -$z * $f * -$base;
  94. // if($motion->y > $base){
  95. // $motion->y = $base + 0.2;
  96. // }
  97. if(!isset($this->doubleJump[$player->getName()])) {
  98. $this->doubleJump[$player->getName()] = 10;
  99. }
  100. if($this->doubleJump[$player->getName()] > 0) {
  101. $player->setMotion($motion);
  102. $player->getInventory()->getItemInHand()->setCount($this->doubleJump[$player->getName()]);
  103. $this->doubleJump[$player->getName()]--;
  104. $player->sendPopup("§c" . $this->doubleJump[$player->getName()] . " double jumps left");
  105. }
  106. }
  107. public function tpSafePlayer(Player $player) {
  108. $xrand = rand(-3 * count($this->getLevel()->getPlayers()) - 1, 3 * count($this->getLevel()->getPlayers()) - 1);
  109. $zrand = rand(-3 * count($this->getLevel()->getPlayers()) - 1, 3 * count($this->getLevel()->getPlayers()) - 1);
  110. $v3 = new Vector3($this->getConfig()->get($this->getLevel()->getName())["center"][0] + $xrand, $this->layers[count($this->layers) - 1], $this->getConfig()->get($this->getLevel()->getName())["center"][0] + $xrand);
  111. if($this->getLevel()->getBlock($v3)->getId() == $this->getConfig()->get($this->getLevel()->getName())["TNTBlock"] and $this->getLevel()->getBlock(new Vector3($v3->x, $v3->y+1, $v3->z))->getId() == 0) {
  112. $player->teleport(new Vector3($v3->x, $v3->y+1, $v3->z));
  113. return true;
  114. } else {
  115. return $this->tpSafePlayer($player);
  116. }
  117. }
  118. public function onGameStop() {
  119. $this->getServer()->getScheduler()->cancelTask($this->task->getTaskId());
  120. foreach($this->getLevel()->getPlayers() as $p) {
  121. $p->setGamemode(0);
  122. if($p !== $this->winner) {
  123. $p->teleport($this->getServer()->getDefaultLevel()->getSafeSpawn());
  124. $p->sendMessage("§l§o§4[§r§l§fTNTRun§o§4]§r§f" . $this->winner->getName() . " won the game ! Teleporting back to spawn...");
  125. } else {
  126. $this->winner->sendMessage("§l§o§4[§r§l§fTNTRun§o§4]§r§fYou won the game ! Teleporting back to spawn...");
  127. }
  128. if(isset($this->getConfig()->get($this->getLevel()->getName())["WinCommand"])) {
  129. $this->getServer()->dispatchCommand(new \pocketmine\command\ConsoleCommandSender(), str_ireplace("{winner}", $this->winner->getName(), $this->getConfig()->get($this->getLevel()->getName())["WinCommand"]));
  130. }
  131. if(isset($this->getConfig()->get($this->getLevel()->getName())["WinPHPCode"])) {
  132. eval($this->getConfig()->get($this->getLevel()->getName())["WinPHPCode"]);
  133. }
  134. foreach($this->layers as $y) {
  135. for($x = -3 * count($this->getMaxPlayers()) + $this->getConfig()->get($this->getLevel()->getName())["center"][0]; $x <= 3 * count($this->getMaxPlayers()) + $this->getConfig()->get($this->getLevel()->getName())["center"][0]; $x++) {
  136. for($z = -3 * count($this->getMaxPlayers()) + $this->getConfig()->get($this->getLevel()->getName())["center"][1]; $z <= 3 * count($this->getMaxPlayers()) + $this->getConfig()->get($this->getLevel()->getName())["center"][1]; $z++) {
  137. if($this->getLevel()->getBlock(new Vector3($x, $y, $z))->getId() == 0) {
  138. $this->getLevel()->setBlock(new Vector3($x, $y, $z), Item::fromString($this->getConfig()->get($this->getLevel()->getName())["TNTBlock"])->getBlock());
  139. }
  140. }
  141. }
  142. }
  143. }
  144. }
  145. public function onJoin(Player $player) {
  146. if($this->main->getInGamePlayers($this->getLevel()) + 1 >= $this->getMinPlayers() and !$this->isStarted()) {
  147. // $this->getLogger()->info("Started !");
  148. $h = $this->getServer()->getScheduler()->scheduleRepeatingTask($t = new TNTRunStartTask($this), 20);
  149. $t->setHandler($h);
  150. if(isset($this->task)) {
  151. $this->getServer()->getScheduler()->cancelTask($this->task->getTaskId());
  152. }
  153. $this->task = $t;
  154. }
  155. if($this->main->getInGamePlayers($this->getLevel()) + 1 > $this->getMaxPlayers() and !$this->isStarted()) {
  156. $player->teleport($this->getServer()->getDefaultLevel()->getSafeSpawn());
  157. $player->sendMessage("Too many players already in the game !");
  158. } elseif(!$this->isStarted()) {
  159. $player->setGamemode(2);
  160. $player->getInventory()->clearAll();
  161. }
  162. foreach($this->getLevel()->getPlayers() as $p) {
  163. $p->sendMessage(\pocketmine\utils\TextFormat::YELLOW . $player->getName() . " joined the TNTRun game. " . ($this->getMinPlayers() - $this->getPlugin()->getInGamePlayers($this->getLevel()) - 1) ." players left before starting !");
  164. }
  165. $player->sendMessage(\pocketmine\utils\TextFormat::YELLOW . "You joined the TNTRun game. " . ($this->getMinPlayers() - $this->getPlugin()->getInGamePlayers($this->getLevel()) - 1) ." players left before starting !");
  166. }
  167. public function onQuit(Player $player) {
  168. if($player->isSurvival() or $player->isAdventure()) {
  169. if($this->isStarted()) {
  170. foreach($this->getLevel()->getPlayers() as $p) {
  171. $p ->sendMessage(\pocketmine\utils\TextFormat::YELLOW . $player->getName() . " left the TNTRun game. {$this->getPlugin()->getInGamePlayers($this->getLevel())} players left !", [$this->getLevel()->getPlayers()]);
  172. }
  173. $player->sendMessage(\pocketmine\utils\TextFormat::YELLOW . "You left the TNTRun game. " . ($this->getMinPlayers() - $this->getPlugin()->getInGamePlayers($this->getLevel())) ." players left !");
  174. } else {
  175. foreach($this->getLevel()->getPlayers() as $p) {
  176. $p ->sendMessage(\pocketmine\utils\TextFormat::YELLOW . $player->getName() . " left the TNTRun game. {$this->getPlugin()->getInGamePlayers($this->getLevel())} players left before starting !", [$this->getLevel()->getPlayers()]);
  177. }
  178. $player->sendMessage(\pocketmine\utils\TextFormat::YELLOW . "You left the TNTRun game. " . ($this->getMinPlayers() - $this->getPlugin()->getInGamePlayers($this->getLevel())) ." players left before starting !");
  179. }
  180. }
  181. }
  182. public function onPlayerDeath(\pocketmine\event\player\PlayerDeathEvent $event) {
  183. $event->getPlayer()->setGamemode(3);
  184. $event->getPlayer()->removeEffect(3);
  185. if(($this->getPlugin()->getInGamePlayers($this->getLevel()) - 1) == 1) {
  186. foreach($this->getLevel()->getPlayers() as $p) {
  187. if($p->isSurvival()) {
  188. $this->winner = $p;
  189. $p->removeEffect(3);
  190. $p->getInventory()->clearAll();
  191. $this->stop();
  192. }
  193. }
  194. $this->stop();
  195. } elseif(($this->getPlugin()->getInGamePlayers($this->getLevel()) - 1) == 0) {
  196. $this->winner = $event->getPlayer();
  197. $event->getPlayer()->getInventory()->clearAll();
  198. $this->stop();
  199. }
  200. }
  201. public function onBreak(\pocketmine\event\block\BlockBreakEvent $event) {
  202. $event->setCancelled();
  203. if($event->getPlayer()->isOp() and $event->getPlayer()->isCreative() and !$this->isStarted()) {
  204. $event->setCancelled(false);
  205. }
  206. }
  207. public function onEntityDamage(\pocketmine\event\entity\EntityDamageEvent $event) {
  208. if(!in_array($event->getCause(), [7, 11, 12, 13, 14])) {
  209. $event->setCancelled();
  210. }
  211. }
  212. public function onCommand(\pocketmine\command\CommandSender $sender, \pocketmine\command\Command $cmd, $label, array $args) {
  213. if(strtolower($cmd->getName()) == "tntrun") {
  214. if(isset($args[0])) {
  215. switch(strtolower($args[0])) {
  216. case "start":
  217. if(isset($this->gm->getLevels()[$sender->getLevel()->getName()])) {
  218. if(!$this->gm->getLevels()[$sender->getLevel()->getName()]->isStarted() and $this->gm->getLevels()[$sender->getLevel()->getName()]->getName() == "TNTRun") {
  219. $h = $this->getServer()->getScheduler()->scheduleRepeatingTask($t = new TNTRunStartTask($this), 20);
  220. $t->setHandler($h);
  221. }
  222. }
  223. break;
  224. case "stop":
  225. if(isset($this->gm->getLevels()[$sender->getLevel()->getName()])) {
  226. if($this->gm->getLevels()[$sender->getLevel()->getName()]->isStarted() and $this->gm->getLevels()[$sender->getLevel()->getName()]->getName() == "TNTRun") {
  227. $this->winner = $sender;
  228. $this->stop();
  229. }
  230. }
  231. break;
  232. }
  233. }
  234. }
  235. }
  236. public function getMinPlayers() : int {
  237. // echo $this->getConfig()->get($this->getLevel()->getName())["minplayers"] . PHP_EOL;
  238. return $this->getConfig()->get($this->getLevel()->getName())["minplayers"];
  239. }
  240. public function getMaxPlayers() : int {
  241. // echo $this->getConfig()->get($this->getLevel()->getName())["maxplayers"] . PHP_EOL;
  242. return $this->getConfig()->get($this->getLevel()->getName())["maxplayers"];
  243. }
  244. public function getName() : string {
  245. return "TNTRun";
  246. }
  247. }
  248. class TNTRunStartTask extends \pocketmine\scheduler\PluginTask {
  249. public function __construct(TNTRun $main) {
  250. parent::__construct($main->getPlugin());
  251. $this->seconds = 0;
  252. $this->main = $main;
  253. }
  254. public function onRun($tick) {
  255. if($this->getOwner()->getInGamePlayers($this->main->getLevel()) < $this->main->getMinPlayers()) {
  256. $this->main->getServer()->broadcastMessage("Start cancelled ! Not enought players to start !");
  257. $this->main->getServer()->getScheduler()->cancelTask($this->getTaskId());
  258. } else {
  259. switch($this->seconds) {
  260. case 0:
  261. case 10:
  262. case 20:
  263. case 25:
  264. case 26:
  265. case 27:
  266. case 28:
  267. case 29:
  268. foreach($this->main->getLevel()->getPlayers() as $p) {
  269. $p->sendMessage("§l§o§4[§r§l§fTNTRun§o§4]§r§f " . strval(30 - $this->seconds) . " seconds left before the game starts !");
  270. }
  271. break;
  272. case 30:
  273. $this->main->start();
  274. $this->main->broadcastMessage("§l§o§4[§r§l§fTNTRun§o§4]§r§f 5 seconds before starts breaking...");
  275. case 35:
  276. $h = $this->main->getServer()->getScheduler()->scheduleRepeatingTask($this->main->task = new TNTRunBreakBlockTask($this->main), 5);
  277. $this->main->task->setHandler($h);
  278. $this->main->getServer()->getScheduler()->cancelTask($this->getTaskId());
  279. break;
  280. }
  281. $this->seconds++;
  282. }
  283. }
  284. }
  285. class TNTRunBreakBlockTask extends \pocketmine\scheduler\PluginTask {
  286. public function __construct(TNTRun $main) {
  287. $this->level = $main->getLevel();
  288. $this->main = $main;
  289. parent::__contruct($main);
  290. }
  291. public function onRun($tick) {
  292. foreach($this->level->getPlayers() as $p) {
  293. if($this->level->getBlock(new Vector3($p->x, $p->y-1, $p->z))->getId() == $this->main->getConfig()->get($this->level->getName())["TNTBlock"]) {
  294. $this->level->setBlock(new Vector3($p->x, $p->y-1, $p->z), new Block(0, 0));
  295. }
  296. }
  297. }
  298. }