Skip to content

Commit 3407d50

Browse files
committed
bugfix新的UI表单API
1 parent 5e26cbd commit 3407d50

3 files changed

Lines changed: 169 additions & 34 deletions

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
declare(strict_types = 1);
3+
namespace Anders\PlayerJoinSettings\API;
4+
5+
use pocketmine\Player;
6+
use pocketmine\network\mcpe\protocol\ModalFormRequestPacket;
7+
8+
class ModalForm extends RootUIAPI
9+
{
10+
11+
public $id;
12+
13+
private $data = [];
14+
15+
private $content = "";
16+
17+
public $playerName;
18+
19+
/**
20+
* I创建选择性UI
21+
*
22+
* @param int $id
23+
*/
24+
public function __construct(int $id)
25+
{
26+
parent::__construct($id);
27+
$this->data["type"] = "modal";
28+
$this->data["title"] = "";
29+
$this->data["content"] = $this->content;
30+
$this->data["button1"] = "";
31+
$this->data["button2"] = "";
32+
}
33+
34+
/**
35+
* UI表单发送至玩家
36+
*
37+
* {@inheritdoc}
38+
* @see Anders\PlayerJoinSettings\API\RootUIAPI::sendToPlayer()
39+
*/
40+
public function sendToPlayer(Player $player): void
41+
{
42+
$pk = new ModalFormRequestPacket();
43+
$pk->formId = $this->id;
44+
$pk->formData = json_encode($this->data);
45+
$player->dataPacket($pk);
46+
$this->playerName = $player->getName();
47+
}
48+
49+
/**
50+
* UI 标题
51+
*
52+
* @param string $title
53+
*/
54+
public function setTitle(string $title): void
55+
{
56+
$this->data["title"] = $title;
57+
}
58+
59+
/**
60+
* UI标题
61+
*
62+
* @param string $content
63+
*/
64+
public function setContent(string $content): void
65+
{
66+
$this->data["content"] = $content;
67+
}
68+
69+
/**
70+
* UI第一个按钮
71+
*
72+
* @param string $text
73+
*/
74+
public function setButton1(string $text): void
75+
{
76+
$this->data["button1"] = $text;
77+
}
78+
79+
/**
80+
* UI 第二个按钮
81+
*
82+
* @param string $text
83+
*/
84+
public function setButton2(string $text): void
85+
{
86+
$this->data["button2"] = $text;
87+
}
88+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
namespace Anders\PlayerJoinSettings\API;
3+
4+
use pocketmine\form\Form;
5+
use pocketmine\network\mcpe\protocol\ModalFormRequestPacket;
6+
use pocketmine\Player;
7+
8+
abstract class RootUIAPI
9+
{
10+
11+
private $data = [];
12+
13+
public $id;
14+
15+
private $callable;
16+
17+
public $playerName;
18+
19+
/**
20+
*
21+
* @param String|int $id
22+
*/
23+
public function __construct($id)
24+
{
25+
$this->id = $id;
26+
}
27+
28+
public function sendToPlayer(Player $player): void
29+
{
30+
$pk = new ModalFormRequestPacket();
31+
$pk->formId = $this->id;
32+
$pk->formData = json_encode($this->data);
33+
$player->dataPacket($pk);
34+
$this->playerName = $player->getName();
35+
}
36+
}

src/Anders/PlayerJoinSettings/Main.php

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
use pocketmine\network\mcpe\protocol\ModalFormRequestPacket;
3131
use pocketmine\network\mcpe\protocol\ModalFormResponsePacket;
3232
use pocketmine\form\Form;
33+
use Anders\PlayerJoinSettings\API\ModalForm;
34+
use Anders\PlayerJoinSettings\API\RootUIAPI;
3335

3436
class Main extends PluginBase implements Listener, Form{
3537

@@ -40,7 +42,9 @@ public function onEnable(){//Plugin enabled
4042

4143
public function onLoad(){//Plugin Load
4244
@mkdir($this->getDataFolder(),0777,true);
43-
$this->saveResource('config.yml', false);
45+
if(!file_exists($this->getDataFolder()."config.yml")){
46+
$this->saveResource("config.yml");
47+
}
4448
$this->config = new Config($this->getDataFolder().'config.yml', Config::YAML);
4549
}
4650

@@ -96,43 +100,50 @@ public function onPlayerJoin(PlayerJoinEvent $event):void{//Try to send a messag
96100
$content = str_replace("{max_online}", $Max_online, $content);
97101
$button1 = str_replace("&", "§", strval($this->config->get("UIformbutton1")));
98102
$button2 = str_replace("&", "§", strval($this->config->get("UIformbutton2")));
99-
$data = ["type" => "modal","title" => $title,"content" => $content,"button1" => $button1,"button2" => $button2];
100-
$packet = new ModalFormRequestPacket();
101-
$packet->formId = 9527;
102-
$packet->formData = json_encode($data, JSON_PRETTY_PRINT | JSON_BIGINT_AS_STRING | JSON_UNESCAPED_UNICODE);
103-
$player->dataPacket($packet);
103+
$UI = new ModalForm(9527);
104+
$UI->setTitle($title);
105+
$UI->setContent($content);
106+
$UI->setButton1($button1);
107+
$UI->setButton2($button2);
108+
$UI->sendToPlayer($player);
104109
}
105110
}
106-
111+
112+
public function jsonSerialize() : void{
113+
}
114+
115+
public function handleResponse(Player $player, $data) : void{
116+
}
117+
107118
public function onDataPacketReceive(DataPacketReceiveEvent $event): void{
108-
$packet = $event->getPacket();
109-
$player = $event->getPlayer();
110-
$name = $player->getName();
111-
if ($packet instanceof ModalFormResponsePacket) {
112-
$id = $packet->formId;
113-
$data = $packet->formData;
114-
$result = json_decode($data);
115-
if($data == "null\n"){
116-
} else {
117-
if ($id === 9527) {
118-
if($result == true){
119-
if($this->config->get("ButtonCommand") == true){//Add some switches
120-
$ButtonCommand = $this->config->get("UIformbutton1cmd");
121-
$ButtonCommand = str_replace("{name}", '"' . $name . '"', $ButtonCommand);
122-
$this->getServer()->dispatchCommand(new consoleCommandSender(), $ButtonCommand);
123-
}
124-
}else{
125-
if($this->config->get("ButtonCommand") == true){//Add some switches
126-
$ButtonCommand = $this->config->get("UIformbutton2cmd");
127-
$ButtonCommand = str_replace("{name}", '"' . $name . '"', $ButtonCommand);
128-
$this->getServer()->dispatchCommand(new consoleCommandSender() ,$ButtonCommand);
129-
}
130-
}
131-
}
132-
}
133-
}
119+
$packet = $event->getPacket();
120+
$player = $event->getPlayer();
121+
$name = $player->getName();
122+
if ($packet instanceof ModalFormResponsePacket) {
123+
$id = $packet->formId;
124+
$data = $packet->formData;
125+
$result = json_decode($data);
126+
if($data == "null\n"){
127+
} else {
128+
if ($id === 9527) {
129+
if($result == true){
130+
if($this->config->get("ButtonCommand") == true){//Add some switches
131+
$ButtonCommand = $this->config->get("UIformbutton1cmd");
132+
$ButtonCommand = str_replace("{name}", '"' . $name . '"', $ButtonCommand);
133+
$this->getServer()->dispatchCommand(new consoleCommandSender(), $ButtonCommand);
134+
}
135+
}else{
136+
if($this->config->get("ButtonCommand") == true){//Add some switches
137+
$ButtonCommand = $this->config->get("UIformbutton2cmd");
138+
$ButtonCommand = str_replace("{name}", '"' . $name . '"', $ButtonCommand);
139+
$this->getServer()->dispatchCommand(new consoleCommandSender() ,$ButtonCommand);
140+
}
141+
}
142+
}
143+
}
144+
}
134145
}
135-
146+
136147
public function onPlayerQuit(PlayerQuitEvent $event):void{
137148
$player = $event->getPlayer();
138149
$name = $player->getName();

0 commit comments

Comments
 (0)