PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Block-Hunt/Block-Hunt-master/src/NetherManiacsKingdomTeam/Block-Hunt/defence/DefenceManager.php

https://gitlab.com/Skull3x/WorkingInProgress-Plugins-Sourcecode-For-Dev
PHP | 379 lines | 298 code | 19 blank | 62 comment | 49 complexity | e25c2936f9916f977bc5dd1ff731e858 MD5 | raw file
  1. <?php
  2. namespace mcg76\game\blockhunt\defence;
  3. use pocketmine\command\Command;
  4. use pocketmine\command\CommandSender;
  5. use pocketmine\plugin\PluginBase;
  6. use pocketmine\utils\TextFormat;
  7. use pocketmine\Player;
  8. use pocketmine\OfflinePlayer;
  9. use pocketmine\Server;
  10. use pocketmine\utils\Config;
  11. use pocketmine\level\Position;
  12. use pocketmine\level\Level;
  13. use pocketmine\event\block\BlockEvent;
  14. use pocketmine\event\block\BlockPlaceEvent;
  15. use pocketmine\event\block\BlockBreakEvent;
  16. use pocketmine\event\entity\EntityDamageEvent;
  17. use pocketmine\event\Listener;
  18. use pocketmine\block\Block;
  19. use pocketmine\item\ItemBlock;
  20. use pocketmine\item\Item;
  21. use mcg76\game\blockhunt\BlockHuntPlugIn;
  22. use mcg76\game\blockhunt\MiniGameBase;
  23. /**
  24. * BlockHunt - Made by minecraftgenius76
  25. *
  26. * You're allowed to use for own usage only, you're not allowed to republish or resell or for any commercial purpose.
  27. *
  28. * Thanks for your cooperate!
  29. *
  30. * Copyright (C) 2015 minecraftgenius76
  31. * YouTube Channel: http://www.youtube.com/user/minecraftgenius76
  32. *
  33. * @author minecraftgenius76
  34. *
  35. */
  36. /**
  37. * Defence Manager
  38. */
  39. class DefenceManager extends MiniGameBase {
  40. const COMMAND_ARENA_SIGN_STAT = "setSignStat";
  41. const COMMAND_ARENA_SIGN_JOIN = "setSignJoin";
  42. const COMMAND_DEFENCE_POS1 = "setDefencePos1";
  43. const COMMAND_DEFENCE_POS2 = "setDefencePos2";
  44. const COMMAND_DEFENCE_POSITION = "setDefencePos";
  45. public $defences = [ ];
  46. /**
  47. *
  48. * @param BlockHuntPlugIn $plugin
  49. */
  50. public function __construct(BlockHuntPlugIn $plugin) {
  51. parent::__construct ( $plugin );
  52. }
  53. /**
  54. * Returns the *Singleton* instance of this class.
  55. *
  56. * @staticvar Singleton $instance The *Singleton* instances of this class.
  57. *
  58. * @return Singleton The *Singleton* instance.
  59. */
  60. public static function getInstance(BlockHuntPlugIn $plugin)
  61. {
  62. static $instance = null;
  63. if (null === $instance) {
  64. $instance = new DefenceManager($plugin);
  65. }
  66. return $instance;
  67. }
  68. public function &session(Player $sender) {
  69. if (! isset ( $this->plugin->sessions [$sender->getName ()] )) {
  70. $this->plugin->sessions [$sender->getName ()] = array (
  71. "selection" => array (
  72. false,
  73. false
  74. ),
  75. "arena-name" => false,
  76. "arena-type" => false,
  77. "wand-usage" => false,
  78. "edit-mode" => false
  79. );
  80. }
  81. return $this->plugin->sessions [$sender->getName ()];
  82. }
  83. public function setPosition1(&$session, Position $position, &$output) {
  84. $output = "";
  85. $session ["selection"] [0] = array (
  86. round ( $position->x ),
  87. round ( $position->y ),
  88. round ( $position->z ),
  89. $position->level
  90. );
  91. $count = $this->countBlocks ( $session ["selection"] );
  92. if ($count === false) {
  93. $count = "";
  94. } else {
  95. $count = " ($count)";
  96. }
  97. $output .= "First position set to (" . $session ["selection"] [0] [0] . ", " . $session ["selection"] [0] [1] . ", " . $session ["selection"] [0] [2] . ")$count.\n";
  98. return true;
  99. }
  100. public function setPosition2(&$session, Position $position, &$output) {
  101. $output = "";
  102. $session ["selection"] [1] = array (
  103. round ( $position->x ),
  104. round ( $position->y ),
  105. round ( $position->z ),
  106. $position->level
  107. );
  108. $count = $this->countBlocks ( $session ["selection"] );
  109. if ($count === false) {
  110. $count = "";
  111. } else {
  112. $count = " ($count)";
  113. }
  114. $output .= "Second position set to (" . $session ["selection"] [1] [0] . ", " . $session ["selection"] [1] [1] . ", " . $session ["selection"] [1] [2] . ")$count.\n";
  115. return true;
  116. }
  117. private function countBlocks($selection, &$startX = null, &$startY = null, &$startZ = null) {
  118. if (! is_array ( $selection ) or $selection [0] === false or $selection [1] === false or $selection [0] [3] !== $selection [1] [3]) {
  119. return false;
  120. }
  121. $startX = min ( $selection [0] [0], $selection [1] [0] );
  122. $endX = max ( $selection [0] [0], $selection [1] [0] );
  123. $startY = min ( $selection [0] [1], $selection [1] [1] );
  124. $endY = max ( $selection [0] [1], $selection [1] [1] );
  125. $startZ = min ( $selection [0] [2], $selection [1] [2] );
  126. $endZ = max ( $selection [0] [2], $selection [1] [2] );
  127. return ($endX - $startX + 1) * ($endY - $startY + 1) * ($endZ - $startZ + 1);
  128. }
  129. public static function lengthSq($x, $y, $z) {
  130. return ($x * $x) + ($y * $y) + ($z * $z);
  131. }
  132. public function newDefence(Player $sender, array $args) {
  133. if (! $sender->isOp ()) {
  134. $sender->sendMessage ( "[BH] You are not authorized to use this command." );
  135. return;
  136. }
  137. if (count ( $args ) != 2) {
  138. $sender->sendMessage ( "[BH] Usage:/bh newdefence [name]" );
  139. return;
  140. }
  141. $sender->sendMessage ( "[BH] Creating new defence" );
  142. $defenceName = $args [1];
  143. if (isset ( $this->getPlugin ()->defences [$defenceName] )) {
  144. $sender->sendMessage ( "[BH] Warning! arena ALREADY Exist!. please use another name!" );
  145. return;
  146. }
  147. // $id = time();
  148. $name = $args [1];
  149. $position = $sender->getPosition ();
  150. $newDefence = new DefenceModel ( $name );
  151. $newDefence->levelName = $sender->level->getName ();
  152. $newDefence->save ( $this->getPlugin ()->getDataFolder () );
  153. $this->getPlugin ()->defences [$name] = $newDefence;
  154. $sender->sendMessage ( "[BH] New Defence Saved!" );
  155. // clear selection
  156. // $this->handleDeSelCommand ( $sender );
  157. return;
  158. }
  159. public function handlePos1Command($sender, $args) {
  160. $output = "";
  161. if (! ($sender instanceof Player)) {
  162. $output .= "Please run this command in-game.\n";
  163. $sender->sendMessage ( $output );
  164. return;
  165. }
  166. $session = & $this->session ( $sender );
  167. $this->setPosition1 ( $session, new Position ( $sender->x - 0.5, $sender->y, $sender->z - 0.5, $sender->getLevel () ), $output );
  168. $sender->sendMessage ( $output );
  169. }
  170. public function handlePos2Command($sender, $args) {
  171. $output = "";
  172. if (! ($sender instanceof Player)) {
  173. $output .= "Please run this command in-game.\n";
  174. $sender->sendMessage ( $output );
  175. return;
  176. }
  177. $session = & $this->session ( $sender );
  178. $this->setPosition2 ( $session, new Position ( $sender->x - 0.5, $sender->y, $sender->z - 0.5, $sender->getLevel () ), $output );
  179. $sender->sendMessage ( $output );
  180. }
  181. public function handleWandCommand(Player $player, $args) {
  182. $session = & $this->session ( $player );
  183. $this->handleDeSelCommand ( $player );
  184. $session ["wand-usage"] = true;
  185. $player->sendMessage ( "Wand selected" );
  186. // check player items on hand
  187. if ($player->getInventory ()->getItemInHand ()->getId () != 292) {
  188. $player->getInventory ()->setItemInHand ( new Item ( 292 ) );
  189. }
  190. if (count ( $args ) != 2) {
  191. $output = "[BH] Usage: /bh defencewand [arena name].\n";
  192. $player->sendMessage ( $output );
  193. return;
  194. }
  195. if (! isset ( $this->getPlugin ()->defences [$args [1]] )) {
  196. $output = "[BH] Name not found!.\n";
  197. $player->sendMessage ( $output );
  198. return;
  199. }
  200. $this->getPlugin ()->setupModeAction = self::COMMAND_DEFENCE_POSITION;
  201. $this->getPlugin ()->setupModeData = $args [1];
  202. $player->sendMessage ( "[BH] Break a block to set the #1 position, place for the #1.\n" );
  203. }
  204. public function handleDeSelCommand($sender) {
  205. $session = & $this->session ( $sender );
  206. $session ["selection"] = array (
  207. false,
  208. false
  209. );
  210. unset ( $session ["wand-pos1"] );
  211. unset ( $session ["wand-pos2"] );
  212. $output = "Selection cleared.\n";
  213. $sender->sendMessage ( $output );
  214. }
  215. /**
  216. * Recursively delete a directory
  217. *
  218. * @param string $dir
  219. * Directory name
  220. * @param boolean $deleteRootToo
  221. * Delete specified top-level directory as well
  222. */
  223. public function unlinkRecursive($dir, $deleteRootToo) {
  224. if (! $dh = @opendir ( $dir )) {
  225. return;
  226. }
  227. while ( false !== ($obj = readdir ( $dh )) ) {
  228. if ($obj == '.' || $obj == '..') {
  229. continue;
  230. }
  231. if (! @unlink ( $dir . '/' . $obj )) {
  232. $this->unlinkRecursive ( $dir . '/' . $obj, true );
  233. }
  234. }
  235. closedir ( $dh );
  236. if ($deleteRootToo) {
  237. @rmdir ( $dir );
  238. }
  239. return;
  240. }
  241. private function hasCommandAccess(CommandSender $sender) {
  242. if ($sender->getName () == "CONSOLE") {
  243. return true;
  244. } elseif ($sender->isOp ()) {
  245. return true;
  246. }
  247. return false;
  248. }
  249. public function loadDefences() {
  250. $path = $this->getPlugin ()->getDataFolder () . DefenceModel::DEFENCE_DIR;
  251. if (! file_exists ( $path )) {
  252. @mkdir ( $path, 0755, true );
  253. // $resource = new \SplFileInfo($file_name);
  254. foreach ( $this->plugin->getResources () as $resource ) {
  255. if (! $resource->isDir ()) {
  256. $fp = $resource->getPathname ();
  257. if (strpos ( $fp, "defence" ) !== false) {
  258. $this->log ( " *** setup defence: " . $resource->getFilename () );
  259. copy ( $resource->getPathname (), $path . $resource->getFilename () );
  260. }
  261. }
  262. }
  263. }
  264. $this->log ( "#loading defences on " . $path );
  265. $handler = opendir ( $path );
  266. while ( ($filename = readdir ( $handler )) !== false ) {
  267. $this->log ( $filename );
  268. if ($filename != "." && $filename != "..") {
  269. $data = new Config ( $path . $filename, Config::YAML );
  270. Server::getInstance ()->loadLevel ( $data->get ( "levelName" ) );
  271. $pLevel = Server::getInstance ()->getLevelByName ( $data->get ( "levelName" ) );
  272. // if (($pLevel = Server::getInstance ()->getLevelByName ( $data->get ( "levelName" ) )) === null)
  273. // continue;
  274. $name = str_replace ( ".yml", "", $filename );
  275. $p1 = new Position ( $data->get ( "point1X" ), $data->get ( "point1Y" ), $data->get ( "point1Z" ), $pLevel );
  276. $p2 = new Position ( $data->get ( "point2X" ), $data->get ( "point2Y" ), $data->get ( "point2Z" ), $pLevel );
  277. $entrance = new Position ( $data->get ( "entranceX" ), $data->get ( "entranceY" ), $data->get ( "entranceZ" ), $pLevel );
  278. $name = $data->get ( "name" );
  279. $type = $data->get ( "type" );
  280. $levelName = $data->get ( "levelName" );
  281. $effect = $data->get ( "effect" );
  282. $newdefence = new DefenceModel ( $name );
  283. $newdefence->effect = $effect;
  284. $newdefence->levelName = $levelName;
  285. $newdefence->p1 = $p1;
  286. $newdefence->p2 = $p2;
  287. $newdefence->entrance = $entrance;
  288. $this->defences [$name] = $newdefence;
  289. }
  290. }
  291. closedir ( $handler );
  292. }
  293. /**
  294. *
  295. * @param Player $player
  296. * @param $b
  297. * @internal param BlockBreakEvent $event
  298. */
  299. public function handleBlockBreakSelection(Player $player, $b) {
  300. $output="[BH] ";
  301. if ($player instanceof Player) {
  302. if ($this->getPlugin ()->setupModeAction == DefenceManager::COMMAND_DEFENCE_POSITION) {
  303. $session = &$this->getPlugin ()->defenceManager->session ( $player );
  304. if ($session != null && $session ["wand-usage"] == true) {
  305. if (! isset ( $session ["wand-pos1"] ) || $session ["wand-pos1"] == null) {
  306. $session ["wand-pos1"] = $b;
  307. $this->getPlugin ()->defenceManager->setPosition1 ( $session, new Position ( $b->x - 0.5, $b->y, $b->z - 0.5, $player->getLevel () ), $output );
  308. // setup
  309. $defence = $this->getPlugin ()->defenceManager->defences [$this->getPlugin ()->setupModeData];
  310. $defence->p1 = new Position ( $b->x, $b->y, $b->z );
  311. $defence->save ( $this->getPlugin ()->getDataFolder () );
  312. $this->getPlugin ()->defenceManager->defences [$this->getPlugin ()->setupModeData] = $defence;
  313. $player->sendMessage ( "Set Defence Position #1 [" . round ( $b->x ) . " " . round ( $b->y ) . " " . round ( $b->z ));
  314. $player->sendMessage ( $output );
  315. return;
  316. }
  317. if (! isset ( $session ["wand-pos2"] ) || $session ["wand-pos2"] == null) {
  318. $session ["wand-pos2"] = $b;
  319. $this->getPlugin ()->defenceManager->setPosition2 ( $session, new Position ( $b->x - 0.5, $b->y, $b->z - 0.5, $player->getLevel () ), $output );
  320. // setup
  321. $defence = $this->getPlugin ()->defenceManager->defences [$this->getPlugin ()->setupModeData];
  322. $defence->p2 = new Position ( $b->x, $b->y, $b->z );
  323. $defence->save ( $this->getPlugin ()->getDataFolder () );
  324. $this->getPlugin ()->defenceManager->defences [$this->getPlugin ()->setupModeData] = $defence;
  325. $player->sendMessage ( "Set Defence Position #2 [" . round ( $b->x ) . " " . round ( $b->y ) . " " . round ( $b->z ) ) ;
  326. $this->getPlugin ()->setupModeAction = "";
  327. $this->getPlugin ()->setupModeData = "";
  328. $player->sendMessage ( $output );
  329. return;
  330. }
  331. }
  332. }
  333. }
  334. }
  335. public function listCachedDefences(Player $player) {
  336. $i = 1;
  337. foreach ( $this->getPlugin ()->defenceManager->defences as $arena ) {
  338. $player->sendMessage ( $i . "." . $arena->name );
  339. $i ++;
  340. }
  341. }
  342. /**
  343. * Private clone method to prevent cloning of the instance of the
  344. * *Singleton* instance.
  345. *
  346. * @return void
  347. */
  348. private function __clone()
  349. {
  350. }
  351. /**
  352. * Private unserialize method to prevent unserializing of the *Singleton*
  353. * instance.
  354. *
  355. * @return void
  356. */
  357. private function __wakeup()
  358. {
  359. }
  360. }