Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin for PM4 (Fixed Version) #138

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/Frago9876543210/EasyForms/EasyForms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Frago9876543210\EasyForms;

use Frago9876543210\EasyForms\forms\CustomForm;
use pocketmine\{player\Player, plugin\PluginBase};
use pocketmine\event\{Listener, server\DataPacketReceiveEvent};
use pocketmine\network\mcpe\protocol\{ServerSettingsRequestPacket, ServerSettingsResponsePacket};
use ReflectionObject;
use function json_encode;

class EasyForms extends PluginBase implements Listener{

public function onEnable() : void{
$this->getServer()->getPluginManager()->registerEvents($this, $this);
}

/**
* @throws \JsonException
*/
private function sendSetting(Player $player, CustomForm $form) : void{
$reflection = new ReflectionObject($player);

$idProperty = $reflection->getProperty("formIdCounter"); //TODO: sync it with SOF3 PR
$idProperty->setAccessible(true);
$id = $idProperty->getValue($player);

$idProperty->setValue($player, ++$id);
$id--;

$pk = new ServerSettingsResponsePacket();
$pk->formId = $id;
$pk->formData = json_encode($form, JSON_THROW_ON_ERROR);
if($player->getNetworkSession()->sendDataPacket($pk)){
$formsProperty = $reflection->getProperty("forms");
$formsProperty->setAccessible(true);

$formsValue = $formsProperty->getValue($player);
$formsValue[$id] = $form;

$formsProperty->setValue($player, $formsValue);
}
}

/**
* @throws \JsonException
*/
public function onDataPacketReceive(DataPacketReceiveEvent $e) : void{
$pk = $e->getPacket();
if($pk instanceof ServerSettingsRequestPacket){
($ev = new ServerSettingsRequestEvent($player = $e->getOrigin()->getPlayer()))->call();
if(($form = $ev->getForm()) !== null){
$this->sendSetting($player, $form);
}
}
}
}
42 changes: 42 additions & 0 deletions src/Frago9876543210/EasyForms/ServerSettingsRequestEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Frago9876543210\EasyForms;

use Frago9876543210\EasyForms\forms\CustomForm;
use pocketmine\{event\Event, player\Player};

class ServerSettingsRequestEvent extends Event{

private Player $player;
private ?CustomForm $form;

/**
* @param Player $player
*/
public function __construct(Player $player){
$this->player = $player;
}

/**
* @return Player
*/
public function getPlayer() : Player{
return $this->player;
}

/**
* @return CustomForm|null
*/
public function getForm() : ?CustomForm{
return $this->form;
}

/**
* @param CustomForm|null $form
*/
public function setForm(?CustomForm $form) : void{
$this->form = $form;
}
}
46 changes: 46 additions & 0 deletions src/Frago9876543210/EasyForms/elements/Button.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Frago9876543210\EasyForms\elements;

class Button extends Element{
/** @var Image|null */
private $image;
/** @var string */
private $type;

/**
* @param string $text
* @param Image|null $image
*/
public function __construct(string $text, ?Image $image = null){
parent::__construct($text);
$this->image = $image;
}

/**
* @return string|null
*/
public function getType() : ?string{
return null;
}

/**
* @return bool
*/
public function hasImage() : bool{
return $this->image !== null;
}

/**
* @return array
*/
public function serializeElementData() : array{
$data = ["text" => $this->text];
if($this->hasImage()){
$data["image"] = $this->image;
}
return $data;
}
}
70 changes: 70 additions & 0 deletions src/Frago9876543210/EasyForms/elements/Dropdown.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace Frago9876543210\EasyForms\elements;

use pocketmine\form\FormValidationException;

class Dropdown extends Element{
/** @var string[] */
private $options;
/** @var int */
private $default;

/**
* @param string $text
* @param string[] $options
* @param int $default
*/
public function __construct(string $text, array $options, int $default = 0){
parent::__construct($text);
$this->options = $options;
$this->default = $default;
}

/**
* @return array
*/
public function getOptions() : array{
return $this->options;
}

/**
* @return string
*/
public function getSelectedOption() : string{
return $this->options[$this->value];
}

/**
* @return int
*/
public function getDefault() : int{
return $this->default;
}

/**
* @return string
*/
public function getType() : string{
return "dropdown";
}

/**
* @return array
*/
public function serializeElementData() : array{
return [
"options" => $this->options,
"default" => $this->default
];
}

public function validate($value) : void{
parent::validate($value);
if(!isset($this->options[$value])){
throw new FormValidationException("Option with index $value does not exist in dropdown");
}
}
}
74 changes: 74 additions & 0 deletions src/Frago9876543210/EasyForms/elements/Element.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace Frago9876543210\EasyForms\elements;

use JsonSerializable;
use pocketmine\form\FormValidationException;
use function is_int;

abstract class Element implements JsonSerializable{
/** @var string */
protected $text;
/** @var mixed */
protected $value;

/**
* @param string $text
*/
public function __construct(string $text){
$this->text = $text;
}

/**
* @return mixed
*/
public function getValue(){
return $this->value;
}

/**
* @param mixed $value
*/
public function setValue($value){
$this->value = $value;
}

/**
* @return array
*/
final public function jsonSerialize() : array{
$array = ["text" => $this->getText()];
if($this->getType() !== null){
$array["type"] = $this->getType();
}
return $array + $this->serializeElementData();
}

/**
* @return string
*/
public function getText() : string{
return $this->text;
}

/**
* @return string|null
*/
abstract public function getType() : ?string;

/**
* @return array
*/
abstract public function serializeElementData() : array;

/**
* @param mixed $value
*/
public function validate($value) : void{
if(!is_int($value)){
throw new FormValidationException("Expected int, got " . gettype($value));
}
}
}
47 changes: 47 additions & 0 deletions src/Frago9876543210/EasyForms/elements/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Frago9876543210\EasyForms\elements;

use JsonSerializable;

class Image implements JsonSerializable{
public const TYPE_URL = "url";
public const TYPE_PATH = "path";

/** @var string */
private $type;
/** @var string */
private $data;

/**
* @param string $data
* @param string $type
*/
public function __construct(string $data, string $type = self::TYPE_URL){
$this->type = $type;
$this->data = $data;
}

/**
* @return string
*/
public function getType() : string{
return $this->type;
}

/**
* @return string
*/
public function getData() : string{
return $this->data;
}

public function jsonSerialize() : array{
return [
"type" => $this->type,
"data" => $this->data
];
}
}
Loading