PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/GoldStd/src/aliuly/goldstd/Main.php

https://gitlab.com/Skull3x/pocketmine-plugins
PHP | 247 lines | 226 code | 8 blank | 13 comment | 68 complexity | 85ff3df4e66c7f42dbf16e9c19985294 MD5 | raw file
  1. <?php
  2. namespace aliuly\goldstd;
  3. use pocketmine\plugin\PluginBase;
  4. use pocketmine\command\CommandExecutor;
  5. use pocketmine\command\CommandSender;
  6. use pocketmine\command\Command;
  7. use pocketmine\utils\TextFormat;
  8. use pocketmine\Player;
  9. use pocketmine\item\Item;
  10. use pocketmine\utils\Config;
  11. use aliuly\goldstd\common\mc;
  12. use aliuly\goldstd\common\MPMU;
  13. use aliuly\goldstd\common\MoneyAPI;
  14. class Main extends PluginBase implements CommandExecutor {
  15. protected $currency;
  16. protected $trading;
  17. protected $keepers;
  18. protected $api;
  19. public function getCurrency() { return $this->currency; }
  20. public function onEnable(){
  21. if (!is_dir($this->getDataFolder())) mkdir($this->getDataFolder());
  22. mc::plugin_init($this,$this->getFile());
  23. $defaults = [
  24. "version" => $this->getDescription()->getVersion(),
  25. //= cfg:settings
  26. "settings" => [
  27. "# currency" => "Item to use for currency",// false or zero disables currency exchange.
  28. "currency" => "GOLD_INGOT",
  29. "# signs" => "set to true to enable shops|casino signs",
  30. "signs" => true,
  31. ],
  32. //= cfg:other-sections
  33. "# trade-goods" => "List of tradeable goods",
  34. "trade-goods" => [],
  35. "defaults" => TradingMgr::defaults(),
  36. "# signs" => "Text used to identify GoldStd signs",
  37. "signs" => SignMgr::defaults(),
  38. "shop-keepers" => ShopKeep::defaults(),
  39. ];
  40. $this->saveResource("shops.yml");
  41. $cf = (new Config($this->getDataFolder()."config.yml",
  42. Config::YAML,$defaults))->getAll();
  43. if ($cf["settings"]["currency"]) {
  44. $item = Item::fromString($cf["settings"]["currency"]);
  45. if ($item->getId() == Item::AIR) {
  46. $this->getLogger()->error(TextFormat::RED.
  47. mc::_("Invalid currency item"));
  48. $this->currency = Item::GOLD_INGOT;
  49. } else {
  50. $this->currency = $item->getId();
  51. }
  52. $this->api = null;
  53. } else {
  54. // No currency defined, so we use an external API
  55. $pm = $this->getServer()->getPluginManager();
  56. if(!($money = $pm->getPlugin("PocketMoney"))
  57. && !($money = $pm->getPlugin("EconomyAPI"))
  58. && !($money = $pm->getPlugin("MassiveEconomy"))){
  59. $this->api = null;
  60. $this->getLogger()->warning(TextFormat::YELLOW.
  61. mc::_("Using GOLD_INGOT as currency"));
  62. $this->currency = Item::GOLD_INGOT;
  63. } else {
  64. $this->api = $money;
  65. $this->currency = false;
  66. $this->getLogger()->info(TextFormat::BLUE.
  67. mc::_("Using Money API of %1%",
  68. $money->getFullName()));
  69. }
  70. }
  71. if ($this->currency || $cf["trade-goods"]) {
  72. $this->trading = new TradingMgr($this,
  73. $cf["trade-goods"],
  74. $cf["defaults"]);
  75. } else {
  76. $this->trading = null;
  77. $this->getLogger()->warning(TextFormat::RED.
  78. mc::_("Goods trading disabled!"));
  79. }
  80. if ($cf["signs"]) {
  81. new SignMgr($this,$cf["signs"]);
  82. } else {
  83. $this->getLogger()->warning(TextFormat::RED.
  84. mc::_("SignShops disabled"));
  85. }
  86. if (ShopKeep::cfEnabled($cf["shop-keepers"])) {
  87. $this->saveResource("default.skin");
  88. $this->keepers = new ShopKeep($this,$cf["shop-keepers"]);
  89. if (!$this->keepers->isEnabled()) {
  90. $this->keepers = null;
  91. }
  92. } else {
  93. $this->keepers = null;
  94. $this->getLogger()->warning(TextFormat::RED.
  95. mc::_("Shop-Keepers disabled"));
  96. }
  97. }
  98. public function isWeapon($item) {
  99. return $item->isAxe() || $item->isSword() || $item->isPickaxe();
  100. }
  101. //////////////////////////////////////////////////////////////////////
  102. //
  103. // Command implementations
  104. //
  105. //////////////////////////////////////////////////////////////////////
  106. public function onCommand(CommandSender $sender, Command $cmd, $label, array $args) {
  107. switch($cmd->getName()) {
  108. case "pay":
  109. if (!$this->trading) {
  110. $sender->sendMessage(mc::_("PAY command has been disabled"));
  111. return true;
  112. }
  113. if (!MPMU::inGame($sender)) return false;
  114. if ($sender->isCreative() || $sender->isSpectator()) {
  115. $sender->sendMessage(mc::_("You cannot use this in creative or specator mode"));
  116. return true;
  117. }
  118. if (count($args) == 1) {
  119. if (is_numeric($args[0])) {
  120. $money = intval($args[0]);
  121. if ($this->getMoney($sender->getName()) < $money) {
  122. $sender->sendMessage(mc::_("You do not have enough money"));
  123. return true;
  124. }
  125. $this->trading->setAttr($sender,"payment",$money);
  126. $sender->sendMessage(mc::_("Next payout will be for %1%G",$money));
  127. return true;
  128. }
  129. return false;
  130. } elseif (count($args) == 0) {
  131. $sender->sendMessage(mc::_("Next payout will be for %1%G",
  132. $this->trading->getAttr($sender,"payment")));
  133. return true;
  134. }
  135. return false;
  136. case "balance":
  137. if (!MPMU::inGame($sender)) return false;
  138. if ($sender->isCreative() || $sender->isSpectator()) {
  139. $sender->sendMessage(mc::_("You cannot use this in creative or specator mode"));
  140. return true;
  141. }
  142. $sender->sendMessage(mc::_("You have %1%G",$this->getMoney($sender->getName())));
  143. return true;
  144. case "shopkeep":
  145. if ($this->keepers) return $this->keepers->subCmd($sender,$args);
  146. $sender->sendMessage(mc::_("shopkeep command disabled"));
  147. return true;
  148. }
  149. return false;
  150. }
  151. //////////////////////////////////////////////////////////////////////
  152. //
  153. // Economy/Money API
  154. //
  155. //////////////////////////////////////////////////////////////////////
  156. public function giveMoney($player,$money) {
  157. if ($this->api) return MoneyAPI::grantMoney($this->api,$player,$money);
  158. if ($player instanceof Player) {
  159. $pl = $player;
  160. $player = $pl->getName();
  161. } else {
  162. $pl = $this->getServer()->getPlayer($player);
  163. if (!$pl) return false;
  164. }
  165. if ($pl->isCreative() || $pl->isSpectator()) return false;
  166. while ($money > 0) {
  167. $item = Item::get($this->currency);
  168. if ($money > $item->getMaxStackSize()) {
  169. $item->setCount($item->getMaxStackSize());
  170. } else {
  171. $item->setCount($money);
  172. }
  173. $money -= $item->getCount();
  174. $pl->getInventory()->addItem(clone $item);
  175. }
  176. return true;
  177. }
  178. public function takeMoney($player,$money) {
  179. if ($this->api) return MoneyAPI::grantMoney($this->api,$player,-$money);
  180. if ($player instanceof Player) {
  181. $pl = $player;
  182. $player = $pl->getName();
  183. } else {
  184. $pl = $this->getServer()->getPlayer($player);
  185. if (!$pl) return false;
  186. }
  187. if ($pl->isCreative() || $pl->isSpectator()) return false;
  188. foreach ($pl->getInventory()->getContents() as $slot => &$item) {
  189. if ($item->getId() != $this->currency) continue;
  190. if ($item->getCount() > $money) {
  191. $item->setCount($item->getCount() - $money);
  192. $pl->getInventory()->setItem($slot,clone $item);
  193. break;
  194. }
  195. $money -= $item->getCount();
  196. $pl->getInventory()->clear($slot);
  197. if (!$money) break;
  198. }
  199. if ($money) return $money; // They don't have enough money!
  200. return true;
  201. }
  202. public function grantMoney($p,$money) {
  203. if ($this->api) return MoneyAPI::grantMoney($this->api,$p,$money);
  204. if ($money < 0) {
  205. return $this->takeMoney($p,-$money);
  206. } elseif ($money > 0) {
  207. return $this->giveMoney($p,$money);
  208. } else {
  209. return true;
  210. }
  211. }
  212. public function getMoney($player) {
  213. if ($this->api) return MoneyAPI::getMoney($this->api,$player);
  214. if ($player instanceof Player) {
  215. $pl = $player;
  216. $player = $pl->getName();
  217. } else {
  218. $pl = $this->getServer()->getPlayer($player);
  219. if (!$pl) return null;
  220. }
  221. $g = 0;
  222. if ($pl->isCreative() || $pl->isSpectator()) return null;
  223. foreach ($pl->getInventory()->getContents() as $slot => &$item) {
  224. if ($item->getId() != $this->currency) continue;
  225. $g += $item->getCount();
  226. }
  227. return $g;
  228. }
  229. public function setMoney($player,$money) {
  230. $now = $this->getMoney($player);
  231. if ($money < $now) {
  232. return $this->takeMoney($player, $now - $money);
  233. } elseif ($money > $now) {
  234. return $this->giveMoney($player, $money - $now);
  235. } elseif ($money == $now) return true; // Nothing to do!
  236. $this->getLogger()->error("INTERNAL ERROR AT ".__FILE__.",".__LINE__);
  237. return false;
  238. }
  239. }