Skip to content
This repository has been archived by the owner on Dec 12, 2022. It is now read-only.

Commit

Permalink
feat: remove warp command added
Browse files Browse the repository at this point in the history
  • Loading branch information
Josscoder committed Sep 14, 2021
1 parent 49801f1 commit 9b5799f
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 62 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ If you encounter any bugs, have suggestions or questions, [create an issue](http

## Setup

1) Download the latest version [here](https://github.com/Josscoder/Warps/releases/latest)
1) Download the latest version [here](https://poggit.pmmp.io/ci/Josscoder/Warps#)
2) Put the .phar in your plugins folder
3) And finally, start your server!

Expand Down
26 changes: 3 additions & 23 deletions src/jossc/warps/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function onEnable() {
$this->giveItemWhenJoin = (bool) $itemConfig["give_item_when_join"];
$this->itemCustomName = TextFormat::colorize((string) $itemConfig["custom_name"]);

$this->reloadWarpsConfig();
$this->storage->reload();

$this->registerCommands();

Expand Down Expand Up @@ -84,24 +84,6 @@ private function registerCommands(): void {
);
}

public function reloadWarpsConfig(): void {
$config = $this->getConfig();

if (!$config->exists('warps')) {
return;
}

$storage = $this->storage;

$warps = $config->get('warps');

foreach ($warps as $warpData) {
$warpDataArray = explode(';', $warpData);

$storage->add($warpDataArray[0], $warpDataArray[1]);
}
}

