Add group filtering to /as info <rented|forrent|sold|forsale>

- Filter regions by group for the mentioned subcommands of /as info
- Clean/refactor code
- Sort the output
- Add Comparable, equals() and toString() to GeneralRegion

Closes #44
This commit is contained in:
Thijs Wiefferink 2015-08-11 22:41:30 +02:00
parent 0d5db8fa4e
commit 40d3d4d8af
4 changed files with 133 additions and 168 deletions

View File

@ -5,6 +5,8 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import nl.evolutioncoding.areashop.AreaShop;
import nl.evolutioncoding.areashop.Utils;
@ -13,6 +15,7 @@ import nl.evolutioncoding.areashop.regions.GeneralRegion;
import nl.evolutioncoding.areashop.regions.RegionGroup;
import nl.evolutioncoding.areashop.regions.RentRegion;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.command.Command;
@ -38,6 +41,31 @@ public class InfoCommand extends CommandAreaShop {
return null;
}
/**
* Filter group of regions, join their names and printout the correct message
* @param sender The CommandSender to receive the message
* @param regions The regions
* @param filterGroup The group to filter to the regions with
* @param keySomeFound The key of the message to display when some regions are found
* @param keyNoneFound The key of the message to display when no regions are found
*/
public void displayMessage(CommandSender sender, Set<? extends GeneralRegion> regions, RegionGroup filterGroup, String keySomeFound, String keyNoneFound) {
if(filterGroup != null) {
Iterator<? extends GeneralRegion> it = regions.iterator();
while(it.hasNext()) {
GeneralRegion region = it.next();
if(!filterGroup.isMember(region)) {
it.remove();
}
}
}
if(regions.isEmpty()) {
plugin.message(sender, keyNoneFound);
} else {
plugin.message(sender, keySomeFound, StringUtils.join(regions.iterator(), ", "));
}
}
@Override
public void execute(CommandSender sender, Command command, String[] args) {
if(!sender.hasPermission("areashop.info")) {
@ -45,166 +73,89 @@ public class InfoCommand extends CommandAreaShop {
return;
}
if(args.length > 1 && args[1] != null) {
/* List of all regions */
if(args[1].equalsIgnoreCase("all")) {
String message = "";
/* Message for rents */
Iterator<RentRegion> itRent = plugin.getFileManager().getRents().iterator();
if(itRent.hasNext()) {
message = itRent.next().getName();
while(itRent.hasNext()) {
message += ", " + itRent.next().getName();
}
}
if(message.equals("")) {
plugin.message(sender, "info-all-noRents");
} else {
plugin.message(sender, "info-all-rents", message);
}
/* Message for buys */
message = "";
Iterator<BuyRegion> itBuy = plugin.getFileManager().getBuys().iterator();
if(itBuy.hasNext()) {
message = itBuy.next().getName();
while(itBuy.hasNext()) {
message += ", " + itBuy.next().getName();
}
}
if(message.equals("")) {
plugin.message(sender, "info-all-noBuys");
} else {
plugin.message(sender, "info-all-buys", message);
// Get filter group (only used by some commands)
RegionGroup filterGroup = null;
if(args.length > 2 && args[2] != null) {
filterGroup = plugin.getFileManager().getGroup(args[2]);
if(filterGroup == null) {
plugin.message(sender, "info-noFiltergroup", args[2]);
return;
}
}
/* List of rented regions */
// All regions
if(args[1].equalsIgnoreCase("all")) {
Set<GeneralRegion> regions = new TreeSet<GeneralRegion>(plugin.getFileManager().getRents());
displayMessage(sender, regions, filterGroup, "info-all-rents", "info-all-noRents");
regions = new TreeSet<GeneralRegion>(plugin.getFileManager().getBuys());
displayMessage(sender, regions, filterGroup, "info-all-buys", "info-all-noBuys");
}
// Rented regions
else if(args[1].equalsIgnoreCase("rented")) {
String message = "";
Iterator<RentRegion> it = plugin.getFileManager().getRents().iterator();
boolean first = true;
while(it.hasNext()) {
RentRegion next = it.next();
if(next.isRented()) {
if(!first) {
message += ", " + next.getName();
} else {
first = false;
message += next.getName();
}
Set<GeneralRegion> regions = new TreeSet<GeneralRegion>();
for(RentRegion region : plugin.getFileManager().getRents()) {
if(region.isRented()) {
regions.add(region);
}
}
if(message.equals("")) {
plugin.message(sender, "info-noRented");
} else {
plugin.message(sender, "info-rented", message);
}
displayMessage(sender, regions, filterGroup, "info-rented", "info-noRented");
}
/* List of unrented regions */
// Forrent regions
else if(args[1].equalsIgnoreCase("forrent")) {
String message = "";
Iterator<RentRegion> it = plugin.getFileManager().getRents().iterator();
boolean first = true;
while(it.hasNext()) {
RentRegion next = it.next();
if(!next.isRented()) {
if(!first) {
message += ", " + next.getName();
} else {
first = false;
message = next.getName();
}
Set<GeneralRegion> regions = new TreeSet<GeneralRegion>();
for(RentRegion region : plugin.getFileManager().getRents()) {
if(!region.isRented()) {
regions.add(region);
}
}
if(message.equals("")) {
plugin.message(sender, "info-noUnrented");
} else {
plugin.message(sender, "info-unrented", message);
}
} else if(args[1].equalsIgnoreCase("sold")) {
String message = "";
Iterator<BuyRegion> it = plugin.getFileManager().getBuys().iterator();
boolean first = true;
while(it.hasNext()) {
BuyRegion next = it.next();
if(next.isSold()) {
if(!first) {
message += ", ";
} else {
first = false;
}
message += next.getName();
displayMessage(sender, regions, filterGroup, "info-unrented", "info-noUnrented");
}
// Sold regions
else if(args[1].equalsIgnoreCase("sold")) {
Set<GeneralRegion> regions = new TreeSet<GeneralRegion>();
for(BuyRegion region : plugin.getFileManager().getBuys()) {
if(region.isSold()) {
regions.add(region);
}
}
if(message.equals("")) {
plugin.message(sender, "info-noSold");
} else {
plugin.message(sender, "info-sold", message);
}
} else if(args[1].equalsIgnoreCase("forsale")) {
String message = "";
Iterator<BuyRegion> it = plugin.getFileManager().getBuys().iterator();
boolean first = true;
while(it.hasNext()) {
BuyRegion next =it.next();
if(!next.isSold()) {
if(!first) {
message += ", " + next.getName();
} else {
first = false;
message = next.getName();
}
displayMessage(sender, regions, filterGroup, "info-sold", "info-noSold");
}
// Forsale regions
else if(args[1].equalsIgnoreCase("forsale")) {
Set<GeneralRegion> regions = new TreeSet<GeneralRegion>();
for(BuyRegion region : plugin.getFileManager().getBuys()) {
if(!region.isSold()) {
regions.add(region);
}
}
if(message.equals("")) {
plugin.message(sender, "info-noForsale");
} else {
plugin.message(sender, "info-forsale", message);
}
} else if(args[1].equalsIgnoreCase("player")) {
displayMessage(sender, regions, filterGroup, "info-forsale", "info-noForsale");
}
// Player regions
else if(args[1].equalsIgnoreCase("player")) {
if(args.length > 2 && args[2] != null) {
String message = "";
Iterator<RentRegion> itRent = plugin.getFileManager().getRents().iterator();
boolean first = true;
while(itRent.hasNext()) {
RentRegion next = itRent.next();
if(next.isRented() && next.getPlayerName().equalsIgnoreCase(args[2])) {
if(!first) {
message += ", " + next.getName();
} else {
first = false;
message = next.getName();
}
Set<GeneralRegion> regions = new TreeSet<GeneralRegion>();
for(RentRegion region : plugin.getFileManager().getRents()) {
if(region.isRented() && region.getPlayerName().equalsIgnoreCase(args[2])) {
regions.add(region);
}
}
if(message.equals("")) {
plugin.message(sender, "info-playerNoRents", args[2]);
} else {
plugin.message(sender, "info-playerRents", args[2], message);
}
displayMessage(sender, regions, null, "info-playerRents", "info-playerNoRents");
message = "";
Iterator<BuyRegion> itBuy = plugin.getFileManager().getBuys().iterator();
first = true;
while(itBuy.hasNext()) {
BuyRegion next = itBuy.next();
if(next.isSold() && next.getPlayerName().equalsIgnoreCase(args[2])) {
if(!first) {
message += ", ";
} else {
first = false;
}
message += next.getName();
regions = new TreeSet<GeneralRegion>();
for(BuyRegion region : plugin.getFileManager().getBuys()) {
if(region.isSold() && region.getPlayerName().equalsIgnoreCase(args[2])) {
regions.add(region);
}
}
if(message.equals("")) {
plugin.message(sender, "info-playerNoBuys", args[2]);
} else {
plugin.message(sender, "info-playerBuys", args[2], message);
}
displayMessage(sender, regions, null, "info-playerBuys", "info-playerNoBuys");
} else {
plugin.message(sender, "info-playerHelp");
}
} else if(args[1].equalsIgnoreCase("region")) {
}
// Region info
else if(args[1].equalsIgnoreCase("region")) {
if(args.length > 1) {
RentRegion rent = null;
BuyRegion buy = null;
@ -373,42 +324,24 @@ public class InfoCommand extends CommandAreaShop {
/* List of regions without a group */
else if(args[1].equalsIgnoreCase("nogroup")) {
String message = "";
/* Message for rents */
List<String> rents = plugin.getFileManager().getRentNames();
// Remove regions that have a group
// Rental regions
Set<String> rents = new TreeSet<String>(plugin.getFileManager().getRentNames());
for(RegionGroup group : plugin.getFileManager().getGroups()) {
rents.removeAll(group.getMembers());
}
// Create the list message
Iterator<String> itRent = rents.iterator();
if(itRent.hasNext()) {
message = itRent.next();
while(itRent.hasNext()) {
message += ", " + itRent.next();
}
}
String message = StringUtils.join(rents, ", ");
if(message.equals("")) {
plugin.message(sender, "info-nogroupNoRents");
} else {
plugin.message(sender, "info-nogroupRents", message);
}
/* Message for buys */
message = "";
// Remove regions that have a group
List<String> buys = plugin.getFileManager().getBuyNames();
// Buy regions
Set<String> buys = new TreeSet<String>(plugin.getFileManager().getBuyNames());
for(RegionGroup group : plugin.getFileManager().getGroups()) {
buys.removeAll(group.getMembers());
}
// Create the list message
Iterator<String> itBuy = buys.iterator();
if(itBuy.hasNext()) {
message = itBuy.next();
while(itBuy.hasNext()) {
message += ", " + itBuy.next();
}
}
message = StringUtils.join(buys, ", ");
if(message.equals("")) {
plugin.message(sender, "info-nogroupNoBuys");
} else {

View File

@ -44,7 +44,7 @@ import com.sk89q.worldguard.protection.flags.RegionGroupFlag;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import com.sk89q.worldguard.protection.regions.ProtectedRegion.CircularInheritanceException;
public abstract class GeneralRegion implements GeneralRegionInterface {
public abstract class GeneralRegion implements GeneralRegionInterface, Comparable<GeneralRegion> {
protected YamlConfiguration config;
private static ArrayList<Material> canSpawnIn = new ArrayList<Material>(Arrays.asList(Material.WOOD_DOOR, Material.WOODEN_DOOR, Material.SIGN_POST, Material.WALL_SIGN, Material.STONE_PLATE, Material.IRON_DOOR_BLOCK, Material.WOOD_PLATE, Material.TRAP_DOOR, Material.REDSTONE_LAMP_OFF, Material.REDSTONE_LAMP_ON, Material.DRAGON_EGG, Material.GOLD_PLATE, Material.IRON_PLATE));
private static ArrayList<Material> cannotSpawnOn = new ArrayList<Material>(Arrays.asList(Material.PISTON_EXTENSION, Material.PISTON_MOVING_PIECE, Material.SIGN_POST, Material.WALL_SIGN, Material.STONE_PLATE, Material.IRON_DOOR_BLOCK, Material.WOOD_PLATE, Material.TRAP_DOOR, Material.REDSTONE_LAMP_OFF, Material.REDSTONE_LAMP_ON, Material.CACTUS, Material.IRON_FENCE, Material.FENCE_GATE, Material.THIN_GLASS, Material.NETHER_FENCE, Material.DRAGON_EGG, Material.GOLD_PLATE, Material.IRON_PLATE, Material.STAINED_GLASS_PANE));
@ -157,6 +157,22 @@ public abstract class GeneralRegion implements GeneralRegionInterface {
*/
public abstract RegionType getType();
// Sorting by name
@Override
public int compareTo(GeneralRegion o) {
return getName().compareTo(o.getName());
}
@Override
public String toString() {
return getName();
}
@Override
public boolean equals(Object region) {
return region instanceof GeneralRegion && ((GeneralRegion)region).getName().equals(getName());
}
/**
* Update the region flags according the region data
*/

View File

@ -1,7 +1,9 @@
package nl.evolutioncoding.areashop.regions;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import nl.evolutioncoding.areashop.AreaShop;
@ -93,6 +95,18 @@ public class RegionGroup {
return getSettings().getStringList("regions");
}
/**
* Get all members of the group as GeneralRegions
* @return A Set with all group members
*/
public Set<GeneralRegion> getMemberRegions() {
Set<GeneralRegion> result = new HashSet<GeneralRegion>();
for(String name : getMembers()) {
result.add(plugin.getFileManager().getRegion(name));
}
return result;
}
/**
* Get the name of the group
* @return The name of the group

View File

@ -13,7 +13,7 @@ total-maximum: "You can't rent and buy more than %0% region(s) in total (you alr
general-notReady: "AreaShop has not fully loaded yet, please wait."
general-noWorld: "You cannot do that, the world of this region is currently unavailable (world '%0%')."
general-noRegion: "You cannot do that, the WorldGuard region is currently unavailable (region '%0%')."
general-noEconomy: "There is no economy plugin installed or it is not setup correctly, notify the server owner about this."
general-noEconomy: "There is no Economy plugin installed or it is not setup correctly, notify the server owner about this."
cmd-notValid: "That command is not valid, use '/as help' for info."
cmd-onlyByPlayer: "This command can only be run by a player."
@ -71,6 +71,7 @@ help-setlandlord: "&6/as setlandlord &7-&r Set the landlord of a region."
rent-help: "/as rent [regionname], the region you stand in will be used if not specified."
rent-noPermission: "You don't have permission to rent a region."
rent-maximum: "You can't rent more than %0% region(s) (you already have %1% in group '%2%')."
rent-maximumExtend: "You can't extend this rent because you have more than %0% region(s) (you already have %1% in group '%2%')."
rent-payError: "Something went wrong with paying, try again later."
rent-rented: "You rented %0% until %1%."
rent-extended: "You extended your rent of %0% until %1%."
@ -122,7 +123,7 @@ reload-updateComplete: "&7Updating regions complete."
reload-noPermission: "You don't have permission to reload the config files."
reload-updateCommandChanged: "'/as updaterents' and '/as updatebuys' have been removed, '/as reload' will also update the regions now so use that command instead."
info-help: "/as info <all|rented|forrent|sold|forsale|player|region|nogroup>."
info-help: "/as info\n all\n rented [group]\n forrent [group]\n sold [group]\n forsale [group]\n player <player>\n region <region>\n nogroup"
info-noPermission: "You don't have permission to get information about regions."
info-all-rents: "Regions registered for renting: &7%0%."
info-all-noRents: "There are no regions registered for renting."
@ -140,10 +141,11 @@ info-sold: "Sold regions: &7%0%."
info-noSold: "No regions are sold."
info-forsale: "Regions for sale: &7%0%."
info-noForsale: "All regions are sold."
info-noFiltergroup: "Group '%0%' does not exist and therefore cannot be used to limit the results."
info-playerHelp: "/as info player <name>."
info-playerRents: "Regions rented by %0%: &7%1%"
info-playerRents: "Regions rented by %0%: &7%1%"
info-playerNoRents: "%0% has not rented a region."
info-playerBuys: "Regions bought by %0%: &7%1%."
info-playerBuys: "Regions bought by %0%: &7%1%."
info-playerNoBuys: "%0% has not bought a region."
info-regionHelp: "/as info region [name], the region you stand in will be used if not specified."
info-regionHeaderRent: "&2Information about %region%:"
@ -427,4 +429,4 @@ greeting-forsale: "%region% can be bought for %price%."
greeting-bought: "%region% is bought by %player%."
greeting-resale: "%region% can be bought for %resaleprice% from %player%."
update-playerNotify: "&2Update from AreaShop V%0% to %1% available, get the latest version at http://dev.bukkit.org/bukkit-plugins/regionbuyandrent/."
update-playerNotify: "&2Update from AreaShop V%0% to %1% available, get the latest version at http://dev.bukkit.org/bukkit-plugins/regionbuyandrent/."