Skip to content

Commit

Permalink
Replace libFormAPI with pmforms (fix #120, #121)
Browse files Browse the repository at this point in the history
  • Loading branch information
fuyutsuki committed Jun 1, 2022
1 parent f81a89e commit 2282da9
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 99 deletions.
4 changes: 2 additions & 2 deletions .poggit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ projects:
path: ""
icon: "/assets/Logo.png"
libs:
- src: jojoe77777/FormAPI/libFormAPI
version: 1.3.0
- src: dktapps-pm-pl/pmforms/pmforms
version: ^2.0.0
...
4 changes: 2 additions & 2 deletions plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ softdepend:
# aieuo/Mineflow
- Mineflow # >= 2.0
virions:
# jojoe77777/libFormAPI
- libFormAPI # >= 1.3
# dktapps-pm-pl/pmforms
- pmforms # ^2.0.0

author: yuko fuyutsuki
authors:
Expand Down
2 changes: 1 addition & 1 deletion resources/language/en_us.ini
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ form.list.description.1=Lists editable FloatingTexts within 10 blocks of straigh
form.list.description.2=Specified FloatingText name: {%0}\nPlease select the operation to do for this.
error.on.enable.not.packaged=Please use the phar file downloaded from PoggitCI (https://poggit.pmmp.io/p/Texter).
error.on.enable.not.found.libformapi=Can't find the libFormAPI in the virions folder. Please download libFormAPI and install it correctly.
error.on.enable.not.found.virions=Can't find the {%0} in the virions folder. Please download {%0} and install it correctly.
error.ft.name.not.specified=A FloatingText name is not specified
error.ft.name.exists=A FloatingText with the specified name: {%0} already exists.
error.ft.name.not.exists=No FloatingText with the specified name: {%0} does not exist.
Expand Down
2 changes: 1 addition & 1 deletion resources/language/ja_jp.ini
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ form.list.description.1=自分から直線距離10ブロック以内の編集可
form.list.description.2=指定された浮き文字: "{%0}"\nこの浮き文字に行う操作を選択して下さい。

error.on.enable.not.packaged=PoggitCI(https://poggit.pmmp.io/p/Texter)からダウンロードしたpharファイルを使ってください。
error.on.enable.not.found.libformapi=virionsフォルダにlibFormAPIが見つかりません。libFormAPIをダウンロードし、正しく導入してください。
error.on.enable.not.found.virions=virionsフォルダに{%0}が見つかりません。{%0}をダウンロードし、正しく導入してください。
error.ft.name.not.specified=浮き文字の名前が指定されていません
error.ft.name.exists=このワールドには"{%0}"の名前を持つ浮き文字が既に存在します。
error.ft.name.not.exists=このワールドには"{%0}"の名前を持つ浮き文字は存在しません。
Expand Down
8 changes: 5 additions & 3 deletions src/jp/mcbe/fuyutsuki/Texter/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
use function explode;
use function file_exists;
use function glob;
use function implode;
use function mkdir;
use function str_starts_with;

class Main extends PluginBase {

Expand Down Expand Up @@ -220,7 +222,7 @@ private function mineflowLinkage() {

private function isPackaged(): bool {
if (str_starts_with($this->getFile(), self::PHAR_HEADER)) {
if (class_exists(Dependencies::PACKAGED_LIBRARY_NAMESPACE . Dependencies::LIB_FORM_API)) {
if (class_exists(Dependencies::PACKAGED_LIBRARY_NAMESPACE . Dependencies::PMFORMS)) {
return true;// PoggitCI
}else {
$message = $this->lang->translateString("error.on.enable.not.packaged");
Expand All @@ -230,10 +232,10 @@ private function isPackaged(): bool {
}else {
$plugins = $this->getServer()->getPluginManager()->getPlugins();
if (isset($plugins["DEVirion"])) {
if (class_exists(Dependencies::LIB_FORM_API)) {
if (class_exists(Dependencies::PMFORMS)) {
return true;// developer
}else {
$message = $this->lang->translateString("error.on.enable.not.found.libformapi");
$message = $this->lang->translateString("error.on.enable.not.found.virions", [implode(", ", ["pmforms"])]);
$this->getLogger()->critical($message);
return false;
}
Expand Down
70 changes: 41 additions & 29 deletions src/jp/mcbe/fuyutsuki/Texter/command/form/AddFloatingTextForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

namespace jp\mcbe\fuyutsuki\Texter\command\form;

use jojoe77777\FormAPI\CustomForm;
use dktapps\pmforms\CustomForm;
use dktapps\pmforms\CustomFormResponse;
use dktapps\pmforms\element\Input;
use dktapps\pmforms\element\Label;
use dktapps\pmforms\element\StepSlider;
use dktapps\pmforms\element\Toggle;
use jp\mcbe\fuyutsuki\Texter\data\FloatingTextData;
use jp\mcbe\fuyutsuki\Texter\i18n\TexterLang;
use jp\mcbe\fuyutsuki\Texter\Main;
Expand All @@ -13,80 +18,87 @@
use pocketmine\math\Vector3;
use pocketmine\player\Player;
use pocketmine\utils\TextFormat;
use Ramsey\Uuid\Uuid;
use function count;
use function range;
use function strtolower;

class AddFloatingTextForm extends CustomForm {

private ?FloatingTextSession $session;

public function __construct(Player $player) {
parent::__construct(null);
$playerName = strtolower($player->getName());
$lang = TexterLang::fromLocale($player->getLocale());
$this->session = FloatingTextSession::get($playerName);
if ($this->session === null) {
$this->session = new FloatingTextSession($playerName, $lang);
}

$elements = [];
$inputName = $lang->translateString("form.ft.name.unique");

$this->setTitle(Main::prefix() . " txt > " . ($this->session->isEdit() ? "edit" : "add"));

if ($this->session->hasNoTexts()) {
$this->session->setNoTexts(false);
$this->addLabel(TextFormat::RED . "[!]" . $lang->translateString("form.add.error.no.texts"));
$elements[] = new Label(Uuid::uuid4()->getBytes(), TextFormat::RED . "[!]" . $lang->translateString("form.add.error.no.texts"));
}
if ($this->session->isDuplicateName()) {
$this->session->setDuplicateName(false);
$this->addLabel(TextFormat::RED . "[!]" . $lang->translateString("error.ft.name.exists", [
$elements[] = new Label(Uuid::uuid4()->getBytes(), TextFormat::RED . "[!]" . $lang->translateString("error.ft.name.exists", [
$this->session->name(),
]));
}

$this->addLabel($lang->translateString("form.add.description"));
$this->addInput($inputName, $inputName, $this->session->name(), FormLabels::NAME);
$elements[] = new Label(Uuid::uuid4()->getBytes(), $lang->translateString("form.add.description"));
$elements[] = new Input(FormLabels::NAME, $inputName, $inputName, $this->session->name());

if (count($this->session->texts()) >= 2) {
$spacing = $this->session->spacing()->multiply(10)->add(30, 30, 30);
$range = range(-30, 30);
foreach ($range as $k => $v) {
$range[$k] = (string)($v/10);
}
$this->addLabel($lang->translateString("form.add.spacing.description"));
$this->addStepSlider($lang->translateString("form.add.spacing.x"), $range, $spacing->getFloorX(), FormLabels::X);
$this->addStepSlider($lang->translateString("form.add.spacing.y"), $range, $spacing->getFloorY(), FormLabels::Y);
$this->addStepSlider($lang->translateString("form.add.spacing.z"), $range, $spacing->getFloorZ(), FormLabels::Z);
$this->addLabel($lang->translateString("command.txt.usage.cluster.remove"));
$elements[] = new Label(Uuid::uuid4()->getBytes(), $lang->translateString("form.add.spacing.description"));
$elements[] = new StepSlider(FormLabels::X, $lang->translateString("form.add.spacing.x"), $range, $spacing->getFloorX());
$elements[] = new StepSlider(FormLabels::Y, $lang->translateString("form.add.spacing.y"), $range, $spacing->getFloorY());
$elements[] = new StepSlider(FormLabels::Z, $lang->translateString("form.add.spacing.z"), $range, $spacing->getFloorZ());
$elements[] = new Label(Uuid::uuid4()->getBytes(), $lang->translateString("command.txt.usage.cluster.remove"));
}

$this->addLabel($lang->translateString("command.txt.usage.new.line"));
$elements[] = new Label(Uuid::uuid4()->getBytes(), $lang->translateString("command.txt.usage.new.line"));
if (empty($this->session->texts())) {
$this->session->addText("");
}
foreach ($this->session->texts() as $key => $text) {
$inputText = $lang->translateString("form.add.text", [
$key + 1,
]);
$this->addInput($inputText, $inputText, $text, FormLabels::TEXT . "_$key");
}
$this->addToggle($lang->translateString("form.add.more.ft"), null, FormLabels::ADD_MORE);
}

public function handleResponse(Player $player, $data): void {
if ($data === null) {
FloatingTextSession::remove(strtolower($player->getName()));
return;
$elements[] = new Input(FormLabels::TEXT . "_$key", $inputText, $inputText, $text);
}
$elements[] = new Toggle(FormLabels::ADD_MORE, $lang->translateString("form.add.more.ft"));

parent::__construct(
Main::prefix() . " txt > " . ($this->session->isEdit() ? "edit" : "add"),
$elements,
function(Player $player, CustomFormResponse $response): void {
$this->handleSubmit($player, $response);
},
function(Player $player): void {
FloatingTextSession::remove(strtolower($player->getName()));
}
);

$this->processData($data);
}

$this->session->setName($data[FormLabels::NAME]);
private function handleSubmit(Player $player, CustomFormResponse $response): void {
$this->session->setName($response->getString(FormLabels::NAME));
$texts = $this->session->texts();
$this->session->setTexts([]);

$empty = 0;
foreach ($texts as $key => $text) {
if (!empty($data[FormLabels::TEXT . "_$key"])) {
$this->session->addText($data[FormLabels::TEXT . "_$key"]);
if (!empty($response->getString(FormLabels::TEXT . "_$key"))) {
$this->session->addText($response->getString(FormLabels::TEXT . "_$key"));
}else {
++$empty;
}
Expand All @@ -104,11 +116,11 @@ public function handleResponse(Player $player, $data): void {

$spacing = null;
if (count($texts) >= 2) {
$spacing = new Vector3($data[FormLabels::X], $data[FormLabels::Y], $data[FormLabels::Z]);
$spacing = new Vector3($response->getFloat(FormLabels::X), $response->getFloat(FormLabels::Y), $response->getFloat(FormLabels::Z));
$spacing = $spacing->subtract(30, 30, 30)->divide(10);
}

if ($data[FormLabels::ADD_MORE]) {
if ($response->getBool(FormLabels::ADD_MORE)) {
$this->session->addText("");
self::send($player);
}else {
Expand Down
44 changes: 27 additions & 17 deletions src/jp/mcbe/fuyutsuki/Texter/command/form/ListFloatingTextForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,74 @@

namespace jp\mcbe\fuyutsuki\Texter\command\form;

use jojoe77777\FormAPI\SimpleForm;
use dktapps\pmforms\MenuForm;
use dktapps\pmforms\MenuOption;
use jp\mcbe\fuyutsuki\Texter\command\sub\EditSubCommand;
use jp\mcbe\fuyutsuki\Texter\command\sub\MoveSubCommand;
use jp\mcbe\fuyutsuki\Texter\data\FloatingTextData;
use jp\mcbe\fuyutsuki\Texter\i18n\TexterLang;
use jp\mcbe\fuyutsuki\Texter\Main;
use pocketmine\player\Player;
use pocketmine\utils\TextFormat;
use Ramsey\Uuid\Uuid;

class ListFloatingTextForm extends SimpleForm {
class ListFloatingTextForm extends MenuForm {

private TexterLang $lang;

private ?FloatingTextData $floatingTextData;

/** @var string[] */
private array $keys;

public function __construct(
Player $player,
private string $action = ""
) {
parent::__construct(null);
$this->lang = TexterLang::fromLocale($player->getLocale());

$this->setTitle(Main::prefix() . " txt > list");
$this->setContent($this->lang->translateString("form.list.description.1"));

$folderName = $player->getWorld()->getFolderName();
$this->floatingTextData = FloatingTextData::getInstance($folderName);

$options = [];
$floatingTexts = $this->floatingTextData->floatingTexts();
foreach ($floatingTexts as $name => $floatingText) {
foreach ($floatingTexts as $floatingText) {
if ($player->getPosition()->distanceSquared($floatingText->position()) <= 100) {
$text = mb_substr($floatingText->get(0)->text(), 0, 26);
$this->addButton("[{$floatingText->name()}]\n" . TextFormat::BOLD . "$text...", -1, "", $name);
$options[$floatingText->name()] = new MenuOption("[{$floatingText->name()}]\n" . TextFormat::BOLD . "$text...");
}
}
$this->addButton(TextFormat::DARK_RED . $this->lang->translateString("form.close"));
}
$options[Uuid::uuid4()->getBytes()] = new MenuOption(TextFormat::DARK_RED . $this->lang->translateString("form.close"));

public function handleResponse(Player $player, $data): void {
$this->processData($data);
if (!is_string($data)) return;
$this->keys = array_keys($options);
parent::__construct(
Main::prefix() . " txt > list",
$this->lang->translateString("form.list.description.1"),
$options,
function(Player $player, int $selected): void {
$this->handleSubmit($player, $selected);
}
);
}

if ($this->floatingTextData->existsFloatingText($data)) {
public function handleSubmit(Player $player, int $selected): void {
$selected = $this->keys[$selected];
if ($this->floatingTextData->existsFloatingText($selected)) {
if (!empty($this->action)) {
switch ($this->action) {
case EditSubCommand::NAME:
case EditSubCommand::ALIAS:
$subCommand = new EditSubCommand($data);
$subCommand = new EditSubCommand($selected);
$subCommand->execute($player);
return;

case MoveSubCommand::NAME:
case MoveSubCommand::ALIAS:
SelectMoveTargetForm::send($player, $data);
SelectMoveTargetForm::send($player, $selected);
return;
}
}
$form = new SelectActionForm($this->lang, $data);
$form = new SelectActionForm($this->lang, $selected);
$player->sendForm($form);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@

namespace jp\mcbe\fuyutsuki\Texter\command\form;

use jojoe77777\FormAPI\CustomForm;
use dktapps\pmforms\CustomForm;
use dktapps\pmforms\CustomFormResponse;
use dktapps\pmforms\element\Input;
use dktapps\pmforms\element\Label;
use jp\mcbe\fuyutsuki\Texter\data\FloatingTextData;
use jp\mcbe\fuyutsuki\Texter\i18n\TexterLang;
use jp\mcbe\fuyutsuki\Texter\Main;
use jp\mcbe\fuyutsuki\Texter\text\SendType;
use pocketmine\math\Vector3;
use pocketmine\player\Player;
use Ramsey\Uuid\Uuid;

class MoveFloatingTextToPositionForm extends CustomForm {

Expand All @@ -19,29 +23,36 @@ public function __construct(
private string $name,
bool $isNotValid = false
) {
parent::__construct(null);
$inputX = $lang->translateString("form.move.position.x");
$inputY = $lang->translateString("form.move.position.y");
$inputZ = $lang->translateString("form.move.position.z");

$this->setTitle(Main::prefix() . " txt > move > position");
if ($isNotValid) {
$this->addLabel($lang->translateString("form.move.position.error.is.not.valid"));
$elements[] = new Label(Uuid::uuid4()->toString(), $lang->translateString("form.move.position.error.is.not.valid"));
}
$this->addLabel($lang->translateString("form.move.position.description"));
$this->addInput($inputX, $inputX, null, FormLabels::X);
$this->addInput($inputY, $inputY, null, FormLabels::Y);
$this->addInput($inputZ, $inputZ, null, FormLabels::Z);
$elements[] = new Label(Uuid::uuid4()->toString(), $lang->translateString("form.move.position.description"));
$elements[] = new Input(FormLabels::X, $inputX, $inputX);
$elements[] = new Input(FormLabels::Y, $inputY, $inputY);
$elements[] = new Input(FormLabels::Z, $inputZ, $inputZ);

parent::__construct(
Main::prefix() . " txt > move > position",
$elements,
function(Player $player, CustomFormResponse $response): void {
$this->handleSubmit($player, $response);
}
);
}

public function handleResponse(Player $player, $data): void {
$this->processData($data);
if ($data === null) return;
if (!empty($data[FormLabels::X]) && !empty($data[FormLabels::Y]) && !empty($data[FormLabels::Z])) {
private function handleSubmit(Player $player, CustomFormResponse $response): void {
if (!empty($response->getString(FormLabels::X)) &&
!empty($response->getString(FormLabels::Y)) &&
!empty($response->getString(FormLabels::Z))
) {
$position = new Vector3(
floatval($data[FormLabels::X]),
floatval($data[FormLabels::Y]),
floatval($data[FormLabels::Z])
$response->getFloat(FormLabels::X),
$response->getFloat(FormLabels::Y),
$response->getFloat(FormLabels::Z)
);
$world = $player->getWorld();
$folderName = $world->getFolderName();
Expand Down
Loading

0 comments on commit 2282da9

Please sign in to comment.