First of all, I'm sorry that my English is not good. My problem is with the formapi. I'm adding a slider to my menu, I don't have any problems, but I want the slider option to be as much as the number of items in the inventory. I tried many ways but couldn't find a solution. Please help. PHP: public function Stone(Player $o){ $f = $this->getServer()->getPluginManager()->getPlugin("FormAPI")->createCustomForm(function(Player $o, array $data){ if($data[0] === null){ if(!isset($data[1])){ return true; } }if(is_numeric($data[1])){ if($o->getInventory()->contains(Item::get(1, 0, $data[1]))){ $o->getInventory()->removeItem(Item::get(1, 0, $data[1])); EconomyAPI::getInstance()->addMoney($o->getName(), 1 * $data[1]); $o->sendPopup("§a" . $data[1] . " adet taş satıldı!"); } }else{ $o->sendPopup("§cBir sayı gir!");}}); $f->setTitle("Sat->Bloklar->Stone"); $f->addLabel("\n§f» Adet Fiyatı: §a1TL\n"); $f->addSlider("Değer Gir" , 1, 64, 1); // I want it to be as much as at max inventory. $f->addInput("", "Satılacak miktarı buraya giriniz."); $f->sendToPlayer($o);}
The inventory has 27 storage slots, and 9 hotbar slots. (excluding the Armor slots and off-hand slot) Each slot can hold upto 64 or 16 items depending on the category. Therefore 27*64 = 1,728 and 9*64 = 576, simple addition. 1,728 + 576 = 2,304. This is the total amount of items you can have in your inventory. PHP: $f->addSlider("Slider" , 1, 2304, 1); // I want it to be as much as at max inventory. Unless you want to calculate how many items does a player have this could work. PHP: $items_count = 0;foreach($player->getInventory()->getContents() as $item){ $items_count += $item->getCount(); // Add item count to variable}$f->addSlider("Slider" , 1, $items_count, 1);
This code working but its calculates all inventory. It can only calculate the amount of stone? Edit: I solved it. Now just calculates stone.
@ErikX i got an error. If the amount is less than 1, it gives an internal error. I'm sending you my code. I couldn't find how to fix it. PHP: public function Stone(Player $o){ $f = $this->getServer()->getPluginManager()->getPlugin("FormAPI")->createCustomForm(function(Player $o, array $data){ if($data[0] === null){ if(!isset($data[1])){ return true; } } if(is_numeric($data[1])){ if($o->getInventory()->contains(Item::get(1, 0, $data[1]))){ $o->getInventory()->removeItem(Item::get(1, 0, $data[1])); EconomyAPI::getInstance()->addMoney($o->getName(), 1 * $data[1]); $o->sendPopup("§a" . $data[1] . " adet taş satıldı!"); } }else{ $o->sendPopup("§cBir sayı gir!");} foreach($o->getInventory()->getContents() as $item){ if($item->getID() == "1"){ $miktar = $item->getCount(); }} if($miktar < 1){ $o->sendPopup("you dont have stone"); } });
$miktar is not defined. But its working if i have stone. If i didn't have stone server crashing with internal error. I want to make; if players dont have stone, stone ui dont open and sendPopup you dont have stone.
PHP: foreach($o->getInventory()->getContents() as $item){ if($item->getId() === "1"){ // How is this even working? I thought getId() returns int? and this is a string... $miktar = $item->getCount(); }}$item = Item::get(Item::STONE);if(!$player->getInventory()->contains($item)){ $o->sendPopup("you dont have stone");} Use function Inventory->contains()!
So an example would be like this: PHP: // Code where it calls the form function$item = Item::get(Item::STONE);if(!$player->getInventory()->contains($item)){ $player->sendPopup("You dont have stone in your inventory!"); return false;}$this->openForm($player);return true;