Java 1.6 compatibility and tweaks to the previous commit.

This commit is contained in:
Olof Larsson 2014-12-30 10:01:54 +01:00
parent 68fe1f2d28
commit 1f479bd699
3 changed files with 100 additions and 99 deletions

View File

@ -25,13 +25,13 @@ public class CmdFactionsRank extends FactionsCommand
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
//These fields are set upon perform() and unset afterwards
// These fields are set upon perform() and unset afterwards.
//Target
// Target
private Faction targetFaction = null;
private MPlayer target = null;
//Roles
// Roles
private Rel senderRole = null;
private Rel targetRole = null;
private Rel rank = null;
@ -46,7 +46,7 @@ public class CmdFactionsRank extends FactionsCommand
this.addAliases("r","rank");
// Args
this.addOptionalArg("player","you");
this.addOptionalArg("player", "you");
this.addOptionalArg("action", "show");
// Requirements
@ -60,51 +60,51 @@ public class CmdFactionsRank extends FactionsCommand
@Override
public void perform()
{
// This sets target and much other. Returns false if not succeeded
if(!registerFields())
// This sets target and much other. Returns false if not succeeded.
if ( ! this.registerFields())
{
return;
}
// Sometimes we just want to show the rank
if(!this.argIsSet(1))
// Sometimes we just want to show the rank.
if ( ! this.argIsSet(1))
{
if(!Perm.RANK_SHOW.has(sender, true))
if ( ! Perm.RANK_SHOW.has(sender, true))
{
return;
}
showRank();
this.showRank();
return;
}
// Permission check
if(!Perm.RANK_ACTION.has(sender, true))
if ( ! Perm.RANK_ACTION.has(sender, true))
{
return;
}
// Is the player allowed or not. Method can be found later down
if(!isPlayerAllowed())
// Is the player allowed or not. Method can be found later down.
if ( ! this.isPlayerAllowed())
{
return;
}
// Does the change make sense.
if(!this.isChangeRequired())
if ( ! this.isChangeRequired())
{
return;
}
// Should we fire an event when rank is changed?
// Currently we don't
// Currently we don't.
// Change the rank
changeRank();
this.changeRank();
}
// This is always run after performing a MassiveCommand
// It might be a bit hacky, but is easier than adding a line of code at every return statement
// Sometimes it is nice to know the exact mechanics of MassiveCore
// This is always run after performing a MassiveCommand.
// It might be a bit hacky, but is easier than adding a line of code at every return statement.
// Sometimes it is nice to know the exact mechanics of MassiveCore.
@Override
public void unsetSenderVars()
{
@ -127,10 +127,10 @@ public class CmdFactionsRank extends FactionsCommand
if (this.argIsSet(1))
{
rank = this.arg(1, ARRank.get(target.getRole()));
if(null == rank) return false;
if (null == rank) return false;
}
//Roles/ranks
// Roles/ranks
senderRole = msender.getRole();
targetRole = target.getRole();
@ -165,14 +165,14 @@ public class CmdFactionsRank extends FactionsCommand
// We can at least try to limit their powers
if (targetFaction.isNone())
{
msg( "<b>Wilderness doesn't use ranks sorry :(" );
msg("<b>Wilderness doesn't use ranks sorry :(");
return false;
}
if (targetFaction != msenderFaction)
{
//Don't change ranks outside of your faction
msg(Txt.parse( "%s <b>is not in the same faction as you" ,target.describeTo(msender)));
// Don't change ranks outside of your faction
msg(Txt.parse("%s <b>is not in the same faction as you", target.describeTo(msender)));
return false;
}
@ -214,47 +214,47 @@ public class CmdFactionsRank extends FactionsCommand
return false;
}
// If it wasn't cancelled above, player is allowe
// If it wasn't cancelled above, player is allowed.
return true;
}
private boolean isChangeRequired()
{
// Just a nice msg. It would however be caught by an if statement below
// Just a nice msg. It would however be caught by an if statement below.
if (target.getRole() == Rel.RECRUIT && arg(1).equalsIgnoreCase("demote"))
{
msg("<b>You can't demote a recruit");
return false;
}
// Just a nice msg. It would however be caught by an if statement below
// Just a nice msg. It would however be caught by an if statement below.
if (target.getRole() == Rel.LEADER && arg(1).equalsIgnoreCase("promote"))
{
msg("<b>You can't promote the leader");
return false;
}
// There must be a change, else it is all waste of time
// There must be a change, else it is all waste of time.
if (target.getRole() == rank)
{
msg("<b>Player already has that rank");
return false;
}
return true;
}
private void changeRank()
{
// In case of leadership change, we do special things not done in other rank changes
// In case of leadership change, we do special things not done in other rank changes.
if (rank == Rel.LEADER)
{
changeRankLeader();
this.changeRankLeader();
}
else
{
changeRankOther();
this.changeRankOther();
}
}
private void changeRankLeader()
@ -265,14 +265,20 @@ public class CmdFactionsRank extends FactionsCommand
{
// Inform & demote the old leader
targetFactionCurrentLeader.setRole(Rel.OFFICER);
if (targetFactionCurrentLeader != msender) //They kinda know if they fired the command themself
if (targetFactionCurrentLeader != msender)
{
// They kinda know if they fired the command themself
targetFactionCurrentLeader.msg("<i>You have been demoted from the position of faction leader by %s<i>.", msender.describeTo(targetFactionCurrentLeader, true));
}
}
// Inform & promote the new leader
target.setRole(Rel.LEADER);
if (target != msender) //They kinda know if they fired the command themself
if (target != msender)
{
// They kinda know if they fired the command themself
target.msg("<i>You have been promoted to the position of faction leader by %s<i>.", msender.describeTo(target, true));
}
// Inform the msg sender
msg("<i>You have promoted %s<i> to the position of faction leader.", target.describeTo(msender, true));
@ -288,15 +294,16 @@ public class CmdFactionsRank extends FactionsCommand
private void changeRankOther()
{
// If the target is currently the leader and faction isn't permanent...
if(targetRole == Rel.LEADER && (!MConf.get().permanentFactionsDisableLeaderPromotion
|| !targetFaction.getFlag(MFlag.ID_PERMANENT)))
if (targetRole == Rel.LEADER && (!MConf.get().permanentFactionsDisableLeaderPromotion || !targetFaction.getFlag(MFlag.ID_PERMANENT)))
{
// ...we must promote a new one
targetFaction.promoteNewLeader();
}
// Were they demoted or promoted?
String change = (rank.isLessThan(targetRole) ? "demoted" : "promoted");
// The rank will be set before the msg, so they have the appropriate prefix
// The rank will be set before the msg, so they have the appropriate prefix.
target.setRole(rank);
String rankName = Txt.getNicedEnum(rank).toLowerCase();
msenderFaction.msg("%s<i> was %s to being %s %s in your faction.", target.describeTo(msenderFaction, true), change, Txt.aan(rankName), rankName);

View File

@ -10,7 +10,7 @@ public class CmdFactionsRankOld extends FactionsCommand
// FIELDS
// -------------------------------------------- //
final String rankName;
public final String rankName;
// -------------------------------------------- //
// CONSTRUCT
@ -18,6 +18,7 @@ public class CmdFactionsRankOld extends FactionsCommand
public CmdFactionsRankOld(String rank)
{
// Fields
this.rankName = rank.toLowerCase();
// Aliases
@ -26,6 +27,7 @@ public class CmdFactionsRankOld extends FactionsCommand
// Args
this.addRequiredArg("player");
// VisibilityMode
this.setVisibilityMode(VisibilityMode.INVISIBLE);
}
@ -36,6 +38,7 @@ public class CmdFactionsRankOld extends FactionsCommand
@Override
public void perform()
{
Factions.get().getOuterCmdFactions().cmdFactionsRank.execute(sender, MUtil.list(arg(0),rankName));
Factions.get().getOuterCmdFactions().cmdFactionsRank.execute(sender, MUtil.list(this.arg(0), this.rankName));
}
}

