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

Commit

Permalink
Add a permission to set a maximum number of shops
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonw4331 committed Jul 5, 2019
1 parent 937b7c3 commit f28ea2d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
12 changes: 11 additions & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,14 @@ permissions:
children:
chestshop.command.id:
default: true
description: "Allows searching item ID"
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"
30 changes: 30 additions & 0 deletions src/ChestShop/ChestShop.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
2 changes: 2 additions & 0 deletions src/ChestShop/EventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f28ea2d

Please sign in to comment.