2013-02-01 19:41:26 +01:00
package com.gmail.nossr50.commands.player ;
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-02-04 16:33:34 +01:00
import com.gmail.nossr50.mcMMO ;
2013-01-15 23:55:41 +01:00
import com.gmail.nossr50.config.Config ;
2013-03-01 06:52:01 +01:00
import com.gmail.nossr50.database.LeaderboardManager ;
import com.gmail.nossr50.datatypes.player.McMMOPlayer ;
import com.gmail.nossr50.datatypes.player.PlayerProfile ;
import com.gmail.nossr50.datatypes.skills.SkillType ;
2013-01-16 20:58:14 +01:00
import com.gmail.nossr50.locale.LocaleLoader ;
2013-03-01 06:52:01 +01:00
import com.gmail.nossr50.runnables.commands.McrankCommandAsyncTask ;
2013-02-03 22:35:36 +01:00
import com.gmail.nossr50.util.Misc ;
2013-02-19 06:56:25 +01:00
import com.gmail.nossr50.util.Permissions ;
2013-03-01 06:52:01 +01:00
import com.gmail.nossr50.util.player.UserManager ;
import com.gmail.nossr50.util.skills.SkillUtils ;
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-02-05 15:18:06 +01:00
switch ( args . length ) {
2013-03-01 06:52:01 +01:00
case 0 :
if ( ! Permissions . mcrank ( sender ) ) {
sender . sendMessage ( command . getPermissionMessage ( ) ) ;
return true ;
}
2013-02-02 12:20:02 +01:00
2013-03-01 06:52:01 +01:00
if ( ! ( sender instanceof Player ) ) {
return false ;
}
2013-02-04 16:33:34 +01:00
2013-03-01 06:52:01 +01:00
if ( Config . getInstance ( ) . getUseMySQL ( ) ) {
sqlDisplay ( sender , sender . getName ( ) ) ;
}
else {
LeaderboardManager . updateLeaderboards ( ) ; // Make sure the information is up to date
flatfileDisplay ( sender , sender . getName ( ) ) ;
}
2013-01-16 23:37:35 +01:00
2013-02-03 22:14:26 +01:00
return true ;
2013-02-05 15:18:06 +01:00
2013-03-01 06:52:01 +01:00
case 1 :
if ( ! Permissions . mcrankOthers ( sender ) ) {
sender . sendMessage ( command . getPermissionMessage ( ) ) ;
2013-02-05 15:18:06 +01:00
return true ;
}
2013-03-01 06:52:01 +01:00
McMMOPlayer mcMMOPlayer = UserManager . getPlayer ( args [ 0 ] ) ;
if ( mcMMOPlayer = = null ) {
PlayerProfile profile = new PlayerProfile ( args [ 0 ] , false ) ; // Temporary Profile
if ( ! profile . isLoaded ( ) ) {
sender . sendMessage ( LocaleLoader . getString ( " Commands.DoesNotExist " ) ) ;
return true ;
}
if ( sender instanceof Player & & ! Permissions . mcrankOffline ( sender ) ) {
sender . sendMessage ( LocaleLoader . getString ( " Inspect.Offline " ) ) ;
return true ;
}
2013-02-05 15:18:06 +01:00
}
2013-03-01 06:52:01 +01:00
else {
Player target = mcMMOPlayer . getPlayer ( ) ;
2013-02-03 22:35:36 +01:00
2013-03-01 06:52:01 +01:00
if ( sender instanceof Player & & ! Misc . isNear ( ( ( Player ) sender ) . getLocation ( ) , target . getLocation ( ) , 5 . 0 ) & & ! Permissions . mcrankFar ( sender ) ) {
sender . sendMessage ( LocaleLoader . getString ( " Inspect.TooFar " ) ) ;
return true ;
}
2013-02-03 22:35:36 +01:00
}
2013-02-05 15:18:06 +01:00
2013-03-01 06:52:01 +01:00
if ( Config . getInstance ( ) . getUseMySQL ( ) ) {
sqlDisplay ( sender , args [ 0 ] ) ;
}
else {
LeaderboardManager . updateLeaderboards ( ) ; // Make sure the information is up to date
flatfileDisplay ( sender , args [ 0 ] ) ;
}
2013-02-03 22:35:36 +01:00
2013-03-01 06:52:01 +01:00
return true ;
2013-01-16 00:35:21 +01:00
2013-03-01 06:52:01 +01:00
default :
return false ;
2013-01-16 23:37:35 +01:00
}
2013-01-16 00:35:21 +01:00
}
2013-02-05 15:18:37 +01:00
private 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-02-04 16:33:34 +01:00
2013-01-16 00:35:21 +01:00
for ( SkillType skillType : SkillType . values ( ) ) {
2013-03-01 06:52:01 +01:00
int [ ] rankInts = LeaderboardManager . getPlayerRank ( playerName , skillType ) ;
2013-02-04 16:33:34 +01:00
2013-01-24 15:08:30 +01:00
if ( skillType . isChildSkill ( ) ) {
continue ;
}
2013-01-16 23:13:04 +01:00
2013-03-10 20:45:25 +01:00
if ( ( sender instanceof Player ) & & ! Permissions . skillEnabled ( ( Player ) sender , skillType ) ) {
continue ;
}
2013-01-16 20:58:14 +01:00
if ( rankInts [ 1 ] = = 0 ) {
2013-03-01 06:52:01 +01:00
sender . sendMessage ( LocaleLoader . getString ( " Commands.mcrank.Skill " , SkillUtils . getSkillName ( 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-03-01 06:52:01 +01:00
sender . sendMessage ( LocaleLoader . getString ( " Commands.mcrank.Skill " , SkillUtils . getSkillName ( skillType ) , rankInts [ 0 ] ) ) ;
2013-01-16 20:58:14 +01:00
}
}
2013-01-16 23:13:04 +01:00
2013-02-04 16:33:34 +01:00
// Show the powerlevel ranking
2013-03-01 06:52:01 +01:00
int [ ] rankInts = LeaderboardManager . getPlayerRank ( playerName ) ;
2013-01-21 23:22:54 +01:00
2013-01-16 20:58:14 +01:00
if ( rankInts [ 1 ] = = 0 ) {
2013-02-15 05:27:19 +01:00
sender . sendMessage ( LocaleLoader . getString ( " Commands.mcrank.Overall " , 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-15 19:56:30 +01:00
sender . sendMessage ( LocaleLoader . getString ( " Commands.mcrank.Overall " , rankInts [ 0 ] ) ) ;
2013-01-16 00:35:21 +01:00
}
}
private void sqlDisplay ( CommandSender sender , String playerName ) {
2013-03-10 20:07:07 +01:00
mcMMO . p . getServer ( ) . getScheduler ( ) . runTaskAsynchronously ( mcMMO . p , new McrankCommandAsyncTask ( playerName , sender ) ) ;
2013-01-16 00:35:21 +01:00
}
}