From 21340af89f76e2e6308f710a50ebfa6b6089fddc Mon Sep 17 00:00:00 2001 From: Thijs Wiefferink Date: Mon, 12 Jan 2015 21:55:01 +0100 Subject: [PATCH] Change the '/as find' command to randomly pick a region that suits First searches all matching regions (rent/buy, maxprice and group), then randomly picks one. --- .../areashop/commands/FindCommand.java | 112 ++++++++++-------- 1 file changed, 63 insertions(+), 49 deletions(-) diff --git a/src/nl/evolutioncoding/areashop/commands/FindCommand.java b/src/nl/evolutioncoding/areashop/commands/FindCommand.java index 525f042..9915776 100644 --- a/src/nl/evolutioncoding/areashop/commands/FindCommand.java +++ b/src/nl/evolutioncoding/areashop/commands/FindCommand.java @@ -4,6 +4,7 @@ import java.math.BigDecimal; import java.math.RoundingMode; import java.util.ArrayList; import java.util.List; +import java.util.Random; import nl.evolutioncoding.areashop.AreaShop; import nl.evolutioncoding.areashop.regions.BuyRegion; @@ -72,72 +73,85 @@ public class FindCommand extends CommandAreaShop { } if(args[1].equalsIgnoreCase("buy")) { List regions = plugin.getFileManager().getBuys(); + List results = new ArrayList(); for(BuyRegion region : regions) { if(!region.isSold() && ((region.getPrice() <= balance && !maxPriceSet) || (region.getPrice() <= maxPrice && maxPriceSet)) && (group == null || group.isMember(region))) { - BigDecimal bigDecimal = new BigDecimal(balance); - bigDecimal = bigDecimal.setScale(2, RoundingMode.HALF_UP); - balance = bigDecimal.doubleValue(); - String onlyInGroup = ""; - if(group != null) { - onlyInGroup = plugin.getLanguageManager().getLang("find-onlyInGroup", args[3]); - } - if(maxPriceSet) { - plugin.message(player, "find-successMax", "buy", region.getName(), maxPrice, onlyInGroup); - } else { - plugin.message(player, "find-success", "buy", region.getName(), balance, onlyInGroup); - } - region.teleportPlayer(player, true, false); - return; + results.add(region); } - } - BigDecimal bigDecimal = new BigDecimal(balance); - bigDecimal = bigDecimal.setScale(2, RoundingMode.HALF_UP); - balance = bigDecimal.doubleValue(); - String onlyInGroup = ""; - if(group != null) { - onlyInGroup = plugin.getLanguageManager().getLang("find-onlyInGroup", args[3]); } - if(maxPriceSet) { - plugin.message(player, "find-noneFound", "buy", maxPrice, onlyInGroup); + if(!results.isEmpty()) { + // Draw a random one + BuyRegion region = results.get(new Random().nextInt(results.size())); + + BigDecimal bigDecimal = new BigDecimal(balance); + bigDecimal = bigDecimal.setScale(2, RoundingMode.HALF_UP); + balance = bigDecimal.doubleValue(); + String onlyInGroup = ""; + if(group != null) { + onlyInGroup = plugin.getLanguageManager().getLang("find-onlyInGroup", args[3]); + } + if(maxPriceSet) { + plugin.message(player, "find-successMax", "buy", region.getName(), maxPrice, onlyInGroup); + } else { + plugin.message(player, "find-success", "buy", region.getName(), balance, onlyInGroup); + } + region.teleportPlayer(player, true, false); } else { - plugin.message(player, "find-noneFoundMax", "buy", balance, onlyInGroup); + BigDecimal bigDecimal = new BigDecimal(balance); + bigDecimal = bigDecimal.setScale(2, RoundingMode.HALF_UP); + balance = bigDecimal.doubleValue(); + String onlyInGroup = ""; + if(group != null) { + onlyInGroup = plugin.getLanguageManager().getLang("find-onlyInGroup", args[3]); + } + if(maxPriceSet) { + plugin.message(player, "find-noneFound", "buy", maxPrice, onlyInGroup); + } else { + plugin.message(player, "find-noneFoundMax", "buy", balance, onlyInGroup); + } } - } else { List regions = plugin.getFileManager().getRents(); + List results = new ArrayList(); for(RentRegion region : regions) { if(!region.isRented() && ((region.getPrice() <= balance && !maxPriceSet) || (region.getPrice() <= maxPrice && maxPriceSet)) && (group == null || group.isMember(region))) { - BigDecimal bigDecimal = new BigDecimal(balance); - bigDecimal = bigDecimal.setScale(2, RoundingMode.HALF_UP); - balance = bigDecimal.doubleValue(); - String onlyInGroup = ""; - if(group != null) { - onlyInGroup = plugin.getLanguageManager().getLang("find-onlyInGroup", args[3]); - } - if(maxPriceSet) { - plugin.message(player, "find-successMax", "rent", region.getName(), maxPrice, onlyInGroup); - } else { - plugin.message(player, "find-success", "rent", region.getName(), balance, onlyInGroup); - } - region.teleportPlayer(player, true, false); - return; + results.add(region); } - } - BigDecimal bigDecimal = new BigDecimal(balance); - bigDecimal = bigDecimal.setScale(2, RoundingMode.HALF_UP); - balance = bigDecimal.doubleValue(); - String onlyInGroup = ""; - if(group != null) { - onlyInGroup = plugin.getLanguageManager().getLang("find-onlyInGroup", args[3]); } - if(maxPriceSet) { - plugin.message(player, "find-noneFound", "rent", maxPrice, onlyInGroup); + if(!results.isEmpty()) { + // Draw a random one + RentRegion region = results.get(new Random().nextInt(results.size())); + + BigDecimal bigDecimal = new BigDecimal(balance); + bigDecimal = bigDecimal.setScale(2, RoundingMode.HALF_UP); + balance = bigDecimal.doubleValue(); + String onlyInGroup = ""; + if(group != null) { + onlyInGroup = plugin.getLanguageManager().getLang("find-onlyInGroup", args[3]); + } + if(maxPriceSet) { + plugin.message(player, "find-successMax", "rent", region.getName(), maxPrice, onlyInGroup); + } else { + plugin.message(player, "find-success", "rent", region.getName(), balance, onlyInGroup); + } + region.teleportPlayer(player, true, false); } else { - plugin.message(player, "find-noneFoundMax", "rent", balance, onlyInGroup); + BigDecimal bigDecimal = new BigDecimal(balance); + bigDecimal = bigDecimal.setScale(2, RoundingMode.HALF_UP); + balance = bigDecimal.doubleValue(); + String onlyInGroup = ""; + if(group != null) { + onlyInGroup = plugin.getLanguageManager().getLang("find-onlyInGroup", args[3]); + } + if(maxPriceSet) { + plugin.message(player, "find-noneFound", "rent", maxPrice, onlyInGroup); + } else { + plugin.message(player, "find-noneFoundMax", "rent", balance, onlyInGroup); + } } }