1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

FormAPI

Discussion in 'Development' started by RiseMCPE, Jul 5, 2021.

  1. RiseMCPE

    RiseMCPE Spider

    Messages:
    14
    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(10$data[1]))){
                            
    $o->getInventory()->removeItem(Item::get(10$data[1]));
                            
    EconomyAPI::getInstance()->addMoney($o->getName(), $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" 1641); // I want it to be as much as at max inventory.
                
    $f->addInput("""Satılacak miktarı buraya giriniz.");
                
    $f->sendToPlayer($o);}
     
  2. Axon

    Axon Zombie

    Messages:
    276
    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" 123041); // 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_count1);
     
    Agent likes this.
  3. RiseMCPE

    RiseMCPE Spider

    Messages:
    14
    This code working but its calculates all inventory. It can only calculate the amount of stone?

    Edit: I solved it. Now just calculates stone.
     
    Last edited: Jul 5, 2021
  4. RiseMCPE

    RiseMCPE Spider

    Messages:
    14
    @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(10$data[1]))){
                            
    $o->getInventory()->removeItem(Item::get(10$data[1]));
                            
    EconomyAPI::getInstance()->addMoney($o->getName(), $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");
                        }

                    });
     
  5. Axon

    Axon Zombie

    Messages:
    276
    What’s the error?
     
    Agent likes this.
  6. RiseMCPE

    RiseMCPE Spider

    Messages:
    14
    $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.
     
  7. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    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()!
     
    Agent likes this.
  8. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    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
     
    Agent and Axon like this.
  9. RiseMCPE

    RiseMCPE Spider

    Messages:
    14
    Thanks, working.
     
    minijaham likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.