refactoring of command list

This commit is contained in:
Tobias Ottenweller 2012-05-16 23:16:08 +02:00
parent 649777c939
commit afc7efcf26

View File

@ -7,9 +7,9 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.mcteam.ancientgates.Gate; import org.mcteam.ancientgates.Gate;
import org.mcteam.ancientgates.Plugin;
import org.mcteam.ancientgates.util.TextUtil; import org.mcteam.ancientgates.util.TextUtil;
@ -24,17 +24,22 @@ public class CommandList extends BaseCommand
hasGateParam = false; hasGateParam = false;
helpDescription = "Display a list of the gates"; helpDescription = "Display a list of the gates";
requiredPermission = Plugin.permissionInfo;
} }
protected String intToTitleString(int i) protected String intToTitleString(int i)
{ {
if ( i < 26 ) if ( i < 26 ) {
return ChatColor.GREEN + "" + (char)(i+65) + ":"; return ChatColor.GREEN + "" + (char)(i+65) + ":";
else if ( i == 26 ) }
else if ( i == 26 ) {
return ChatColor.GREEN + "0 - 9:"; return ChatColor.GREEN + "0 - 9:";
else }
else {
return ChatColor.GREEN + "!@#$:"; return ChatColor.GREEN + "!@#$:";
}
} }
@ -45,8 +50,9 @@ public class CommandList extends BaseCommand
{ {
Collection<Gate> gates = Gate.getAll(); Collection<Gate> gates = Gate.getAll();
if (gates.size() == 0) if (gates.size() == 0) {
return null; return null;
}
/* sort all gates by there first character /* sort all gates by there first character
* put gates in corresponding Lists * put gates in corresponding Lists
@ -56,22 +62,26 @@ public class CommandList extends BaseCommand
*/ */
List<List<String>> ids = new ArrayList<List<String>>(); List<List<String>> ids = new ArrayList<List<String>>();
for (int i=0; i<28; i++) for (int i=0; i<28; i++) {
ids.add(new ArrayList<String>()); ids.add(new ArrayList<String>());
}
for (Gate gate : gates) for (Gate gate : gates) {
{
String id = gate.getId(); String id = gate.getId();
int first = id.charAt(0); int first = id.charAt(0);
if (first > 96 && first < 123) // convert lower case chars if (first > 96 && first < 123) { // convert lower case chars
first -= 97; first -= 97;
else if (first > 64 && first < 91) // convert upper case chars }
else if (first > 64 && first < 91) { // convert upper case chars
first -= 65; first -= 65;
else if (first > 47 && first < 58) // convert numbers }
else if (first > 47 && first < 58) { // convert numbers
first = 26; first = 26;
else // everything else }
else { // everything else
first = 27; first = 27;
}
ids.get(first).add(id); ids.get(first).add(id);
} }
@ -86,10 +96,10 @@ public class CommandList extends BaseCommand
// Integer: the number of lines neccessary for displaying the corresponding list // Integer: the number of lines neccessary for displaying the corresponding list
HashMap<List<String>, Integer> lines = new HashMap<List<String>, Integer>(27); HashMap<List<String>, Integer> lines = new HashMap<List<String>, Integer>(27);
for (List<String> currentIds : ids) for (List<String> currentIds : ids) {
{ if (currentIds.size() == 0) {
if (currentIds.size() == 0)
continue; continue;
}
int characters = TextUtil.implode(currentIds, ", ").length(); int characters = TextUtil.implode(currentIds, ", ").length();
lines.put(currentIds, characters / 52 + 2); lines.put(currentIds, characters / 52 + 2);
@ -102,60 +112,49 @@ public class CommandList extends BaseCommand
List<String> pageMessages = new ArrayList<String>(); List<String> pageMessages = new ArrayList<String>();
while (currentStartingCharList < ids.size()) while (currentStartingCharList < ids.size()) {
{
int linesLeftOnCurrentPage = 9; int linesLeftOnCurrentPage = 9;
while (linesLeftOnCurrentPage > 1 && currentStartingCharList < ids.size()) while (linesLeftOnCurrentPage > 1 && currentStartingCharList < ids.size()) {
{
List<String> currentIds = ids.get(currentStartingCharList); List<String> currentIds = ids.get(currentStartingCharList);
if (currentIds.size() > 0) if (currentIds.size() > 0) {
{
// add header line // add header line
if (currentPage == page) if (currentPage == page) {
pageMessages.add(intToTitleString(currentStartingCharList)); pageMessages.add(intToTitleString(currentStartingCharList));
}
//sort //sort
Collections.sort(currentIds); Collections.sort(currentIds);
// add ids // add ids
if (lines.get(currentIds) <= linesLeftOnCurrentPage) // all ids fit on current page if (lines.get(currentIds) <= linesLeftOnCurrentPage) { // all ids fit on current page
{
linesLeftOnCurrentPage -= lines.get(currentIds); linesLeftOnCurrentPage -= lines.get(currentIds);
if (currentPage == page) {
if (currentPage == page)
{
pageMessages.add(TextUtil.implode(currentIds, ", ")); pageMessages.add(TextUtil.implode(currentIds, ", "));
if (finishedCurrentIds == false) if (finishedCurrentIds == false) {
pageMessages.set(pageMessages.size() -2, pageMessages.get(pageMessages.size() -2) + " (more on previous page)"); pageMessages.set(pageMessages.size() -2, pageMessages.get(pageMessages.size() -2) + " (more on previous page)");
}
} }
finishedCurrentIds = true; finishedCurrentIds = true;
} }
else // NOT all ids fit on current page else { // NOT all ids fit on current page
{
int charsAvailible = (linesLeftOnCurrentPage - 1) * 52; int charsAvailible = (linesLeftOnCurrentPage - 1) * 52;
int idsPos = 0; int idsPos = 0;
while (true) do {
{
charsAvailible -= currentIds.get(idsPos).length() + 2; charsAvailible -= currentIds.get(idsPos).length() + 2;
if (charsAvailible <= 0)
break;
idsPos++; idsPos++;
} } while (charsAvailible > 0);
List<String> idsToPutOnCurrentPage = currentIds.subList(0, idsPos); List<String> idsToPutOnCurrentPage = currentIds.subList(0, idsPos);
currentIds.remove(idsToPutOnCurrentPage); currentIds.remove(idsToPutOnCurrentPage);
String stringToPutOnCurrentPage = TextUtil.implode(idsToPutOnCurrentPage, ", "); String stringToPutOnCurrentPage = TextUtil.implode(idsToPutOnCurrentPage, ", ");
if (currentPage == page) if (currentPage == page) {
{
pageMessages.add(stringToPutOnCurrentPage); pageMessages.add(stringToPutOnCurrentPage);
pageMessages.set(pageMessages.size() -2, pageMessages.get(pageMessages.size() -2) + " (more on next page)"); pageMessages.set(pageMessages.size() -2, pageMessages.get(pageMessages.size() -2) + " (more on next page)");
} }
@ -167,17 +166,18 @@ public class CommandList extends BaseCommand
} }
} }
if (finishedCurrentIds) if (finishedCurrentIds) {
currentStartingCharList++; currentStartingCharList++;
}
} }
currentPage++; currentPage++;
} }
if (pageMessages.isEmpty()) if (pageMessages.isEmpty()) {
return null; return null;
else }
{ else {
ArrayList<String> retVal = new ArrayList<String>(); ArrayList<String> retVal = new ArrayList<String>();
retVal.add(ChatColor.LIGHT_PURPLE + "This is page " + ChatColor.WHITE + page + ChatColor.LIGHT_PURPLE + "/" + ChatColor.WHITE + --currentPage + ChatColor.LIGHT_PURPLE + ". There are " + gates.size() + " gates on this server: "); retVal.add(ChatColor.LIGHT_PURPLE + "This is page " + ChatColor.WHITE + page + ChatColor.LIGHT_PURPLE + "/" + ChatColor.WHITE + --currentPage + ChatColor.LIGHT_PURPLE + ". There are " + gates.size() + " gates on this server: ");
retVal.addAll(pageMessages); retVal.addAll(pageMessages);
@ -191,34 +191,27 @@ public class CommandList extends BaseCommand
{ {
Collection<Gate> gates = Gate.getAll(); Collection<Gate> gates = Gate.getAll();
if (gates.size() == 0) if (gates.size() == 0) {
sendMessage("There are no gates yet."); sendMessage("There are no gates yet.");
else }
{ else {
int page = 1; int page = 1;
try try {
{
page = new Integer(parameters.get(0)); page = new Integer(parameters.get(0));
} }
catch (Exception e) catch (Exception e) {
{
} }
List<String> messages = message(page); List<String> messages = message(page);
if (messages == null) if (messages == null) {
sendMessage("The requested page is not availible"); sendMessage("The requested page is not availible");
else }
else {
sendMessage(messages); sendMessage(messages);
}
} }
} }
@Override
public boolean hasPermission(CommandSender sender)
{
return sender.hasPermission(permissionInfo) || sender.hasPermission(permissionManage);
}
} }