From f28ea2db5ed7a47a3657428d66af0be8038706e3 Mon Sep 17 00:00:00 2001 From: jasonwynn10 Date: Fri, 5 Jul 2019 08:37:42 -0400 Subject: [PATCH] Add a permission to set a maximum number of shops --- plugin.yml | 12 +++++++++++- src/ChestShop/ChestShop.php | 30 ++++++++++++++++++++++++++++++ src/ChestShop/EventListener.php | 2 ++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/plugin.yml b/plugin.yml index a413c8a..9bba6ae 100644 --- a/plugin.yml +++ b/plugin.yml @@ -26,4 +26,14 @@ permissions: children: chestshop.command.id: default: true - description: "Allows searching item ID" \ No newline at end of file + description: "Allows searching item ID" + chestshop.makeshop: + default: op + description: "Allows making chest shops" + children: + chestshop.makeshop.1: + default: true + description: "Allows making 1 chest shop" + chestshop.makeshop.unlimited: + default: op + description: "Allows making unlimited chest shops" \ No newline at end of file diff --git a/src/ChestShop/ChestShop.php b/src/ChestShop/ChestShop.php index 47ef597..bcf4976 100644 --- a/src/ChestShop/ChestShop.php +++ b/src/ChestShop/ChestShop.php @@ -5,6 +5,9 @@ use pocketmine\command\Command; use pocketmine\command\CommandSender; use pocketmine\item\ItemIds; +use pocketmine\permission\Permission; +use pocketmine\permission\PermissionManager; +use pocketmine\Player; use pocketmine\plugin\PluginBase; class ChestShop extends PluginBase @@ -33,4 +36,31 @@ public function onCommand(CommandSender $sender, Command $command, string $label return false; } } + + /** + * Get the maximum number of shops a player can create + * + * @param Player $player + * + * @return int + */ + public function getMaxPlayerShops(Player $player) : int { + if($player->hasPermission("chestshop.makeshop.unlimited")) + return PHP_INT_MAX; + /** @var Permission[] $perms */ + $perms = array_merge(PermissionManager::getInstance()->getDefaultPermissions($player->isOp()), $player->getEffectivePermissions()); + $perms = array_filter($perms, function($name) { + return (substr($name, 0, 19) === "chestshop.makeshop."); + }, ARRAY_FILTER_USE_KEY); + if(count($perms) === 0) + return 0; + krsort($perms, SORT_FLAG_CASE | SORT_NATURAL); + foreach($perms as $name => $perm) { + $maxPlots = substr($name, 19); + if(is_numeric($maxPlots)) { + return (int) $maxPlots; + } + } + return 0; + } } \ No newline at end of file diff --git a/src/ChestShop/EventListener.php b/src/ChestShop/EventListener.php index 4c08b1d..63ae2d7 100644 --- a/src/ChestShop/EventListener.php +++ b/src/ChestShop/EventListener.php @@ -163,6 +163,8 @@ public function onSignChange(SignChangeEvent $event) if (!is_numeric($price) or $price < 0) return; if ($pID === false) return; if (($chest = $this->getSideChest($sign)) === false) return; + $shops = $this->databaseManager->selectByCondition(["shopOwner" => $shopOwner]); + if(!is_array($shops) or count($shops) + 1 > $this->plugin->getMaxPlayerShops($event->getPlayer())) return; $productName = ItemFactory::get($pID, $pMeta)->getName(); $event->setLine(0, $shopOwner);