Skip to content

Commit

Permalink
Now available in API 4.0 (TODO: Refactoring)
Browse files Browse the repository at this point in the history
  • Loading branch information
crazydeacy committed Jan 17, 2022
1 parent a9a3a14 commit 2bca755
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 242 deletions.
14 changes: 9 additions & 5 deletions plugin.yml
@@ -1,10 +1,14 @@
name: ServerDocument
api: [3.9]
version: 1.0.0
api: [ 4.0.0 ]
version: 1.1.0
main: net\mydeacy\serverdocument\ServerDocument
author: MyDeacy
description: View text files from server
commands:
sd:
description: open document directory.
usage: /sd
sd:
description: open document directory.
usage: /sd
permission: serverdocument.common
permissions:
serverdocument.common:
default: true
16 changes: 8 additions & 8 deletions src/net/mydeacy/serverdocument/ServerDocument.php
Expand Up @@ -2,12 +2,12 @@

namespace net\mydeacy\serverdocument;

use Exception;
use net\mydeacy\serverdocument\forms\ExplorerForm;
use net\mydeacy\serverdocument\util\ElementManager;
use net\mydeacy\serverdocument\util\ElementManagerImpl;
use net\mydeacy\serverdocument\util\ElementLoader;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\Player;
use pocketmine\player\Player;
use pocketmine\plugin\PluginBase;

