List players in a HashSet in pages

Discussion in 'Plugin Development' started by LRFLEW, Jan 7, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    LRFLEW

    Currently in my plugin, when I try to list the players in a HashSet, it just lists them all. However, I know that there is a chance that the list would be too long to appear in the chat window for players. I want to make it print in pages, much like how most help functions, but I'm not sure about the best way to go about it. Currently, I'm using the for loop that goes through the iterator to list all the players in the set. I was think that to make the pages, I could just iterate through until I get to the next page (or number of players per page times the page number after the first page to view), then start listing, but I'm sure there's a better way. What do you think?
     
  2. I have a command with pages on a WIP project of mine:
    Code:
    private final static int perPage = 6; // how many items per page
    	
    	@Override
    	public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args)
    	{
    		final int size = list.size() // size of your list
    		
    		if(size == 0)
    		{
    			// ...
    			return true;
    		}
    		
    		int pageIndex = 0;
    		
    		if(args.length >= 1)
    		{
    			try
    			{
    				pageIndex = Math.max(Integer.parseInt(args[0]), 1) - 1; // limit the input arg to minimum 1 and subtract 1
    			}
    			catch(final Exception e)
    			{
    				// invalid argument, not a number
    				return true;
    			}
    		}
    		
    		final int min = Math.max(pageIndex * perPage, 0);
    		final int max = Math.min(min + perPage, size);
    		
    		if(min > size)
    		{
    			// number exceeds max page
    			return true;
    		}
    		
    		for(int i = min; i < max; i++)
    		{
    			// "i" is the current item index
    		}
    		
    		if(size > max)
    			// more items available, display command with (pageIndex + 2)
    		
    		return true;
    	}
    }
    I'm using a List to retrieve stuff by index, I dunno about HashSet.
     
  3. Offline

    Sagacious_Zed Bukkit Docs

    You can convert the HashSet to an array and it will give you indexes to work with. and its really as simple as yourHashSet.toArray(yourType[])
     
  4. Offline

    LRFLEW

    yeah, the problem is Sets don't have indexes. Otherwise, I would have done this.

    If you look at the code that performs with this, it takes the iterator and makes an array of what the iterator. It would be faster to just make my own iterator and only go up to where I need. Thanks for pointing this out, though
     
  5. You could make a separate List that contains only the displayed data when you add/remove players from the HashSet and the use the List on that command.
     
  6. Offline

    Chiller

    And since everyone's name is a different length you might want to go by bytes...
     
  7. Offline

    Sagacious_Zed Bukkit Docs

    @LRFLEW
    Alternately you could use something like a LinkedHashSet or a TreeSet which have orders to them.
     
  8. Offline

    LRFLEW

    the maintenance of that sounds like a lot of work :p.

    ...what are you talking about?

    TreeSets are way slow and appear to have no real use in anything I've done :p. As for LinkedHashSets, they still have O(n) time to go to a specific index, which is no better than what I already have with my iterator.

    Besides, I finished some code on what I originally thought to do, which can be found between lines 166 and 204 here. Feel free to check it out.
     
  9. It's not actually =) I'm doing that and I added 1 extra line in 2-3 classes, that's it :}
     
Thread Status:
Not open for further replies.

Share This Page