From 30ede5e01e8c698c3fa8e3fb042ab83dccd7e1cf Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 5 Jan 2017 14:35:42 +0100 Subject: [PATCH] Added configurable maximum prices Closes #55 --- .../de/epiceric/shopchest/ShopCommand.java | 40 ++++++++++++++++--- .../de/epiceric/shopchest/config/Config.java | 10 ++++- .../de/epiceric/shopchest/config/Regex.java | 1 + .../shopchest/language/LanguageUtils.java | 2 + .../shopchest/language/LocalizedMessage.java | 2 + src/main/resources/config.yml | 18 ++++++--- src/main/resources/lang/de_DE.lang | 2 + src/main/resources/lang/en_US.lang | 8 ++++ 8 files changed, 72 insertions(+), 11 deletions(-) diff --git a/src/main/java/de/epiceric/shopchest/ShopCommand.java b/src/main/java/de/epiceric/shopchest/ShopCommand.java index 6d0f712..e4f3c09 100644 --- a/src/main/java/de/epiceric/shopchest/ShopCommand.java +++ b/src/main/java/de/epiceric/shopchest/ShopCommand.java @@ -301,7 +301,7 @@ class ShopCommand extends BukkitCommand { for (String key : plugin.getShopChestConfig().minimum_prices) { ItemStack itemStack; - double price = plugin.getConfig().getDouble("minimum-prices." + key); + double minPrice = plugin.getConfig().getDouble("minimum-prices." + key); if (key.contains(":")) { itemStack = new ItemStack(Material.getMaterial(key.split(":")[0]), 1, Short.parseShort(key.split(":")[1])); @@ -311,15 +311,45 @@ class ShopCommand extends BukkitCommand { if (itemStack.getType().equals(Utils.getPreferredItemInHand(p).getType()) && itemStack.getDurability() == Utils.getPreferredItemInHand(p).getDurability()) { if (buyEnabled) { - if ((buyPrice <= amount * price) && (buyPrice > 0)) { - p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.BUY_PRICE_TOO_LOW, new LocalizedMessage.ReplacedRegex(Regex.MIN_PRICE, String.valueOf(amount * price)))); + if ((buyPrice < amount * minPrice) && (buyPrice > 0)) { + p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.BUY_PRICE_TOO_LOW, new LocalizedMessage.ReplacedRegex(Regex.MIN_PRICE, String.valueOf(amount * minPrice)))); return; } } if (sellEnabled) { - if ((sellPrice <= amount * price) && (sellPrice > 0)) { - p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SELL_PRICE_TOO_LOW, new LocalizedMessage.ReplacedRegex(Regex.MIN_PRICE, String.valueOf(amount * price)))); + if ((sellPrice < amount * minPrice) && (sellPrice > 0)) { + p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SELL_PRICE_TOO_LOW, new LocalizedMessage.ReplacedRegex(Regex.MIN_PRICE, String.valueOf(amount * minPrice)))); + return; + } + } + } + } + + plugin.debug(p.getName() + "'s prices are higher than the minimum"); + + for (String key : plugin.getShopChestConfig().maximum_prices) { + + ItemStack itemStack; + double maxPrice = plugin.getConfig().getDouble("maximum-prices." + key); + + if (key.contains(":")) { + itemStack = new ItemStack(Material.getMaterial(key.split(":")[0]), 1, Short.parseShort(key.split(":")[1])); + } else { + itemStack = new ItemStack(Material.getMaterial(key), 1); + } + + if (itemStack.getType().equals(Utils.getPreferredItemInHand(p).getType()) && itemStack.getDurability() == Utils.getPreferredItemInHand(p).getDurability()) { + if (buyEnabled) { + if ((buyPrice > amount * maxPrice) && (buyPrice > 0)) { + p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.BUY_PRICE_TOO_HIGH, new LocalizedMessage.ReplacedRegex(Regex.MAX_PRICE, String.valueOf(amount * maxPrice)))); + return; + } + } + + if (sellEnabled) { + if ((sellPrice > amount * maxPrice) && (sellPrice > 0)) { + p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SELL_PRICE_TOO_HIGH, new LocalizedMessage.ReplacedRegex(Regex.MAX_PRICE, String.valueOf(amount * maxPrice)))); return; } } diff --git a/src/main/java/de/epiceric/shopchest/config/Config.java b/src/main/java/de/epiceric/shopchest/config/Config.java index 0e0bce1..cc38ef9 100644 --- a/src/main/java/de/epiceric/shopchest/config/Config.java +++ b/src/main/java/de/epiceric/shopchest/config/Config.java @@ -57,10 +57,17 @@ public class Config { /** *

