2013-02-01 19:41:26 +01:00
package com.gmail.nossr50.commands.player ;
2013-01-15 23:55:41 +01:00
2013-01-16 08:50:18 +01:00
import org.bukkit.Bukkit ;
2013-01-15 23:55:41 +01:00
import org.bukkit.command.Command ;
import org.bukkit.command.CommandExecutor ;
import org.bukkit.command.CommandSender ;
import org.bukkit.entity.Player ;
2013-01-16 23:37:35 +01:00
import com.gmail.nossr50.commands.CommandHelper ;
2013-01-15 23:55:41 +01:00
import com.gmail.nossr50.config.Config ;
2013-02-03 22:35:36 +01:00
import com.gmail.nossr50.datatypes.McMMOPlayer ;
2013-01-16 20:58:14 +01:00
import com.gmail.nossr50.locale.LocaleLoader ;
2013-01-16 23:13:56 +01:00
import com.gmail.nossr50.runnables.McRankAsync ;
2013-01-30 17:53:51 +01:00
import com.gmail.nossr50.skills.utilities.SkillTools ;
import com.gmail.nossr50.skills.utilities.SkillType ;
2013-01-15 23:55:41 +01:00
import com.gmail.nossr50.util.Leaderboard ;
2013-02-03 22:35:36 +01:00
import com.gmail.nossr50.util.Misc ;
import com.gmail.nossr50.util.Permissions ;
import com.gmail.nossr50.util.Users ;
2013-01-15 23:55:41 +01:00
public class McrankCommand implements CommandExecutor {
2013-01-16 00:35:21 +01:00
@Override
2013-01-16 08:50:18 +01:00
public boolean onCommand ( CommandSender sender , Command command , String label , String [ ] args ) {
2013-01-16 23:37:35 +01:00
//TODO: Better input handling, add usage string
if ( ! Config . getInstance ( ) . getUseMySQL ( ) ) {
Leaderboard . updateLeaderboards ( ) ; //Make sure the information is up to date
}
if ( CommandHelper . noConsoleUsage ( sender ) ) {
return true ;
}
2013-02-02 12:20:02 +01:00
if ( CommandHelper . noCommandPermissions ( sender , " mcmmo.commands.mcrank " ) ) {
return true ;
}
2013-01-16 23:37:35 +01:00
Player player = ( Player ) sender ;
String playerName ;
switch ( args . length ) {
2013-01-21 23:22:54 +01:00
case 0 :
playerName = player . getName ( ) ;
break ;
2013-01-16 23:37:35 +01:00
2013-01-21 23:22:54 +01:00
case 1 :
2013-02-03 22:14:26 +01:00
if ( CommandHelper . noCommandPermissions ( sender , " mcmmo.commands.mcrank.others " ) ) {
return true ;
}
2013-01-21 23:22:54 +01:00
playerName = args [ 0 ] ;
2013-02-03 22:35:36 +01:00
McMMOPlayer mcmmoPlayer = Users . getPlayer ( playerName ) ;
if ( mcmmoPlayer ! = null ) {
Player target = mcmmoPlayer . getPlayer ( ) ;
if ( sender instanceof Player & & ! Misc . isNear ( ( ( Player ) sender ) . getLocation ( ) , target . getLocation ( ) , 5 . 0 ) & & ! Permissions . hasPermission ( sender , " mcmmo.commands.mcrank.others.far " ) ) {
sender . sendMessage ( LocaleLoader . getString ( " Inspect.TooFar " ) ) ;
return true ;
}
}
else if ( sender instanceof Player & & ! Permissions . hasPermission ( sender , " mcmmo.commands.mcrank.others.offline " ) ) {
sender . sendMessage ( LocaleLoader . getString ( " Inspect.Offline " ) ) ;
return true ;
}
2013-01-21 23:22:54 +01:00
break ;
2013-01-16 00:35:21 +01:00
2013-01-21 23:22:54 +01:00
default :
return false ;
2013-01-16 23:37:35 +01:00
}
if ( Config . getInstance ( ) . getUseMySQL ( ) ) {
sqlDisplay ( sender , playerName ) ;
}
else {
flatfileDisplay ( sender , playerName ) ;
2013-01-16 00:35:21 +01:00
}
return true ;
}
2013-01-16 01:09:48 +01:00
public void flatfileDisplay ( CommandSender sender , String playerName ) {
2013-01-16 23:13:04 +01:00
sender . sendMessage ( LocaleLoader . getString ( " Commands.mcrank.Heading " ) ) ;
2013-02-02 08:55:49 +01:00
sender . sendMessage ( LocaleLoader . getString ( " Commands.mcrank.Player " , playerName ) ) ;
2013-01-16 00:35:21 +01:00
for ( SkillType skillType : SkillType . values ( ) ) {
2013-01-16 23:13:04 +01:00
int [ ] rankInts = Leaderboard . getPlayerRank ( playerName , skillType ) ;
2013-01-24 15:08:30 +01:00
if ( skillType . isChildSkill ( ) ) {
continue ;
}
2013-01-16 23:13:04 +01:00
2013-01-16 23:37:35 +01:00
if ( skillType . equals ( SkillType . ALL ) ) {
2013-01-16 00:35:21 +01:00
continue ; // We want the overall ranking to be at the bottom
2013-01-16 23:37:35 +01:00
}
2013-01-16 23:13:04 +01:00
2013-01-16 20:58:14 +01:00
if ( rankInts [ 1 ] = = 0 ) {
2013-02-02 08:55:49 +01:00
sender . sendMessage ( LocaleLoader . getString ( " Commands.mcrank.Skill " , SkillTools . localizeSkillName ( skillType ) , LocaleLoader . getString ( " Commands.mcrank.Unranked " ) ) ) ; //Don't bother showing ranking for players without skills
2013-01-16 23:37:35 +01:00
}
else {
2013-02-02 08:55:49 +01:00
sender . sendMessage ( LocaleLoader . getString ( " Commands.mcrank.Skill " , SkillTools . localizeSkillName ( skillType ) , String . valueOf ( rankInts [ 0 ] ) ) ) ;
2013-01-16 20:58:14 +01:00
}
}
2013-01-16 23:13:04 +01:00
2013-01-16 20:58:14 +01:00
//Show the powerlevel ranking
int [ ] rankInts = Leaderboard . getPlayerRank ( playerName , SkillType . ALL ) ;
2013-01-21 23:22:54 +01:00
2013-01-16 20:58:14 +01:00
if ( rankInts [ 1 ] = = 0 ) {
2013-02-02 08:55:49 +01:00
sender . sendMessage ( LocaleLoader . getString ( " Commands.mcrank.Overalll " , LocaleLoader . getString ( " Commands.mcrank.Unranked " ) ) ) ; //Don't bother showing ranking for players without skills
2013-01-16 23:37:35 +01:00
}
else {
2013-02-02 08:55:49 +01:00
sender . sendMessage ( LocaleLoader . getString ( " Commands.mcrank.Overall " , String . valueOf ( rankInts [ 0 ] ) ) ) ;
2013-01-16 00:35:21 +01:00
}
}
private void sqlDisplay ( CommandSender sender , String playerName ) {
2013-01-16 23:13:56 +01:00
Bukkit . getScheduler ( ) . runTaskAsynchronously ( Bukkit . getPluginManager ( ) . getPlugin ( " mcMMO " ) , new McRankAsync ( playerName , sender ) ) ;
2013-01-16 00:35:21 +01:00
}
}