2013-02-01 19:41:26 +01:00
package com.gmail.nossr50.commands.player ;
2012-01-09 20:00:13 +01:00
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
2013-02-02 12:20:02 +01:00
import com.gmail.nossr50.commands.CommandHelper ;
2012-04-26 16:27:57 +02:00
import com.gmail.nossr50.config.Config ;
2013-01-25 04:53:02 +01:00
import com.gmail.nossr50.database.Database ;
2012-04-27 11:47:11 +02:00
import com.gmail.nossr50.locale.LocaleLoader ;
2013-01-30 17:53:51 +01:00
import com.gmail.nossr50.skills.utilities.SkillTools ;
import com.gmail.nossr50.skills.utilities.SkillType ;
2012-04-27 11:47:11 +02:00
import com.gmail.nossr50.util.Leaderboard ;
import com.gmail.nossr50.util.Misc ;
2012-01-09 20:00:13 +01:00
public class MctopCommand implements CommandExecutor {
2012-04-20 20:01:21 +02:00
@Override
public boolean onCommand ( CommandSender sender , Command command , String label , String [ ] args ) {
2013-02-02 12:20:02 +01:00
if ( CommandHelper . noCommandPermissions ( sender , " mcmmo.commands.mctop " ) ) {
return true ;
}
2013-02-02 08:55:49 +01:00
String usage = LocaleLoader . getString ( " Commands.Usage.2 " , " mctop " , " [ " + LocaleLoader . getString ( " Commands.Usage.Skill " ) + " ] " , " [ " + LocaleLoader . getString ( " Commands.Usage.Page " ) + " ] " ) ;
2012-06-05 15:57:10 +02:00
2012-04-27 05:58:21 +02:00
if ( ! Config . getInstance ( ) . getUseMySQL ( ) ) {
2012-04-20 20:01:21 +02:00
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 ] ) ) {
2012-04-20 20:01:21 +02:00
flatfileDisplay ( Integer . valueOf ( args [ 0 ] ) , " ALL " , sender ) ;
}
2013-01-26 23:01:55 +01:00
else if ( SkillTools . isSkill ( args [ 0 ] ) ) {
2012-04-20 20:01:21 +02:00
flatfileDisplay ( 1 , args [ 0 ] . toUpperCase ( ) , sender ) ;
}
2013-01-26 23:01:55 +01:00
else if ( SkillTools . isLocalizedSkill ( args [ 0 ] ) ) {
flatfileDisplay ( 1 , SkillTools . translateLocalizedSkill ( args [ 0 ] ) . toUpperCase ( ) , sender ) ;
2013-01-16 06:06:14 +01:00
}
2012-04-20 20:01:21 +02:00
else {
2012-04-27 11:47:11 +02:00
sender . sendMessage ( LocaleLoader . getString ( " Commands.Skill.Invalid " ) ) ;
2012-04-20 20:01:21 +02:00
}
return true ;
case 2 :
2012-04-27 11:47:11 +02:00
if ( Misc . isInt ( args [ 1 ] ) ) {
2013-01-26 23:01:55 +01:00
if ( SkillTools . isSkill ( args [ 0 ] ) ) {
2013-01-16 06:06:14 +01:00
flatfileDisplay ( Integer . valueOf ( args [ 1 ] ) , args [ 0 ] . toUpperCase ( ) , sender ) ;
}
2013-01-26 23:01:55 +01:00
else if ( SkillTools . isLocalizedSkill ( args [ 0 ] ) ) {
flatfileDisplay ( Integer . valueOf ( args [ 1 ] ) , SkillTools . translateLocalizedSkill ( args [ 0 ] ) . toUpperCase ( ) , sender ) ;
2013-01-16 06:06:14 +01:00
}
else {
sender . sendMessage ( LocaleLoader . getString ( " Commands.Skill.Invalid " ) ) ;
}
2012-04-20 20:01:21 +02:00
}
else {
sender . sendMessage ( usage ) ;
}
return true ;
default :
sender . sendMessage ( usage ) ;
return true ;
}
}
2013-01-18 02:57:06 +01:00
String powerlevel = " taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing " ;
2012-04-20 20:01:21 +02:00
2013-01-10 04:43:21 +01:00
switch ( args . length ) {
case 0 :
sqlDisplay ( 1 , powerlevel , sender ) ;
return true ;
2012-04-20 20:01:21 +02:00
2013-01-10 04:43:21 +01:00
case 1 :
if ( Misc . isInt ( args [ 0 ] ) ) {
sqlDisplay ( Integer . valueOf ( args [ 0 ] ) , powerlevel , sender ) ;
}
2013-01-26 23:01:55 +01:00
else if ( SkillTools . isSkill ( args [ 0 ] ) ) {
2013-01-10 04:43:21 +01:00
sqlDisplay ( 1 , args [ 0 ] . toLowerCase ( ) , sender ) ;
}
2013-01-26 23:01:55 +01:00
else if ( SkillTools . isLocalizedSkill ( args [ 0 ] ) ) {
sqlDisplay ( 1 , SkillTools . translateLocalizedSkill ( args [ 0 ] ) . toLowerCase ( ) , sender ) ;
2013-01-16 06:06:14 +01:00
}
2013-01-10 04:43:21 +01:00
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 ;
2012-04-20 20:01:21 +02:00
2013-01-10 04:43:21 +01:00
case 2 :
if ( Misc . isInt ( args [ 1 ] ) ) {
2013-01-26 23:01:55 +01:00
if ( SkillTools . isSkill ( args [ 0 ] ) ) {
2013-01-16 06:06:14 +01:00
sqlDisplay ( Integer . valueOf ( args [ 1 ] ) , args [ 0 ] . toLowerCase ( ) , sender ) ;
}
2013-01-26 23:01:55 +01:00
else if ( SkillTools . isLocalizedSkill ( args [ 0 ] ) ) {
sqlDisplay ( Integer . valueOf ( args [ 1 ] ) , SkillTools . translateLocalizedSkill ( args [ 0 ] ) . toLowerCase ( ) , sender ) ;
2013-01-16 06:06:14 +01:00
}
else {
sender . sendMessage ( LocaleLoader . getString ( " Commands.Skill.Invalid " ) ) ;
}
2013-01-10 04:43:21 +01:00
}
else {
2012-04-20 20:01:21 +02:00
sender . sendMessage ( usage ) ;
}
2013-01-10 04:43:21 +01:00
return true ;
default :
sender . sendMessage ( usage ) ;
return true ;
2012-04-20 20:01:21 +02:00
}
}
private void flatfileDisplay ( int page , String skill , CommandSender sender ) {
2013-02-03 23:17:40 +01:00
if ( ! skill . equalsIgnoreCase ( " all " ) & & CommandHelper . noCommandPermissions ( sender , " mcmmo.commands.mctop. " + skill . toLowerCase ( ) ) ) {
return ;
}
2013-01-21 23:22:54 +01:00
Leaderboard . updateLeaderboards ( ) ; //Make sure we have the latest information
SkillType skillType = SkillType . getSkill ( skill ) ;
2013-01-15 23:49:20 +01:00
String [ ] info = Leaderboard . retrieveInfo ( skillType , page ) ;
2012-04-20 20:01:21 +02:00
2013-01-16 06:06:14 +01:00
if ( skill . equalsIgnoreCase ( " ALL " ) ) {
2012-04-27 11:47:11 +02:00
sender . sendMessage ( LocaleLoader . getString ( " Commands.PowerLevel.Leaderboard " ) ) ;
2012-04-20 20:01:21 +02:00
}
else {
2013-02-02 08:55:49 +01:00
sender . sendMessage ( LocaleLoader . getString ( " Commands.Skill.Leaderboard " , Misc . getCapitalized ( skill ) ) ) ;
2012-04-20 20:01:21 +02:00
}
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 + + ;
}
}
2013-01-17 07:36:56 +01:00
sender . sendMessage ( LocaleLoader . getString ( " Commands.mctop.Tip " ) ) ;
2012-04-20 20:01:21 +02:00
}
private void sqlDisplay ( int page , String query , CommandSender sender ) {
2013-02-03 23:17:40 +01:00
if ( ! query . equalsIgnoreCase ( " taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing " ) ) {
if ( CommandHelper . noCommandPermissions ( sender , " mcmmo.commands.mctop. " + query . toLowerCase ( ) ) ) {
return ;
}
}
2012-04-27 05:58:21 +02:00
String tablePrefix = Config . getInstance ( ) . getMySQLTablePrefix ( ) ;
2013-01-26 23:01:55 +01:00
HashMap < Integer , ArrayList < String > > userslist = Database . read ( " SELECT " + query + " , user, NOW() FROM " + tablePrefix + " users JOIN " + tablePrefix + " skills ON (user_id = id) WHERE " + query + " > 0 ORDER BY " + query + " DESC, user LIMIT " + ( ( page * 10 ) - 10 ) + " ,10 " ) ;
2012-04-20 20:01:21 +02:00
2013-01-18 02:57:06 +01:00
if ( query . equals ( " taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing " ) ) {
2012-04-27 11:47:11 +02:00
sender . sendMessage ( LocaleLoader . getString ( " Commands.PowerLevel.Leaderboard " ) ) ;
2012-04-20 20:01:21 +02:00
}
else {
2013-02-02 08:55:49 +01:00
sender . sendMessage ( LocaleLoader . getString ( " Commands.Skill.Leaderboard " , Misc . getCapitalized ( query ) ) ) ;
2012-04-20 20:01:21 +02:00
}
2013-01-13 03:58:02 +01:00
int place = ( page * 10 ) - 9 ;
2013-01-13 05:24:35 +01:00
for ( int i = 1 ; i < = 10 ; i + + ) {
2013-01-13 03:58:02 +01:00
if ( userslist . get ( i ) = = null ) {
break ;
}
sender . sendMessage ( String . valueOf ( place ) + " . " + ChatColor . GREEN + userslist . get ( i ) . get ( 1 ) + " - " + ChatColor . WHITE + userslist . get ( i ) . get ( 0 ) ) ;
2013-01-13 03:30:53 +01:00
place + + ;
2012-04-20 20:01:21 +02:00
}
2013-01-17 07:36:56 +01:00
sender . sendMessage ( LocaleLoader . getString ( " Commands.mctop.Tip " ) ) ;
2012-04-20 20:01:21 +02:00
}
2012-01-09 20:00:13 +01:00
}