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

How can i make simple Form working?

Discussion in 'Development' started by Enrico Angelon, Mar 27, 2018.

  1. Enrico Angelon

    Enrico Angelon Spider Jockey

    Messages:
    37
    GitHub:
    herryyt
    Hello everyone, today i have a big queston, this form api is a big mess for me, can someone help me understanding what can i do in this case?
    Code:
                        $speed_gui = new SimpleForm(3, null);
                        $speed_gui->setTitle(T::GREEN . 'Speed Toggler');
                        $speed_gui->addButton(T::RED . "Enable");
                        $speed_gui->addButton(T::RED . 'Disable');
                        $speed_gui->sendToPlayer($player);
                        break;
    That's my gui class
    code:
    Code:
        const IMAGE_TYPE_PATH = 0;
        const IMAGE_TYPE_URL = 1;
    
        /** @var int */
        public $id;
        /** @var array */
        private $data = [];
        /** @var string */
        private $content = "";
        /** @var string */
        public $playerName;
        private $labelMap = [];
    
        /**
         * @param int $id
         * @param callable $callable
         */
        public function __construct(int $id, ?callable $callable) {
            parent::__construct($id, $callable);
            $this->data["type"] = "form";
            $this->data["title"] = "";
            $this->data["content"] = $this->content;
        }
    
        /**
         * @return int
         */
        public function getId() : int {
            return $this->id;
        }
    
        /**
         * @param Player $player
         */
        public function sendToPlayer(Player $player) : void {
            $pk = new ModalFormRequestPacket();
            $pk->formId = $this->id;
            $pk->formData = json_encode($this->data);
            $player->dataPacket($pk);
            $this->playerName = $player->getName();
        }
    
        public function processData(&$data) : void {
            $data = $this->labelMap[$data] ?? null;
        }
    
        /**
         * @param string $title
         */
        public function setTitle(string $title) : void {
            $this->data["title"] = $title;
        }
    
        /**
         * @return string
         */
        public function getTitle() : string {
            return $this->data["title"];
        }
    
        /**
         * @return string
         */
        public function getContent() : string {
            return $this->data["content"];
        }
    
        /**
         * @param string $content
         */
        public function setContent(string $content) : void {
            $this->data["content"] = $content;
        }
    
        /**
         * @param string $text
         * @param int $imageType
         * @param string $imagePath
         * @param string $label
         */
        public function addButton(string $text, int $imageType = -1, string $imagePath = "", ?string $label = null) : void {
            $content = ["text" => $text];
            if($imageType !== -1) {
                $content["image"]["type"] = $imageType === 0 ? "path" : "url";
                $content["image"]["data"] = $imagePath;
            }
            $this->data["buttons"][] = $content;
            $this->labelMap[] = $label ?? count($this->labelMap);
        }
    And that's my test:
    Code:
        public function dataPacketReceiveEvent(DataPacketReceiveEvent $e){
            $pk = $e->getPacket();
            if($pk instanceof ModalFormResponsePacket){
                json_decode($pk->formData, true);
    
                if ($pk->formId === 1)
                {
                    $player->sendMessage("gg");
                    var_dump((array) json_decode($pk->formData, true));
    
                }
    
            }
    How can i get the first button and the second to give a function on it? ps: on code test of datapacketrecive, player message was working, but with all buttons!!
    Thanks for everyone..
     
  2. Enrico Angelon

    Enrico Angelon Spider Jockey

    Messages:
    37
    GitHub:
    herryyt
    ***********UPDATE***********
    I got it working but when i click the exit button it make working function 1, how can i prevent it?
     
  3. armagadon159753

    armagadon159753 Zombie

    Messages:
    217
    GitHub:
    armagadon159753
    In your case its better to use ModalForm
     
  4. korado531m7

    korado531m7 Slime

    Messages:
    77
    GitHub:
    korado531m7
    if you make a form using modal packet,
    write like
    PHP:
    $pk = new ModalFormRequestPacket();
    $pk->formId 12345//form id(int) here
    $form["title"] = "Title";
    $form["type"] = "form";
    $form["content"] = "Message";
    $form["buttons"] = array(array("text" => "Button text","image" => array("type" => "url","data" => "Image URL")); //you don't have to write image data
    $player->dataPacket($pk);
    then, get the result from packet receive event
    PHP:
    public function onRecievePacket (DataPacketReceiveEvent $ev){
      
    $pk $ev->getPacket();
      
    $player $ev->getPlayer();
      if(
    $pk instanceof ModalFormResponsePacket) {
          if(
    $pk->formId === 12345){
              
    $data json_decode($pk->formDatatrue);
              switch(
    $data){
                  case 
    0://first button is 0
                      
    $player->sendMessage("False");
                 break;

                  case 
    1://second is 1
                      
    $player->sendMessage("True");
                  break;

                  
    //third is 2, fourth is 3....
              
    }
          }
      }
    }
     
    NutXzG and Enrico Angelon like this.
  5. Enrico Angelon

    Enrico Angelon Spider Jockey

    Messages:
    37
    GitHub:
    herryyt
    Thanks, you are my saver :D
     
    korado531m7 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.