From 24c557750ab86137f52fa89c5c4f3dee2bdd9090 Mon Sep 17 00:00:00 2001 From: Tobias Ottenweller Date: Sat, 3 Mar 2012 16:11:58 +0100 Subject: [PATCH] started working on improvements for the list command --- .../ancientgates/commands/CommandList.java | 79 ++++++++++++++++--- 1 file changed, 66 insertions(+), 13 deletions(-) diff --git a/src/org/mcteam/ancientgates/commands/CommandList.java b/src/org/mcteam/ancientgates/commands/CommandList.java index 2c71ce2..265961a 100644 --- a/src/org/mcteam/ancientgates/commands/CommandList.java +++ b/src/org/mcteam/ancientgates/commands/CommandList.java @@ -1,15 +1,18 @@ package org.mcteam.ancientgates.commands; import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.List; import org.mcteam.ancientgates.Conf; import org.mcteam.ancientgates.Gate; import org.mcteam.ancientgates.util.TextUtil; -public class CommandList extends BaseCommand { - - public CommandList() { +public class CommandList extends BaseCommand +{ + public CommandList() + { aliases.add("list"); aliases.add("ls"); @@ -18,21 +21,71 @@ public class CommandList extends BaseCommand { helpDescription = "Display a list of the gates"; } - public void perform() { - List ids = new ArrayList(); + + public void perform() + { + Collection gates = Gate.getAll(); - for (Gate gate : Gate.getAll()) { - ids.add(Conf.colorAlly + gate.getId()); - } - - if (ids.size() == 0) { + if (gates.size() == 0) + { sendMessage("There are no gates yet."); return; + } + + /* sort all gates by there first character + * put gates in corresponding Lists + * list 0-25: a,b,c, ... ,z + * list 26: 0-9 + * list 27: other + */ + List> ids = new ArrayList>(); + + for (int i=0; i<28; i++) + ids.add(new ArrayList()); + + for (Gate gate : gates) + { + String id = gate.getId(); + int first = id.charAt(0); + + if (first > 96 && first < 123) // convert lower case chars + first -= 97; + else if (first > 64 && first < 91) // convert upper case chars + first -= 65; + else if (first > 47 && first < 58) // convert numbers + first = 26; + else // everything else + first = 27; + + ids.get(first).add(Conf.colorAlly + id); + } + + // sort all lists + for (List list : ids) + Collections.sort(list); + + + sendMessage("There are currently " + gates.size() + " gates on this server: "); + + for (int i=0; i currentIds = ids.get(i); + + if (currentIds.size() == 0) + continue; + + String head; + if ( i < 26 ) + head = "" + (char)(i+65) + ":"; + else if ( i == 26 ) + head = "0 - 9:"; + else + head = "!@#$:"; + + sendMessage(Conf.colorMember + head); + sendMessage(TextUtil.implode(currentIds, Conf.colorSystem+", ")); } - sendMessage("There are currently "+ids.size()+" gates on this server: "); - sendMessage(TextUtil.implode(ids, Conf.colorSystem+", ")); } - }