From 59bfcf7192f31478a9dffc34010a77164df735fa Mon Sep 17 00:00:00 2001 From: portlek Date: Sun, 24 Nov 2019 03:11:20 +0300 Subject: [PATCH] Update SubCommand_Price.java Former-commit-id: 2320d09a95f0b19b0993c2ae403148c585d5c3f9 [formerly ce07cd2be23ea3c2684b3224b91c958c21f9d955] Former-commit-id: 7e722dcead0e6c465cb55f8343af9c876c83025e --- .../Command/SubCommands/SubCommand_Price.java | 251 ++++++++++-------- 1 file changed, 140 insertions(+), 111 deletions(-) diff --git a/src/main/java/org/maxgamer/quickshop/Command/SubCommands/SubCommand_Price.java b/src/main/java/org/maxgamer/quickshop/Command/SubCommands/SubCommand_Price.java index 9281239c8..9a2ef46f5 100644 --- a/src/main/java/org/maxgamer/quickshop/Command/SubCommands/SubCommand_Price.java +++ b/src/main/java/org/maxgamer/quickshop/Command/SubCommands/SubCommand_Price.java @@ -17,12 +17,13 @@ import java.util.ArrayList; import java.util.List; import java.util.logging.Level; -// TODO: 24/11/2019 Optimize codes public class SubCommand_Price implements CommandProcesser { - private QuickShop plugin = QuickShop.instance; + private final QuickShop plugin = QuickShop.instance; + + @NotNull @Override - public @Nullable List onTabComplete(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] cmdArg) { + public List onTabComplete(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] cmdArg) { ArrayList list = new ArrayList<>(); list.add(MsgUtil.getMessage("tabcomplete.price", sender)); return list; @@ -30,124 +31,152 @@ public class SubCommand_Price implements CommandProcesser { @Override public void onCommand(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] cmdArg) { - if (sender instanceof Player) { - Player p = (Player) sender; - if (cmdArg.length < 1) { - sender.sendMessage(MsgUtil.getMessage("no-price-given", sender)); - return; - } - double price; - double minPrice = plugin.getConfig().getDouble("shop.minimum-price"); - try { - if (plugin.getConfig().getBoolean("whole-number-prices-only")) { - try { - price = Long.parseLong(cmdArg[0]); - } catch (NumberFormatException ex2) { - // input is number, but not Integer - Util.debugLog(ex2.getMessage()); - p.sendMessage(MsgUtil.getMessage("not-a-integer", p, cmdArg[0])); - return; - } - } else { - price = Double.parseDouble(cmdArg[0]); - } + if (!(sender instanceof Player)) { + sender.sendMessage("Can't run this command by Console"); + return; + } - } catch (NumberFormatException ex) { - //No number input - Util.debugLog(ex.getMessage()); - p.sendMessage(MsgUtil.getMessage("not-a-number", p, cmdArg[0])); - return; - } - boolean format = plugin.getConfig().getBoolean("use-deciaml-format"); - if (plugin.getConfig().getBoolean("shop.allow-free-shop")) { - if (price != 0 && price < minPrice) { - p.sendMessage(MsgUtil.getMessage("price-too-cheap", p, (format) ? MsgUtil.decimalFormat(minPrice) : "" + minPrice)); + final Player p = (Player) sender; + + if (cmdArg.length < 1) { + sender.sendMessage(MsgUtil.getMessage("no-price-given", sender)); + return; + } + + final double price; + final double minPrice = plugin.getConfig().getDouble("shop.minimum-price"); + + try { + if (plugin.getConfig().getBoolean("whole-number-prices-only")) { + try { + price = Long.parseLong(cmdArg[0]); + } catch (NumberFormatException ex2) { + // input is number, but not Integer + Util.debugLog(ex2.getMessage()); + p.sendMessage(MsgUtil.getMessage("not-a-integer", p, cmdArg[0])); return; } } else { - if (price < minPrice) { - p.sendMessage(MsgUtil.getMessage("price-too-cheap", p, (format) ? MsgUtil.decimalFormat(minPrice) : "" + minPrice)); - return; - } + price = Double.parseDouble(cmdArg[0]); } - double price_limit = plugin.getConfig().getDouble("shop.maximum-price"); - if (price_limit != -1) { - if (price > price_limit) { - p.sendMessage(MsgUtil.getMessage("price-too-high", p, (format) ? MsgUtil.decimalFormat(price_limit) : "" + price_limit)); - return; - } - } - double fee = 0; - if (plugin.isPriceChangeRequiresFee()) { - fee = plugin.getConfig().getDouble("shop.fee-for-price-change"); - } - // if (fee > 0 && plugin.getEconomy().getBalance(p.getUniqueId()) < fee) { - // sender.sendMessage( - // MsgUtil.getMessage("you-cant-afford-to-change-price", plugin.getEconomy().format(fee))); - // return; - // } - BlockIterator bIt = new BlockIterator(p, 10); - // Loop through every block they're looking at upto 10 blocks away - if (!bIt.hasNext()) { - sender.sendMessage(MsgUtil.getMessage("not-looking-at-shop", sender)); + + } catch (NumberFormatException ex) { + //No number input + Util.debugLog(ex.getMessage()); + p.sendMessage(MsgUtil.getMessage("not-a-number", p, cmdArg[0])); + return; + } + + final boolean format = plugin.getConfig().getBoolean("use-deciaml-format"); + + if (plugin.getConfig().getBoolean("shop.allow-free-shop")) { + if (price != 0 && price < minPrice) { + p.sendMessage(MsgUtil.getMessage("price-too-cheap", p, (format) ? MsgUtil.decimalFormat(minPrice) : "" + minPrice)); return; } - while (bIt.hasNext()) { - Block b = bIt.next(); - Shop shop = plugin.getShopManager().getShop(b.getLocation()); - if (shop != null && (shop.getModerator().isModerator(((Player) sender).getUniqueId()) || QuickShop.getPermissionManager().hasPermission(sender, "quickshop.other.price"))) { - if (shop.getPrice() == price) { - // Stop here if there isn't a price change - sender.sendMessage(MsgUtil.getMessage("no-price-change", sender)); - return; - } - if (fee > 0) { - if (!plugin.getEconomy().withdraw(p.getUniqueId(), fee)) { - sender.sendMessage(MsgUtil.getMessage("you-cant-afford-to-change-price", sender, - plugin.getEconomy().format(fee))); - return; - } - sender.sendMessage( - MsgUtil.getMessage("fee-charged-for-price-change", sender, plugin.getEconomy().format(fee))); - try { - plugin.getEconomy().deposit(plugin.getServer().getOfflinePlayer(plugin.getConfig().getString("tax-account")) - .getUniqueId(), fee); - } catch (Exception e) { - e.getMessage(); - plugin.getLogger().log(Level.WARNING, - "QuickShop can't pay tax to the account in config.yml, please set the tax account name to an existing player!"); - } - - } - // Update the shop - shop.setPrice(price); - //shop.setSignText(); - shop.update(); - sender.sendMessage(MsgUtil.getMessage("price-is-now", sender, plugin.getEconomy().format(shop.getPrice()))); - // Chest shops can be double shops. - if (shop instanceof ContainerShop) { - ContainerShop cs = (ContainerShop) shop; - if (cs.isDoubleShop()) { - Shop nextTo = cs.getAttachedShop(); - if (cs.isSelling()) { - if (cs.getPrice() < nextTo.getPrice()) { - sender.sendMessage(MsgUtil.getMessage("buying-more-than-selling", sender)); - } - } else { - // Buying - if (cs.getPrice() > nextTo.getPrice()) { - sender.sendMessage(MsgUtil.getMessage("buying-more-than-selling", sender)); - } - } - } - return; - } - return; - } + } else { + if (price < minPrice) { + p.sendMessage(MsgUtil.getMessage("price-too-cheap", p, (format) ? MsgUtil.decimalFormat(minPrice) : "" + minPrice)); + return; } + } + + final double price_limit = plugin.getConfig().getDouble("shop.maximum-price"); + + if (price_limit != -1 && price > price_limit) { + p.sendMessage(MsgUtil.getMessage("price-too-high", p, (format) ? MsgUtil.decimalFormat(price_limit) : "" + price_limit)); + return; + } + + double fee = 0; + + if (plugin.isPriceChangeRequiresFee()) { + fee = plugin.getConfig().getDouble("shop.fee-for-price-change"); + } + + /*if (fee > 0 && plugin.getEconomy().getBalance(p.getUniqueId()) < fee) { + sender.sendMessage( + MsgUtil.getMessage("you-cant-afford-to-change-price", plugin.getEconomy().format(fee))); + return; + }*/ + final BlockIterator bIt = new BlockIterator(p, 10); + // Loop through every block they're looking at upto 10 blocks away + if (!bIt.hasNext()) { sender.sendMessage(MsgUtil.getMessage("not-looking-at-shop", sender)); return; } - sender.sendMessage("Can't run this command by Console"); + + while (bIt.hasNext()) { + final Block b = bIt.next(); + final Shop shop = plugin.getShopManager().getShop(b.getLocation()); + + if (shop == null || + (!shop.getModerator().isModerator(((Player) sender).getUniqueId()) && + !QuickShop.getPermissionManager().hasPermission(sender, "quickshop.other.price"))) { + continue; + } + + if (shop.getPrice() == price) { + // Stop here if there isn't a price change + sender.sendMessage(MsgUtil.getMessage("no-price-change", sender)); + return; + } + + if (fee > 0) { + if (!plugin.getEconomy().withdraw(p.getUniqueId(), fee)) { + sender.sendMessage(MsgUtil.getMessage("you-cant-afford-to-change-price", sender, + plugin.getEconomy().format(fee))); + return; + } + + sender.sendMessage( + MsgUtil.getMessage("fee-charged-for-price-change", sender, plugin.getEconomy().format(fee))); + try { + plugin.getEconomy().deposit(plugin.getServer().getOfflinePlayer(plugin.getConfig().getString("tax-account")) + .getUniqueId(), fee); + } catch (Exception e) { + e.getMessage(); + plugin.getLogger().log(Level.WARNING, + "QuickShop can't pay tax to the account in config.yml, please set the tax account name to an existing player!"); + } + + } + // Update the shop + shop.setPrice(price); + //shop.setSignText(); + shop.update(); + sender.sendMessage(MsgUtil.getMessage("price-is-now", sender, plugin.getEconomy().format(shop.getPrice()))); + // Chest shops can be double shops. + if (!(shop instanceof ContainerShop)) { + return; + } + + final ContainerShop cs = (ContainerShop) shop; + + if (!cs.isDoubleShop()) { + return; + } + + final Shop nextTo = cs.getAttachedShop(); + + if (nextTo == null) { + // TODO: 24/11/2019 Send message about that issue. + return; + } + + if (cs.isSelling()) { + if (cs.getPrice() < nextTo.getPrice()) { + sender.sendMessage(MsgUtil.getMessage("buying-more-than-selling", sender)); + } + } + // Buying + else if (cs.getPrice() > nextTo.getPrice()) { + sender.sendMessage(MsgUtil.getMessage("buying-more-than-selling", sender)); + } + + return; + } + + sender.sendMessage(MsgUtil.getMessage("not-looking-at-shop", sender)); } }