From b73ad05034640635f783e8097c34ee6ba9ad4d20 Mon Sep 17 00:00:00 2001 From: Eric Date: Sat, 12 Nov 2016 13:11:08 +0100 Subject: [PATCH] Use permission based shop limits This also removes the permission "shopchest.limits" Closes #27 --- .../de/epiceric/shopchest/ShopCommand.java | 18 ++---- .../shopchest/language/LanguageUtils.java | 1 - .../shopchest/language/LocalizedMessage.java | 1 - .../epiceric/shopchest/utils/ShopUtils.java | 57 +++++-------------- src/main/resources/config.yml | 29 +--------- src/main/resources/lang/de_DE.lang | 1 - src/main/resources/lang/en_US.lang | 3 - src/main/resources/plugin.yml | 7 +-- 8 files changed, 25 insertions(+), 92 deletions(-) diff --git a/src/main/java/de/epiceric/shopchest/ShopCommand.java b/src/main/java/de/epiceric/shopchest/ShopCommand.java index 8e468de..08ca2b5 100644 --- a/src/main/java/de/epiceric/shopchest/ShopCommand.java +++ b/src/main/java/de/epiceric/shopchest/ShopCommand.java @@ -120,17 +120,13 @@ class ShopCommand extends BukkitCommand { return true; } } else if (args[0].equalsIgnoreCase("limits")) { - if (perm.has(p, "shopchest.limits")) { - plugin.debug(p.getName() + " is viewing his shop limits: " + shopUtils.getShopAmount(p) + "/" + shopUtils.getShopLimit(p)); - int limit = shopUtils.getShopLimit(p); - p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.OCCUPIED_SHOP_SLOTS, - new LocalizedMessage.ReplacedRegex(Regex.LIMIT, (limit < 0 ? "∞" : String.valueOf(limit))), - new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(shopUtils.getShopAmount(p))))); + plugin.debug(p.getName() + " is viewing his shop limits: " + shopUtils.getShopAmount(p) + "/" + shopUtils.getShopLimit(p)); + int limit = shopUtils.getShopLimit(p); + p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.OCCUPIED_SHOP_SLOTS, + new LocalizedMessage.ReplacedRegex(Regex.LIMIT, (limit < 0 ? "∞" : String.valueOf(limit))), + new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(shopUtils.getShopAmount(p))))); - return true; - } else { - p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_LIMITS)); - } + return true; } else if (args[0].equalsIgnoreCase("config")) { if (perm.has(p, "shopchest.config")) { if (args.length >= 4) { @@ -167,8 +163,6 @@ class ShopCommand extends BukkitCommand { sendBasicHelpMessage(p); return true; } - - return true; } } else { diff --git a/src/main/java/de/epiceric/shopchest/language/LanguageUtils.java b/src/main/java/de/epiceric/shopchest/language/LanguageUtils.java index 6d0b4d2..5208aac 100644 --- a/src/main/java/de/epiceric/shopchest/language/LanguageUtils.java +++ b/src/main/java/de/epiceric/shopchest/language/LanguageUtils.java @@ -914,7 +914,6 @@ public class LanguageUtils { messages.add(new LocalizedMessage(LocalizedMessage.Message.NO_PERMISSION_REMOVE_OTHERS, langConfig.getString("message.noPermission.remove-others", "&cYou don't have permission to remove this shop."))); messages.add(new LocalizedMessage(LocalizedMessage.Message.NO_PERMISSION_RELOAD, langConfig.getString("message.noPermission.reload", "&cYou don't have permission to reload the shops."))); messages.add(new LocalizedMessage(LocalizedMessage.Message.NO_PERMISSION_UPDATE, langConfig.getString("message.noPermission.update", "&cYou don't have permission to check for updates."))); - messages.add(new LocalizedMessage(LocalizedMessage.Message.NO_PERMISSION_LIMITS, langConfig.getString("message.noPermission.limits", "&cYou don't have permission to view the shop limits."))); messages.add(new LocalizedMessage(LocalizedMessage.Message.NO_PERMISSION_CONFIG, langConfig.getString("message.noPermission.config", "&cYou don't have permission to change configuration values."))); messages.add(new LocalizedMessage(LocalizedMessage.Message.COMMAND_DESC_CREATE, langConfig.getString("message.commandDescription.create", "Create a shop."))); messages.add(new LocalizedMessage(LocalizedMessage.Message.COMMAND_DESC_REMOVE, langConfig.getString("message.commandDescription.remove", "Remove a shop."))); diff --git a/src/main/java/de/epiceric/shopchest/language/LocalizedMessage.java b/src/main/java/de/epiceric/shopchest/language/LocalizedMessage.java index ae01c76..c1daaf5 100644 --- a/src/main/java/de/epiceric/shopchest/language/LocalizedMessage.java +++ b/src/main/java/de/epiceric/shopchest/language/LocalizedMessage.java @@ -111,7 +111,6 @@ public class LocalizedMessage { NO_PERMISSION_REMOVE_OTHERS, NO_PERMISSION_RELOAD, NO_PERMISSION_UPDATE, - NO_PERMISSION_LIMITS, NO_PERMISSION_CONFIG, COMMAND_DESC_CREATE, COMMAND_DESC_REMOVE, diff --git a/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java b/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java index 827945b..7dedb7c 100644 --- a/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java +++ b/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java @@ -7,10 +7,9 @@ import de.epiceric.shopchest.sql.Database; import org.bukkit.*; import org.bukkit.block.Chest; import org.bukkit.block.DoubleChest; -import org.bukkit.entity.Entity; -import org.bukkit.entity.Item; import org.bukkit.entity.Player; import org.bukkit.inventory.InventoryHolder; +import org.bukkit.permissions.PermissionAttachmentInfo; import java.util.*; @@ -124,53 +123,27 @@ public class ShopUtils { public int getShopLimit(Player p) { int limit = plugin.getShopChestConfig().default_limit; - if (plugin.getPermission().hasGroupSupport()) { - List groups = new ArrayList(); + for (PermissionAttachmentInfo permInfo : p.getEffectivePermissions()) { + if (permInfo.getPermission().startsWith("shopchest.limit.")) { + if (permInfo.getPermission().contains("shopchest.limit.*")) { + limit = -1; + break; + } else { + String[] spl = permInfo.getPermission().split("shopchest.limit."); - for (String key : plugin.getShopChestConfig().shopLimits_group) { - for (int i = 0; i < plugin.getPermission().getGroups().length; i++) { - if (plugin.getPermission().getGroups()[i].equals(key)) { - if (plugin.getPermission().playerInGroup(p, key)) { - groups.add(key); + if (spl.length > 1) { + try { + int newLimit = Integer.valueOf(spl[1]); + limit = Math.max(limit, newLimit); + } catch (NumberFormatException ignored) { + /* Ignore and continue */ } } } } - - if (groups.size() != 0) { - List limits = new ArrayList<>(); - for (String group : groups) { - int gLimit = ShopChest.getInstance().getConfig().getInt("shop-limits.group." + group); - limits.add(gLimit); - } - - int highestLimit = 0; - for (int l : limits) { - if (l > highestLimit) { - highestLimit = l; - } else if (l == -1) { - highestLimit = -1; - break; - } - } - - limit = highestLimit; - } - } - - for (String key : plugin.getShopChestConfig().shopLimits_player) { - int pLimit = ShopChest.getInstance().getConfig().getInt("shop-limits.player." + key); - if (Utils.isUUID(key)) { - if (p.getUniqueId().equals(UUID.fromString(key))) { - limit = pLimit; - } - } else { - if (p.getName().equals(key)) { - limit = pLimit; - } - } } + if (limit < -1) limit = -1; return limit; } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 92018c4..d3f4e43 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -131,31 +131,4 @@ database: # ...password you are going to login with # Be careful, as anyone who can read this file, can read the password! - password: "" - -# Priority: default < group < player -shop-limits: - - # Set whether admin shops should be excluded of the shop limits. - # If set to true, admin shops won't be added to a player's shop count. - exclude-admin-shops: true - - # Set the amount of shops anyone who's not listed in the sections below can have. - # If you don't want the players to have a limit, set the value to -1. - default: 5 - - # Set the amount of shops a player in a specific permission group can have. - # If you don't want the group to have a limit, set the value to -1. - # To add an item DELETE THE '[]' after 'group:' and follow the format below. - # Important: You must have exactly 4 spaces between the text and the edge. - group: [] - # "VIP": 10 - - # Set the amount of shops a specific player can have. - # You can add a player by its name or by its UUID, but please do NOT provide the name and the UUID. - # If you don't want the player to have a limit, set the value to -1. - # To add an item DELETE THE '[]' after 'player:' and follow the format below. - # Important: You must have exactly 4 spaces between the text and the edge. - player: [] - # "EpicEric": 50 - # "898afbbe-6566-4a0f-b0ac-145868b3cb12": 50 + password: "" \ No newline at end of file diff --git a/src/main/resources/lang/de_DE.lang b/src/main/resources/lang/de_DE.lang index ca6c0d7..d1b2020 100644 --- a/src/main/resources/lang/de_DE.lang +++ b/src/main/resources/lang/de_DE.lang @@ -67,7 +67,6 @@ message.noPermission.sell=&cDu hast keine Berechtigung etwas zu verkaufen. message.noPermission.remove-others=&cDu hast keine Berechtigung diesen Shop zu entfernen. message.noPermission.reload=&cDu hast keine Berechtigung die Shops neu zu laden. message.noPermission.update=&cDu hast keine Berechtigung nach Aktualisierungen zu suchen. -message.noPermission.limits=&cDu hast keine Berechtigung die Shop Limits zu sehen. message.noPermission.config=&cDu hast keine Berechtigung Konfigurationswerte zu verändern. message.commandDescription.create=Erstelle einen Shop. message.commandDescription.remove=Entferne einen Shop. diff --git a/src/main/resources/lang/en_US.lang b/src/main/resources/lang/en_US.lang index e7d04b2..78fafb6 100644 --- a/src/main/resources/lang/en_US.lang +++ b/src/main/resources/lang/en_US.lang @@ -235,9 +235,6 @@ message.noPermission.reload=&cYou don't have permission to reload the shops. # Set the message when a not permitted player tries to check for updates. message.noPermission.update=&cYou don't have permission to check for updates. -# Set the message when a not permitted player tries to view the shop limits. -message.noPermission.limits=&cYou don't have permission to view the shop limits. - # Set the message when a not permitted player tries to change configuration values. message.noPermission.config=&cYou don't have permission to change configuration values. diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 50cf8eb..c1fc722 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -18,7 +18,7 @@ permissions: shopchest.notification.update: true shopchest.reload: true shopchest.update: true - shopchest.limits: true + shopchest.limit.*: true shopchest.config: true shopchest.create: description: Allows you to create a shop. @@ -54,9 +54,8 @@ permissions: shopchest.update: description: Allows you to check for updates. default: op - shopchest.limits: - description: Allows you to view shop limits. - default: true + shopchest.limit.*: + default: op shopchest.config: description: Allows you to change configuration values per command. default: op