Skip to content

Commit

Permalink
add MaintenanceSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
royaljacques authored Jan 11, 2022
1 parent 3c55217 commit a0d1eab
Show file tree
Hide file tree
Showing 9 changed files with 578 additions and 0 deletions.
5 changes: 5 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: MaintenanceSystem
author: royal
version: 0.0.1
api: 4.0.0
main: royal\maintenanceSystem\Main
132 changes: 132 additions & 0 deletions src/jojoe77777/FormAPI/CustomForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

declare(strict_types = 1);

namespace jojoe77777\FormAPI;

class CustomForm extends Form {

private array $labelMap = [];

/**
* @param callable|null $callable
*/
public function __construct(?callable $callable) {
parent::__construct($callable);
$this->data["type"] = "custom_form";
$this->data["title"] = "";
$this->data["content"] = [];
}

public function processData(&$data) : void {
if(is_array($data)) {
$new = [];
foreach ($data as $i => $v) {
$new[$this->labelMap[$i]] = $v;
}
$data = $new;
}
}

/**
* @param string $title
*/
public function setTitle(string $title) : void {
$this->data["title"] = $title;
}

/**
* @return string
*/
public function getTitle() : string {
return $this->data["title"];
}

/**
* @param string $text
* @param string|null $label
*/
public function addLabel(string $text, ?string $label = null) : void {
$this->addContent(["type" => "label", "text" => $text]);
$this->labelMap[] = $label ?? count($this->labelMap);
}

/**
* @param string $text
* @param bool|null $default
* @param string|null $label
*/
public function addToggle(string $text, bool $default = null, ?string $label = null) : void {
$content = ["type" => "toggle", "text" => $text];
if($default !== null) {
$content["default"] = $default;
}
$this->addContent($content);
$this->labelMap[] = $label ?? count($this->labelMap);
}

/**
* @param string $text
* @param int $min
* @param int $max
* @param int $step
* @param int $default
* @param string|null $label
*/
public function addSlider(string $text, int $min, int $max, int $step = -1, int $default = -1, ?string $label = null) : void {
$content = ["type" => "slider", "text" => $text, "min" => $min, "max" => $max];
if($step !== -1) {
$content["step"] = $step;
}
if($default !== -1) {
$content["default"] = $default;
}
$this->addContent($content);
$this->labelMap[] = $label ?? count($this->labelMap);
}

/**
* @param string $text
* @param array $steps
* @param int $defaultIndex
* @param string|null $label
*/
public function addStepSlider(string $text, array $steps, int $defaultIndex = -1, ?string $label = null) : void {
$content = ["type" => "step_slider", "text" => $text, "steps" => $steps];
if($defaultIndex !== -1) {
$content["default"] = $defaultIndex;
}
$this->addContent($content);
$this->labelMap[] = $label ?? count($this->labelMap);
}

/**
* @param string $text
* @param array $options
* @param int $default
* @param string|null $label
*/
public function addDropdown(string $text, array $options, int $default = null, ?string $label = null) : void {
$this->addContent(["type" => "dropdown", "text" => $text, "options" => $options, "default" => $default]);
$this->labelMap[] = $label ?? count($this->labelMap);
}

/**
* @param string $text
* @param string $placeholder
* @param string $default
* @param string|null $label
*/
public function addInput(string $text, string $placeholder = "", string $default = null, ?string $label = null) : void {
$this->addContent(["type" => "input", "text" => $text, "placeholder" => $placeholder, "default" => $default]);
$this->labelMap[] = $label ?? count($this->labelMap);
}

/**
* @param array $content
*/
private function addContent(array $content) : void {
$this->data["content"][] = $content;
}

}
56 changes: 56 additions & 0 deletions src/jojoe77777/FormAPI/Form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types = 1);

namespace jojoe77777\FormAPI;

use pocketmine\form\Form as IForm;
use pocketmine\player\Player;

abstract class Form implements IForm{

/** @var array */
protected array $data = [];
/** @var callable|null */
private $callable;

/**
* @param callable|null $callable
*/
public function __construct(?callable $callable) {
$this->callable = $callable;
}

/**
* @deprecated
* @see Player::sendForm()
*
* @param Player $player
*/
public function sendToPlayer(Player $player) : void {
$player->sendForm($this);
}

public function getCallable() : ?callable {
return $this->callable;
}

public function setCallable(?callable $callable) {
$this->callable = $callable;
}

public function handleResponse(Player $player, $data) : void {
$this->processData($data);
$callable = $this->getCallable();
if($callable !== null) {
$callable($player, $data);
}
}

public function processData(&$data) : void {
}

public function jsonSerialize(){
return $this->data;
}
}
40 changes: 40 additions & 0 deletions src/jojoe77777/FormAPI/FormAPI.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types = 1);

namespace jojoe77777\FormAPI;

use pocketmine\plugin\PluginBase;

class FormAPI extends PluginBase{

/**
* @deprecated
*
* @param callable|null $function
* @return CustomForm
*/
public function createCustomForm(?callable $function = null) : CustomForm {
return new CustomForm($function);
}

/**
* @deprecated
*
* @param callable|null $function
* @return SimpleForm
*/
public function createSimpleForm(?callable $function = null) : SimpleForm {
return new SimpleForm($function);
}

/**
* @deprecated
*
* @param callable|null $function
* @return ModalForm
*/
public function createModalForm(?callable $function = null) : ModalForm {
return new ModalForm($function);
}
}
79 changes: 79 additions & 0 deletions src/jojoe77777/FormAPI/ModalForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types = 1);

namespace jojoe77777\FormAPI;

class ModalForm extends Form {

/** @var string */
private string $content = "";

/**
* @param callable|null $callable
*/
public function __construct(?callable $callable) {
parent::__construct($callable);
$this->data["type"] = "modal";
$this->data["title"] = "";
$this->data["content"] = $this->content;
$this->data["button1"] = "";
$this->data["button2"] = "";
}

/**
* @param string $title
*/
public function setTitle(string $title) : void {
$this->data["title"] = $title;
}

/**
* @return string
*/
public function getTitle() : string {
return $this->data["title"];
}

/**
* @return string
*/
public function getContent() : string {
return $this->data["content"];
}

/**
* @param string $content
*/
public function setContent(string $content) : void {
$this->data["content"] = $content;
}

/**
* @param string $text
*/
public function setButton1(string $text) : void {
$this->data["button1"] = $text;
}

/**
* @return string
*/
public function getButton1() : string {
return $this->data["button1"];
}

/**
* @param string $text
*/
public function setButton2(string $text) : void {
$this->data["button2"] = $text;
}

/**
* @return string
*/
public function getButton2() : string {
return $this->data["button2"];
}
}
Loading

0 comments on commit a0d1eab

Please sign in to comment.