Factions/src/com/massivecraft/factions/cmd/CmdFactionsRank.java

348 lines
9.6 KiB
Java
Raw Normal View History

2014-12-28 17:22:53 +01:00
package com.massivecraft.factions.cmd;
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;
import com.massivecraft.factions.event.EventFactionsRankChange;
2015-02-12 12:00:55 +01:00
import com.massivecraft.massivecore.MassiveException;
2014-12-28 17:22:53 +01:00
import com.massivecraft.massivecore.cmd.req.ReqHasPerm;
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
// -------------------------------------------- //
// The rank required to do any rank changes.
2014-12-28 17:22:53 +01:00
final static Rel rankReq = Rel.OFFICER;
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
// These fields are set upon perform() and unset afterwards.
2014-12-28 17:22:53 +01:00
// Target
2014-12-28 17:22:53 +01:00
private Faction targetFaction = null;
private MPlayer target = null;
// Roles
private Rel senderRank = null;
private Rel targetRank = null;
2014-12-28 17:22:53 +01:00
private Rel rank = null;
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public CmdFactionsRank()
{
// Aliases
this.addAliases("rank");
2014-12-28 17:22:53 +01:00
// Args
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-12 12:00:55 +01:00
public void perform() throws MassiveException
2014-12-28 17:22:53 +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;
}
// Sometimes we just want to show the rank.
if ( ! this.argIsSet(1))
2014-12-28 17:22:53 +01:00
{
if ( ! Perm.RANK_SHOW.has(sender, true))
2014-12-28 17:22:53 +01:00
{
return;
}
this.showRank();
2014-12-28 17:22:53 +01:00
return;
}
// Permission check.
if ( ! Perm.RANK_ACTION.has(sender, true))
2014-12-28 17:22:53 +01:00
{
return;
}
// 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.
if ( ! this.isChangeRequired())
2014-12-28 17:22:53 +01:00
{
return;
}
EventFactionsRankChange event = new EventFactionsRankChange(sender, target, rank);
event.run();
if (event.isCancelled()) return;
rank = event.getNewRank();
2014-12-28 17:22:53 +01:00
// Change the rank.
this.changeRank();
2014-12-28 17:22:53 +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-12 12:00:55 +01:00
private boolean registerFields() throws MassiveException
2014-12-28 17:22:53 +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()));
if (null == rank) return false;
2014-12-28 17:22:53 +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;
senderRank = null;
targetRank = null;
2014-12-28 17:22:53 +01:00
rank = null;
}
private void showRank()
{
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()
{
// People with permission don't follow the normal rules.
2014-12-28 17:22:53 +01:00
if (msender.isUsingAdminMode())
{
return true;
}
// 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())
{
msg("%s <b>doesn't use ranks sorry :(", targetFaction.getName() );
2014-12-28 17:22:53 +01:00
return false;
}
if (targetFaction != msenderFaction)
{
// 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)
{
// 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;
}
if (senderRank.isLessThan(rankReq))
2014-12-28 17:22:53 +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.
if (senderRank == targetRank)
2014-12-28 17:22:53 +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;
}
if (senderRank.isLessThan(targetRank))
2014-12-28 17:22:53 +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;
}
if (senderRank.isAtMost(rank) && senderRank != Rel.LEADER)
2014-12-28 17:22:53 +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;
}
// If it wasn't cancelled above, player is allowed.
2014-12-28 17:22:53 +01:00
return true;
}
private boolean isChangeRequired()
{
// 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"))
{
msg("%s <b>is already recruit.", target.describeTo(msender));
2014-12-28 17:22:53 +01:00
return false;
}
// 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"))
{
msg("%s <b>is already leader.", target.describeTo(msender));
2014-12-28 17:22:53 +01:00
return false;
}
// There must be a change, else it is all waste of time.
2014-12-28 17:22:53 +01:00
if (target.getRole() == rank)
{
msg("%s <b>already has that rank.", target.describeTo(msender));
2014-12-28 17:22:53 +01:00
return false;
}
2014-12-28 17:22:53 +01:00
return true;
}
private void changeRank()
{
// 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)
{
this.changeRankLeader();
2014-12-28 17:22:53 +01:00
}
else
{
this.changeRankOther();
2014-12-28 17:22:53 +01:00
}
}
private void changeRankLeader()
{
// If there is a current leader. Demote & inform them.
2014-12-28 17:22:53 +01:00
MPlayer targetFactionCurrentLeader = targetFaction.getLeader();
if (targetFactionCurrentLeader != null)
{
// Inform & demote the old leader.
2014-12-28 17:22:53 +01:00
targetFactionCurrentLeader.setRole(Rel.OFFICER);
if (targetFactionCurrentLeader != msender)
{
// 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-28 17:22:53 +01:00
}
// Inform & promote the new leader.
2014-12-28 17:22:53 +01:00
target.setRole(Rel.LEADER);
if (target != msender)
{
// 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-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
for (MPlayer recipient : MPlayerColl.get().getAllOnline())
2014-12-28 17:22:53 +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...
if (targetRank == Rel.LEADER && !MConf.get().permanentFactionsDisableLeaderPromotion && targetFaction.getFlag(MFlag.ID_PERMANENT))
2014-12-28 17:22:53 +01:00
{
// ...we must promote a new one.
2014-12-28 17:22:53 +01:00
targetFaction.promoteNewLeader();
}
// 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-28 17:22:53 +01:00
// Were they demoted or promoted?
String change = (rank.isLessThan(targetRank) ? "demoted" : "promoted");
// 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);
String oldRankName = Txt.getNicedEnum(targetRank).toLowerCase();
2014-12-28 17:22:53 +01:00
String rankName = Txt.getNicedEnum(rank).toLowerCase();
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
}
}