Restructured /rg list.

- When used with a player name to filter things down, it displays indexes that can be used as "#index" in place of region names now.
- No longer displayer "+-regionname" for regions the target player is the owner of.
This commit is contained in:
TomyLobo 2012-03-23 14:51:34 +01:00
parent 69119e6db7
commit 22db589742

View File

@ -20,8 +20,10 @@
package com.sk89q.worldguard.bukkit.commands; package com.sk89q.worldguard.bukkit.commands;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.Arrays; import java.util.ArrayList;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
@ -525,109 +527,129 @@ public void displayRegionInfo(CommandSender sender, final LocalPlayer localPlaye
+ " (" + max.getBlockX() + "," + max.getBlockY() + "," + max.getBlockZ() + ")" + " (" + max.getBlockX() + "," + max.getBlockY() + "," + max.getBlockZ() + ")"
); );
} }
public class RegionEntry implements Comparable<RegionEntry>{
private final String id;
private final int index;
private boolean isOwner;
private boolean isMember;
public RegionEntry(String id, int index) {
this.id = id;
this.index = index;
}
@Override
public int compareTo(RegionEntry o) {
if (isOwner != o.isOwner) {
return isOwner ? 1 : -1;
}
if (isMember != o.isMember) {
return isMember ? 1 : -1;
}
return id.compareTo(o.id);
}
@Override
public String toString() {
if (isOwner) {
return (index + 1) + ". +" + id;
} else if (isMember) {
return (index + 1) + ". -" + id;
} else {
return (index + 1) + ". " + id;
}
}
}
@Command(aliases = {"list"}, usage = "[.player] [page] [world]", @Command(aliases = {"list"}, usage = "[.player] [page] [world]",
desc = "Get a list of regions", max = 3) desc = "Get a list of regions", max = 3)
// @CommandPermissions({"worldguard.region.list"}) //@CommandPermissions({"worldguard.region.list"})
public void list(CommandContext args, CommandSender sender) throws CommandException { public void list(CommandContext args, CommandSender sender) throws CommandException {
World world; World world;
int page = 0; int page = 0;
int argl = 0; int argOffset = 0;
String name = ""; String name = "";
boolean own = false; boolean own = false;
LocalPlayer localPlayer = null; LocalPlayer localPlayer = null;
final String senderName = sender.getName().toLowerCase();
if (args.argsLength() > 0 && args.getString(0).startsWith(".")) { if (args.argsLength() > 0 && args.getString(0).startsWith(".")) {
name = args.getString(0).substring(1).toLowerCase(); name = args.getString(0).substring(1).toLowerCase();
argl = 1; argOffset = 1;
if (name.equals("me") || name.isEmpty() || if (name.equals("me") || name.isEmpty() || name.equals(senderName)) {
name.equals(plugin.checkPlayer(sender).getDisplayName().toLowerCase())) {
plugin.checkPermission(sender, "worldguard.region.list.own");
name = plugin.checkPlayer(sender).getDisplayName().toLowerCase();
localPlayer = plugin.wrapPlayer(plugin.checkPlayer(sender));
own = true; own = true;
} }
} }
if (!own)
plugin.checkPermission(sender, "worldguard.region.list"); // Make /rg list default to "own" mode if the "worldguard.region.list" permission is not given
if (!own && !plugin.hasPermission(sender, "worldguard.region.list")) {
if (args.argsLength() > argl) { own = true;
page = Math.max(0, args.getInteger(argl) - 1);
} }
if (args.argsLength() > 1 + argl) { if (own) {
world = plugin.matchWorld(sender, args.getString(1 + argl)); plugin.checkPermission(sender, "worldguard.region.list.own");
name = senderName;
localPlayer = plugin.wrapPlayer(plugin.checkPlayer(sender));
}
if (args.argsLength() > argOffset) {
page = Math.max(0, args.getInteger(argOffset) - 1);
}
if (args.argsLength() > 1 + argOffset) {
world = plugin.matchWorld(sender, args.getString(1 + argOffset));
} else { } else {
world = plugin.checkPlayer(sender).getWorld(); world = plugin.checkPlayer(sender).getWorld();
} }
int listSize = 10;
RegionManager mgr = plugin.getGlobalRegionManager().get(world); final RegionManager mgr = plugin.getGlobalRegionManager().get(world);
Map<String, ProtectedRegion> regions = mgr.getRegions(); final Map<String, ProtectedRegion> regions = mgr.getRegions();
int size = regions.size(); List<RegionEntry> regionEntries = new ArrayList<RegionEntry>();
String[] regionIDList = new String[size];
int index = 0; int index = 0;
boolean show;
String prefix;
for (String id : regions.keySet()) { for (String id : regions.keySet()) {
show = false; RegionEntry entry = new RegionEntry(id, index++);
prefix = ""; if (!name.isEmpty()) {
if (name.isEmpty()) {
show = true;
}
else {
if (own) { if (own) {
if (regions.get(id).isOwner(localPlayer)) { entry.isOwner = regions.get(id).isOwner(localPlayer);
show = true; entry.isMember = regions.get(id).isMember(localPlayer);
prefix += "+";
}
else if (regions.get(id).isMember(localPlayer)) {
show = true;
prefix += "-";
}
} }
else { else {
if (regions.get(id).getOwners().getPlayers().contains(name)) { entry.isOwner = regions.get(id).isOwner(name);
show = true; entry.isMember = regions.get(id).isMember(name);
prefix += "+"; }
}
if (regions.get(id).getMembers().getPlayers().contains(name)) { if (!entry.isOwner && !entry.isMember) {
show = true; continue;
prefix += "-";
}
} }
} }
if (show) {
regionIDList[index] = prefix + " " + id; regionEntries.add(entry);
index++;
}
} }
if (!name.isEmpty())
regionIDList = Arrays.copyOf(regionIDList, index); Collections.sort(regionEntries);
Arrays.sort(regionIDList);
size = index; final int totalSize = regionEntries.size();
int pages = (int) Math.ceil(size / (float) listSize); final int pageSize = 10;
final int pages = (int) Math.ceil(totalSize / (float) pageSize);
sender.sendMessage(ChatColor.RED sender.sendMessage(ChatColor.RED
+ (name.equals("") ? "Regions (page " : "Regions for " + name + " (page ") + (name.equals("") ? "Regions (page " : "Regions for " + name + " (page ")
+ (page + 1) + " of " + pages + "):"); + (page + 1) + " of " + pages + "):");
if (page < pages) { if (page < pages) {
for (int i = page * listSize; i < page * listSize + listSize; i++) { for (int i = page * pageSize; i < page * pageSize + pageSize; i++) {
if (i >= size) { if (i >= totalSize) {
break; break;
} }
sender.sendMessage(ChatColor.YELLOW.toString() + (i + 1) + sender.sendMessage(ChatColor.YELLOW.toString() + regionEntries.get(i));
"." + regionIDList[i]);
} }
} }
} }
@Command(aliases = {"flag", "f"}, usage = "<id> <flag> [-g group] [value]", flags = "g:", @Command(aliases = {"flag", "f"}, usage = "<id> <flag> [-g group] [value]", flags = "g:",
desc = "Set flags", min = 2) desc = "Set flags", min = 2)
public void flag(CommandContext args, CommandSender sender) throws CommandException { public void flag(CommandContext args, CommandSender sender) throws CommandException {