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

Solved How To Check Player's Money on CustomForm

Discussion in 'Development' started by byyEmirhanWSD, Feb 21, 2018.

  1. byyEmirhanWSD

    byyEmirhanWSD Witch

    Messages:
    50
    GitHub:
    EmirhanWSD
    Hey guys. I don't solve this problem. Please help me and give me an custom form example.
     
  2. armagadon159753

    armagadon159753 Zombie

    Messages:
    217
    GitHub:
    armagadon159753
    Plz i need more informations
    Which type of Form?
    Where (Title, content, label...)?
    Wath do you want to do with that?
     
  3. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    please show previous attempts too
     
    jasonwynn10 likes this.
  4. byyEmirhanWSD

    byyEmirhanWSD Witch

    Messages:
    50
    GitHub:
    EmirhanWSD
    Custom Form type
    I making shop plugin. I added amount with slider. But I dont check player's money. Please example.
     
  5. byyEmirhanWSD

    byyEmirhanWSD Witch

    Messages:
    50
    GitHub:
    EmirhanWSD
    I tested $data[0] !== null. This method worked but I don't check player's money.
    After I tested switch case. It's not worked. Exampleee! :D
     
  6. byyEmirhanWSD

    byyEmirhanWSD Witch

    Messages:
    50
    GitHub:
    EmirhanWSD
    My code :
    PHP:
    $api $this->getServer()->getPluginManager()->getPlugin("FormAPI");
            
    $eco $this->getServer()->getPluginManager()->getPlugin("EconomyAPI");
            
    $parasi $eco->myMoney($g);
            
    $f $api->createCustomForm(function (Player $g, array $data){
                
    $s $data[0];
                if(
    $s == null) {
                    return 
    true;
                
                }
                switch(
    $s) {
                    case 
    0:
                    if(!empty(
    $parasi)) {
                        if(
    $parasi 75*$s) {
                            
    $g->getInventory()->addItem(Item::get(1,0,$s));
                            
    $g->sendMessage("You boght $s stone.");
                            
    EconomyAPI::reduceMoney($g75*$s);
                            
                        }else{
                            
    $g->sendMessage("§cNot moneu.");
                            
                        }
                        
                    }
                    break;
                    
                }
                
            });
            
    $f->setTitle("§lShop");
            
    $f->addSlider("§7Amount",1,64);
            
    $f->sendToPlayer($g);
     
  7. Khaled

    Khaled Slime

    Messages:
    81
    GitHub:
    xXKHaLeD098Xx
    Did you import the EconomyAPI?
     
  8. Khaled

    Khaled Slime

    Messages:
    81
    GitHub:
    xXKHaLeD098Xx
    Also i suggest you to try that on PluginCommand its much better than the default one, i was trying FormAPI in onCommand() function and it didnt work but i tested it again with PluginCommand and it worked perfectly
     
  9. yuko fuyutsuki

    yuko fuyutsuki Slime

    Messages:
    77
    GitHub:
    fuyutsuki
    Is the logic collapsed?
    Points to note:
    PHP:
    $api $this->getServer()->getPluginManager()->getPlugin("FormAPI");
      
    $eco $this->getServer()->getPluginManager()->getPlugin("EconomyAPI");
      
    // $parasi = $eco->myMoney($g); NOTE: Move for explanation to example()
      
    $f $api->createCustomForm([$this"example"]);
      
    $f->setTitle("§lShop");
      
    $f->addSlider("§7Amount",1,64);
      
    $f->sendToPlayer($g);

      
    /**
       * Callback function.
       * The execution result of the form is assigned to the argument $data
       * If the form is closed rather than sent, NULL is assigned to $data
       * @param  Player $g
       * @param  array  $data
       * @return void
       */
      
    public function example(Player $g, array $data) {
        
    $s $data[0];// slider`s value
        
    if($s == null) {
          return 
    true;// cancelled
        
    }
        
    $parasi $eco->myMoney($g);
        
    // Since the number of items is variable, you do not need to switch the process with switch
        
    switch($s) {
          case 
    0:
          
    // Since the value of the slider is set between 1 and 64, processing does not go to here
          // The value in $perasi is **float** or **bool** according to EconomyAPI.
            
    if ($parasi !== false) {
              if(
    $parasi 75*$s) {
                
    $g->getInventory()->addItem(Item::get(1,0,$s));
                
    $g->sendMessage("You boght $s stone.");
                
    EconomyAPI::reduceMoney($g75*$s);
              }else{
                
    $g->sendMessage("§cNot moneu.");
              }
            }
          break;
        }
      }

    Please rewrite like this
    :
    PHP:
    $api $this->getServer()->getPluginManager()->getPlugin("FormAPI");
      
    $eco $this->getServer()->getPluginManager()->getPlugin("EconomyAPI");
      
    $parasi $eco->myMoney($g);
      
    $f $api->createCustomForm(function (Player $g, array $data) {
        
    // NOTE: Callback function - START
        // If you do not check whether the form was interrupted for the first time,
        // $data may be null and it can not be acquired
        
    if($s == null) {
          return 
    true;// cancelled
        
    }
        
    $s $data[0];// slider`s value. The value from 1 to 64 should come back
        
    $parasi $eco->myMoney($g);
        if (
    $parasi !== false) {
          if(
    $parasi 75*$s) {
            
    $g->getInventory()->addItem(Item::get(1,0,$s));
            
    $g->sendMessage("You boght $s stone.");
            
    EconomyAPI::reduceMoney($g75*$s);
          }else{
            
    $g->sendMessage("§cNot moneu.");
          }
        }
      });
      
    // NOTE: Callback function - END
      
    $f->setTitle("§lShop");
      
    $f->addSlider("§7Amount",1,64);
      
    $f->sendToPlayer($g);
     
    Last edited: Feb 21, 2018
  10. byyEmirhanWSD

    byyEmirhanWSD Witch

    Messages:
    50
    GitHub:
    EmirhanWSD
    Thanks for your helps. I solved this problem. :D
     
  11. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    those variables hurts my eyes
     
    yuko fuyutsuki likes this.
  12. yuko fuyutsuki

    yuko fuyutsuki Slime

    Messages:
    77
    GitHub:
    fuyutsuki
    Its the result of respecting the originality of the variable name of the code :shoghi:
     
  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.