-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathFormManager.php
More file actions
168 lines (141 loc) · 5.3 KB
/
Copy pathFormManager.php
File metadata and controls
168 lines (141 loc) · 5.3 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
163
164
165
166
167
168
<?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\Button;
use pocketmine\network\mcpe\protocol\NetworkStackLatencyPacket;
use pocketmine\network\PacketHandlingException;
use pocketmine\player\Player;
use pocketmine\plugin\PluginBase;
use pocketmine\scheduler\ClosureTask;
use pocketmine\utils\TextFormat;
use function count;
use function microtime;
use function min;
use function mt_rand;
class FormManager
{
/** @var float[] */
private static array $lastForm = [];
/** @var Form[] */
private static array $staticForms = [];
/** @var PluginBase */
private static PluginBase $plugin;
public function __construct(PluginBase $plugin)
{
$plugin->getServer()->getPluginManager()->registerEvents(new EventListener(), $plugin);
self::$plugin = $plugin;
}
public static function saveStaticForm(Form $form, int $id): void
{
if (isset(self::$staticForms[$id])) {
throw new PacketHandlingException('Tried to overwrite static form');
}
self::$staticForms[$id] = $form;
}
public static function scheduleNetworkStackLatency(Player $player): void
{
self::$plugin->getScheduler()->scheduleDelayedTask(new ClosureTask(static function () use ($player): void {
if ($player->isOnline()) {
$timestamp = mt_rand() * 1000;
$player->getNetworkSession()->sendDataPacket(NetworkStackLatencyPacket::request($timestamp));
EventListener::$timestampData[$player->getId()] = $timestamp;
}
}), 2);
}
public static function onQuit(Player $player): void
{
unset(self::$lastForm[$player->getId()]);
}
public static function createCustomForm(?Player $player = null, ?Closure $callable = null, ?Closure $onClose = null): ?CustomForm
{
if ($player === null || self::canSend($player)) {
return new CustomForm($player, $callable, $onClose);
}
return null;
}
public static function canSend(Player $player): bool
{
return (self::$lastForm[$player->getId()] ?? 0.0) < microtime(true) - 0.2;
}
public static function sendLastForm(Player $player): void
{
self::$lastForm[$player->getId()] = microtime(true);
}
public static function createSimpleForm(?Player $player = null, ?Closure $onClose = null): ?SimpleForm
{
if ($player === null || self::canSend($player)) {
return new SimpleForm($player, $onClose);
}
return null;
}
/**
* @template T
*
* @param list<T> $array
* @param Closure(SimpleForm, int, list<T>): void $onPageChange
*/
public static function createSimplePaginatedForm(Player $player, array $array, Closure $onPageChange, int $pageSize, ?Closure $onClose = null): void
{
self::createSimplePageForm($player, $array, 0, $onPageChange, $pageSize, $onClose);
}
/**
* @template T
*
* @param list<T> $array
* @param Closure(SimpleForm, int, list<T>): void $onPageChange
*/
private static function createSimplePageForm(Player $player, array $array, int $page, Closure $onPageChange, int $pageSize, ?Closure $onClose = null): void
{
$form = self::createSimpleForm($player, $onClose);
if ($form !== null) {
$start = $page * $pageSize;
$onPageChange($form, $page, array_slice($array, $start, $pageSize));
if ($page > 0) {
$form->addButton(new Button(TextFormat::GREEN . TextFormat::BOLD . 'Previous Page', fn(Player $player) => self::createSimplePageForm($player, $array, $page - 1, $onPageChange, $pageSize, $onClose)));
}
if (count($array) > $start + $pageSize) {
$form->addButton(new Button(TextFormat::GREEN . TextFormat::BOLD . 'Next Page', fn(Player $player) => self::createSimplePageForm($player, $array, $page + 1, $onPageChange, $pageSize, $onClose)));
}
$form->sendForm();
}
}
public static function createModalForm(?Player $player = null, ?Closure $onClose = null): ?ModalForm
{
if ($player === null || self::canSend($player)) {
return new ModalForm($player, $onClose);
}
return null;
}
public static function sendStaticForm(Player $player, int $id): void
{
$form = clone self::getStaticForm($id);
$form->setPlayer($player);
$form->sendForm();
}
private static function getStaticForm(int $id): Form
{
if (!isset(self::$staticForms[$id])) {
throw new PacketHandlingException('static form ' . $id . " doesn't exist");
}
return self::$staticForms[$id];
}
}