├── .gitignore ├── README.md ├── plugin.yml ├── resources └── config.yml └── src └── Enes5519 └── ShopMenu ├── ShopMenu.php ├── forms ├── BuyForm.php ├── ItemsForm.php └── ShopForm.php └── lang ├── Lang.php └── locale ├── en_US.ini └── tr_TR.ini /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/* -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ShopMenu 2 | Shop plugin with forms for [Altay](http://github.com/TuranicTeam/Altay). 3 | This plugin support EconomyAPI. 4 | 5 | ## USAGE 6 | * Type /shop in the chat. 7 | 8 | ## IMAGES 9 | Screenshot_20180623_132913 10 | Screenshot_20180623_132932 11 | Screenshot_20180623_132938 12 | Screenshot_20180623_132951 -------------------------------------------------------------------------------- /plugin.yml: -------------------------------------------------------------------------------- 1 | name: ShopMenu 2 | main: Enes5519\ShopMenu\ShopMenu 3 | version: 1.0 4 | api: 3.0.0 5 | author: Enes5519 6 | 7 | commands: 8 | shop: 9 | description: Open shop menu 10 | permission: enes5519.openshop.command 11 | overloads: 12 | default: 13 | 14 | permission: 15 | enes5519.shopmenu.command: 16 | default: true -------------------------------------------------------------------------------- /resources/config.yml: -------------------------------------------------------------------------------- 1 | categories: 2 | weapons: 3 | img-path: 'textures/items/iron_sword' 4 | text: "§6Weapons" 5 | armors: 6 | img-url: 'https://image.ibb.co/cRO3h8/skin_2013052515355615141both.png' 7 | text: "§bArmors" 8 | 9 | items: 10 | # itemId : itemMeta : optionName : price per item count : url/path : imgpath 11 | # Example : 276:0:Sword:2500:path:textures/items/diamond_sword 12 | weapons: 13 | - "268:0:Wooden Sword:100:path:textures/items/wood_sword" 14 | - "272:0:Stone Sword:200:path:textures/items/stone_sword" 15 | - "283:0:Golden Sword:300:path:textures/items/gold_sword" 16 | - "267:0:Iron Sword:400:path:textures/items/iron_sword" 17 | - "276:0:Diamond Sword:500:path:textures/items/diamond_sword" 18 | - "261:0:Bow:600:path:textures/items/bow_standby" 19 | - "262:0:Arrow:1000:path:textures/items/arrow" 20 | armors: 21 | - "310:0:§bDiamond Helmet:250:path:textures/items/diamond_helmet" 22 | - "311:0:§bDiamond Chestplate:350:path:textures/items/diamond_chestplate" 23 | - "312:0:§bDiamond Leggings:450:path:textures/items/diamond_leggings" 24 | - "313:0:§bDiamond Boots:550:path:textures/items/diamond_boots" -------------------------------------------------------------------------------- /src/Enes5519/ShopMenu/ShopMenu.php: -------------------------------------------------------------------------------- 1 | getLogger()->error("EconomyAPI is required to use this plugin."); 48 | $this->setEnabled(false); 49 | return; 50 | } 51 | 52 | $this->economyAPI = EconomyAPI::getInstance(); 53 | 54 | @mkdir($this->getDataFolder()); 55 | $this->saveDefaultConfig(); 56 | Lang::init(); 57 | } 58 | 59 | public function onCommand(CommandSender $sender, Command $command, string $label, array $args) : bool{ 60 | if(!($sender instanceof Player) or !($command->testPermission($sender))){ 61 | return true; 62 | } 63 | 64 | $sender->sendForm(new ShopForm($sender)); 65 | 66 | return true; 67 | } 68 | 69 | public function getEconomyAPI() : EconomyAPI{ 70 | return $this->economyAPI; 71 | } 72 | 73 | public static function createIconFromConfigData(array $data) : ?FormIcon{ 74 | $check = [FormIcon::IMAGE_TYPE_URL, FormIcon::IMAGE_TYPE_PATH]; 75 | foreach($check as $c) 76 | if(isset($data["img-".$c])) 77 | return new FormIcon($data["img-".$c], $c); 78 | 79 | return null; 80 | } 81 | 82 | } -------------------------------------------------------------------------------- /src/Enes5519/ShopMenu/forms/BuyForm.php: -------------------------------------------------------------------------------- 1 | item = $item; 47 | $this->price = $price; 48 | $this->cfg = ShopMenu::getAPI()->getConfig(); 49 | $this->lang = new Lang($player); 50 | $this->monetaryUnit = EconomyAPI::getInstance()->getMonetaryUnit(); 51 | 52 | parent::__construct($this->lang->translate("title.buy"), [ 53 | new Label(str_pad($this->lang->translate("your.money", [EconomyAPI::getInstance()->myMoney($player), $this->monetaryUnit]), 34, " ", STR_PAD_LEFT)."\n\n\n"), 54 | new Label($this->lang->translate("items.to.buy", [$item->getName()])."\n".$this->lang->translate("price", [$price, $this->monetaryUnit])."\n\n"), 55 | new Slider($this->lang->translate("amount"), 1, 64) 56 | ]); 57 | } 58 | 59 | public function onSubmit(Player $player) : ?Form{ 60 | $miktar = (int) $this->getElement(2)->getValue(); 61 | $ucret = $this->price * $miktar; 62 | $item = $this->item->setCount($miktar); 63 | 64 | if($player->getInventory()->canAddItem($item)){ 65 | if(EconomyAPI::getInstance()->myMoney($player) >= $ucret){ 66 | EconomyAPI::getInstance()->reduceMoney($player, $ucret); 67 | $player->getInventory()->addItem($item); 68 | $player->sendMessage($this->lang->translate("bought", [$miktar, $item->getName(), $ucret, $this->monetaryUnit])); 69 | }else{ 70 | $player->sendMessage($this->lang->translate("not.enough.money", [$miktar, $item->getName()])); 71 | } 72 | }else{ 73 | $player->sendMessage($this->lang->translate("inventory.full")); 74 | } 75 | 76 | return null; 77 | } 78 | } -------------------------------------------------------------------------------- /src/Enes5519/ShopMenu/forms/ItemsForm.php: -------------------------------------------------------------------------------- 1 | index = $index; 34 | parent::__construct($title, "", $options); 35 | } 36 | 37 | public function onSubmit(Player $player) : ?Form{ 38 | $index = $this->getSelectedOptionIndex(); 39 | $decode = ShopMenu::getAPI()->getConfig()->get("items"); 40 | $decode = $decode[$this->index]; 41 | $decode = explode(":", $decode[$index]); 42 | 43 | $item = ItemFactory::get((int) $decode[0], (int) $decode[1]); 44 | 45 | return new BuyForm($player, $item, intval($decode[3])); 46 | } 47 | } -------------------------------------------------------------------------------- /src/Enes5519/ShopMenu/forms/ShopForm.php: -------------------------------------------------------------------------------- 1 | cfg = ShopMenu::getAPI()->getConfig(); 40 | $this->lang = new Lang($player); 41 | 42 | $options = []; 43 | foreach($this->cfg->get("categories", []) as $name => $data){ 44 | $options[] = new MenuOption(isset($data["text"]) ? $data["text"] : $name, ShopMenu::createIconFromConfigData($data)); 45 | } 46 | 47 | parent::__construct( 48 | $this->lang->translate("title.categories"), 49 | "", 50 | $options 51 | ); 52 | } 53 | 54 | public function onSubmit(Player $player) : ?Form{ 55 | $index = $this->getSelectedOptionIndex(); 56 | $categories = $this->cfg->get("categories", []); 57 | $index = array_keys($categories)[$index]; 58 | 59 | $items = $this->cfg->get("items"); 60 | if(isset($items[$index]) && is_array($items[$index])){ 61 | $options = []; 62 | foreach($items[$index] as $item){ 63 | $decode = explode(":", $item); 64 | 65 | $imgPath = ""; 66 | foreach($decode as $i => $value) 67 | if($i >= 5) $imgPath .= $value; 68 | 69 | $options[] = new MenuOption($this->lang->translate("title.item", [$decode[2], $decode[3], EconomyAPI::getInstance()->getMonetaryUnit()]), new FormIcon($imgPath, $decode[4])); 70 | } 71 | 72 | return new ItemsForm($this->getSelectedOption()->getText(), $options, $index); 73 | } 74 | 75 | return null; 76 | } 77 | } -------------------------------------------------------------------------------- /src/Enes5519/ShopMenu/lang/Lang.php: -------------------------------------------------------------------------------- 1 | getLocale(); 37 | $this->locale = $locale; 38 | 39 | if(!self::loadLang($file = __DIR__."/locale/$locale.ini", $this->lang)){ 40 | MainLogger::getLogger()->debug("Missing required language file $file"); 41 | } 42 | } 43 | 44 | public static function init(){ 45 | if(!self::loadLang($file = __DIR__."/locale/".self::DEFAULT_LOCALE.".ini", self::$defaultLang)){ 46 | MainLogger::getLogger()->error("Missing required language file $file"); 47 | } 48 | } 49 | 50 | protected static function loadLang(string $path, array &$output){ 51 | if(file_exists($path)){ 52 | $output = array_map('stripcslashes', parse_ini_file($path, false, INI_SCANNER_RAW)); 53 | return true; 54 | } 55 | 56 | return false; 57 | } 58 | 59 | public function translate(string $str, array $params = []) : string{ 60 | $baseText = $this->get($str); 61 | $baseText = $this->parseTranslation($baseText !== null ? $baseText : $str); 62 | 63 | foreach($params as $i => $p){ 64 | $baseText = str_replace("{%$i}", $this->parseTranslation((string) $p), $baseText); 65 | } 66 | 67 | return $baseText; 68 | } 69 | 70 | public function get(string $id){ 71 | return isset($this->lang[$id]) ? $this->lang[$id] : (isset(self::$defaultLang[$id]) ? self::$defaultLang[$id] : $id); 72 | } 73 | 74 | public function internalGet(string $id){ 75 | return isset($this->lang[$id]) ? $this->lang[$id] : (isset(self::$defaultLang[$id]) ? self::$defaultLang[$id] : null); 76 | } 77 | 78 | protected function parseTranslation(string $text) : string{ 79 | $newString = ""; 80 | 81 | $replaceString = null; 82 | 83 | $len = strlen($text); 84 | for($i = 0; $i < $len; ++$i){ 85 | $c = $text{$i}; 86 | if($replaceString !== null){ 87 | $ord = ord($c); 88 | if( 89 | ($ord >= 0x30 and $ord <= 0x39) // 0-9 90 | or ($ord >= 0x41 and $ord <= 0x5a) // A-Z 91 | or ($ord >= 0x61 and $ord <= 0x7a) or // a-z 92 | $c === "." or $c === "-" 93 | ){ 94 | $replaceString .= $c; 95 | }else{ 96 | if(($t = $this->internalGet(substr($replaceString, 1))) !== null){ 97 | $newString .= $t; 98 | }else{ 99 | $newString .= $replaceString; 100 | } 101 | $replaceString = null; 102 | 103 | if($c === "%"){ 104 | $replaceString = $c; 105 | }else{ 106 | $newString .= $c; 107 | } 108 | } 109 | }elseif($c === "%"){ 110 | $replaceString = $c; 111 | }else{ 112 | $newString .= $c; 113 | } 114 | } 115 | 116 | if($replaceString !== null){ 117 | if(($t = $this->internalGet(substr($replaceString, 1))) !== null){ 118 | $newString .= $t; 119 | }else{ 120 | $newString .= $replaceString; 121 | } 122 | } 123 | 124 | return $newString; 125 | } 126 | } -------------------------------------------------------------------------------- /src/Enes5519/ShopMenu/lang/locale/en_US.ini: -------------------------------------------------------------------------------- 1 | title.categories=Categories 2 | title.buy=Buy 3 | title.item={%0} §8: §f{%1}{%2} 4 | 5 | your.money=§bYour Money: §6{%0}{%1} 6 | items.to.buy="§fItems to buy: §a{%0}" 7 | price=§fPrice: §6{%0}{%1} 8 | amount=Amount 9 | bought=§aYou have bought §fx{%0} {%1} §afor §f{%2}{%3} 10 | not.enough.money=§cYou do not have enough money to buy §4x{%0} {%1}. 11 | inventory.full="§cYour inventory may be full. -------------------------------------------------------------------------------- /src/Enes5519/ShopMenu/lang/locale/tr_TR.ini: -------------------------------------------------------------------------------- 1 | title.categories=Kategoriler 2 | title.buy=Satın Al 3 | title.item={%0} §8: §f{%1}{%2} 4 | 5 | your.money=§bParanız: §6{%0}{%1} 6 | items.to.buy="§fAlınacak eşya: §a{%0}" 7 | price=§fÜcret: §6{%0}{%1} 8 | amount=Miktar 9 | bought=§fx{%0} {%1} §aeşyasını §f{%2}{%3} §aparaya satın aldın. 10 | not.enough.money=§4x{%0} {%1} §ceşyasını alacak kadar paran yok. 11 | inventory.full=§cEnvanterin dolu olabilir. --------------------------------------------------------------------------------