-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathModalForm.php
More file actions
140 lines (119 loc) · 3.42 KB
/
Copy pathModalForm.php
File metadata and controls
140 lines (119 loc) · 3.42 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
<?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 InvalidArgumentException;
use libforms\elements\Button;
use pocketmine\form\FormValidationException;
use pocketmine\player\Player;
use function array_replace;
use function is_bool;
class ModalForm extends Form
{
/** @var Button[] */
private array $buttons = [];
/** @var string */
private string $content;
/** @phpstan-var Closure(Player): void */
private ?Closure $onClose;
public function __construct(?Player $player, ?Closure $onClose = null)
{
$this->onClose = $onClose;
parent::__construct($player);
}
public function handleResponse(Player $player, $data): void
{
if (is_bool($data)) {
if ($data) {
$button = $this->getButton1();
} else {
$button = $this->getButton2();
}
$button?->runCallable($player);
} elseif ($data === null) {
if (($callable = $this->getCloseClosure()) !== null) {
$callable($player);
}
} elseif (!empty($data)) {
throw new FormValidationException();
}
}
public function getButton1(): ?Button
{
return $this->getButton(1);
}
private function getButton(int $buttonId): ?Button
{
return $this->buttons[$buttonId] ?? null;
}
public function getButton2(): ?Button
{
return $this->getButton(2);
}
public function getCloseClosure(): ?Closure
{
return $this->onClose;
}
public function setContent(string $content): void
{
$this->content = $content;
}
public function getContent(): string
{
return $this->content;
}
public function setButton1(Button $button): void
{
$this->setButton(1, $button);
}
private function setButton(int $buttonId, Button $button): void
{
if ($buttonId !== 1 && $buttonId !== 2) {
throw new InvalidArgumentException("Button ID must be between 1 and 2");
}
$this->buttons[$buttonId] = $button;
}
public function jsonSerialize(): array
{
$data = parent::jsonSerialize();
return array_replace($data, [
'type' => 'modal',
'content' => $this->getContent(),
'button1' => $this->getButton1()?->getData($this->getPlayer())['text'] ?? '',
'button2' => $this->getButton2()?->getData($this->getPlayer())['text'] ?? ''
]);
}
/**
* @return Button[]
*/
public function getButtons(): array
{
return $this->buttons;
}
public function setButton2(Button $button): void
{
$this->setButton(2, $button);
}
public function setCloseClosure(?Closure $onClose): void
{
$this->onClose = $onClose;
}
}