-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathCustomForm.php
More file actions
162 lines (139 loc) · 4.49 KB
/
Copy pathCustomForm.php
File metadata and controls
162 lines (139 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/**
* _ _ _ __
* | (_) | / _|
* | |_| |__ | |_ ___ _ __ _ __ ___ ___
* | | | '_ \| _/ _ \| '__| '_ ` _ \/ __|
* | | | |_) | || (_) | | | | | | | \__ \
* |_|_|_.__/|_| \___/|_| |_| |_| |_|___/
*
* Copyright (C) 2016-2026 NetherGames Network
*
* This is private software, you cannot redistribute and/or modify it in any way
* unless given explicit permission to do so. If you have not been given explicit
* permission to view or modify this software you should take the appropriate actions
* to remove this software from your device immediately.
*
* @author matcracker, driesboy
*
*/
declare(strict_types=1);
namespace libforms;
use Closure;
use libforms\elements\Dropdown;
use libforms\elements\Element;
use libforms\elements\Input;
use libforms\elements\Label;
use libforms\elements\Slider;
use libforms\elements\StepSlider;
use libforms\elements\Toggle;
use pocketmine\form\FormValidationException;
use pocketmine\network\mcpe\protocol\ProtocolInfo;
use pocketmine\player\Player;
use function array_filter;
use function array_map;
use function array_replace;
use function is_array;
class CustomForm extends Form
{
/** @var Element[] */
private array $elements = [];
/** @var Closure|null */
private ?Closure $callable;
/** @phpstan-var Closure(Player): void */
private ?Closure $onClose;
/** @phpstan-var Closure(Player): void */
private ?Closure $onSubmit = null;
public function __construct(?Player $player, ?Closure $callable = null, ?Closure $onClose = null)
{
$this->callable = $callable;
$this->onClose = $onClose;
parent::__construct($player);
}
public function handleResponse(Player $player, $data): void
{
$elements = $this->getElements();
if ($player->getNetworkSession()->getProtocolId() === ProtocolInfo::PROTOCOL_1_21_70) {
$elements = array_values(array_filter($elements, fn(Element $element) => !($element instanceof Label)));
}
if (is_array($data)) {
$goBack = true;
$sendData = false;
foreach ($data as $index => $value) {
if (isset($elements[$index])) {
/** @var Dropdown|Input|Label|Slider|StepSlider|Toggle $element */
$element = $elements[$index];
if ($element instanceof Label || ($value === $element->getDefault() && !$element->isCallbackOnDefault())) {
continue;
}
$goBack = false;
if (!$element->runCallable($player, $value)) {
$sendData = true;
}
}
}
if (($callable = $this->getCallable()) !== null) {
if ($goBack) {
$callable($player);
} else {
if ($sendData) {
$callable($player, $data);
} elseif (($onSubmit = $this->getSubmitClosure()) !== null) {
$onSubmit($player);
}
}
}
} elseif ($data === null) {
if (($callable = $this->getCloseClosure()) !== null) {
$callable($player);
}
} elseif (!empty($data)) {
throw new FormValidationException();
}
}
public function getSubmitClosure(): ?Closure
{
return $this->onSubmit;
}
public function setSubmitClosure(?Closure $onSubmit): void
{
$this->onSubmit = $onSubmit;
}
/**
* @return Element[]
*/
public function getElements(): array
{
return $this->elements;
}
public function getCallable(): ?Closure
{
return $this->callable;
}
public function setCallable(?Closure $callable): void
{
$this->callable = $callable;
}
public function getCloseClosure(): ?Closure
{
return $this->onClose;
}
public function setCloseClosure(?Closure $onClose): void
{
$this->onClose = $onClose;
}
public function jsonSerialize(): array
{
$data = parent::jsonSerialize();
return array_replace($data, [
'type' => 'custom_form',
'content' => array_map(function (Element $element): array {
return $element->getData($this->getPlayer());
}, $this->getElements())
]);
}
public function addElement(Element $element): void
{
$this->elements[] = $element;
}
}