The minimum prices for certain items

* This returns a key set, which contains e.g "STONE", "STONE:1", of the minimum-prices section in ShopChest's config. - * To actually retrieve the price for an item, you have to get the Double minimum-prices.key. + * To actually retrieve the minimum price for an item, you have to get the double {@code minimum-prices.}. **/ public Set minimum_prices; + /** + *

The maximum prices for certain items

+ * This returns a key set, which contains e.g "STONE", "STONE:1", of the {@code maximum-prices} section in ShopChest's config. + * To actually retrieve the maximum price for an item, you have to get the double {@code maximum-prices.}. + **/ + public Set maximum_prices; + /** *

List containing items, of which players can't create a shop

* If this list contains an item (e.g "STONE", "STONE:1"), it's in the blacklist. @@ -315,6 +322,7 @@ public class Config { database_mysql_password = plugin.getConfig().getString("database.mysql.password"); database_type = Database.DatabaseType.valueOf(plugin.getConfig().getString("database.type")); minimum_prices = (plugin.getConfig().getConfigurationSection("minimum-prices") == null) ? new HashSet() : plugin.getConfig().getConfigurationSection("minimum-prices").getKeys(true); + maximum_prices = (plugin.getConfig().getConfigurationSection("maximum-prices") == null) ? new HashSet() : plugin.getConfig().getConfigurationSection("maximum-prices").getKeys(true); allow_decimals_in_price = plugin.getConfig().getBoolean("allow-decimals-in-price"); allow_broken_items = plugin.getConfig().getBoolean("allow-broken-items"); auto_calculate_item_amount = (allow_decimals_in_price && plugin.getConfig().getBoolean("auto-calculate-item-amount")); diff --git a/src/main/java/de/epiceric/shopchest/config/Regex.java b/src/main/java/de/epiceric/shopchest/config/Regex.java index 5f523f3..19ba6a8 100644 --- a/src/main/java/de/epiceric/shopchest/config/Regex.java +++ b/src/main/java/de/epiceric/shopchest/config/Regex.java @@ -9,6 +9,7 @@ public enum Regex { ERROR("%ERROR%"), ENCHANTMENT("%ENCHANTMENT%"), MIN_PRICE("%MIN-PRICE%"), + MAX_PRICE("%MAX-PRICE%"), VERSION("%VERSION%"), BUY_PRICE("%BUY-PRICE%"), SELL_PRICE("%SELL-PRICE%"), diff --git a/src/main/java/de/epiceric/shopchest/language/LanguageUtils.java b/src/main/java/de/epiceric/shopchest/language/LanguageUtils.java index 18bcb49..ab103f6 100644 --- a/src/main/java/de/epiceric/shopchest/language/LanguageUtils.java +++ b/src/main/java/de/epiceric/shopchest/language/LanguageUtils.java @@ -953,6 +953,8 @@ public class LanguageUtils { messages.add(new LocalizedMessage(LocalizedMessage.Message.CANNOT_SELL_BROKEN_ITEM, langConfig.getString("message.cannot-sell-broken-item", "&cYou can't sell a broken item."))); messages.add(new LocalizedMessage(LocalizedMessage.Message.BUY_PRICE_TOO_LOW, langConfig.getString("message.buy-price-too-low", "&cThe buy price must be higher than %MIN-PRICE%."), Regex.MIN_PRICE)); messages.add(new LocalizedMessage(LocalizedMessage.Message.SELL_PRICE_TOO_LOW, langConfig.getString("message.sell-price-too-low", "&cThe sell price must be higher than %MIN-PRICE%."), Regex.MIN_PRICE)); + messages.add(new LocalizedMessage(LocalizedMessage.Message.BUY_PRICE_TOO_HIGH, langConfig.getString("message.buy-price-too-high", "&cThe buy price must be lower than %MAX-PRICE%."), Regex.MAX_PRICE)); + messages.add(new LocalizedMessage(LocalizedMessage.Message.SELL_PRICE_TOO_HIGH, langConfig.getString("message.sell-price-too-high", "&cThe sell price must be lower than %MAX-PRICE%."), Regex.MAX_PRICE)); messages.add(new LocalizedMessage(LocalizedMessage.Message.BUYING_DISABLED, langConfig.getString("message.buying-disabled", "&cBuying is disabled at this shop."))); messages.add(new LocalizedMessage(LocalizedMessage.Message.SELLING_DISABLED, langConfig.getString("message.selling-disabled", "&cSelling is disabled at this shop."))); messages.add(new LocalizedMessage(LocalizedMessage.Message.RELOADED_SHOPS, langConfig.getString("message.reloaded-shops", "&aSuccessfully reloaded %AMOUNT% shop/s."), Regex.AMOUNT)); diff --git a/src/main/java/de/epiceric/shopchest/language/LocalizedMessage.java b/src/main/java/de/epiceric/shopchest/language/LocalizedMessage.java index aed652b..d34d3c5 100644 --- a/src/main/java/de/epiceric/shopchest/language/LocalizedMessage.java +++ b/src/main/java/de/epiceric/shopchest/language/LocalizedMessage.java @@ -88,6 +88,8 @@ public class LocalizedMessage { CANNOT_SELL_BROKEN_ITEM, BUY_PRICE_TOO_LOW, SELL_PRICE_TOO_LOW, + BUY_PRICE_TOO_HIGH, + SELL_PRICE_TOO_HIGH, BUYING_DISABLED, SELLING_DISABLED, RELOADED_SHOPS, diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index e2dcdf5..36f6889 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -128,18 +128,26 @@ explosion-protection: true buy-greater-or-equal-sell: true # Set the minimum prices for each individual Item. Not per Stack, per single Item! -# To add an item DELETE THE '[]' after 'minimum-prices:' and follow the format below. +# To add an item, follow the format below. # Important: You must have exactly 2 spaces between the text and the edge. # You can find the item names in the 'item_names.txt' file. -minimum-prices: [] +minimum-prices: # "STONE:1": 0.5 # "DIAMOND_SWORD": 100 +# Set the maximum prices for each individual Item. Not per Stack, per single Item! +# To add an item, follow the format below. +# Important: You must have exactly 2 spaces between the text and the edge. +# You can find the item names in the 'item_names.txt' file. +maximum-prices: +# "STONE:1": 19.9 +# "DIAMOND_SWORD": 1000 + # Set the items of which a player can't create a shop. -# To add an item DELETE THE '[]' after 'blacklist:' and follow format below. +# To add an item, follow the format below. # Important: You must have exactly 2 spaces between the text and edge. # You can find the item names in the 'item_names.txt' file. -blacklist: [] +blacklist: # - "STONE:1" # - "DIAMOND_BLOCK" @@ -188,7 +196,7 @@ database: # Shop limits are handled with permissions. # A player with permission "shopchest.limit.X" has a limit of X shops, -# a player with permission "shopchest.limit.*" doesn't have a shop limit. +# a player with permission "shopchest.limit.*" does not have a shop limit. shop-limits: # Set whether admin shops should be excluded of the shop limits. diff --git a/src/main/resources/lang/de_DE.lang b/src/main/resources/lang/de_DE.lang index ec71b8b..24f3201 100644 --- a/src/main/resources/lang/de_DE.lang +++ b/src/main/resources/lang/de_DE.lang @@ -44,6 +44,8 @@ message.cannot-break-shop=&cDu kannst einen Shop nicht zerstören. message.cannot-sell-broken-item=&cDu kannst kein kaputtes Artikel verkaufen. message.buy-price-too-low=&cDer Kaufpreis muss höher sein als %MIN-PRICE%. message.sell-price-too-low=&cDer Verkaufspreis muss höher sein als %MIN-PRICE%. +message.buy-price-too-high=&cDer Kaufpreis muss geringer sein als %MAX-PRICE%. +message.sell-price-too-high=&cDer Verkaufspreis muss geringer sein als %MAX-PRICE%. message.buying-disabled=&cKaufen ist an diesem Shop nicht möglich. message.selling-disabled=&cVerkaufen ist an diesem Shop nicht möglich. message.reloaded-shops=&a%AMOUNT% Shop/s wurden erfolgreich neu geladen. diff --git a/src/main/resources/lang/en_US.lang b/src/main/resources/lang/en_US.lang index bee5d76..01f851a 100644 --- a/src/main/resources/lang/en_US.lang +++ b/src/main/resources/lang/en_US.lang @@ -159,6 +159,14 @@ message.buy-price-too-low=&cThe buy price must be higher than %MIN-PRICE%. # Usable regex: %MIN-PRICE% message.sell-price-too-low=&cThe sell price must be higher than %MIN-PRICE%. +# Set the message when the entered buy price is below the maximum price. +# Usable regex: %MAX-PRICE% +message.buy-price-too-high=&cThe buy price must be lower than %MAX-PRICE%. + +# Set the message when the entered sell price is below the maximum price. +# Usable regex: %MAX-PRICE% +message.sell-price-too-high=&cThe sell price must be lower than %MAX-PRICE%. + # Set the message when buying is disabled at the shop. message.buying-disabled=&cBuying is disabled at this shop.