View File

@ -12,11 +12,11 @@ import com.massivecraft.massivecore.util.Txt;
public class ARRank extends ARAbstractSelect<Rel>
{
//----------------------------------------------//
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
//Default constructor. Can't use promote and demote.
// Default constructor. Can't use promote and demote.
private static ARRank i = new ARRank();
public static ARRank get() { return i; }
@ -25,7 +25,7 @@ public class ARRank extends ARAbstractSelect<Rel>
this.startRank = null;
}
//Fancy constructor. Can use promote and demote
// Fancy constructor. Can use promote and demote.
public static ARRank get(Rel rank) { return new ARRank(rank); }
public ARRank(Rel rank)
@ -33,11 +33,12 @@ public class ARRank extends ARAbstractSelect<Rel>
this.startRank = rank;
}
//----------------------------------------------//
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
final Rel startRank;
private final Rel startRank;
public Rel getStartRank() { return this.startRank; }
// -------------------------------------------- //
// OVERRIDE
@ -52,67 +53,57 @@ public class ARRank extends ARAbstractSelect<Rel>
@Override
public Rel select(String arg, CommandSender sender)
{
//Default it is nothing
Rel ret = null;
// This is especially useful when one rank can have aliases.
// In the case of promote/demote,
// that would require 10 lines of code repeated for each alias.
arg = this.prepareArg(arg);
switch(arg)
// All the normal ranks
if (arg.equals("leader")) return Rel.LEADER;
if (arg.equals("officer")) return Rel.OFFICER;
if (arg.equals("member")) return Rel.MEMBER;
if (arg.equals("recruit")) return Rel.RECRUIT;
// No start rank?
if (startRank == null)
{
// All the normal ranks
case "leader": ret = Rel.LEADER; break;
case "officer": ret = Rel.OFFICER; break;
case "member": ret = Rel.MEMBER; break;
case "recruit": ret = Rel.RECRUIT; break;
// Promote
case "promote":
switch(startRank)
{
case LEADER : ret = Rel.LEADER; break;
case OFFICER : ret = Rel.LEADER; break;
case MEMBER : ret = Rel.OFFICER; break;
case RECRUIT : ret = Rel.MEMBER; break;
// This should not happen
default:
//This might happen of the default constrcutor is used
Mixin.msgOne(sender, Txt.parse("<b>You can't use promote & demote"));
ret = null; break;
} break;
// Demote
case "demote":
switch(startRank)
{
case LEADER : ret = Rel.OFFICER; break;
case OFFICER : ret = Rel.MEMBER; break;
case MEMBER : ret = Rel.RECRUIT; break;
case RECRUIT : ret = Rel.RECRUIT; break;
// This should not happen
default:
//This might happen of the default constrcutor is used
Mixin.msgOne(sender, Txt.parse("<b>You can't use promote & demote"));
ret = null; break;
} break;
// This might happen of the default constructor is used
Mixin.msgOne(sender, Txt.parse("<b>You can't use promote & demote"));
return null;
}
return ret;
// Promote
if (arg.equals("promote"))
{
if (Rel.LEADER.equals(startRank)) return Rel.LEADER;
if (Rel.OFFICER.equals(startRank)) return Rel.LEADER;
if (Rel.MEMBER.equals(startRank)) return Rel.OFFICER;
if (Rel.RECRUIT.equals(startRank)) return Rel.MEMBER;
}
// Demote
if (arg.equals("demote"))
{
if (Rel.LEADER.equals(startRank)) return Rel.OFFICER;
if (Rel.OFFICER.equals(startRank)) return Rel.MEMBER;
if (Rel.MEMBER.equals(startRank)) return Rel.RECRUIT;
if (Rel.RECRUIT.equals(startRank)) return Rel.RECRUIT;
}
return null;
}
@Override
public Collection<String> altNames(CommandSender sender)
{
return MUtil.list(
Txt.getNicedEnum(Rel.LEADER),
Txt.getNicedEnum(Rel.OFFICER),
Txt.getNicedEnum(Rel.MEMBER),
Txt.getNicedEnum(Rel.RECRUIT),
"Promote",
"Demote");
Txt.getNicedEnum(Rel.LEADER),
Txt.getNicedEnum(Rel.OFFICER),
Txt.getNicedEnum(Rel.MEMBER),
Txt.getNicedEnum(Rel.RECRUIT),
"Promote",
"Demote"
);
}
// -------------------------------------------- //
@ -121,29 +112,29 @@ public class ARRank extends ARAbstractSelect<Rel>
private String prepareArg(String str)
{
String ret = str;
String ret = str.toLowerCase();
if (str.startsWith("admin") || str.startsWith("lea"))
if (ret.startsWith("admin") || ret.startsWith("lea"))
{
ret = "leader";
}
else if (str.startsWith("mod") || str.startsWith("off"))
else if (ret.startsWith("mod") || ret.startsWith("off"))
{
ret = "officer";
}
else if (str.startsWith("mem"))
else if (ret.startsWith("mem"))
{
ret = "member";
}
else if (str.startsWith("rec"))
else if (ret.startsWith("rec"))
{
ret = "recruit";
}
else if (str.startsWith("+") || str.startsWith("plus"))
else if (ret.startsWith("+") || ret.startsWith("plus") || ret.startsWith("up"))
{
ret = "promote";
}
else if (str.startsWith("-") || str.startsWith("minus"))
else if (ret.startsWith("-") || ret.startsWith("minus") || ret.startsWith("down"))
{
ret = "demote";
}