2014-12-28 17:22:53 +01:00
package com.massivecraft.factions.cmd ;
2015-01-02 11:32:27 +01:00
import java.util.List ;
import com.massivecraft.factions.Factions ;
2014-12-28 17:22:53 +01:00
import com.massivecraft.factions.Perm ;
import com.massivecraft.factions.Rel ;
import com.massivecraft.factions.cmd.arg.ARMPlayer ;
import com.massivecraft.factions.cmd.arg.ARRank ;
import com.massivecraft.factions.entity.Faction ;
import com.massivecraft.factions.entity.MConf ;
import com.massivecraft.factions.entity.MFlag ;
import com.massivecraft.factions.entity.MPlayer ;
import com.massivecraft.factions.entity.MPlayerColl ;
2015-01-02 11:32:27 +01:00
import com.massivecraft.factions.event.EventFactionsRankChange ;
2015-02-04 01:30:07 +01:00
import com.massivecraft.massivecore.cmd.MassiveCommandException ;
2014-12-28 17:22:53 +01:00
import com.massivecraft.massivecore.cmd.req.ReqHasPerm ;
2015-01-02 11:32:27 +01:00
import com.massivecraft.massivecore.util.IdUtil ;
import com.massivecraft.massivecore.util.MUtil ;
2014-12-28 17:22:53 +01:00
import com.massivecraft.massivecore.util.Txt ;
public class CmdFactionsRank extends FactionsCommand
{
// -------------------------------------------- //
// CONSTANTS
// -------------------------------------------- //
2015-01-02 11:32:27 +01:00
// The rank required to do any rank changes.
2014-12-28 17:22:53 +01:00
final static Rel rankReq = Rel . OFFICER ;
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
2014-12-30 10:01:54 +01:00
// These fields are set upon perform() and unset afterwards.
2014-12-28 17:22:53 +01:00
2014-12-30 10:01:54 +01:00
// Target
2014-12-28 17:22:53 +01:00
private Faction targetFaction = null ;
private MPlayer target = null ;
2014-12-30 10:01:54 +01:00
// Roles
2015-01-02 11:32:27 +01:00
private Rel senderRank = null ;
private Rel targetRank = null ;
2014-12-28 17:22:53 +01:00
private Rel rank = null ;
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public CmdFactionsRank ( )
{
// Aliases
2015-01-02 11:32:27 +01:00
this . addAliases ( " rank " ) ;
2014-12-28 17:22:53 +01:00
// Args
2014-12-30 10:01:54 +01:00
this . addOptionalArg ( " player " , " you " ) ;
2014-12-28 17:22:53 +01:00
this . addOptionalArg ( " action " , " show " ) ;
// Requirements
this . addRequirements ( ReqHasPerm . get ( Perm . RANK . node ) ) ;
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
2015-02-04 01:30:07 +01:00
public void perform ( ) throws MassiveCommandException
2014-12-28 17:22:53 +01:00
{
2014-12-30 10:01:54 +01:00
// This sets target and much other. Returns false if not succeeded.
if ( ! this . registerFields ( ) )
2014-12-28 17:22:53 +01:00
{
return ;
}
2014-12-30 10:01:54 +01:00
// Sometimes we just want to show the rank.
if ( ! this . argIsSet ( 1 ) )
2014-12-28 17:22:53 +01:00
{
2014-12-30 10:01:54 +01:00
if ( ! Perm . RANK_SHOW . has ( sender , true ) )
2014-12-28 17:22:53 +01:00
{
return ;
}
2014-12-30 10:01:54 +01:00
this . showRank ( ) ;
2014-12-28 17:22:53 +01:00
return ;
}
2015-01-02 11:32:27 +01:00
// Permission check.
2014-12-30 10:01:54 +01:00
if ( ! Perm . RANK_ACTION . has ( sender , true ) )
2014-12-28 17:22:53 +01:00
{
return ;
}
2014-12-30 10:01:54 +01:00
// Is the player allowed or not. Method can be found later down.
if ( ! this . isPlayerAllowed ( ) )
2014-12-28 17:22:53 +01:00
{
return ;
}
// Does the change make sense.
2014-12-30 10:01:54 +01:00
if ( ! this . isChangeRequired ( ) )
2014-12-28 17:22:53 +01:00
{
return ;
}
2015-01-02 11:32:27 +01:00
EventFactionsRankChange event = new EventFactionsRankChange ( sender , target , rank ) ;
event . run ( ) ;
if ( event . isCancelled ( ) ) return ;
rank = event . getNewRank ( ) ;
2014-12-28 17:22:53 +01:00
2015-01-02 11:32:27 +01:00
// Change the rank.
2014-12-30 10:01:54 +01:00
this . changeRank ( ) ;
2014-12-28 17:22:53 +01:00
}
2014-12-30 10:01:54 +01:00
// This is always run after performing a MassiveCommand.
2014-12-28 17:22:53 +01:00
@Override
public void unsetSenderVars ( )
{
super . unsetSenderVars ( ) ;
this . unregisterFields ( ) ;
}
// -------------------------------------------- //
// PRIVATE
// -------------------------------------------- //
2015-02-04 01:30:07 +01:00
private boolean registerFields ( ) throws MassiveCommandException
2014-12-28 17:22:53 +01:00
{
2015-01-02 11:32:27 +01:00
// Getting the target and faction.
2014-12-28 17:22:53 +01:00
target = this . arg ( 0 , ARMPlayer . getAny ( ) , msender ) ;
if ( null = = target ) return false ;
targetFaction = target . getFaction ( ) ;
// Rank if any passed.
if ( this . argIsSet ( 1 ) )
{
rank = this . arg ( 1 , ARRank . get ( target . getRole ( ) ) ) ;
2014-12-30 10:01:54 +01:00
if ( null = = rank ) return false ;
2014-12-28 17:22:53 +01:00
}
2015-01-02 11:32:27 +01:00
// Ranks
senderRank = msender . getRole ( ) ;
targetRank = target . getRole ( ) ;
2014-12-28 17:22:53 +01:00
return true ;
}
private void unregisterFields ( )
{
targetFaction = null ;
target = null ;
2015-01-02 11:32:27 +01:00
senderRank = null ;
targetRank = null ;
2014-12-28 17:22:53 +01:00
rank = null ;
}
private void showRank ( )
{
2015-01-02 11:32:27 +01:00
String targetName = target . describeTo ( msender , true ) ;
String isAre = target = = msender ? " are " : " is " ;
String theAan = targetRank = = Rel . LEADER ? " the " : Txt . aan ( targetRank . name ( ) ) ;
String rankName = Txt . getNicedEnum ( targetRank ) . toLowerCase ( ) ;
String ofIn = targetRank = = Rel . LEADER ? " of " : " in " ;
String factionName = targetFaction . describeTo ( msender , true ) ;
if ( targetFaction = = msenderFaction )
{
factionName = factionName . toLowerCase ( ) ;
}
msg ( " %s <i>%s %s <h>%s <i>%s %s<i>. " , targetName , isAre , theAan , rankName , ofIn , factionName ) ;
2014-12-28 17:22:53 +01:00
}
private boolean isPlayerAllowed ( )
{
2015-01-02 11:32:27 +01:00
// People with permission don't follow the normal rules.
2014-12-28 17:22:53 +01:00
if ( msender . isUsingAdminMode ( ) )
{
return true ;
}
2015-01-02 11:32:27 +01:00
// If somone gets the leadership of wilderness (Which has happened before).
// We can at least try to limit their powers.
2014-12-28 17:22:53 +01:00
if ( targetFaction . isNone ( ) )
{
2015-01-02 11:32:27 +01:00
msg ( " %s <b>doesn't use ranks sorry :( " , targetFaction . getName ( ) ) ;
2014-12-28 17:22:53 +01:00
return false ;
}
if ( targetFaction ! = msenderFaction )
{
2015-01-02 11:32:27 +01:00
// Don't change ranks outside of your faction.
msg ( " %s <b>is not in the same faction as you. " , target . describeTo ( msender ) ) ;
2014-12-28 17:22:53 +01:00
return false ;
}
if ( target = = msender )
{
2015-01-02 11:32:27 +01:00
// Don't change your own rank.
2014-12-28 17:22:53 +01:00
msg ( " <b>The target player mustn't be yourself. " ) ;
return false ;
}
2015-01-02 11:32:27 +01:00
if ( senderRank . isLessThan ( rankReq ) )
2014-12-28 17:22:53 +01:00
{
2015-01-02 11:32:27 +01:00
// You need a specific rank to change ranks.
msg ( " <b>You must be %s or higher to change ranks. " , Txt . getNicedEnum ( rankReq ) . toLowerCase ( ) ) ;
2014-12-28 17:22:53 +01:00
return false ;
}
// The following two if statements could be merged.
// But isn't for the sake of nicer error messages.
2015-01-02 11:32:27 +01:00
if ( senderRank = = targetRank )
2014-12-28 17:22:53 +01:00
{
2015-01-02 11:32:27 +01:00
// You can't change someones rank if it is equal to yours.
msg ( " <b>%s can't manage eachother. " , Txt . getNicedEnum ( rankReq ) + " s " ) ;
2014-12-28 17:22:53 +01:00
return false ;
}
2015-01-02 11:32:27 +01:00
if ( senderRank . isLessThan ( targetRank ) )
2014-12-28 17:22:53 +01:00
{
2015-01-02 11:32:27 +01:00
// You can't change someones rank if it is higher than yours.
msg ( " <b>You can't manage people of higher rank. " ) ;
2014-12-28 17:22:53 +01:00
return false ;
}
2015-01-02 11:32:27 +01:00
if ( senderRank . isAtMost ( rank ) & & senderRank ! = Rel . LEADER )
2014-12-28 17:22:53 +01:00
{
2015-01-02 11:32:27 +01:00
// You can't set ranks equal to or higer than your own. Unless you are the leader.
msg ( " <b>You can't set ranks higher than or equal to your own. " ) ;
2014-12-28 17:22:53 +01:00
return false ;
}
2014-12-30 10:01:54 +01:00
// If it wasn't cancelled above, player is allowed.
2014-12-28 17:22:53 +01:00
return true ;
}
private boolean isChangeRequired ( )
{
2014-12-30 10:01:54 +01:00
// Just a nice msg. It would however be caught by an if statement below.
2014-12-28 17:22:53 +01:00
if ( target . getRole ( ) = = Rel . RECRUIT & & arg ( 1 ) . equalsIgnoreCase ( " demote " ) )
{
2015-01-02 11:32:27 +01:00
msg ( " %s <b>is already recruit. " , target . describeTo ( msender ) ) ;
2014-12-28 17:22:53 +01:00
return false ;
}
2014-12-30 10:01:54 +01:00
// Just a nice msg. It would however be caught by an if statement below.
2014-12-28 17:22:53 +01:00
if ( target . getRole ( ) = = Rel . LEADER & & arg ( 1 ) . equalsIgnoreCase ( " promote " ) )
{
2015-01-02 11:32:27 +01:00
msg ( " %s <b>is already leader. " , target . describeTo ( msender ) ) ;
2014-12-28 17:22:53 +01:00
return false ;
}
2014-12-30 10:01:54 +01:00
// There must be a change, else it is all waste of time.
2014-12-28 17:22:53 +01:00
if ( target . getRole ( ) = = rank )
{
2015-01-02 11:32:27 +01:00
msg ( " %s <b>already has that rank. " , target . describeTo ( msender ) ) ;
2014-12-28 17:22:53 +01:00
return false ;
}
2014-12-30 10:01:54 +01:00
2014-12-28 17:22:53 +01:00
return true ;
}
private void changeRank ( )
{
2014-12-30 10:01:54 +01:00
// In case of leadership change, we do special things not done in other rank changes.
2014-12-28 17:22:53 +01:00
if ( rank = = Rel . LEADER )
{
2014-12-30 10:01:54 +01:00
this . changeRankLeader ( ) ;
2014-12-28 17:22:53 +01:00
}
else
{
2014-12-30 10:01:54 +01:00
this . changeRankOther ( ) ;
2014-12-28 17:22:53 +01:00
}
}
private void changeRankLeader ( )
{
2015-01-02 11:32:27 +01:00
// If there is a current leader. Demote & inform them.
2014-12-28 17:22:53 +01:00
MPlayer targetFactionCurrentLeader = targetFaction . getLeader ( ) ;
if ( targetFactionCurrentLeader ! = null )
{
2015-01-02 11:32:27 +01:00
// Inform & demote the old leader.
2014-12-28 17:22:53 +01:00
targetFactionCurrentLeader . setRole ( Rel . OFFICER ) ;
2014-12-30 10:01:54 +01:00
if ( targetFactionCurrentLeader ! = msender )
{
2015-01-02 11:32:27 +01:00
// They kinda know if they fired the command themself.
2014-12-28 17:22:53 +01:00
targetFactionCurrentLeader . msg ( " <i>You have been demoted from the position of faction leader by %s<i>. " , msender . describeTo ( targetFactionCurrentLeader , true ) ) ;
2014-12-30 10:01:54 +01:00
}
2014-12-28 17:22:53 +01:00
}
2015-01-02 11:32:27 +01:00
// Inform & promote the new leader.
2014-12-28 17:22:53 +01:00
target . setRole ( Rel . LEADER ) ;
2014-12-30 10:01:54 +01:00
if ( target ! = msender )
{
2015-01-02 11:32:27 +01:00
// They kinda know if they fired the command themself.
2014-12-28 17:22:53 +01:00
target . msg ( " <i>You have been promoted to the position of faction leader by %s<i>. " , msender . describeTo ( target , true ) ) ;
2014-12-30 10:01:54 +01:00
}
2014-12-28 17:22:53 +01:00
// Inform the msg sender
msg ( " <i>You have promoted %s<i> to the position of faction leader. " , target . describeTo ( msender , true ) ) ;
// Inform everyone
2015-01-02 11:32:27 +01:00
for ( MPlayer recipient : MPlayerColl . get ( ) . getAllOnline ( ) )
2014-12-28 17:22:53 +01:00
{
2015-01-02 11:32:27 +01:00
String changerName = senderIsConsole ? " A server admin " : msender . describeTo ( recipient ) ;
recipient . msg ( " %s<i> gave %s<i> the leadership of %s<i>. " , changerName , target . describeTo ( recipient ) , targetFaction . describeTo ( recipient ) ) ;
2014-12-28 17:22:53 +01:00
}
}
private void changeRankOther ( )
{
// If the target is currently the leader and faction isn't permanent...
2015-01-02 11:32:27 +01:00
if ( targetRank = = Rel . LEADER & & ! MConf . get ( ) . permanentFactionsDisableLeaderPromotion & & targetFaction . getFlag ( MFlag . ID_PERMANENT ) )
2014-12-28 17:22:53 +01:00
{
2015-01-02 11:32:27 +01:00
// ...we must promote a new one.
2014-12-28 17:22:53 +01:00
targetFaction . promoteNewLeader ( ) ;
}
2015-01-02 11:32:27 +01:00
// But if still no leader exists...
if ( targetFaction . getLeader ( ) = = null & & ! targetFaction . getFlag ( MFlag . ID_PERMANENT ) )
{
// ...we will disband it.
// I'm kinda lazy, so I just make the console perform the command.
Factions . get ( ) . getOuterCmdFactions ( ) . cmdFactionsDisband . execute ( IdUtil . getConsole ( ) , MUtil . list ( targetFaction . getName ( ) ) ) ;
}
List < MPlayer > recipients = targetFaction . getMPlayers ( ) ;
if ( ! recipients . contains ( msender ) )
{
recipients . add ( msender ) ;
}
2014-12-30 10:01:54 +01:00
2014-12-28 17:22:53 +01:00
// Were they demoted or promoted?
2015-01-02 11:32:27 +01:00
String change = ( rank . isLessThan ( targetRank ) ? " demoted " : " promoted " ) ;
2014-12-30 10:01:54 +01:00
// The rank will be set before the msg, so they have the appropriate prefix.
2014-12-28 17:22:53 +01:00
target . setRole ( rank ) ;
2015-01-02 11:32:27 +01:00
String oldRankName = Txt . getNicedEnum ( targetRank ) . toLowerCase ( ) ;
2014-12-28 17:22:53 +01:00
String rankName = Txt . getNicedEnum ( rank ) . toLowerCase ( ) ;
2015-01-02 11:32:27 +01:00
for ( MPlayer recipient : recipients )
{
String targetName = target . describeTo ( recipient , true ) ;
String wasWere = recipient = = target ? " were " : " was " ;
recipient . msg ( " %s<i> %s %s from %s to <h>%s <i>in %s<i>. " , targetName , wasWere , change , oldRankName , rankName , targetFaction . describeTo ( msender ) ) ;
}
2014-12-28 17:22:53 +01:00
}
2014-12-30 10:01:54 +01:00
}