Skip to content
This repository has been archived by the owner on Jan 4, 2021. It is now read-only.

Commit

Permalink
Implement pagination and bump version to 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
vxnick committed Aug 2, 2013
1 parent c2fbe52 commit f46d11e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
44 changes: 42 additions & 2 deletions src/com/vxnick/areatp/AreaTP.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.TreeMap;

import net.milkbowl.vault.permission.Permission;

Expand Down Expand Up @@ -31,6 +34,27 @@ public void onDisable() {

}

public void paginate(CommandSender sender, SortedMap<Integer, String> map, int page, int pageLength) {
int maxPages = (((map.size() % pageLength) == 0) ? map.size() / pageLength : (map.size() / pageLength) + 1);

if (page > maxPages) {
page = maxPages;
}

sender.sendMessage(ChatColor.YELLOW + "Page " + String.valueOf(page) + " of " + maxPages + ChatColor.RESET);

int i = 0, k = 0;
page--;

for (final Entry<Integer, String> e : map.entrySet()) {
k++;
if ((((page * pageLength) + i + 1) == k) && (k != ((page * pageLength) + pageLength + 1))) {
i++;
sender.sendMessage(e.getValue());
}
}
}

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (cmd.getName().equalsIgnoreCase("atp")) {
if (!perms.has(sender, "areatp.use")) {
Expand Down Expand Up @@ -172,7 +196,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
} else if (command.equals("help")) {
sender.sendMessage(ChatColor.GOLD + "Area Teleport Commands");
sender.sendMessage("/atp -- Show area TPs that you have created");
sender.sendMessage("/atp list -- Show a list of all area TPs");
sender.sendMessage("/atp list [page] -- Show a list of all area TPs");
sender.sendMessage("/atp set <name> -- Create or update an area TP");
sender.sendMessage("/atp remove <name> -- Remove one of your area TPs");
sender.sendMessage("/atp <name> -- Go to an area teleport");
Expand All @@ -182,11 +206,27 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
if (areas != null) {
List<String> areaList = new ArrayList<String>(areas.getKeys(false));
Collections.sort(areaList);
SortedMap<Integer, String> map = new TreeMap<Integer, String>();
int i = 1;

for (String area : areaList) {
String areaOwner = getConfig().getString(String.format("areas.%s.owner", area));
map.put(i, ChatColor.GOLD + area + ChatColor.RESET + " (owner: " + areaOwner + ")");
i++;
}

try {
int pageNumber;
if (args.length == 1) {
pageNumber = 1;
} else {
pageNumber = Integer.valueOf(args[1]);
}

sender.sendMessage(ChatColor.GOLD + area + ChatColor.RESET + " (owner: " + areaOwner + ")");
sender.sendMessage(ChatColor.GOLD + "Area TP List" + ChatColor.RESET);
paginate(sender, map, pageNumber, getConfig().getInt("page_results", 10));
} catch (NumberFormatException e) {
sender.sendMessage(ChatColor.RED + "Please specify a page number");
}
} else {
sender.sendMessage(ChatColor.YELLOW + "Nothing to list");
Expand Down
5 changes: 3 additions & 2 deletions src/config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
areas:
page_results: 10
groups:
Settler:
limit: 2
Expand All @@ -10,4 +10,5 @@ groups:
limit: 8
Ranger:
limit: 10
players:
players:
areas:
2 changes: 1 addition & 1 deletion src/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: AreaTP
main: com.vxnick.areatp.AreaTP
version: 0.1.0
version: 0.2.0
description: Create area teleports.
author: vxnick
website: http://github.com/vxnick/areatp
Expand Down

0 comments on commit f46d11e

Please sign in to comment.