mcMMO/src/main/java/com/gmail/nossr50/commands/mc/MctopCommand.java

154 lines
5.4 KiB
Java
Raw Normal View History

2012-01-09 20:00:13 +01:00
package com.gmail.nossr50.commands.mc;
import java.util.ArrayList;
import java.util.HashMap;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
2012-06-05 16:13:10 +02:00
import com.gmail.nossr50.mcMMO;
2012-04-26 16:27:57 +02:00
import com.gmail.nossr50.config.Config;
2012-04-27 11:47:11 +02:00
import com.gmail.nossr50.locale.LocaleLoader;
2012-07-09 17:12:46 +02:00
import com.gmail.nossr50.util.Database;
2012-04-27 11:47:11 +02:00
import com.gmail.nossr50.util.Leaderboard;
import com.gmail.nossr50.util.Misc;
2012-05-01 19:58:47 +02:00
import com.gmail.nossr50.util.Skills;
2012-01-09 20:00:13 +01:00
public class MctopCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
2012-04-23 01:34:34 +02:00
String usage = ChatColor.RED + "Proper usage is /mctop [skill] [page]"; //TODO: Needs more locale.
2012-06-05 15:57:10 +02:00
if (!Config.getInstance().getUseMySQL()) {
switch (args.length) {
case 0:
flatfileDisplay(1, "ALL", sender);
return true;
case 1:
2012-04-27 11:47:11 +02:00
if (Misc.isInt(args[0])) {
flatfileDisplay(Integer.valueOf(args[0]), "ALL", sender);
}
else if (Skills.isSkill(args[0])) {
flatfileDisplay(1, args[0].toUpperCase(), sender);
}
else {
2012-04-27 11:47:11 +02:00
sender.sendMessage(LocaleLoader.getString("Commands.Skill.Invalid"));
}
return true;
case 2:
2012-04-23 01:34:34 +02:00
if (!Skills.isSkill(args[0])) {
2012-04-27 11:47:11 +02:00
sender.sendMessage(LocaleLoader.getString("Commands.Skill.Invalid"));
2012-04-23 01:34:34 +02:00
return true;
}
2012-04-27 11:47:11 +02:00
if (Misc.isInt(args[1])) {
flatfileDisplay(Integer.valueOf(args[1]), args[0].toUpperCase(), sender);
}
else {
sender.sendMessage(usage);
}
return true;
default:
sender.sendMessage(usage);
return true;
}
}
String powerlevel = "taming+s.mining+s.woodcutting+s.repair+s.unarmed+s.herbalism+s.excavation+s.archery+s.swords+s.axes+s.acrobatics+s.fishing";
2013-01-10 04:43:21 +01:00
switch (args.length) {
case 0:
sqlDisplay(1, powerlevel, sender);
return true;
2013-01-10 04:43:21 +01:00
case 1:
if (Misc.isInt(args[0])) {
sqlDisplay(Integer.valueOf(args[0]), powerlevel, sender);
}
else if (Skills.isSkill(args[0])) {
sqlDisplay(1, args[0].toLowerCase(), sender);
}
else {
sender.sendMessage(LocaleLoader.getString("Commands.Skill.Invalid"));
}
2012-04-23 01:34:34 +02:00
2013-01-10 04:43:21 +01:00
return true;
2013-01-10 04:43:21 +01:00
case 2:
if (!Skills.isSkill(args[0])) {
sender.sendMessage(LocaleLoader.getString("Commands.Skill.Invalid"));
2012-04-23 01:34:34 +02:00
return true;
2013-01-10 04:43:21 +01:00
}
2012-04-23 01:34:34 +02:00
2013-01-10 04:43:21 +01:00
if (Misc.isInt(args[1])) {
sqlDisplay(Integer.valueOf(args[1]), args[0].toLowerCase(), sender);
}
else {
sender.sendMessage(usage);
}
2013-01-10 04:43:21 +01:00
return true;
default:
sender.sendMessage(usage);
return true;
}
}
private void flatfileDisplay(int page, String skill, CommandSender sender) {
String[] info = Leaderboard.retrieveInfo(skill, page);
2012-07-05 15:25:48 +02:00
if (skill.equals("ALL")) {
2012-04-27 11:47:11 +02:00
sender.sendMessage(LocaleLoader.getString("Commands.PowerLevel.Leaderboard"));
}
else {
2012-04-27 11:47:11 +02:00
sender.sendMessage(LocaleLoader.getString("Commands.Skill.Leaderboard", new Object[] { Misc.getCapitalized(skill) }));
}
int n = (page * 10) - 9; // Position
for (String x : info) {
if (x != null) {
String digit = String.valueOf(n);
if (n < 10) {
digit = "0" + digit;
}
String[] splitx = x.split(":");
// Format: 1. Playername - skill value
sender.sendMessage(digit + ". " + ChatColor.GREEN + splitx[1] + " - " + ChatColor.WHITE + splitx[0]);
n++;
}
}
}
private void sqlDisplay(int page, String query, CommandSender sender) {
String tablePrefix = Config.getInstance().getMySQLTablePrefix();
2012-07-09 17:12:46 +02:00
Database database = mcMMO.getPlayerDatabase();
HashMap<Integer, ArrayList<String>> userslist = database.read("SELECT s." + query + ", u.user FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE (s.user_id = u.id) AND s." + query + " > 0 ORDER BY s." + query + " DESC LIMIT "+((page * 10) - 10)+",10");
if (query.equals("taming+s.mining+s.woodcutting+s.repair+s.unarmed+s.herbalism+s.excavation+s.archery+s.swords+s.axes+s.acrobatics+s.fishing")) {
2012-04-27 11:47:11 +02:00
sender.sendMessage(LocaleLoader.getString("Commands.PowerLevel.Leaderboard"));
}
else {
2012-04-27 11:47:11 +02:00
sender.sendMessage(LocaleLoader.getString("Commands.Skill.Leaderboard", new Object[] { Misc.getCapitalized(query) }));
}
int place = ((page * 10) - 9);
for (ArrayList<String> entry : userslist.values()) {
sender.sendMessage(String.valueOf(place) + ". " + ChatColor.GREEN + entry.get(1) + " - " + ChatColor.WHITE + entry.get(0));
2013-01-13 03:30:53 +01:00
place++;
}
}
2012-01-09 20:00:13 +01:00
}