/*** @return Main */
public static function getInstance(): Main {
return self::$instance;
Expand All @@ -118,14 +100,12 @@ public function getFormManager(): FormManager {
}

/*** @return bool */
public function isGiveItemWhenJoin(): bool
{
public function isGiveItemWhenJoin(): bool {
return $this->giveItemWhenJoin;
}

/*** @return string */
public function getItemCustomName(): string
{
public function getItemCustomName(): string {
return $this->itemCustomName;
}
}
3 changes: 1 addition & 2 deletions src/jossc/warps/command/AddWarpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ class AddWarpCommand extends Command {
private $formManager;

/*** AddWarpCommand constructor.*/
public function __construct()
{
public function __construct() {
parent::__construct(
'addwarp',
'Add a new warp location'
Expand Down
5 changes: 2 additions & 3 deletions src/jossc/warps/command/ReloadWarpsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class ReloadWarpsCommand extends Command {
private $main;

/*** ReloadWarpsCommand constructor.*/
public function __construct()
{
public function __construct() {
parent::__construct(
'reloadwarps',
'Reload all warps'
Expand All @@ -38,7 +37,7 @@ public function execute(CommandSender $sender, string $commandLabel, array $args
return false;
}

$this->main->reloadWarpsConfig();
$this->main->getStorage()->reload();

$sender->sendMessage(TextFormat::GREEN . 'All warps reloaded!');

Expand Down
3 changes: 1 addition & 2 deletions src/jossc/warps/command/RemoveWarpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ class RemoveWarpCommand extends Command {
private $formManager;

/*** RemoveWarpCommand constructor.*/
public function __construct()
{
public function __construct() {
parent::__construct(
'removewarp',
'Remove a warp '
Expand Down
3 changes: 1 addition & 2 deletions src/jossc/warps/command/WarpsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ class WarpsCommand extends Command {
private $formManager;

/*** AddWarpCommand constructor.*/
public function __construct()
{
public function __construct() {
parent::__construct(
'warps',
'See a warps list'
Expand Down
78 changes: 60 additions & 18 deletions src/jossc/warps/manager/FormManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace jossc\warps\manager;

use jossc\warps\utils\LocationUtils;
use jossc\warps\warp\Warp;
use lib\FormAPI\elements\Button;
use lib\FormAPI\window\{
CustomWindowForm,
Expand All @@ -22,8 +23,7 @@ class FormManager {
private $storage;

/*** FormManager constructor.*/
public function __construct()
{
public function __construct() {
$this->main = Main::getInstance();
$this->storage = $this->main->getStorage();
}
Expand All @@ -39,13 +39,14 @@ public function showWarpsMenu(Player $player): void {

$warps = $storage->getWarps();

if (count($warps) == 0) {
if ($storage->isEmpty()) {
$player->sendMessage(TextFormat::RED . 'There are no warps to see!');

return;
}

foreach ($warps as $value) {
/**@var Warp $warp*/
$warp = $storage->get($value->getId());

if (is_null($warp)) {
Expand All @@ -60,6 +61,7 @@ public function showWarpsMenu(Player $player): void {
}

$form->addHandler(function (Player $player, Button $button) {
/**@var Warp $warp*/
$warp = $this->storage->get($button->getName());

if (is_null($warp)) {
Expand All @@ -86,44 +88,84 @@ public function showAddWarpMenu(Player $player): void {
$form->addInput('id', 'Please insert a warp id:');

$form->addHandler(function (Player $player, CustomWindowForm $form) {
$warpId = $form->getElement('id');
$warp = $warpId->getFinalValue();
$warp = $form->getElement('id');
$warpId = $warp->getFinalValue();

if (strlen($warp) === 0) {
if (strlen($warpId) === 0) {
$player->sendMessage(TextFormat::RED . 'Please, insert a valid name or identifier!');

return;
}

if ($this->storage->contains($warp)) {
$storage = $this->storage;

if ($storage->contains($warpId)) {
$player->sendMessage(TextFormat::RED . 'That warp id already exist!');

return;
}

$main = $this->main;

$config = $main->getConfig();

$locationToString = LocationUtils::locationToString($player->getLocation());

$warps = $config->get('warps');
array_push($warps, "$warp;$locationToString");
$warps = $this->main->getConfig()->get('warps');
array_push($warps, "$warpId;$locationToString");

$config->set('warps', $warps);
$config->save();

$main->reloadWarpsConfig();
$storage->set($warps);

$player->sendMessage(
TextFormat::GREEN . "You have successfully created the warp: $warp!"
TextFormat::GREEN . "You have successfully created the warp: $warpId!"
);
});

$form->showTo($player);
}

/*** @param Player $player */
public function showRemoveWarpMenu(Player $player): void {
$form = new CustomWindowForm(
'warps',
'Warps List'
);

$storage = $this->storage;

$warps = $storage->getWarps();

if ($storage->isEmpty()) {
$player->sendMessage(TextFormat::RED . 'There are no warps to see!');

return;
}

$warpsWithId = [];

foreach ($warps as $warp) {
/**@var Warp $warp*/
array_push($warpsWithId, $warp->getId());
}

$form->addDropdown("warp", "Select the warp to remove:", $warpsWithId);

$form->addHandler(function (Player $player, CustomWindowForm $form) {
$warp = $form->getElement("warp");

$warpId = $warp->getFinalValue();

$storage = $this->storage;

if (!$storage->contains($warpId)) {
$player->sendMessage(TextFormat::RED . 'That warp does not exist!');

return;
}

$storage->removeFromStorage($warpId);

$player->sendMessage(
TextFormat::GREEN . "You have successfully removed the warp: $warpId!"
);
});

$form->showTo($player);
}
}
75 changes: 71 additions & 4 deletions src/jossc/warps/storage/WarpsStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@

namespace jossc\warps\storage;

use jossc\warps\Main;
use jossc\warps\utils\LocationUtils;
use jossc\warps\warp\Warp;

class WarpsStorage {

/*** @var Main */
private $main;

public function __construct()
{
$this->main = Main::getInstance();
}

/*** @var array */
private $warps = [];

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

Expand Down Expand Up @@ -41,8 +49,67 @@ public function remove(string $warpId): void {
* @param string $warpId
* @return Warp|null
*/
public function get(string $warpId): ?Warp
{
public function get(string $warpId): ?Warp {
return !$this->contains($warpId) ? null : $this->warps[$warpId];
}

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

/*** @return bool */
public function isEmpty(): bool {
return $this->count() === 0;
}

public function reload(): void {
$config = $this->main->getConfig();

if (!$config->exists('warps')) {
return;
}

unset($this->warps);

$this->warps = [];

$warps = $config->get('warps');

foreach ($warps as $warpData) {
$warpDataArray = explode(';', $warpData);

$this->add($warpDataArray[0], $warpDataArray[1]);
}
}

/*** @param array $warps */
public function set(array $warps): void {
$config = $this->main->getConfig();

$config->set('warps', $warps);
$config->save();

$this->reload();
}

/*** @param string $warpId */
public function removeFromStorage(string $warpId): void {
$warps = $this->main->getConfig()->get('warps');

$warpObject = $this->get($warpId);

if (is_null($warpObject)) {
return;
}

$locationToString = LocationUtils::locationToString($warpObject->getLocation());

$warpIdArray = array($warpId . ";" . $locationToString);

$updatedArray = array_diff($warps, $warpIdArray);

$this->remove($warpId);
$this->set($updatedArray);
}
}
2 changes: 1 addition & 1 deletion src/jossc/warps/utils/LocationUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public static function stringToLocation(string $locationString): Location {
* @return string
*/
public static function locationToString(Location $location): string {
return "$location->x:$location->y:$location->z:$location->yaw:$location->pitch:{$location->getLevel()->getFolderName()}";
return (int) $location->x . ":" . (int) $location->y . ":" . (int) $location->z . ":$location->yaw:$location->pitch:{$location->getLevel()->getFolderName()}";
}
}
9 changes: 3 additions & 6 deletions src/jossc/warps/warp/Warp.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,18 @@ class Warp {
* @param string $id
* @param Location $location
*/
public function __construct(string $id, Location $location)
{
public function __construct(string $id, Location $location) {
$this->id = $id;
$this->location = $location;
}

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

/*** @return Location */
public function getLocation(): Location
{
public function getLocation(): Location {
return $this->location;
}
}

0 comments on commit 9b5799f

Please sign in to comment.