fuyutsuki/libform

Using functions is a shit option dude!

Opened this issue · 14 comments

You can't make function dinamyc with that!

I want get string from $response on CustomForm and i can't , i get an array with integers, that's a bullshit!

For exemple if i create a dropdown with array("Water Bucket", "Lava Bucket"), on $response function i got "Array{ int(0) and int(1)", and i don't get string values from aray.

Code:

        $list = array("Water Bucket", "Lava Bucket");
        $count = array("1", "8", "16", "32", "64");
        API::getShopAPI()->sendFormWindowBuyListFirstOption($player, $list, $count);

    public function sendFormWindowBuyListFirstOption(Player $player, array $list, array $count): void {
        $stringTitlusendFormWindowBuyList = Settings::$sendFormWindowBuyListTitle;
        $stringTitlusendFormWindowBuyList = TextFormat::colorize($stringTitlusendFormWindowBuyList);

        $stringDescrieresendFormWindowBuyList = Settings::$sendFormWindowBuyListDescriere;
        $stringDescrieresendFormWindowBuyList = TextFormat::colorize($stringDescrieresendFormWindowBuyList);

        $custom = FormApi::makeCustomForm([$this, "sendFormWindowBuyFirstOption"]);
        $custom->setTitle($stringTitlusendFormWindowBuyList);
        $custom->addElement(new Label($stringDescrieresendFormWindowBuyList));
        $custom->addElement(new Dropdown("", $list));
        $custom->addElement(new Dropdown("", $count));
        $custom->sendToPlayer($player);
    }

    public function sendFormWindowBuyFirstOption(Player $player, $response): void {
        var_dump($response);
        $firstOptionDropdown = (string) $response[1];
        $countOptionDropdown = $response[2];

        var_dump($firstOptionDropdown);
    }

What is output:

array(3) {
  [0]=>
  NULL
  [1]=>
  int(0)
  [2]=>
  int(0)
}
string(1) "0"

Add to get string values, because can be a good option for Developers, and i can use Ds\Maps for creating a Map with Strings, like my Java Code.
Using that, is not a really good option and a good choise, and performance is very slow.

Good proposal, I'll implement it.

Soon, please.

@fuyutsuki I really need this, can you make faster, please?

@NycuRO Now I started work this, please wait.

@NycuRO Please close this issue if you get the desired result with this change.
Please let me if there are any other improvements.

I'm gonna test tommorow.

Can you improve please readme? It's not really good. Can make people gone crazy. Not any variables are working and code too. Please test first when you do things on readme, thanks so much

That's just an example, it's not actually a usable code.

How do you think it will be easy to understand?

I don't know. Actually, i don't tested properly all exemples, but i see variables who is not used. I can purpose you explain what do all variables and what them output.

Okay, I'll take an actual code (Texter) as an example...

Dear @NycuRO, I used libform on my own, thought that this change would be meaningless.

Why?

First of all, you say wanted to get a String ("Water Bucket" or "Lava Bucket" in this case) from this form,
but the form is returning the key of the array corresponding to the Dropdown element.
Therefore, if you want to get a String using a form, you can implement it by holding an array of its Dropdown element and specifying it with an array holding the value of the key returned from the form.

Code

        // NOTE: hold this
        $this->list = array("Water Bucket", "Lava Bucket");
        $this->count = array("1", "8", "16", "32", "64");
        API::getShopAPI()->sendFormWindowBuyListFirstOption($player, $list, $count);

    public function sendFormWindowBuyListFirstOption(Player $player, array $list, array $count): void {
        $stringTitlusendFormWindowBuyList = Settings::$sendFormWindowBuyListTitle;
        $stringTitlusendFormWindowBuyList = TextFormat::colorize($stringTitlusendFormWindowBuyList);

        $stringDescrieresendFormWindowBuyList = Settings::$sendFormWindowBuyListDescriere;
        $stringDescrieresendFormWindowBuyList = TextFormat::colorize($stringDescrieresendFormWindowBuyList);

        $custom = FormApi::makeCustomForm([$this, "sendFormWindowBuyFirstOption"]);
        $custom->setTitle($stringTitlusendFormWindowBuyList);
        $custom->addElement(new Label($stringDescrieresendFormWindowBuyList));
        $custom->addElement(new Dropdown("", $this->list));
        $custom->addElement(new Dropdown("", $this->count));
        $custom->sendToPlayer($player);
    }

    public function sendFormWindowBuyFirstOption(Player $player, ?array $response): void {
        if (!FormApi::formCancelled($response)) {// check NULL
            var_dump($response);
            $firstOptionDropdown = $this->list[$response[1]];
            $countOptionDropdown = $this->count[$response[2]];
            
            var_dump($firstOptionDropdown);
        }
    }

Output

array(3) {
  [0]=>
  NULL
  [1]=>
  string()"Water Bucket"
  [2]=>
  string()"1"
}
string(1) "Water Bucket"

Conclusion

I'll try to be able to set whether to return a string or a corresponding key(int).

Ok, but this issue it's too oldest and i don't need this library anymore. Sorry. But thanks for changes. I don't recommand callback for formapi.

okay, thanks for your response xD