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

Cleanup and features requested in #11 #12

Merged
merged 1 commit into from
Dec 31, 2019
Merged
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
2 changes: 2 additions & 0 deletions .poggit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ projects:
libs:
- src: thebigsmilexd/customui/customui
version: ^3.0.0
- src: CortexPE/Commando/Commando
version: ^0.3.1
...
4 changes: 2 additions & 2 deletions plugin.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
name: WarpUI
main: xenialdan\WarpUI\Loader
version: 3.0.1
api: [3.0.0, 4.0.0]
version: 3.1.0
api: [3.9]
load: POSTWORLD
authors:
- XenialDan
Expand Down
6 changes: 6 additions & 0 deletions resources/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
warp-item: "compass"
give-warp-item: true
world-item: "clock"
give-world-item: true
...
63 changes: 23 additions & 40 deletions src/xenialdan/WarpUI/EventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,61 +9,40 @@
use pocketmine\item\ItemFactory;
use pocketmine\item\ItemIds;
use pocketmine\Player;
use pocketmine\plugin\Plugin;
use pocketmine\Server;
use pocketmine\utils\TextFormat;
use xenialdan\customui\elements\Button;
use xenialdan\customui\windows\SimpleForm;
use xenialdan\gameapi\API;

/**
* Class EventListener
* @package xenialdan\WarpUI
*/
class EventListener implements Listener
{
/** @var Loader */
public $owner;

public function __construct(Plugin $plugin)
{
$this->owner = $plugin;
}

/**
* @param PlayerInteractEvent $event
* @throws \BadMethodCallException
* @throws \InvalidArgumentException
*/
public function onInteract(PlayerInteractEvent $event)
{
$player = $event->getPlayer();
#if (($level = ($player = $event->getPlayer())->getLevel())->getId() !== Server::getInstance()->getDefaultLevel()->getId()) return;
if (($item = $event->getItem())->getId() === ItemIds::COMPASS) {
#if (($level = ($player = $event->getPlayer())->getLevel())->getId() !== Loader::getInstance()->getServer()->getDefaultLevel()->getId()) return;
if (($item = $event->getItem())->getId() === ItemIds::COMPASS && $item->hasCustomName() && $item->getCustomName() === TextFormat::DARK_PURPLE . "Warps") {
$event->setCancelled();
$form = new SimpleForm(TextFormat::DARK_PURPLE . "Warps", "Teleport to any warp");
foreach (Loader::getWarps() as $warp) {
$form->addButton(new Button($warp));
}
$form->setCallable(function (Player $player, $data) {
$location = Loader::getWarp($data);
if (!is_null($location)) $player->teleport($location);
else $player->sendMessage("Warp was not found. Please contact an administrator about this\nError: Warp not found\nWarpname: " . $data);
}
);
$player->sendForm($form);
Loader::getInstance()->showWarpUI($player);
}
if (($item = $event->getItem())->getId() === ItemIds::CLOCK) {
if (($item = $event->getItem())->getId() === ItemIds::CLOCK && $item->hasCustomName() && $item->getCustomName() === TextFormat::DARK_PURPLE . "Worlds") {
$event->setCancelled();
$form = new SimpleForm(TextFormat::DARK_PURPLE . "Worlds", "Teleport to any world");
foreach (API::getAllWorlds() as $warp) {
$form->addButton(new Button($warp));
}
$form->setCallable(function (Player $player, $data) {
Loader::getInstance()->getServer()->loadLevel($data);
$location = Loader::getInstance()->getServer()->getLevelByName($data)->getSpawnLocation();
if (!is_null($location)) $player->teleport($location);
else $player->sendMessage("World was not found. Please contact an administrator about this\nError: Something is wrong with the world location\nWorld: " . $data);
}
);
$player->sendForm($form);
Loader::getInstance()->showWorldUI($player);
}
}

/**
* @param PlayerJoinEvent $event
*/
public function onJoin(PlayerJoinEvent $event)
{
if (($level = ($player = $event->getPlayer())->getLevel())->getId() !== Server::getInstance()->getDefaultLevel()->getId()) return;
if (($level = ($player = $event->getPlayer())->getLevel())->getId() !== Loader::getInstance()->getServer()->getDefaultLevel()->getId()) return;
$compass = ItemFactory::get(ItemIds::COMPASS)->setCustomName(TextFormat::DARK_PURPLE . "Warps");
if (!$player->getInventory()->contains($compass))
$player->getInventory()->addItem($compass);
Expand All @@ -72,11 +51,15 @@ public function onJoin(PlayerJoinEvent $event)
$player->getInventory()->addItem($clock);
}

/**
* @param EntityLevelChangeEvent $event
* @throws \BadMethodCallException
*/
public function onLevelChange(EntityLevelChangeEvent $event)
{
/** @var Player $player */
if (!($player = $event->getEntity()) instanceof Player || $event->isCancelled()) return;
if (($level = $event->getTarget())->getId() !== Server::getInstance()->getDefaultLevel()->getId()) return;
if (($level = $event->getTarget())->getId() !== Loader::getInstance()->getServer()->getDefaultLevel()->getId()) return;
$compass = ItemFactory::get(ItemIds::COMPASS)->setCustomName(TextFormat::DARK_PURPLE . "Warps");
if (!$player->getInventory()->contains($compass))
$player->getInventory()->addItem($compass);
Expand Down
98 changes: 85 additions & 13 deletions src/xenialdan/WarpUI/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

namespace xenialdan\WarpUI;

use pocketmine\level\format\io\LevelProvider;
use pocketmine\level\format\io\LevelProviderManager;
use pocketmine\level\Location;
use pocketmine\Player;
use pocketmine\plugin\PluginBase;
use pocketmine\Server;
use pocketmine\utils\Config;

use pocketmine\utils\TextFormat;
use xenialdan\customui\elements\Button;
use xenialdan\customui\windows\SimpleForm;

class Loader extends PluginBase
{
Expand All @@ -15,10 +19,37 @@ class Loader extends PluginBase
/** @var Config */
private $warps;

public function onLoad()
{
self::$instance = $this;
#$this->saveDefaultConfig();
$this->warps = new Config($this->getDataFolder() . "warps.yml");
}

/**
* @throws \pocketmine\plugin\PluginException
*/
public function onEnable()
{
$this->getServer()->getPluginManager()->registerEvents(new EventListener(), $this);
$this->getServer()->getCommandMap()->register("WarpUI", new WarpUICommands($this));
$this->getServer()->getCommandMap()->register("WarpUI", new WorldUICommands($this));
}

/**
* Returns an instance of the plugin
* @return Loader
*/
public static function getInstance()
{
return self::$instance;
}

/**
* @param Location $location
* @param string $name
* @return bool
* @throws \InvalidStateException
*/
public static function addWarp(Location $location, string $name): bool
{
Expand All @@ -35,7 +66,7 @@ public static function getWarp(string $name): ?Location
{
$values = self::getInstance()->warps->get($name);
if ($values === false) return null;
return new Location($values["x"], $values["y"], $values["z"], $values["yaw"], $values["pitch"], Server::getInstance()->getLevelByName($values["levelname"]));
return new Location($values["x"], $values["y"], $values["z"], $values["yaw"], $values["pitch"], Loader::getInstance()->getServer()->getLevelByName($values["levelname"]));
}

/**
Expand All @@ -49,6 +80,7 @@ public static function getWarps()
/**
* @param $name
* @return bool
* @throws \InvalidStateException
*/
public static function removeWarp($name)
{
Expand All @@ -58,24 +90,64 @@ public static function removeWarp($name)
return true;
}

public function onLoad()
/**
* @param Player $player
* @throws \InvalidArgumentException
*/
public function showWarpUI(Player $player): void
{
self::$instance = $this;
$this->warps = new Config($this->getDataFolder() . "warps.yml");
$form = new SimpleForm(TextFormat::DARK_PURPLE . "Warps", "Teleport to any warp");
foreach (Loader::getWarps() as $warp) {
$form->addButton(new Button($warp));
}
$form->setCallable(function (Player $player, $data) {
$location = Loader::getWarp($data);
if (!is_null($location)) $player->teleport($location);
else $player->sendMessage("Warp was not found. Please contact an administrator about this\nError: Warp not found\nWarpname: " . $data);
}
);
$player->sendForm($form);
}

public function onEnable()
/**
* @param Player $player
* @throws \InvalidArgumentException
*/
public function showWorldUI(Player $player): void
{
$this->getServer()->getPluginManager()->registerEvents(new EventListener($this), $this);
$this->getServer()->getCommandMap()->register(Commands::class, new Commands($this));
$form = new SimpleForm(TextFormat::DARK_PURPLE . "Worlds", "Teleport to any world");
foreach (self::getAllWorlds() as $warp) {
$form->addButton(new Button($warp));
}
$form->setCallable(function (Player $player, $data) {
Loader::getInstance()->getServer()->loadLevel($data);
$location = Loader::getInstance()->getServer()->getLevelByName($data)->getSpawnLocation();
if (!is_null($location)) $player->teleport($location);
else $player->sendMessage("World was not found. Please contact an administrator about this\nError: Something is wrong with the world location\nWorld: " . $data);
}
);
$player->sendForm($form);
}

/**
* Returns an instance of the plugin
* @return Loader
* Returns all world names (!NOT FOLDER NAMES, level.dat entries) of valid levels in "/worlds"
* @return string[]
*/
public static function getInstance()
private static function getAllWorlds(): array
{
return self::$instance;
$worldNames = [];
$glob = glob(Loader::getInstance()->getServer()->getDataPath() . "worlds/*", GLOB_ONLYDIR);
if ($glob === false) return $worldNames;
foreach ($glob as $path) {
$path .= DIRECTORY_SEPARATOR;
$provider = LevelProviderManager::getProvider($path);
if ($provider !== \null) {
/** @var LevelProvider $c */
$c = (new $provider($path));
$worldNames[] = $c->getName();
}
}
sort($worldNames);
return $worldNames;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use xenialdan\WarpUI\subcommand\SubCommand;
use xenialdan\WarpUI\subcommand\TeleportSubCommand;

class Commands extends PluginCommand
class WarpUICommands extends PluginCommand
{
private $subCommands = [];

Expand All @@ -23,7 +23,6 @@ class Commands extends PluginCommand
public function __construct(Plugin $plugin)
{
parent::__construct("warpui", $plugin);
$this->setAliases(["warpui"]);
$this->setPermission("warpui.command");
$this->setDescription("The main commands for warpui");

Expand All @@ -43,10 +42,27 @@ private function loadSubCommand(SubCommand $command)
}
}

/**
* @param CommandSender $sender
* @param string $commandLabel
* @param array $args
* @return bool|mixed
* @throws \InvalidArgumentException
* @throws \InvalidStateException
*/
public function execute(CommandSender $sender, string $commandLabel, array $args)
{
if (!isset($args[0])) {
return $this->sendHelp($sender);
if (!$sender instanceof Player) {
$sender->sendMessage(TextFormat::RED . "This command must be run ingame");
return true;
}
if (!$sender->hasPermission($this->getPermission())) {
$sender->sendMessage(TextFormat::RED . "No permission");
return true;
}
Loader::getInstance()->showWarpUI($sender);
return true;
}
$subCommand = strtolower(array_shift($args));
if (!isset($this->subCommands[$subCommand])) {
Expand All @@ -58,7 +74,7 @@ public function execute(CommandSender $sender, string $commandLabel, array $args
if (!$command->execute($sender, $args)) {
$sender->sendMessage(TextFormat::YELLOW . "Usage: /warpui " . $command->getName() . TextFormat::BOLD . TextFormat::DARK_AQUA . " > " . TextFormat::RESET . TextFormat::YELLOW . $command->getUsage());
}
} elseif (!($sender instanceof Player)) {
} else if (!($sender instanceof Player)) {
$sender->sendMessage(TextFormat::RED . "Please run this command in-game.");
} else {
$sender->sendMessage(TextFormat::RED . "You do not have permissions to run this command");
Expand Down
42 changes: 42 additions & 0 deletions src/xenialdan/WarpUI/WorldUICommands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace xenialdan\WarpUI;

use pocketmine\command\CommandSender;
use pocketmine\command\PluginCommand;
use pocketmine\Player;
use pocketmine\plugin\Plugin;
use pocketmine\utils\TextFormat;

class WorldUICommands extends PluginCommand
{

public function __construct(Plugin $plugin)
{
parent::__construct("worldui", $plugin);
$this->setPermission("worldui.command");
$this->setDescription("The main commands for worldui");
}

/**
* @param CommandSender $sender
* @param string $commandLabel
* @param array $args
* @return bool
* @throws \InvalidArgumentException
* @throws \InvalidStateException
*/
public function execute(CommandSender $sender, string $commandLabel, array $args): bool
{
if (!$sender instanceof Player) {
$sender->sendMessage(TextFormat::RED . "This command must be run ingame");
return true;
}
if (!$sender->hasPermission($this->getPermission())) {
$sender->sendMessage(TextFormat::RED . "No permission");
return true;
}
Loader::getInstance()->showWorldUI($sender);
return true;
}
}
6 changes: 6 additions & 0 deletions src/xenialdan/WarpUI/subcommand/AddSubCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
class AddSubCommand extends SubCommand
{

/**
* @param CommandSender $sender
* @return bool
* @throws \InvalidStateException
*/
public function canUse(CommandSender $sender)
{
return ($sender instanceof Player) and $sender->hasPermission("warpui.command.add");
Expand Down Expand Up @@ -39,6 +44,7 @@ public function getAliases()
* @param CommandSender $sender
* @param array $args
* @return bool
* @throws \InvalidStateException
*/
public function execute(CommandSender $sender, array $args)
{
Expand Down
Loading