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

/Block-Hunt/Block-Hunt-master/src/NetherManiacsKingdomTeam/Block-Hunt/itemcase/ItemCaseManager.php

https://gitlab.com/Skull3x/WorkingInProgress-Plugins-Sourcecode-For-Dev
PHP | 273 lines | 194 code | 19 blank | 60 comment | 49 complexity | 7c229cbc7d32a8020299dcfc421412dd MD5 | raw file
  1. <?php
  2. namespace mcg76\game\blockhunt\itemcase;
  3. use pocketmine\math\Vector3 as Vector3;
  4. use pocketmine\level\Position;
  5. use pocketmine\Server;
  6. use pocketmine\utils\Config;
  7. use pocketmine\Player;
  8. use pocketmine\event\player\PlayerInteractEvent;
  9. use mcg76\game\blockhunt\BlockHuntPlugIn;
  10. use mcg76\game\blockhunt\MiniGameBase;
  11. /**
  12. * MCPE BlockHunt Minigame - Made by minecraftgenius76
  13. *
  14. * You're allowed to use for own usage only "as-is".
  15. * you're not allowed to republish or resell or for any commercial purpose.
  16. *
  17. * Thanks for your cooperate!
  18. *
  19. * Copyright (C) 2014 minecraftgenius76
  20. * YouTube Channel: http://www.youtube.com/user/minecraftgenius76
  21. *
  22. * @author minecraftgenius76
  23. *
  24. */
  25. class ItemCaseManager extends MiniGameBase {
  26. const ITEMCASE_DIR = 'itemcase/';
  27. const ACTION_ADD_ITEM_CASE = "action_addcase";
  28. const ACTION_DEL_ITEM_CASE = "action_delcase";
  29. const ACTION_SET_ITEM_CASE = "action_setcase";
  30. const ACTION_NULL = "";
  31. const ACTION_SET_ITEM_CASE_LINK = "action_setcase_link";
  32. const ACTION_SET_ITEM_CASE_BUY = "action_setcase_buy";
  33. // item cases
  34. public $storeItemCases = [ ];
  35. public $storeItemCasesLinksPos = [ ];
  36. public $storeItemCasesBuyPos = [ ];
  37. public $npcsSpawns = [ ];
  38. /**
  39. *
  40. * @param BlockHuntPlugIn $plugin
  41. */
  42. public function __construct(BlockHuntPlugIn $plugin) {
  43. parent::__construct ( $plugin );
  44. }
  45. /**
  46. * Returns the *Singleton* instance of this class.
  47. *
  48. * @staticvar Singleton $instance The *Singleton* instances of this class.
  49. *
  50. * @return Singleton The *Singleton* instance.
  51. */
  52. public static function getInstance(BlockHuntPlugIn $plugin)
  53. {
  54. static $instance = null;
  55. if (null === $instance) {
  56. $instance = new ItemCaseManager($plugin);
  57. }
  58. return $instance;
  59. }
  60. /**
  61. */
  62. public function loadItemCases() {
  63. $path = $this->getPlugin ()->getDataFolder () . self::ITEMCASE_DIR;
  64. if (! file_exists ( $path )) {
  65. @mkdir ( $path, 0755, true );
  66. // $resource = new \SplFileInfo($file_name);
  67. foreach ( $this->plugin->getResources () as $resource ) {
  68. if (! $resource->isDir ()) {
  69. $fp = $resource->getPathname ();
  70. if (strpos ( $fp, "itemcase" ) !== false) {
  71. $this->log ( " *** setup default [item cases]: " . $resource->getFilename () );
  72. copy ( $resource->getPathname (), $path . $resource->getFilename () );
  73. }
  74. }
  75. }
  76. }
  77. $handler = opendir ( $path );
  78. while ( ($filename = readdir ( $handler )) !== false ) {
  79. if ($filename != "." && $filename != "..") {
  80. $data = new Config ( $path . $filename, Config::YAML );
  81. $this->getPlugin ()->getLogger ()->info ( "load item-cases file:" . $path . $filename );
  82. $data->getAll ();
  83. Server::getInstance ()->loadLevel ( $data->get ( "levelName" ) );
  84. if (($pLevel = Server::getInstance ()->getLevelByName ( $data->get ( "levelName" ) )) === null) {
  85. $this->getPlugin ()->getLogger ()->info ( " error loading item-cases level :" . $data->get ( "levelName" ) );
  86. continue;
  87. }
  88. $name = str_replace ( ".yml", "", $filename );
  89. $levelname = $data->get ( "levelName" );
  90. $position = new Position ( $data->get ( "positionX" ), $data->get ( "positionY" ), $data->get ( "positionZ" ), $pLevel );
  91. $linkPos = null;
  92. $kx = $data->get ( "linkPosX" );
  93. $ky = $data->get ( "linkPosY" );
  94. $kz = $data->get ( "linkPosZ" );
  95. if ($kx != null && $ky != null && $kz != null) {
  96. $linkPos = new Position ( $kx, $ky, $kz );
  97. }
  98. $bx = $data->get ( "buyPosX" );
  99. $by = $data->get ( "buyPosY" );
  100. $bz = $data->get ( "buyPosZ" );
  101. $buyPos = new Position ( $bx, $by, $bz );
  102. $eid = $data->get ( "eid" );
  103. $itemId = $data->get ( "itemId" );
  104. $itemName = $data->get ( "itemName" );
  105. $itemQuantity = $data->get ( "quantity" );
  106. $itemStandBlockId = $data->get ( "itemStandBlockId" );
  107. $itemCoverBlockId = $data->get ( "itemCoverBlockId" );
  108. $price = $data->get ( "price" );
  109. $discount = $data->get ( "discount" );
  110. $coupon = $data->get ( "coupon" );
  111. $points = $data->get ( "points" );
  112. $itemCaseModel = new ItemCaseModel ( $eid, $name, $itemId, $itemQuantity, $price );
  113. $itemCaseModel->levelName = $levelname;
  114. $itemCaseModel->itemName = $itemName;
  115. $itemCaseModel->itemQuantity = $itemQuantity;
  116. $itemCaseModel->itemStandBlockId = $itemStandBlockId;
  117. $itemCaseModel->itemCoverBlockId = $itemCoverBlockId;
  118. $itemCaseModel->discount = $discount;
  119. $itemCaseModel->points = $points;
  120. $itemCaseModel->position = $position;
  121. $itemCaseModel->linkPos = $linkPos;
  122. $itemCaseModel->buyPos = $buyPos;
  123. $this->storeItemCases [$name] = $itemCaseModel;
  124. if ($itemCaseModel->buyPos != null) {
  125. $posKey = round ( $itemCaseModel->buyPos->x ) . "." . round ( $itemCaseModel->buyPos->y ) . "." . round ( $itemCaseModel->buyPos->z );
  126. $this->storeItemCasesBuyPos [$posKey] = $itemCaseModel;
  127. }
  128. }
  129. }
  130. closedir ( $handler );
  131. $this->getPlugin ()->getLogger ()->info ( "total loaded cases count:" . count ( $this->getPlugin ()->storeCaseManager->storeItemCases ) );
  132. // var_dump($this->pgin->storeItemCases);
  133. }
  134. /**
  135. * @param Player $sender
  136. * @param $path
  137. * @return null|string
  138. */
  139. public static function listsCases(Player $sender, $path) {
  140. $xpath = $path . self::ITEMCASE_DIR;
  141. if (! file_exists ( $xpath )) {
  142. @mkdir ( $xpath, 0777, true );
  143. return null;
  144. }
  145. $output = "Item Cases:\n";
  146. $handler = opendir ( $xpath );
  147. $i = 1;
  148. while ( ($filename = readdir ( $handler )) !== false ) {
  149. if ($filename != "." && $filename != "..") {
  150. $data = new Config ( $xpath . $filename, Config::YAML );
  151. $name = str_replace ( ".yml", "", $filename );
  152. // $this->log ($filename);
  153. $itemId = $data->get ( "itemId" );
  154. $levelname = $data->get ( "levelName" );
  155. $pos = new Position ( $data->get ( "positionX" ), $data->get ( "positionY" ), $data->get ( "positionZ" ) );
  156. $output .= $i . ". " . $name . " | (" . $itemId . ") at " . $pos->x . " " . $pos->y . " " . $pos->z . "\n";
  157. $i ++;
  158. }
  159. }
  160. // $sender->sendMessage ( $output );
  161. closedir ( $handler );
  162. return $output;
  163. }
  164. /**
  165. * @param Player $sender
  166. * @param $statueName
  167. * @return StatueModel|null
  168. */
  169. public function findStatueByName(Player $sender, $statueName) {
  170. $path = $this->getPlugin ()->getDataFolder () . self::STATUE_DIR;
  171. if (! file_exists ( $path )) {
  172. @mkdir ( $this->getPlugin ()->getDataFolder (), 0777, true );
  173. @mkdir ( $path );
  174. }
  175. $handler = opendir ( $path );
  176. while ( ($filename = readdir ( $handler )) !== false ) {
  177. if ($filename != "." && $filename != "..") {
  178. $data = new Config ( $path . $filename, Config::YAML );
  179. $name = str_replace ( ".yml", "", $filename );
  180. $levelname = $data->get ( "levelName" );
  181. $networkId = $data->get ( "networkId" );
  182. $type = $data->get ( "type" );
  183. $pos = new Position ( $data->get ( "positionX" ), $data->get ( "positionY" ), $data->get ( "positionZ" ) );
  184. if ($name == $statueName) {
  185. $this->log ( " found statue " . $name . " | " . $pos );
  186. return new StatueModel ( $name, $type, $networkId, $pos, $levelname );
  187. }
  188. }
  189. }
  190. closedir ( $handler );
  191. return null;
  192. }
  193. /**
  194. *
  195. * @param PlayerInteractEvent $event
  196. * @return boolean
  197. */
  198. public function handleTapOnItemCase(PlayerInteractEvent $event) {
  199. $success = true;
  200. $b = $event->getBlock ();
  201. if ($event->getPlayer () instanceof Player) {
  202. $player = $event->getPlayer ();
  203. foreach ( $this->storeItemCases as $itemcase ) {
  204. if ($itemcase instanceof ItemCaseModel) {
  205. if (! empty ( $itemcase->buyPos )) {
  206. $blockPosKey = round ( $b->x ) . "." . round ( $b->y ) . "." . round ( $b->z );
  207. $casePosKey = round ( $itemcase->buyPos->x ) . "." . round ( $itemcase->buyPos->y ) . "." . round ( $itemcase->buyPos->z );
  208. if ($blockPosKey === $casePosKey) {
  209. // check player balances
  210. $data = $this->getPlugin ()->profileprovider->retrievePlayerByName ( $player->getName () );
  211. if ($data === null || count ( $data ) == 0) {
  212. $this->getPlugin ()->profileprovider->addPlayer ( $player->getName () );
  213. }
  214. if ($data != null && $data [0] ["vip"] == "true") {
  215. $this->getGameKit ()->takeItemCase ( $player, $itemcase );
  216. $player->sendMessage ( $this->getMsg ( "bh.play.itemcase.vipfree" ) );
  217. $player->setNameTag ( "VIP| " . $player->getName () );
  218. break;
  219. } else {
  220. if ($data != null && $data [0] ["balance"] >= $itemcase->price) {
  221. $this->getGameKit ()->takeItemCase ( $player, $itemcase );
  222. $player->sendMessage ( $this->getMsg ( "bh.play.itemcase.thanks" ) . $itemcase->name . $this->getMsg ( "bh.play.itemcase.purchased" ) . $itemcase->price . $this->getMsg ( "bh.play.itemcase.coins" ) . "\n" );
  223. $this->getPlugin ()->profileprovider->withdraw ( $player->getName (), $itemcase->price );
  224. } elseif ($data != null && $data [0] ["balance"] < $itemcase->price) {
  225. $player->sendMessage ( $this->getMsg ( "bh.play.itemcase.notenoughtcoins" ) . $itemcase->price );
  226. }
  227. break;
  228. }
  229. }
  230. }
  231. }
  232. }
  233. }
  234. return $success;
  235. }
  236. /**
  237. * Private clone method to prevent cloning of the instance of the
  238. * *Singleton* instance.
  239. *
  240. * @return void
  241. */
  242. private function __clone()
  243. {
  244. }
  245. /**
  246. * Private unserialize method to prevent unserializing of the *Singleton*
  247. * instance.
  248. *
  249. * @return void
  250. */
  251. private function __wakeup()
  252. {
  253. }
  254. }