From 460abc722eb8f44565ad6068099dfe5bb8534423 Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 26 Apr 2017 16:21:04 +0200 Subject: [PATCH] Added permission nodes for shop creation for specific items Examples: - "shopchest.create.apple" - "shopchest.create.stone.2" But: If a player has permission "shopchest.create", he is allowed to create shops of all items. Also, the creation of admin shops is not affected by these permissions. --- .../de/epiceric/shopchest/ShopCommand.java | 7 +++-- .../de/epiceric/shopchest/utils/Utils.java | 26 +++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/epiceric/shopchest/ShopCommand.java b/src/main/java/de/epiceric/shopchest/ShopCommand.java index 4aac551..7236144 100644 --- a/src/main/java/de/epiceric/shopchest/ShopCommand.java +++ b/src/main/java/de/epiceric/shopchest/ShopCommand.java @@ -107,8 +107,7 @@ class ShopCommand implements CommandExecutor { Player p = (Player) sender; if (args[0].equalsIgnoreCase("create")) { - - if (p.hasPermission(Permissions.CREATE)) { + if (Utils.hasPermissionToCreateShop(p, Utils.getPreferredItemInHand(p))) { if (args.length == 4) { needsHelp = false; create(args, ShopType.NORMAL, p); @@ -438,7 +437,7 @@ class ShopCommand implements CommandExecutor { } } - plugin.debug(p.getName() + "'s prices are higher than the minimum"); + plugin.debug(p.getName() + "'s prices are lower than the maximum"); if (sellEnabled && buyEnabled) { if (plugin.getShopChestConfig().buy_greater_or_equal_sell) { @@ -552,7 +551,7 @@ class ShopCommand implements CommandExecutor { if (sender instanceof Player) { if (sender.hasPermission(Permissions.CREATE_ADMIN)) { sender.sendMessage(ChatColor.GREEN + "/" + plugin.getShopChestConfig().main_command_name + " create [normal|admin] - " + LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_CREATE)); - } else { + } else if (sender.hasPermission(Permissions.CREATE)) { sender.sendMessage(ChatColor.GREEN + "/" + plugin.getShopChestConfig().main_command_name + " create - " + LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_CREATE)); } diff --git a/src/main/java/de/epiceric/shopchest/utils/Utils.java b/src/main/java/de/epiceric/shopchest/utils/Utils.java index 0e98212..3fcef13 100644 --- a/src/main/java/de/epiceric/shopchest/utils/Utils.java +++ b/src/main/java/de/epiceric/shopchest/utils/Utils.java @@ -257,6 +257,32 @@ public class Utils { return true; } + /** + *

Check if a player is allowed to create a shop that sells (or buys) the given item.

+ * The player is allowed to create the shop if has either the permission {@code shopchest.create}, + * {@code shopchest.create.[ITEM]} or {@code shopchest.create.[ITEM].[DURABILITY]} + * @param player Player to check + * @param item Item to be sold or bought + * @return Whether the player is allowed + */ + public static boolean hasPermissionToCreateShop(Player player, ItemStack item) { + if (player.hasPermission(Permissions.CREATE)) { + return true; + } + + if (item.getDurability() == 0) { + if (player.hasPermission(Permissions.CREATE + "." + item.getType().toString())) { + return true; + } + } + + if (player.hasPermission(Permissions.CREATE + "." + item.getType().toString() + "." + item.getDurability())) { + return true; + } + + return false; + } + /** * @param className Name of the class * @return Class in {@code net.minecraft.server.[VERSION]} package with the specified name or {@code null} if the class was not found