class ServerDocument extends PluginBase {
Expand All @@ -17,14 +17,14 @@ class ServerDocument extends PluginBase {
const READ_ERROR = "Reading of the file failed.";

/**
* @var ElementManager
* @var ElementLoader
*/
private $manager;
private ElementLoader $manager;

public function onEnable() {
public function onEnable() :void {
try {
$this->manager = new ElementManagerImpl($this->getDataFolder() . self::PUBLIC_DIR);
} catch (\Exception $e) {
$this->manager = new ElementLoader($this->getDataFolder() . self::PUBLIC_DIR);
} catch (Exception $e) {
$this->getLogger()->critical(self::READ_ERROR);
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/net/mydeacy/serverdocument/elements/CommandElement.php
Expand Up @@ -10,7 +10,7 @@ class CommandElement extends ElementBase implements CommandFile {
/**
* @var string
*/
private $command;
private string $command;

/**
* CommandElement constructor.
Expand All @@ -24,9 +24,6 @@ public function __construct(string $title, string $command, ?Directory $director
$this->command = $command;
}

/**
* @inheritDoc
*/
function getCommand() :string {
return $this->command;
}
Expand Down
6 changes: 0 additions & 6 deletions src/net/mydeacy/serverdocument/elements/DirectoryElement.php
Expand Up @@ -6,12 +6,6 @@

class DirectoryElement extends ElementBase implements Directory {

/**
* DirectoryElement constructor.
*
* @param string $title
* @param Directory|null $directory
*/
public function __construct(string $title, ?Directory $directory) {
parent::__construct($title, self::BUTTON_IMAGE, $directory);
}
Expand Down
22 changes: 3 additions & 19 deletions src/net/mydeacy/serverdocument/elements/ElementBase.php
Expand Up @@ -9,48 +9,32 @@ abstract class ElementBase {
/**
* @var string
*/
private $buttonImage;
private string $buttonImage;

/**
* @var string
*/
private $title;
private string $title;

/**
* @var Directory|null
*/
private $directory;
private ?Directory $directory;

/**
* ElementBase constructor.
*
* @param string $title
* @param string $buttonImage
* @param Directory|null $directory
*/
public function __construct(string $title, string $buttonImage, ?Directory $directory) {
$this->buttonImage = $buttonImage;
$this->title = $title;
$this->directory = $directory;
}

/**
* @return string
*/
public final function getButtonImage() :string {
return $this->buttonImage;
}

/**
* @return string
*/
public final function getTitle() :string {
return $this->title;
}

/**
* @return Directory|null
*/
public final function getDirectory() :?Directory {
return $this->directory;
}
Expand Down
12 changes: 1 addition & 11 deletions src/net/mydeacy/serverdocument/elements/FileElement.php
Expand Up @@ -10,23 +10,13 @@ class FileElement extends ElementBase implements TextFile {
/**
* @var string
*/
private $content;
private string $content;

/**
* FileElement constructor.
*
* @param string $title
* @param string $content
* @param Directory|null $directory
*/
public function __construct(string $title, string $content, ?Directory $directory) {
parent::__construct($title, self::BUTTON_IMAGE, $directory);
$this->content = $content;
}

/**
* @inheritDoc
*/
function getContent() :string {
return $this->content;
}
Expand Down
24 changes: 6 additions & 18 deletions src/net/mydeacy/serverdocument/forms/ContentForm.php
Expand Up @@ -2,10 +2,10 @@

namespace net\mydeacy\serverdocument\forms;

use net\mydeacy\serverdocument\util\ElementManager;
use net\mydeacy\serverdocument\elements\interfaces\TextFile;
use net\mydeacy\serverdocument\util\ElementLoader;
use pocketmine\form\Form;
use pocketmine\Player;
use pocketmine\player\Player;

class ContentForm implements Form {

Expand All @@ -15,37 +15,25 @@ class ContentForm implements Form {
/**
* @var TextFile
*/
private $fileElement;
private TextFile $fileElement;

/**
* @var ElementManager
* @var ElementLoader
*/
private $manager;
private ElementLoader $manager;

/**
* ContentForm constructor.
*
* @param TextFile $fileElement
* @param ElementManager $manager
*/
public function __construct(TextFile $fileElement, ElementManager $manager) {
public function __construct(TextFile $fileElement, ElementLoader $manager) {
$this->fileElement = $fileElement;
$this->manager = $manager;
}

/**
* @inheritDoc
*/
public function handleResponse(Player $player, $data) :void {
if (!isset($data)) {
return;
}
$player->sendForm(new ExplorerForm($this->fileElement->getDirectory(), $this->manager));
}

/**
* @inheritDoc
*/
public function jsonSerialize() {
$form = (new SimpleForm())
->setTitle($this->fileElement->getTitle())
Expand Down
29 changes: 8 additions & 21 deletions src/net/mydeacy/serverdocument/forms/ExplorerForm.php
Expand Up @@ -6,9 +6,9 @@
use net\mydeacy\serverdocument\elements\interfaces\Directory;
use net\mydeacy\serverdocument\elements\interfaces\Element;
use net\mydeacy\serverdocument\elements\interfaces\TextFile;
use net\mydeacy\serverdocument\util\ElementManager;
use net\mydeacy\serverdocument\util\ElementLoader;
use pocketmine\form\Form;
use pocketmine\Player;
use pocketmine\player\Player;
use pocketmine\Server;

class ExplorerForm implements Form {
Expand All @@ -19,39 +19,30 @@ class ExplorerForm implements Form {
/**
* @var Element[]
*/
private $elements = [];
private array $elements = [];

/**
* @var Directory|null
*/
private $directory;
private ?Directory $directory;

/**
* @var ElementManager
* @var ElementLoader
*/
private $manager;
private ElementLoader $manager;

/**
* @var bool
*/
private $isBaseDir;
private bool $isBaseDir;

/**
* ExplorerForm constructor.
*
* @param Directory|null $directory
* @param ElementManager $manager
*/
public function __construct(?Directory $directory, ElementManager $manager) {
public function __construct(?Directory $directory, ElementLoader $manager) {
$this->directory = $directory;
$this->isBaseDir = !isset($directory);
$this->elements = $manager->getFilesByDirectory($directory);
$this->manager = $manager;
}

/**
* @inheritDoc
*/
public function handleResponse(Player $player, $data) :void {
if (!isset($data)) {
return;
Expand All @@ -60,7 +51,6 @@ public function handleResponse(Player $player, $data) :void {
if ($select === -1 && !$this->isBaseDir) {
$beforeDir = $this->isBaseDir ? null : $this->directory->getDirectory();
$player->sendForm(new ExplorerForm($beforeDir, $this->manager));
return;
} else {
$element = $this->elements[$select];
if ($element instanceof TextFile) {
Expand All @@ -73,9 +63,6 @@ public function handleResponse(Player $player, $data) :void {
}
}

/**
* @inheritDoc
*/
public function jsonSerialize() {
$form = new SimpleForm();
$head = $this->isBaseDir ? "/" : $this->directory->getTitle();
Expand Down
8 changes: 4 additions & 4 deletions src/net/mydeacy/serverdocument/forms/SimpleForm.php
Expand Up @@ -7,7 +7,7 @@ class SimpleForm {
/**
* @var string[]
*/
private $formData = [];
private array $formData = [];

public function __construct() {
$this->formData = [
Expand All @@ -18,17 +18,17 @@ public function __construct() {
];
}

public function setTitle(String $title) {
public function setTitle(string $title) :static {
$this->formData["title"] = $title;
return $this;
}

public function setContent(String $text) {
public function setContent(string $text) :static {
$this->formData["content"] = $text;
return $this;
}

public function addButton(String $text, $image = null) {
public function addButton(string $text, $image = null) :static {
if ($image !== null) {
$this->formData["buttons"][] = [
"text" => $text,
Expand Down
45 changes: 36 additions & 9 deletions src/net/mydeacy/serverdocument/util/ElementFactory.php
Expand Up @@ -2,17 +2,44 @@

namespace net\mydeacy\serverdocument\util;

use net\mydeacy\serverdocument\elements\CommandElement;
use net\mydeacy\serverdocument\elements\DirectoryElement;
use net\mydeacy\serverdocument\elements\FileElement;
use net\mydeacy\serverdocument\elements\interfaces\Directory;
use net\mydeacy\serverdocument\elements\interfaces\Element;
use pocketmine\utils\Config;

interface ElementFactory {
class ElementFactory {

/**
* @param string $fileName
* @param string $dir
* @param Directory|null $directory
*
* @return Element|null
*/
function createElement(string $fileName, string $dir, ?Directory $directory) :?Element;
public function createElement(string $fileName, string $dir, ?Directory $directory) :?Element {
$element = null;
$fullPath = $dir . $fileName;
if (is_file($fullPath)) {
switch (pathinfo($fullPath)["extension"] ?? "") {
case "txt":
$content = preg_replace('/(\r\n|\r|\n)/s', "\n", file_get_contents($fullPath));
$element = new FileElement(pathinfo($fullPath)["filename"], $content, $directory);
break;
case "yml":
$element = $this->createByYaml($fullPath, $directory);
break;
}
} elseif (is_dir($fullPath)) {
$element = new DirectoryElement($fileName, $directory);
}
return $element;
}

private function createByYaml(string $fullPath, ?Directory $directory) :Element {
$config = new Config($fullPath, CONFIG::YAML);
switch ($config->get("type", "")) {
case "command":
$command = $config->get("cmd-body", "");
$element = new CommandElement(pathinfo($fullPath)["filename"], $command, $directory);
break;
default:
$element = null;
}
return $element;
}
}

0 comments on commit 2bca755

Please sign in to comment.