├── .gitignore
├── .poggit.yml
├── LICENSE
├── README.md
├── plugin.yml
└── src
└── FormAPI
├── FormAPI.php
├── elements
├── Button.php
├── ButtonImage.php
├── Dropdown.php
├── Element.php
├── ElementCustom.php
├── Input.php
├── Slider.php
├── StepSlider.php
└── Toggle.php
├── response
└── PlayerWindowResponse.php
└── window
├── CustomWindowForm.php
├── ModalWindowForm.php
├── SimpleWindowForm.php
└── WindowForm.php
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea/
--------------------------------------------------------------------------------
/.poggit.yml:
--------------------------------------------------------------------------------
1 | --- # Poggit-CI Manifest. Open the CI at https://poggit.pmmp.io/ci/Yahir-AR/FormAPI-PMMP
2 | build-by-default: true
3 | branches:
4 | - main
5 | projects:
6 | FormAPI-PMMP:
7 | path: ""
8 | ...
9 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # FormAPI-PMMP
2 | Plugin to create easy forms for PocketMine-MP.
3 |
4 | 🎉 New update:
5 | - Now you can choose between getting the response via callback, or an event.
6 | - Now compatible with api 4.0.0
7 |
8 | ⚠ These new features are only available in the version compatible with api 4.0.0
9 |
10 |
11 | # Example Simple Form
12 | For create a simple form
13 | ```php
14 | use FormAPI\window\SimpleWindowForm;
15 | use FormAPI\elements\ButtonImage;
16 | use FormAPI\elements\Button;
17 | use pocketmine\player\Player;
18 |
19 | $window = new SimpleWindowForm("name", "Select game", "Choose the game");//without callback
20 | $window = new SimpleWindowForm("name", "Select game", "Choose the game", function (Player $player, Button $selected) {
21 | $player->sendMessage("Hello, you select " . $selected->getText());
22 | });//with callback
23 |
24 | $window->addButton("name", "SkyWars");//without image
25 | $window->addButton("name1", "BedWars", new ButtonImage("path", "textures/items/bed_blue.png"));//with image
26 | $window->showTo($player);
27 | ```
28 |
29 |
30 |
31 | If your decision was not to use callback, you can get the response through this event
32 |
33 | ```php
34 | use FormAPI\response\PlayerWindowResponse;
35 | use FormAPI\window\SimpleWindowForm;
36 |
37 | public function onResponse(PlayerWindowResponse $event){
38 | $player = $event->getPlayer();
39 | $form = $event->getForm();
40 |
41 | if(!$form instanceof SimpleWindowForm) return;
42 | if($form->getName() !== "name") return;
43 |
44 | if($form->isClosed()) {
45 | $player->sendMessage("The form has been closed");
46 | return;
47 | }
48 |
49 | $player->sendMessage($form->getClickedButton()->getText());
50 | }
51 | ```
52 | # Example Modal Form
53 | For create a modal form
54 |
55 | ```php
56 | use FormAPI\window\ModalWindowForm;
57 | use pocketmine\player\Player;
58 |
59 | $window = new ModalWindowForm("name", "A little question", "The plugin is good?", "Yes", "No, sorry");//without callback
60 | $window = new ModalWindowForm("name", "A little question", "The plugin is good?", "Yes", "No, sorry", function (Player $player, bool $accept) {
61 | if ($accept) {
62 | $player->sendMessage("User accept");
63 | } else {
64 | $player->sendMessage("User cancel");
65 | }
66 | });//with callback
67 |
68 | $window->showTo($player);
69 | ```
70 |
71 |
72 |
73 |
74 | If your decision was not to use callback, you can get the response through this event
75 |
76 | ```php
77 | use FormAPI\response\PlayerWindowResponse;
78 | use FormAPI\window\ModalWindowForm;
79 |
80 | public function onResponse(PlayerWindowResponse $event){
81 | $player = $event->getPlayer();
82 | $form = $event->getForm();
83 |
84 | if(!$form instanceof ModalWindowForm) return;
85 | if($form->getName() !== "name") return;
86 |
87 | if($form->isClosed()) {
88 | $player->sendMessage("The form has been closed");
89 | return;
90 | }
91 |
92 | if($form->isAccept()) {//responsexD
93 | $player->sendMessage("User accept");
94 | } else {
95 | $player->sendMessage("User cancel");
96 | }
97 | }
98 | ```
99 |
100 | # Example Custom Form
101 | For create a custom form
102 |
103 | ```php
104 | use FormAPI\window\CustomWindowForm;
105 | use pocketmine\player\Player;
106 |
107 | $window = new CustomWindowForm("window_test", "Test", "This is a test");//without callback
108 | $window = new CustomWindowForm("window_test", "Test", "This is a test", function (Player $player, CustomWindowForm $form) {
109 | $user = $form->getElement("users");
110 | $password = $form->getElement("password");
111 |
112 | $player->sendMessage($user->getName() . ": " . $user->getFinalValue());
113 | $player->sendMessage($password->getName() . ": " . $password->getFinalValue());
114 | });//with callback
115 |
116 | $window->addDropdown("users", "Select the users", ["ClembArcade", "RomnSD"]);
117 | $window->addInput("password", "Insert your password");
118 | $window->addSlider("age", "Select your age", 6, 20);
119 | $window->addToggle("notifications", "You want receive notifications?");
120 | $window->showTo($player);
121 | ```
122 |
123 |
124 |
125 |
126 | If your decision was not to use callback, you can get the response through this event
127 |
128 | ```php
129 | use FormAPI\response\PlayerWindowResponse;
130 | use FormAPI\window\CustomWindowForm;
131 |
132 | public function onResponse(PlayerWindowResponse $event){
133 | $player = $event->getPlayer();
134 | $form = $event->getForm();
135 |
136 | if(!$form instanceof CustomWindowForm) return;
137 |
138 | if($form->getName() !== "window_test") return;
139 |
140 | if($form->isClosed()) {
141 | $player->sendMessage("The form has been closed");
142 | return;
143 | }
144 |
145 | $user = $form->getElement("users");
146 | $password = $form->getElement("password");
147 | $age = $form->getElement("age");
148 | $noti = $form->getElement("notifications");
149 |
150 | $player->sendMessage($user->getName() . ": " . $user->getFinalValue());
151 | $player->sendMessage($password->getName() . ": " . $password->getFinalValue());
152 | $player->sendMessage($age->getName() . ": " . $age->getFinalValue());
153 | $player->sendMessage($noti->getName() . ": " . $noti->getFinalValue());
154 |
155 | }
156 |
157 | ```
158 |
159 |
--------------------------------------------------------------------------------
/plugin.yml:
--------------------------------------------------------------------------------
1 | name: FormAPI
2 | api: 4.0.0
3 | main: FormAPI\FormAPI
4 | version: 1.0.0-ALPHA
5 | author: ClembArcade
6 | website: https://github.com/Yahir-AR
7 | description: FormAPI for PocketMine-MP
--------------------------------------------------------------------------------
/src/FormAPI/FormAPI.php:
--------------------------------------------------------------------------------
1 | getLogger()->notice(TextFormat::GREEN . "formapi has been loaded successfully");
14 | }
15 |
16 | }
--------------------------------------------------------------------------------
/src/FormAPI/elements/Button.php:
--------------------------------------------------------------------------------
1 | image = $image;
18 |
19 | $this->content = [
20 | "text" => $this->text
21 | ];
22 |
23 | if($this->image !== null) {
24 | $this->content["image"] = [
25 | "type" => $this->image->getType(),
26 | "data" => $this->image->getLocation()
27 | ];
28 | }
29 | }
30 |
31 | /**
32 | * @return ButtonImage
33 | */
34 | public function getImage(): ?ButtonImage
35 | {
36 | return $this->image;
37 | }
38 |
39 | }
--------------------------------------------------------------------------------
/src/FormAPI/elements/ButtonImage.php:
--------------------------------------------------------------------------------
1 | type = $type;
23 | $this->location = $location;
24 | }
25 |
26 | /**
27 | * @return String
28 | */
29 | public function getType(): string
30 | {
31 | return $this->type;
32 | }
33 |
34 | /**
35 | * @return String
36 | */
37 | public function getLocation(): string
38 | {
39 | return $this->location;
40 | }
41 | }
--------------------------------------------------------------------------------
/src/FormAPI/elements/Dropdown.php:
--------------------------------------------------------------------------------
1 | options = $options;
20 | $this->default = $default;
21 |
22 | $this->content = [
23 | "type" => "dropdown",
24 | "text" => $this->text,
25 | "options" => $this->options,
26 | "default" => $this->default
27 | ];
28 | }
29 |
30 | /**
31 | * @return array
32 | */
33 | public function getOptions(): array
34 | {
35 | return $this->options;
36 | }
37 |
38 | /**
39 | * @return int
40 | */
41 | public function getDefault(): int
42 | {
43 | return $this->default;
44 | }
45 |
46 | public function getFinalValue()
47 | {
48 | return $this->options[parent::getFinalValue()];
49 | }
50 |
51 | }
--------------------------------------------------------------------------------
/src/FormAPI/elements/Element.php:
--------------------------------------------------------------------------------
1 | form = $form;
25 | $this->name = $name;
26 | $this->text = $text;
27 | }
28 |
29 | /**
30 | * @return String
31 | */
32 | public function getName(): String
33 | {
34 | return $this->name;
35 | }
36 |
37 | /**
38 | * @return String
39 | */
40 | public function getText(): String
41 | {
42 | return $this->text;
43 | }
44 |
45 | /**
46 | * @return WindowForm
47 | */
48 | public function getForm(): WindowForm
49 | {
50 | return $this->form;
51 | }
52 |
53 | /**
54 | * @return array
55 | */
56 | public function getContent(): array
57 | {
58 | return $this->content;
59 | }
60 |
61 | }
--------------------------------------------------------------------------------
/src/FormAPI/elements/ElementCustom.php:
--------------------------------------------------------------------------------
1 | arrayIndex = $index;
20 | }
21 |
22 | /**
23 | * @return int
24 | */
25 | public function getArrayIndex(): int
26 | {
27 | return $this->arrayIndex;
28 | }
29 |
30 | /**
31 | * @param mixed $data
32 | */
33 | public function setFinalData($data): void
34 | {
35 | $this->finalData = $data;
36 | }
37 |
38 | /**
39 | * @return mixed
40 | */
41 | public function getFinalValue()
42 | {
43 | return $this->finalData;
44 | }
45 | }
--------------------------------------------------------------------------------
/src/FormAPI/elements/Input.php:
--------------------------------------------------------------------------------
1 | placeholder = $placeholder;
21 | $this->value = $value;
22 |
23 | $this->content = [
24 | "type" => "input",
25 | "text" => $this->text,
26 | "placeholder" => $this->placeholder,
27 | "default" => $this->value
28 | ];
29 | }
30 |
31 | /**
32 | * @return String
33 | */
34 | public function getPlaceholder(): string
35 | {
36 | return $this->placeholder;
37 | }
38 |
39 | /**
40 | * @return String
41 | */
42 | public function getValue(): string
43 | {
44 | return $this->value;
45 | }
46 |
47 | }
--------------------------------------------------------------------------------
/src/FormAPI/elements/Slider.php:
--------------------------------------------------------------------------------
1 | min = $min;
27 | $this->max = $max;
28 | $this->step = $step;
29 | $this->default = $default;
30 |
31 | $this->content = [
32 | "type" => "slider",
33 | "text" => $this->text,
34 | "min" => $this->min,
35 | "max" => $this->max
36 | ];
37 |
38 | if($this->step !== -1) $this->content["step"] = $this->step;
39 |
40 | if($this->default !== -1) $this->content["default"] = $this->default;
41 | }
42 |
43 | /**
44 | * @return int
45 | */
46 | public function getMin(): int
47 | {
48 | return $this->min;
49 | }
50 |
51 | /**
52 | * @return int
53 | */
54 | public function getMax(): int
55 | {
56 | return $this->max;
57 | }
58 |
59 | /**
60 | * @return int
61 | */
62 | public function getStep(): int
63 | {
64 | return $this->step;
65 | }
66 |
67 | /**
68 | * @return int
69 | */
70 | public function getDefault(): int
71 | {
72 | return $this->default;
73 | }
74 |
75 | }
--------------------------------------------------------------------------------
/src/FormAPI/elements/StepSlider.php:
--------------------------------------------------------------------------------
1 | steps = $steps;
21 | $this->index = $index;
22 |
23 | $this->content = [
24 | "type" => "step_slider",
25 | "text" => $this->text,
26 | "steps" => $steps
27 | ];
28 |
29 | if($index !== -1) $this->content["default"] = $index;
30 |
31 | }
32 |
33 | /**
34 | * @return int
35 | */
36 | public function getIndex(): int
37 | {
38 | return $this->index;
39 | }
40 |
41 | /**
42 | * @return array
43 | */
44 | public function getSteps(): array
45 | {
46 | return $this->steps;
47 | }
48 |
49 | }
--------------------------------------------------------------------------------
/src/FormAPI/elements/Toggle.php:
--------------------------------------------------------------------------------
1 | default = $default;
18 |
19 | $this->content = [
20 | "type" => "toggle",
21 | "text" => $this->text,
22 | "default" => $this->default
23 | ];
24 | }
25 |
26 | /**
27 | * @return bool
28 | */
29 | public function getValue(): bool
30 | {
31 | return $this->default;
32 | }
33 |
34 | /**
35 | * @return bool
36 | */
37 | public function getFinalValue(): bool
38 | {
39 | return parent::getFinalValue();
40 | }
41 |
42 | }
--------------------------------------------------------------------------------
/src/FormAPI/response/PlayerWindowResponse.php:
--------------------------------------------------------------------------------
1 | player = $player;
18 | $this->form = $form;
19 | }
20 |
21 | /**
22 | * @return WindowForm
23 | */
24 | public function getForm(): WindowForm
25 | {
26 | return $this->form;
27 | }
28 | }
--------------------------------------------------------------------------------
/src/FormAPI/window/CustomWindowForm.php:
--------------------------------------------------------------------------------
1 | name = $name;
30 | $this->title = $title;
31 |
32 | $this->content = [
33 | "type" => "custom_form",
34 | "title" => $this->title,
35 | "content" => []
36 | ];
37 |
38 | if ($description !== "") $this->addLabel($description);
39 |
40 | parent::__construct($response);
41 | }
42 |
43 | /**
44 | * @param mixed $data
45 | */
46 | public function setResponse($data): void
47 | {
48 | foreach($this->elements as $name => $element) {
49 |
50 | if(isset($data[$element->getArrayIndex()]))
51 | $element->setFinalData($data[$element->getArrayIndex()]);
52 |
53 | }
54 |
55 | parent::setResponse($data);
56 | }
57 |
58 | /**
59 | * @param ElementCustom $element
60 | */
61 | private function addElement(ElementCustom $element): void
62 | {
63 | $index = count($this->content["content"]);
64 |
65 | $element->setArrayIndex($index);
66 |
67 | $this->elements[$element->getName()] = $element;
68 | $this->content["content"][$index] = $element->getContent();
69 | }
70 |
71 | /**
72 | * @param String $name
73 | * @return ElementCustom|null
74 | */
75 | public function getElement(String $name): ?ElementCustom
76 | {
77 | if(empty($this->elements[$name])) return null;
78 |
79 | return $this->elements[$name];
80 | }
81 |
82 | /**
83 | * @param String $name
84 | * @param String $text
85 | * @param array $options
86 | * @param int $default
87 | */
88 | public function addDropdown(String $name, String $text, array $options, int $default = 0): void
89 | {
90 | $this->addElement(new Dropdown($this, $name, $text, $options, $default));
91 | }
92 |
93 | /**
94 | * @param String $name
95 | * @param String $text
96 | * @param string $placeholder
97 | * @param string $value
98 | */
99 | public function addInput(String $name, String $text, String $placeholder = "", String $value = ""): void
100 | {
101 | $this->addElement(new Input($this, $name, $text, $placeholder, $value));
102 | }
103 |
104 | /**
105 | * @param String $name
106 | * @param String $text
107 | * @param int $min
108 | * @param int $max
109 | * @param int $step
110 | * @param int $default
111 | */
112 | public function addSlider(String $name, String $text, int $min, int $max, int $step = -1, int $default = -1): void
113 | {
114 | $this->addElement(new Slider($this, $name, $text, $min, $max, $step, $default));
115 | }
116 |
117 | /**
118 | * @param String $name
119 | * @param String $text
120 | * @param array $steps
121 | * @param int $index
122 | */
123 | public function addStepSlider(String $name, String $text, array $steps, int $index = -1): void
124 | {
125 | $this->addElement(new StepSlider($this, $name, $text, $steps, $index));
126 | }
127 |
128 | /**
129 | * @param String $name
130 | * @param String $text
131 | * @param bool $default
132 | */
133 | public function addToggle(String $name, String $text, bool $default = false): void
134 | {
135 | $this->addElement(new Toggle($this, $name, $text, $default));
136 | }
137 |
138 | /**
139 | * @param String $text
140 | */
141 | public function addLabel(String $text): void
142 | {
143 | $this->content["content"][] = [
144 | "type" => "label",
145 | "text" => $text
146 | ];
147 | }
148 |
149 | /**
150 | * @param Player $player
151 | * @param mixed $data
152 | */
153 | public function handleResponse(Player $player, $data): void
154 | {
155 | parent::handleResponse($player, $data);
156 |
157 | if ($this->callable !== null) {
158 | if (!$this->isClosed()) ($this->callable)($player, $this);
159 | }
160 | }
161 |
162 |
163 | }
--------------------------------------------------------------------------------
/src/FormAPI/window/ModalWindowForm.php:
--------------------------------------------------------------------------------
1 | name = $name;
32 | $this->title = $title;
33 | $this->description = $description;
34 | $this->buttonTrue = $buttonTrue;
35 | $this->buttonFalse = $buttonFalse;
36 |
37 | $this->content = [
38 | "type" => "modal",
39 | "title" => $this->title,
40 | "content" => $this->description,
41 | "button1" => $this->buttonTrue,
42 | "button2" => $this->buttonFalse
43 | ];
44 |
45 | parent::__construct($response);
46 | }
47 |
48 | /**
49 | * @param String $text
50 | */
51 | public function setTrueButton(String $text): void
52 | {
53 | $this->buttonTrue = $text;
54 | }
55 |
56 | /**
57 | * @param String $text
58 | */
59 | public function setFalseButton(String $text): void
60 | {
61 | $this->buttonFalse = $text;
62 | }
63 |
64 | /**
65 | * @return String
66 | */
67 | public function getTrueButton(): String
68 | {
69 | return $this->buttonTrue;
70 | }
71 |
72 | /**
73 | * @return String
74 | */
75 | public function getFalseButton(): String
76 | {
77 | return $this->buttonFalse;
78 | }
79 |
80 | /**
81 | * @return bool
82 | */
83 | public function isAccept(): bool
84 | {
85 | return $this->response;
86 | }
87 |
88 | /**
89 | * @param Player $player
90 | * @param mixed $data
91 | */
92 | public function handleResponse(Player $player, $data): void
93 | {
94 | parent::handleResponse($player, $data);
95 |
96 | if ($this->callable !== null) {
97 | if (!$this->isClosed()) ($this->callable)($player, $this->isAccept());
98 | }
99 | }
100 |
101 | }
--------------------------------------------------------------------------------
/src/FormAPI/window/SimpleWindowForm.php:
--------------------------------------------------------------------------------
1 | name = $name;
31 | $this->title = $title;
32 | $this->description = $description;
33 |
34 | $this->content = [
35 | "type" => "form",
36 | "title" => $this->title,
37 | "content" => $this->description,
38 | "buttons" => []
39 | ];
40 |
41 | parent::__construct($response);
42 | }
43 |
44 | /**
45 | * @param String $name
46 | * @param String $text
47 | * @param ButtonImage|null $image
48 | */
49 | public function addButton(String $name, String $text, ButtonImage $image = null): void
50 | {
51 | if(isset($this->elements[$name])) return;
52 |
53 | $this->elements[$name] = new Button($name, $text, $this, $image);
54 | $this->content["buttons"][] = $this->elements[$name]->getContent();
55 | }
56 |
57 | /**
58 | * @param String $name
59 | * @return Button
60 | */
61 | public function getButton(String $name): ?Button
62 | {
63 | if(empty($this->elements[$name])) return null;
64 |
65 | return $this->elements[$name];
66 | }
67 |
68 | /**
69 | * @return Button|null
70 | */
71 | public function getClickedButton(): ?Button
72 | {
73 | if($this->response === null) return null;
74 |
75 | return $this->elements[array_keys($this->elements)[$this->response]];
76 | }
77 |
78 | /**
79 | * @param Player $player
80 | * @param mixed $data
81 | */
82 | public function handleResponse(Player $player, $data): void
83 | {
84 | parent::handleResponse($player, $data);
85 |
86 | if ($this->callable !== null) {
87 | if ($this->getClickedButton() !== null) ($this->callable)($player, $this->getClickedButton());
88 | }
89 | }
90 |
91 |
92 | }
--------------------------------------------------------------------------------
/src/FormAPI/window/WindowForm.php:
--------------------------------------------------------------------------------
1 | onResponse($response);
36 | }
37 |
38 | /**
39 | * @param Player $player
40 | * @param mixed $data
41 | */
42 | public function handleResponse(Player $player, $data): void
43 | {
44 | if(isset($this->viewers[$player->getName()]))
45 | unset($this->viewers[$player->getName()]);
46 |
47 | $this->setResponse($data);
48 | (new PlayerWindowResponse($player, $this))->call();
49 | }
50 |
51 | /**
52 | * @return array|mixed
53 | */
54 | public function jsonSerialize()
55 | {
56 | return $this->getContent();
57 | }
58 |
59 | /**
60 | * @return String
61 | */
62 | public function getName(): String
63 | {
64 | return $this->name;
65 | }
66 |
67 | /**
68 | * @return array
69 | */
70 | public function getContent(): array
71 | {
72 | return $this->content;
73 | }
74 |
75 | /**
76 | * @param mixed $response
77 | */
78 | public function setResponse($response): void
79 | {
80 | $this->response = $response;
81 | }
82 |
83 | /**
84 | * @return mixed
85 | */
86 | public function getResponse()
87 | {
88 | return $this->response;
89 | }
90 |
91 | /**
92 | * @param Closure $closure
93 | * @return $this
94 | */
95 | public function onResponse(Closure $closure): self
96 | {
97 | $this->callable = $closure;
98 |
99 | return $this;
100 | }
101 |
102 | /**
103 | * @return bool
104 | */
105 | public function isClosed(): bool
106 | {
107 | return $this->response === null;
108 | }
109 | /**
110 | * @param Player $player
111 | */
112 | public function showTo(Player $player): void
113 | {
114 | if(isset($this->viewers[$player->getName()])) return;
115 |
116 | $this->viewers[$player->getName()] = $this;
117 | $player->sendForm($this);
118 | }
119 | }
120 |
--------------------------------------------------------------------------------