FormAPI help

Discussion in 'Plugin Help' started by TPE(ImperialPE), Oct 18, 2020.

  1. TPE(ImperialPE)

    TPE(ImperialPE) Spider

    Messages:
    10
    GitHub:
    tpe
    I'm attempting to create a plugin that opens a form for a player that lists their enchants as buttons, it also allows them to upgrade the enchant by clicking on a specific button, however I run into an issue. The buttons are created via a foreach loop like so:
    foreach($newArray as $key => $value) {
    $message = TextFormat::RED . TextFormat::BOLD . " {$newArray[$key][0]} {$newArray[$key][1]} " . TextFormat::GOLD . TextFormat::BOLD . "--> " . TextFormat::RESET . TextFormat::GREEN . "{$newArray[$key][2]}";
    $form->addButton($message);
    }

    I'm creating the form through this function:

    $form = $api->createSimpleForm(function(Player $player, int $data = null) {

    if($data === null) {
    return true;
    }

    switch($data) {
    case 0:
    case 1:
    case 2:
    case 3:
    case 4:
    if(strpos($data, "Efficiency")) {
    $tool = $player->getInventory()->getItemInHand();
    $efficiency = Enchantment::getEnchantment(15);
    $effLevel = $tool->getEnchantment(15)->getLevel();
    if($effLevel + 1 <= 5) {
    $effInstance = new EnchantmentInstance($efficiency, $effLevel + 1);
    $tool->addEnchantment($effInstance);
    } else {
    $player->sendMessage(TextFormat::RED . "You already have max level efficiency!");
    }
    }
    }

    });

    Now I'm aware the strpos won't work as it's an int but would there be another way to do what I'm attempting to do? Thank you.
     
  2. wolfdale

    wolfdale Zombie Pigman

    Messages:
    513
    Save the messages you set on the button in an array and retrieve with index $data later on
     
  3. TPE(ImperialPE)

    TPE(ImperialPE) Spider

    Messages:
    10
    GitHub:
    tpe
    How do I save it as an array? And then index it when it's outside the openForm function.
     
  4. wolfdale

    wolfdale Zombie Pigman

    Messages:
    513
    Example
    PHP:
    $form $api->createSimpleForm(function(Player $player$data) use (&$textArr) {
        if(
    $data !== null) {
            
    $text $textArr[$data];
            
    $player->sendMessage($text);
        }
    });
    foreach([
    'a','b','c','d'] as $text){
        
    $textArr[] = $text;
        
    $form->addButton($text);
    }
     
    Last edited: Oct 20, 2020
    TPE(ImperialPE) likes this.
  5. TPE(ImperialPE)

    TPE(ImperialPE) Spider

    Messages:
    10
    GitHub:
    tpe
    How could I think check if the button contains a word such as Efficiency?
     
  6. wolfdale

    wolfdale Zombie Pigman

    Messages:
    513
    $text would contain the text on the button, a simple strpos on it would do
     
  7. TPE(ImperialPE)

    TPE(ImperialPE) Spider

    Messages:
    10
    GitHub:
    tpe
    Any particular reason why this isn't working?

    PHP:

    public function openToolForm(Player $player) {
    $api = $this->plugin->getServer()->getPluginManager()->getPlugin("FormAPI");
    $form = $api->createSimpleForm(function(Player $player, int $data) use (&$newArray) {

    if($data === null) {
    $text = $newArray[$data];
    $player->sendMessage($text);
    }

    $text = $newArray[$data];
    switch($data) {
    case $text:
    if(strpos($text, "Efficiency")) {
    $tool = $player->getInventory()->getItemInHand();
    $efficiency = Enchantment::getEnchantment(15);
    $effLevel = $tool->getEnchantment(15)->getLevel();
    if($effLevel + 1 <= 5) {
    $effInstance = new EnchantmentInstance($efficiency, $effLevel + 1);
    $tool->addEnchantment($effInstance);
    $player->sendMessage("Test");
    } else {
    $player->sendMessage(TextFormat::RED . "You already have max level efficiency!");
    }
    }
    }

    });
     
  8. wolfdale

    wolfdale Zombie Pigman

    Messages:
    513
    What do you mean by not working? Is there any error message?
    You did not add any buttons?

    Im not sure what you intend to do here? It most likely will not be equal in any situation
    PHP:
    switch($data) {
    case 
    $text:
     
    Primus likes this.
  9. TPE(ImperialPE)

    TPE(ImperialPE) Spider

    Messages:
    10
    GitHub:
    tpe
    The buttons added don't actually do anything, I assume it's because I included an unnecessary switch/case statement?
     
  10. TPE(ImperialPE)

    TPE(ImperialPE) Spider

    Messages:
    10
    GitHub:
    tpe
    Also would $text be defined the same as it is in if($data === null) because if so you're attempting to find an array in a string. $text isn't defined otherwise.
     
  11. wolfdale

    wolfdale Zombie Pigman

    Messages:
    513
    Im not sure what is the purpose of the switch statement, but it causes problems
    If the player closed the form ($data = null) usually i would just return there
     
    Primus likes this.

Share This Page

  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.