From 11422211e58f775ef01327eb1aad8a28dec6ca37 Mon Sep 17 00:00:00 2001 From: Tau Date: Mon, 9 Sep 2024 18:43:51 +1000 Subject: [PATCH 1/4] Don't display a ridiculously long value when max price is unlimited. Only display both min and max price messages if there is a min and max price set. Otherwise, just display one or the other. Don't decimal format price restriction messages as you may lose required granularity. --- crowdin/lang/en-US/messages.yml | 2 + .../quickshop/shop/SimplePriceLimiter.java | 38 +++++++++---------- .../quickshop/shop/SimpleShopManager.java | 29 ++++++++++---- .../com/ghostchu/quickshop/util/ShopUtil.java | 25 ++++++++---- 4 files changed, 58 insertions(+), 36 deletions(-) diff --git a/crowdin/lang/en-US/messages.yml b/crowdin/lang/en-US/messages.yml index 379670c22f..ada1b72fb7 100644 --- a/crowdin/lang/en-US/messages.yml +++ b/crowdin/lang/en-US/messages.yml @@ -296,6 +296,8 @@ support-disable-reason: supertool-is-disabled: Super tool is disabled. Cannot break any shops. unknown-owner: Unknown restricted-prices: 'Restricted price for {0}: Min {1}, max {2}' +restricted-price-max: 'Restricted price for {0}: Max {1}' +restricted-price-min: 'Restricted price for {0}: Min {1}' nearby-shop-this-way: Shop is {0} block(s) away from you. owner-bypass-check: Bypassed all checks. Trade successful! (You are now the shop owner!) april-rick-and-roll-easter-egg: "Click here to acquire your time limited rewards that provided by QuickShop-Hikari developer!" diff --git a/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/shop/SimplePriceLimiter.java b/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/shop/SimplePriceLimiter.java index f6e451d86b..2bf77f8226 100644 --- a/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/shop/SimplePriceLimiter.java +++ b/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/shop/SimplePriceLimiter.java @@ -43,8 +43,8 @@ public class SimplePriceLimiter implements Reloadable, PriceLimiter, SubPasteIte private final QuickShop plugin; private final Map rules = new LinkedHashMap<>(); private boolean wholeNumberOnly = false; - private double undefinedMin = 0.0d; - private double undefinedMax = Double.MAX_VALUE; + private double undefinedMin = -1; + private double undefinedMax = -1; public SimplePriceLimiter(@NotNull QuickShop plugin) { this.plugin = plugin; @@ -72,8 +72,8 @@ public void loadConfiguration() { plugin.logger().warn("Failed to save migrated price-restriction.yml.yml to plugin folder!", e); } } - this.undefinedMax = configuration.getDouble("undefined.max", 99999999999999999999999999999.99d); - this.undefinedMin = configuration.getDouble("undefined.min", 0.0d); + this.undefinedMin = configuration.getDouble("undefined.min", -1); + this.undefinedMax = configuration.getDouble("undefined.max", -1); this.wholeNumberOnly = configuration.getBoolean("whole-number-only", false); if (!configuration.getBoolean("enable", false)) { return; @@ -177,6 +177,7 @@ public PriceLimiterCheckResult check(@NotNull CommandSender sender, @NotNull Ite double minPrice = 0; double maxPrice = 0; + boolean hasMinPrice = false; boolean hasMaxPrice = false; final List flattenedItems = ItemContainerUtil.flattenContents(itemStack, true, false); @@ -194,6 +195,7 @@ public PriceLimiterCheckResult check(@NotNull CommandSender sender, @NotNull Ite } if (rule.hasMinPrice()) { + hasMinPrice = true; minPrice += rule.getMin() * itemTally; } if (rule.hasMaxPrice()) { @@ -202,14 +204,11 @@ public PriceLimiterCheckResult check(@NotNull CommandSender sender, @NotNull Ite } } - if (price < minPrice || (hasMaxPrice && price > maxPrice)) { - return new SimplePriceLimiterCheckResult(PriceLimiterStatus.PRICE_RESTRICTED, minPrice, maxPrice); - } - if (undefinedMin != -1 && price < undefinedMin) { - return new SimplePriceLimiterCheckResult(PriceLimiterStatus.PRICE_RESTRICTED, undefinedMin, undefinedMax); - } - if (undefinedMax != -1 && price > undefinedMax) { - return new SimplePriceLimiterCheckResult(PriceLimiterStatus.PRICE_RESTRICTED, undefinedMin, undefinedMax); + if ((hasMinPrice && price < minPrice) || (hasMaxPrice && price > maxPrice) + || (undefinedMin > 0 && price < undefinedMin) || (undefinedMax >= 0 && price > undefinedMax)) { + return new SimplePriceLimiterCheckResult(PriceLimiterStatus.PRICE_RESTRICTED, + hasMinPrice ? minPrice : undefinedMin, + hasMaxPrice ? maxPrice : undefinedMax); } return new SimplePriceLimiterCheckResult(PriceLimiterStatus.PASS, undefinedMin, undefinedMax); } @@ -243,6 +242,7 @@ public PriceLimiterCheckResult check(@NotNull QUser user, @NotNull ItemStack ite double minPrice = 0; double maxPrice = 0; + boolean hasMinPrice = false; boolean hasMaxPrice = false; final List flattenedItems = ItemContainerUtil.flattenContents(itemStack, true, false); @@ -260,6 +260,7 @@ public PriceLimiterCheckResult check(@NotNull QUser user, @NotNull ItemStack ite } if (rule.hasMinPrice()) { + hasMinPrice = true; minPrice += rule.getMin() * itemTally; } if (rule.hasMaxPrice()) { @@ -268,14 +269,11 @@ public PriceLimiterCheckResult check(@NotNull QUser user, @NotNull ItemStack ite } } - if (price < minPrice || (hasMaxPrice && price > maxPrice)) { - return new SimplePriceLimiterCheckResult(PriceLimiterStatus.PRICE_RESTRICTED, minPrice, maxPrice); - } - if (undefinedMin != -1 && price < undefinedMin) { - return new SimplePriceLimiterCheckResult(PriceLimiterStatus.PRICE_RESTRICTED, undefinedMin, undefinedMax); - } - if (undefinedMax != -1 && price > undefinedMax) { - return new SimplePriceLimiterCheckResult(PriceLimiterStatus.PRICE_RESTRICTED, undefinedMin, undefinedMax); + if ((hasMinPrice && price < minPrice) || (hasMaxPrice && price > maxPrice) + || (undefinedMin > 0 && price < undefinedMin) || (undefinedMax >= 0 && price > undefinedMax)) { + return new SimplePriceLimiterCheckResult(PriceLimiterStatus.PRICE_RESTRICTED, + hasMinPrice ? minPrice : undefinedMin, + hasMaxPrice ? maxPrice : undefinedMax); } return new SimplePriceLimiterCheckResult(PriceLimiterStatus.PASS, undefinedMin, undefinedMax); } diff --git a/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/shop/SimpleShopManager.java b/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/shop/SimpleShopManager.java index 0daebd3520..ef07ccd160 100644 --- a/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/shop/SimpleShopManager.java +++ b/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/shop/SimpleShopManager.java @@ -31,10 +31,7 @@ import com.ghostchu.quickshop.obj.QUserImpl; import com.ghostchu.quickshop.shop.inventory.BukkitInventoryWrapper; import com.ghostchu.quickshop.shop.inventory.BukkitInventoryWrapperManager; -import com.ghostchu.quickshop.util.ChatSheetPrinter; -import com.ghostchu.quickshop.util.MsgUtil; -import com.ghostchu.quickshop.util.PackageUtil; -import com.ghostchu.quickshop.util.Util; +import com.ghostchu.quickshop.util.*; import com.ghostchu.quickshop.util.holder.Result; import com.ghostchu.quickshop.util.logger.Log; import com.ghostchu.simplereloadlib.ReloadResult; @@ -567,14 +564,30 @@ public void createShop(@NotNull Shop shop, @Nullable Block signBlock, boolean by } // Price limit checking - PriceLimiterCheckResult priceCheckResult = this.priceLimiter.check(p, shop.getItem(), plugin.getCurrency(), shop.getPrice()); + final PriceLimiterCheckResult priceCheckResult = this.priceLimiter.check(p, shop.getItem(), plugin.getCurrency(), shop.getPrice()); + final String currency = shop.getCurrency() == null ? (plugin.getCurrency() == null ? "" : plugin.getCurrency()) : shop.getCurrency(); + switch (priceCheckResult.getStatus()) { case REACHED_PRICE_MIN_LIMIT -> plugin.text().of(p, "price-too-cheap", Component.text((useDecFormat) ? MsgUtil.decimalFormat(priceCheckResult.getMax()) : Double.toString(priceCheckResult.getMin()))).send(); case REACHED_PRICE_MAX_LIMIT -> plugin.text().of(p, "price-too-high", Component.text((useDecFormat) ? MsgUtil.decimalFormat(priceCheckResult.getMax()) : Double.toString(priceCheckResult.getMin()))).send(); - case PRICE_RESTRICTED -> - plugin.text().of(p, "restricted-prices", Util.getItemStackName(shop.getItem()), Component.text(priceCheckResult.getMin()), Component.text(priceCheckResult.getMax())).send(); + case PRICE_RESTRICTED -> { + final double min = priceCheckResult.getMin(); + final double max = priceCheckResult.getMax(); + + if (min > 0 && max >= 0) { + plugin.text().of(p, "restricted-prices", Util.getItemStackName(shop.getItem()), + currency + min, + currency + max).send(); + } else if (min > 0) { + plugin.text().of(p, "restricted-price-min", Util.getItemStackName(shop.getItem()), + currency + min).send(); + } else { + plugin.text().of(p, "restricted-price-max", Util.getItemStackName(shop.getItem()), + currency + max).send(); + } + } case NOT_VALID -> plugin.text().of(p, "not-a-number", shop.getPrice()).send(); case NOT_A_WHOLE_NUMBER -> plugin.text().of(p, "not-a-integer", shop.getPrice()).send(); case PASS -> { @@ -595,7 +608,7 @@ public void createShop(@NotNull Shop shop, @Nullable Block signBlock, boolean by if (createCost > 0) { SimpleEconomyTransaction economyTransaction = SimpleEconomyTransaction.builder().taxAccount(cacheTaxAccount).taxModifier(0.0).core(plugin.getEconomy()).from(QUserImpl.createFullFilled(p)).to(null).amount(createCost).currency(plugin.getCurrency()).world(shop.getLocation().getWorld()).build(); if (!economyTransaction.checkBalance()) { - plugin.text().of(p, "you-cant-afford-a-new-shop", format(createCost, shop.getLocation().getWorld(), shop.getCurrency())).send(); + plugin.text().of(p, "you-cant-afford-a-new-shop", format(createCost, shop)).send(); return; } if (!economyTransaction.failSafeCommit()) { diff --git a/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/util/ShopUtil.java b/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/util/ShopUtil.java index c44bbe02e1..089358f8cd 100644 --- a/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/util/ShopUtil.java +++ b/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/util/ShopUtil.java @@ -29,7 +29,6 @@ import com.ghostchu.quickshop.obj.QUserImpl; import com.ghostchu.quickshop.util.logger.Log; import lombok.Data; -import net.kyori.adventure.text.Component; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; @@ -79,8 +78,6 @@ public static void setPrice(final QuickShop plugin, @NotNull QUser user, final d return; } - final boolean format = plugin.getConfig().getBoolean("use-decimal-format"); - double fee = 0; if (plugin.isPriceChangeRequiresFee()) { @@ -102,20 +99,32 @@ public static void setPrice(final QuickShop plugin, @NotNull QUser user, final d } final PriceLimiterCheckResult checkResult = limiter.check(user, shop.getItem(), plugin.getCurrency(), price); + final String currency = shop.getCurrency() == null ? (plugin.getCurrency() == null ? "" : plugin.getCurrency()) : shop.getCurrency(); switch (checkResult.getStatus()) { case PRICE_RESTRICTED -> { - plugin.text().of(user.getUniqueId(), "restricted-prices", Util.getItemStackName(shop.getItem()), - Component.text(checkResult.getMin()), - Component.text(checkResult.getMax())).send(); + final double min = checkResult.getMin(); + final double max = checkResult.getMax(); + + if (min > 0 && max >= 0) { + plugin.text().of(user.getUniqueId(), "restricted-prices", Util.getItemStackName(shop.getItem()), + currency + min, + currency + max).send(); + } else if (min > 0) { + plugin.text().of(user.getUniqueId(), "restricted-price-min", Util.getItemStackName(shop.getItem()), + currency + min).send(); + } else { + plugin.text().of(user.getUniqueId(), "restricted-price-max", Util.getItemStackName(shop.getItem()), + currency + max).send(); + } return; } case REACHED_PRICE_MIN_LIMIT -> { - plugin.text().of(user, "price-too-cheap", (format) ? MsgUtil.decimalFormat(checkResult.getMin()) : Double.toString(checkResult.getMin())).send(); + plugin.text().of(user, "price-too-cheap", currency + checkResult.getMin()).send(); return; } case REACHED_PRICE_MAX_LIMIT -> { - plugin.text().of(user, "price-too-high", (format) ? MsgUtil.decimalFormat(checkResult.getMax()) : Double.toString(checkResult.getMax())).send(); + plugin.text().of(user, "price-too-high", currency + checkResult.getMax()).send(); return; } case NOT_A_WHOLE_NUMBER -> { From f9bf4f568bd3bf69150c8350630b56689777b0ae Mon Sep 17 00:00:00 2001 From: Tau Date: Mon, 9 Sep 2024 18:49:33 +1000 Subject: [PATCH 2/4] Missing entries in messages.yml --- quickshop-bukkit/src/main/resources/lang/messages.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/quickshop-bukkit/src/main/resources/lang/messages.yml b/quickshop-bukkit/src/main/resources/lang/messages.yml index 8c76bbf2ae..539fbdd2c2 100644 --- a/quickshop-bukkit/src/main/resources/lang/messages.yml +++ b/quickshop-bukkit/src/main/resources/lang/messages.yml @@ -374,6 +374,8 @@ support-disable-reason: supertool-is-disabled: Super tool is disabled. Cannot break any shops. unknown-owner: Unknown restricted-prices: 'Restricted price for {0}: Min {1}, max {2}' +restricted-price-max: 'Restricted price for {0}: Max {1}' +restricted-price-min: 'Restricted price for {0}: Min {1}' nearby-shop-this-way: Shop is {0} block(s) away from you. owner-bypass-check: Bypassed all checks. Trade successful! (You are now the shop owner!) From 701201be02d49c689333552ea6f59ed504855944 Mon Sep 17 00:00:00 2001 From: Tau Date: Mon, 9 Sep 2024 21:33:02 +1000 Subject: [PATCH 3/4] Change config defaults --- quickshop-bukkit/src/main/resources/price-restriction.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quickshop-bukkit/src/main/resources/price-restriction.yml b/quickshop-bukkit/src/main/resources/price-restriction.yml index 4fb5d6bc37..ece1cceb18 100644 --- a/quickshop-bukkit/src/main/resources/price-restriction.yml +++ b/quickshop-bukkit/src/main/resources/price-restriction.yml @@ -2,7 +2,7 @@ version: 3 whole-number-only: false # This option not control by enable option, always enabled undefined: # This option not control by enable option, always enabled min: 0.01 # Can be zero if you want player create a free shop - max: 999999999999999999999999999999.99 # Actually this can be up to 1.7976931348623157E308 + max: -1 # This can be up to 1.7976931348623157E308, or -1 to disable maximum price. enable: false rules: # Rules set example: # Rules name, used for identifier and permission node (quickshop.price.restriction.bypass.) From 40e1b8efe008c993239acf545a6d1a828d61adab Mon Sep 17 00:00:00 2001 From: Tau Date: Thu, 12 Sep 2024 00:34:51 +1000 Subject: [PATCH 4/4] format price messages properly --- .../quickshop/shop/SimpleShopManager.java | 32 +++++++++++-------- .../com/ghostchu/quickshop/util/ShopUtil.java | 30 ++++++++++------- 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/shop/SimpleShopManager.java b/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/shop/SimpleShopManager.java index ef07ccd160..b501539c8f 100644 --- a/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/shop/SimpleShopManager.java +++ b/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/shop/SimpleShopManager.java @@ -564,28 +564,32 @@ public void createShop(@NotNull Shop shop, @Nullable Block signBlock, boolean by } // Price limit checking - final PriceLimiterCheckResult priceCheckResult = this.priceLimiter.check(p, shop.getItem(), plugin.getCurrency(), shop.getPrice()); + final PriceLimiterCheckResult priceResult = this.priceLimiter.check(p, shop.getItem(), plugin.getCurrency(), shop.getPrice()); + final String currency = shop.getCurrency() == null ? (plugin.getCurrency() == null ? "" : plugin.getCurrency()) : shop.getCurrency(); + final World world = shop.getLocation().getWorld(); + final AbstractEconomy econ = plugin.getEconomy(); + + final double min = priceResult.getMin(); + final double max = priceResult.getMax(); + final String minFormatted = econ != null ? econ.format(min, world, currency) : String.valueOf(min); + final String maxFormatted = econ != null ? econ.format(max, world, currency) : String.valueOf(max); - switch (priceCheckResult.getStatus()) { + switch (priceResult.getStatus()) { case REACHED_PRICE_MIN_LIMIT -> - plugin.text().of(p, "price-too-cheap", Component.text((useDecFormat) ? MsgUtil.decimalFormat(priceCheckResult.getMax()) : Double.toString(priceCheckResult.getMin()))).send(); + plugin.text().of(p, "price-too-cheap", minFormatted).send(); case REACHED_PRICE_MAX_LIMIT -> - plugin.text().of(p, "price-too-high", Component.text((useDecFormat) ? MsgUtil.decimalFormat(priceCheckResult.getMax()) : Double.toString(priceCheckResult.getMin()))).send(); + plugin.text().of(p, "price-too-high", maxFormatted).send(); case PRICE_RESTRICTED -> { - final double min = priceCheckResult.getMin(); - final double max = priceCheckResult.getMax(); - if (min > 0 && max >= 0) { - plugin.text().of(p, "restricted-prices", Util.getItemStackName(shop.getItem()), - currency + min, - currency + max).send(); + plugin.text().of(p, "restricted-prices", + Util.getItemStackName(shop.getItem()), minFormatted, maxFormatted).send(); } else if (min > 0) { - plugin.text().of(p, "restricted-price-min", Util.getItemStackName(shop.getItem()), - currency + min).send(); + plugin.text().of(p, "restricted-price-min", + Util.getItemStackName(shop.getItem()), minFormatted).send(); } else { - plugin.text().of(p, "restricted-price-max", Util.getItemStackName(shop.getItem()), - currency + max).send(); + plugin.text().of(p, "restricted-price-max", + Util.getItemStackName(shop.getItem()), maxFormatted).send(); } } case NOT_VALID -> plugin.text().of(p, "not-a-number", shop.getPrice()).send(); diff --git a/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/util/ShopUtil.java b/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/util/ShopUtil.java index 089358f8cd..7f7b62a92a 100644 --- a/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/util/ShopUtil.java +++ b/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/util/ShopUtil.java @@ -18,6 +18,7 @@ */ import com.ghostchu.quickshop.QuickShop; +import com.ghostchu.quickshop.api.economy.AbstractEconomy; import com.ghostchu.quickshop.api.event.ShopOwnershipTransferEvent; import com.ghostchu.quickshop.api.event.ShopPriceChangeEvent; import com.ghostchu.quickshop.api.obj.QUser; @@ -30,6 +31,7 @@ import com.ghostchu.quickshop.util.logger.Log; import lombok.Data; import org.bukkit.Bukkit; +import org.bukkit.World; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; @@ -99,32 +101,36 @@ public static void setPrice(final QuickShop plugin, @NotNull QUser user, final d } final PriceLimiterCheckResult checkResult = limiter.check(user, shop.getItem(), plugin.getCurrency(), price); + final String currency = shop.getCurrency() == null ? (plugin.getCurrency() == null ? "" : plugin.getCurrency()) : shop.getCurrency(); + final World world = shop.getLocation().getWorld(); + final AbstractEconomy econ = plugin.getEconomy(); + + final double min = checkResult.getMin(); + final double max = checkResult.getMax(); + final String minFormatted = econ != null ? econ.format(min, world, currency) : String.valueOf(min); + final String maxFormatted = econ != null ? econ.format(max, world, currency) : String.valueOf(max); switch (checkResult.getStatus()) { case PRICE_RESTRICTED -> { - final double min = checkResult.getMin(); - final double max = checkResult.getMax(); - if (min > 0 && max >= 0) { - plugin.text().of(user.getUniqueId(), "restricted-prices", Util.getItemStackName(shop.getItem()), - currency + min, - currency + max).send(); + plugin.text().of(user.getUniqueId(), "restricted-prices", + Util.getItemStackName(shop.getItem()), minFormatted, maxFormatted).send(); } else if (min > 0) { - plugin.text().of(user.getUniqueId(), "restricted-price-min", Util.getItemStackName(shop.getItem()), - currency + min).send(); + plugin.text().of(user.getUniqueId(), "restricted-price-min", + Util.getItemStackName(shop.getItem()), minFormatted).send(); } else { - plugin.text().of(user.getUniqueId(), "restricted-price-max", Util.getItemStackName(shop.getItem()), - currency + max).send(); + plugin.text().of(user.getUniqueId(), "restricted-price-max", + Util.getItemStackName(shop.getItem()), maxFormatted).send(); } return; } case REACHED_PRICE_MIN_LIMIT -> { - plugin.text().of(user, "price-too-cheap", currency + checkResult.getMin()).send(); + plugin.text().of(user, "price-too-cheap", minFormatted).send(); return; } case REACHED_PRICE_MAX_LIMIT -> { - plugin.text().of(user, "price-too-high", currency + checkResult.getMax()).send(); + plugin.text().of(user, "price-too-high", maxFormatted).send(); return; } case NOT_A_WHOLE_NUMBER -> {