Rank rework

This makes an under the hood rank-rework. Nothing is changed from the player perspective.
This commit is contained in:
Magnus Ulf
2018-12-20 01:35:30 +01:00
parent d7728f3a79
commit f347c1058a
33 changed files with 972 additions and 630 deletions

View File

@@ -39,8 +39,6 @@ public class CmdFactions extends FactionsCommand
public CmdFactionsKick cmdFactionsKick = new CmdFactionsKick();
public CmdFactionsTitle cmdFactionsTitle = new CmdFactionsTitle();
public CmdFactionsRank cmdFactionsRank = new CmdFactionsRank();
public CmdFactionsRankOld cmdFactionsRankOldPromote = new CmdFactionsRankOld("promote");
public CmdFactionsRankOld cmdFactionsRankOldDemote = new CmdFactionsRankOld("demote");
public CmdFactionsMoney cmdFactionsMoney = new CmdFactionsMoney();
public CmdFactionsSeeChunk cmdFactionsSeeChunk = new CmdFactionsSeeChunk();
public CmdFactionsSeeChunkOld cmdFactionsSeeChunkOld = new CmdFactionsSeeChunkOld();
@@ -77,7 +75,7 @@ public class CmdFactions extends FactionsCommand
this.addChild(new MassiveCommandDeprecated(this.cmdFactionsUnclaim.cmdFactionsUnclaimAll, "unclaimall"));
this.addChild(new MassiveCommandDeprecated(this.cmdFactionsFlag, "open"));
this.addChild(new MassiveCommandDeprecated(this.cmdFactionsFaction, "show", "who"));
this.addChild(new MassiveCommandDeprecated(this.cmdFactionsRank, "leader", "owner", "officer", "moderator"));
this.addChild(new MassiveCommandDeprecated(this.cmdFactionsRank, "leader", "owner", "officer", "moderator", "demote", "promote"));
}
// -------------------------------------------- //

View File

@@ -1,7 +1,6 @@
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.Rel;
import com.massivecraft.factions.cmd.req.ReqHasntFaction;
import com.massivecraft.factions.cmd.type.TypeFactionNameStrict;
import com.massivecraft.factions.entity.Faction;
@@ -55,7 +54,7 @@ public class CmdFactionsCreate extends FactionsCommand
Faction faction = FactionColl.get().create(factionId);
faction.setName(newName);
msender.setRole(Rel.LEADER);
msender.setRank(faction.getLeaderRank());
msender.setFaction(faction);
EventFactionsMembershipChange joinEvent = new EventFactionsMembershipChange(sender, msender, faction, MembershipChangeReason.CREATE);

View File

@@ -116,6 +116,7 @@ public class CmdFactionsJoin extends FactionsCommand
// Apply
mplayer.resetFactionData();
mplayer.setFaction(faction);
mplayer.setRank(faction.getLowestRank());
faction.uninvite(mplayer);

View File

@@ -1,7 +1,6 @@
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.Rel;
import com.massivecraft.factions.cmd.type.TypeMPlayer;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.FactionColl;
@@ -44,17 +43,17 @@ public class CmdFactionsKick extends FactionsCommand
return;
}
if (mplayer.getRole() == Rel.LEADER && !msender.isOverriding())
if (mplayer.getRank().isLeader() && !msender.isOverriding())
{
throw new MassiveException().addMsg("<b>The leader cannot be kicked.");
}
if (mplayer.getRole().isMoreThan(msender.getRole()) && ! msender.isOverriding())
if (mplayer.getRank().isMoreThan(msender.getRank()) && ! msender.isOverriding())
{
throw new MassiveException().addMsg("<b>You can't kick people of higher rank than yourself.");
}
if (mplayer.getRole() == msender.getRole() && ! msender.isOverriding())
if (mplayer.getRank() == msender.getRank() && ! msender.isOverriding())
{
throw new MassiveException().addMsg("<b>You can't kick people of the same rank as yourself.");
}
@@ -88,7 +87,7 @@ public class CmdFactionsKick extends FactionsCommand
}
// Apply
if (mplayer.getRole() == Rel.LEADER)
if (mplayer.getRank().isLeader())
{
mplayerFaction.promoteNewLeader();
}

View File

@@ -1,10 +1,10 @@
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.Rel;
import com.massivecraft.factions.cmd.type.TypeFaction;
import com.massivecraft.factions.cmd.type.TypeMPerm;
import com.massivecraft.factions.cmd.type.TypeRel;
import com.massivecraft.factions.cmd.type.TypeMPermable;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.MConf;
import com.massivecraft.factions.entity.MPerm;
import com.massivecraft.factions.entity.MPlayer;
import com.massivecraft.factions.event.EventFactionsPermChange;
@@ -25,7 +25,7 @@ public class CmdFactionsPermSet extends FactionsCommand
{
// Parameters
this.addParameter(TypeMPerm.get(), "perm");
this.addParameter(TypeRel.get(), "relation");
this.addParameter(TypeMPermable.get(), "relation");
this.addParameter(TypeBooleanYes.get(), "yes/no");
this.addParameter(TypeFaction.get(), "faction", "you");
}
@@ -38,10 +38,12 @@ public class CmdFactionsPermSet extends FactionsCommand
public void perform() throws MassiveException
{
// Args
MPerm perm = this.readArg();
Rel rel = this.readArg();
Boolean value = this.readArg();
Faction faction = this.readArg(msenderFaction);
MPerm perm = this.readArgAt(0);
Boolean value = this.readArgAt(2);
Faction faction = this.readArgAt(3, msenderFaction);
MPerm.MPermable permable = TypeMPermable.get(faction).read(this.argAt(1), sender);
// Do the sender have the right to change perms for this faction?
if ( ! MPerm.getPermPerms().has(msender, faction, true)) return;
@@ -54,25 +56,26 @@ public class CmdFactionsPermSet extends FactionsCommand
}
// Event
EventFactionsPermChange event = new EventFactionsPermChange(sender, faction, perm, rel, value);
EventFactionsPermChange event = new EventFactionsPermChange(sender, faction, perm, permable, value);
event.run();
if (event.isCancelled()) return;
value = event.getNewValue();
// Apply
boolean change = faction.setPermitted(permable, perm, value);
// No change
if (faction.getPermitted(perm).contains(rel) == value)
if (!change)
{
msg("%s <i>already has %s <i>set to %s <i>for %s<i>.", faction.describeTo(msender), perm.getDesc(true, false), Txt.parse(value ? "<g>YES" : "<b>NOO"), rel.getColor() + rel.getDescPlayerMany());
msg("%s <i>already has %s <i>set to %s <i>for %s<i>.", faction.describeTo(msender), perm.getDesc(true, false), Txt.parse(value ? "<g>YES" : "<b>NOO"), permable.getColor() + permable.getName() + "s");
return;
}
// Apply
faction.setRelationPermitted(perm, rel, value);
// The following is to make sure the leader always has the right to change perms if that is our goal.
if (perm == MPerm.getPermPerms() && MPerm.getPermPerms().getStandard().contains(Rel.LEADER))
if (perm == MPerm.getPermPerms() && MConf.get().defaultPermsLeader.contains(MPerm.ID_PERMS))
{
faction.setRelationPermitted(MPerm.getPermPerms(), Rel.LEADER, true);
faction.setPermitted( faction.getLeaderRank(), MPerm.getPermPerms(), true);
}
// Create messages
@@ -80,8 +83,8 @@ public class CmdFactionsPermSet extends FactionsCommand
// Inform sender
messages.add(Txt.titleize("Perm for " + faction.describeTo(msender, true)));
messages.add(MPerm.getStateHeaders());
messages.add(Txt.parse(perm.getStateInfo(faction.getPermitted(perm), true)));
messages.add(MPerm.getStateHeaders(faction));
messages.add(Txt.parse(perm.getStateInfo(faction, true)));
message(messages);
// Inform faction (their message is slighly different)

View File

@@ -40,10 +40,10 @@ public class CmdFactionsPermShow extends FactionsCommand
List<Object> messages = new ArrayList<>();
messages.add(Txt.titleize("Perm for " + faction.describeTo(msender, true)));
messages.add(MPerm.getStateHeaders());
messages.add(MPerm.getStateHeaders(faction));
for (MPerm mperm : mperms)
{
messages.add(Txt.parse(mperm.getStateInfo(faction.getPermitted(mperm), true)));
messages.add(Txt.parse(mperm.getStateInfo(faction, true)));
}
// Send messages

View File

@@ -2,15 +2,16 @@ package com.massivecraft.factions.cmd;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.Perm;
import com.massivecraft.factions.Rel;
import com.massivecraft.factions.cmd.type.TypeFaction;
import com.massivecraft.factions.cmd.type.TypeMPlayer;
import com.massivecraft.factions.cmd.type.TypeRank;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.MConf;
import com.massivecraft.factions.entity.MFlag;
import com.massivecraft.factions.entity.MPerm;
import com.massivecraft.factions.entity.MPlayer;
import com.massivecraft.factions.entity.MPlayerColl;
import com.massivecraft.factions.entity.Rank;
import com.massivecraft.factions.event.EventFactionsMembershipChange;
import com.massivecraft.factions.event.EventFactionsMembershipChange.MembershipChangeReason;
import com.massivecraft.factions.event.EventFactionsRankChange;
@@ -22,13 +23,6 @@ import java.util.Set;
public class CmdFactionsRank extends FactionsCommand
{
// -------------------------------------------- //
// CONSTANTS
// -------------------------------------------- //
// The rank required to do any rank changes.
final static Rel rankReq = Rel.OFFICER;
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
@@ -43,9 +37,9 @@ public class CmdFactionsRank extends FactionsCommand
private boolean factionChange = false;
// Ranks
private Rel senderRank = null;
private Rel targetRank = null;
private Rel rank = null;
private Rank senderRank = null;
private Rank targetRank = null;
private Rank rank = null;
// -------------------------------------------- //
// CONSTRUCT
@@ -55,8 +49,11 @@ public class CmdFactionsRank extends FactionsCommand
{
// Parameters
this.addParameter(TypeMPlayer.get(), "player");
this.addParameter(TypeRank.get(), "action", "show");
this.addParameter(TypeRank.get(), "rank", "show");
this.addParameter(TypeFaction.get(), "faction", "their");
// Too complicated for that
this.setSwapping(false);
}
// -------------------------------------------- //
@@ -125,19 +122,18 @@ public class CmdFactionsRank extends FactionsCommand
// Ranks
senderRank = msender.getRole();
targetRank = target.getRole();
senderRank = msender.getRank();
targetRank = target.getRank();
endFaction = this.readArgAt(2, targetFaction);
factionChange = (endFaction != targetFaction);
// Rank if any passed.
if (this.argIsSet(1))
{
this.setParameterType(1, TypeRank.get(targetRank));
rank = this.readArg();
TypeRank typeRank = new TypeRank(endFaction);
rank = typeRank.read(this.argAt(1), sender);
}
// Changing peoples faction.
endFaction = this.readArgAt(2, targetFaction);
factionChange = (endFaction != targetFaction);
}
@@ -145,6 +141,9 @@ public class CmdFactionsRank extends FactionsCommand
{
targetFaction = null;
target = null;
endFaction = null;
factionChange = false;
senderRank = null;
targetRank = null;
@@ -164,7 +163,7 @@ public class CmdFactionsRank extends FactionsCommand
// We can at least try to limit their powers.
if (endFaction.isNone())
{
throw new MassiveException().addMsg("%s <b>doesn't use ranks sorry :(", targetFaction.getName() );
throw new MassiveException().addMsg("%s <b>doesn't use ranks sorry :(", endFaction.getName());
}
if (target == msender)
@@ -172,23 +171,16 @@ public class CmdFactionsRank extends FactionsCommand
// Don't change your own rank.
throw new MassiveException().addMsg("<b>The target player mustn't be yourself.");
}
if (targetFaction != msenderFaction)
{
// Don't change ranks outside of your faction.
throw new MassiveException().addMsg("%s <b>is not in the same faction as you.", target.describeTo(msender, true));
}
if (factionChange)
{
// Don't change peoples faction
throw new MassiveException().addMsg("<b>You can't change %s's <b>faction.", target.describeTo(msender));
}
if (senderRank.isLessThan(rankReq))
if (!MPerm.getPermRank().has(msender, targetFaction, false))
{
// You need a specific rank to change ranks.
throw new MassiveException().addMsg("<b>You must be <h>%s <b>or higher to change ranks.", Txt.getNicedEnum(rankReq).toLowerCase());
throw new MassiveException().addMessage(MPerm.getPermRank().createDeniedMessage(msender, targetFaction));
}
// The following two if statements could be merged.
@@ -196,7 +188,7 @@ public class CmdFactionsRank extends FactionsCommand
if (senderRank == targetRank)
{
// You can't change someones rank if it is equal to yours.
throw new MassiveException().addMsg("<h>%s <b>can't manage eachother.", Txt.getNicedEnum(rankReq)+"s");
throw new MassiveException().addMsg("<h>%s <b>can't manage eachother.", senderRank.getName()+"s");
}
if (senderRank.isLessThan(targetRank))
@@ -207,7 +199,7 @@ public class CmdFactionsRank extends FactionsCommand
// The following two if statements could be merged.
// But isn't for the sake of nicer error messages.
if (senderRank == rank && senderRank != Rel.LEADER)
if (senderRank == rank && !senderRank.isLeader())
{
// You can't set ranks equal to your own. Unless you are the leader.
throw new MassiveException().addMsg("<b>You can't set ranks equal to your own.");
@@ -223,9 +215,9 @@ public class CmdFactionsRank extends FactionsCommand
private void ensureMakesSense() throws MassiveException
{
// Don't change their rank to something they already are.
if (target.getRole() == rank)
if (target.getRank() == rank)
{
throw new MassiveException().addMsg("%s <b>is already %s.", target.describeTo(msender), rank.getDescPlayerOne());
throw new MassiveException().addMsg("%s <b>is already %s %s.", target.describeTo(msender), Txt.aan(rank.getName()), rank.getName());
}
}
@@ -238,9 +230,10 @@ public class CmdFactionsRank extends FactionsCommand
// Damn you grammar, causing all these checks.
String targetName = target.describeTo(msender, true);
String isAre = (target == msender) ? "are" : "is"; // "you are" or "he is"
String theAan = (targetRank == Rel.LEADER) ? "the" : Txt.aan(targetRank.name()); // "a member", "an officer" or "the leader"
String rankName = Txt.getNicedEnum(targetRank).toLowerCase();
String ofIn = (targetRank == Rel.LEADER) ? "of" : "in"; // "member in" or "leader of"
String theAan = (targetRank.isLeader()) ? "the" : Txt.aan(targetRank.getName()); // "a member", "an officer" or "the leader"
String rankName = targetRank.getName().toLowerCase();
String ofIn = (targetRank.isLeader()) ? "of" : "in"; // "member in" or "leader of"
String factionName = targetFaction.describeTo(msender, true);
if (targetFaction == msenderFaction)
{
@@ -266,7 +259,7 @@ public class CmdFactionsRank extends FactionsCommand
private void changeFaction() throws MassiveException
{
// Don't change a leader to a new faction.
if (targetRank == Rel.LEADER)
if (targetRank.isLeader())
{
throw new MassiveException().addMsg("<b>You cannot remove the present leader. Demote them first.");
}
@@ -303,8 +296,9 @@ public class CmdFactionsRank extends FactionsCommand
// Now we don't need the old values.
targetFaction = target.getFaction();
targetRank = target.getRole();
senderRank = msender.getRole(); // In case they changed their own rank
targetRank = target.getRank();
senderRank = msender.getRank(); // In case they changed their own rank
}
// -------------------------------------------- //
@@ -314,7 +308,7 @@ public class CmdFactionsRank extends FactionsCommand
private void changeRank() throws MassiveException
{
// In case of leadership change, we do special things not done in other rank changes.
if (rank == Rel.LEADER)
if (rank.isLeader())
{
this.changeRankLeader();
}
@@ -331,7 +325,7 @@ public class CmdFactionsRank extends FactionsCommand
if (targetFactionCurrentLeader != null)
{
// Inform & demote the old leader.
targetFactionCurrentLeader.setRole(Rel.OFFICER);
targetFactionCurrentLeader.setRank(rank.getRankBelow());
if (targetFactionCurrentLeader != msender)
{
// They kinda know if they fired the command themself.
@@ -340,7 +334,7 @@ public class CmdFactionsRank extends FactionsCommand
}
// Promote the new leader.
target.setRole(Rel.LEADER);
target.setRank(rank);
// Inform everyone, this includes sender and target.
for (MPlayer recipient : MPlayerColl.get().getAllOnline())
@@ -354,7 +348,7 @@ public class CmdFactionsRank extends FactionsCommand
{
// If the target is currently the leader and faction isn't permanent a new leader should be promoted.
// Sometimes a bug occurs and multiple leaders exist. Then we should be able to demote without promoting new leader
if (targetRank == Rel.LEADER && ( ! MConf.get().permanentFactionsDisableLeaderPromotion || ! targetFaction.getFlag(MFlag.ID_PERMANENT)) && targetFaction.getMPlayersWhereRole(Rel.LEADER).size() == 1)
if (targetRank.isLeader() && ( ! MConf.get().permanentFactionsDisableLeaderPromotion || ! targetFaction.getFlag(MFlag.ID_PERMANENT)) && targetFaction.getMPlayersWhereRank(targetFaction.getLeaderRank()).size() == 1)
// This if statement is very long. Should I nest it for readability?
{
targetFaction.promoteNewLeader(); // This might disband the faction.
@@ -377,9 +371,9 @@ public class CmdFactionsRank extends FactionsCommand
String change = (rank.isLessThan(targetRank) ? "demoted" : "promoted");
// The rank will be set before the msg, so they have the appropriate prefix.
target.setRole(rank);
String oldRankName = Txt.getNicedEnum(targetRank).toLowerCase();
String rankName = Txt.getNicedEnum(rank).toLowerCase();
target.setRank(rank);
String oldRankName = targetRank.getName().toLowerCase();
String rankName = rank.getName().toLowerCase();
// Send message
for(MPlayer recipient : recipients)

View File

@@ -1,51 +0,0 @@
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.cmd.type.TypeFaction;
import com.massivecraft.factions.cmd.type.TypeMPlayer;
import com.massivecraft.massivecore.command.Visibility;
import com.massivecraft.massivecore.util.MUtil;
public class CmdFactionsRankOld extends FactionsCommand
{
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
public final String rankName;
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public CmdFactionsRankOld(String rank)
{
// Fields
this.rankName = rank.toLowerCase();
this.setSetupEnabled(false);
// Aliases
this.addAliases(rankName);
// Parameters
this.addParameter(TypeMPlayer.get(), "player");
this.addParameter(TypeFaction.get(), "faction", "their");
// Visibility
this.setVisibility(Visibility.INVISIBLE);
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public void perform()
{
CmdFactions.get().cmdFactionsRank.execute(sender, MUtil.list(
this.argAt(0),
this.rankName,
this.argAt(1)
));
}
}

View File

@@ -44,7 +44,7 @@ public class CmdFactionsTitle extends FactionsCommand
if ( ! MPerm.getPermTitle().has(msender, you.getFaction(), true)) return;
// Rank Check
if (!msender.isOverriding() && you.getRole().isMoreThan(msender.getRole()))
if (!msender.isOverriding() && you.getRank().isMoreThan(msender.getRank()))
{
msg("<b>You can not edit titles for higher ranks.");
return;

View File

@@ -1,52 +1,52 @@
package com.massivecraft.factions.cmd.req;
import com.massivecraft.factions.Rel;
import com.massivecraft.factions.entity.MPlayer;
import com.massivecraft.massivecore.command.MassiveCommand;
import com.massivecraft.massivecore.command.requirement.RequirementAbstract;
import com.massivecraft.massivecore.util.MUtil;
import com.massivecraft.massivecore.util.Txt;
import org.bukkit.command.CommandSender;
public class ReqRoleIsAtLeast extends RequirementAbstract
{
// -------------------------------------------- //
// SERIALIZABLE
// -------------------------------------------- //
private static final long serialVersionUID = 1L;
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private final Rel rel;
public Rel getRel() { return this.rel; }
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
public static ReqRoleIsAtLeast get(Rel rel) { return new ReqRoleIsAtLeast(rel); }
private ReqRoleIsAtLeast(Rel rel) { this.rel = rel; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public boolean apply(CommandSender sender, MassiveCommand command)
{
if (MUtil.isntSender(sender)) return false;
MPlayer mplayer = MPlayer.get(sender);
return mplayer.getRole().isAtLeast(this.getRel());
}
@Override
public String createErrorMessage(CommandSender sender, MassiveCommand command)
{
return Txt.parse("<b>You must be <h>%s <b>or higher to %s.", Txt.getNicedEnum(this.getRel()), getDesc(command));
}
}
package com.massivecraft.factions.cmd.req;
import com.massivecraft.factions.entity.MPlayer;
import com.massivecraft.factions.entity.Rank;
import com.massivecraft.massivecore.command.MassiveCommand;
import com.massivecraft.massivecore.command.requirement.RequirementAbstract;
import com.massivecraft.massivecore.util.MUtil;
import com.massivecraft.massivecore.util.Txt;
import org.bukkit.command.CommandSender;
public class ReqRankIsAtLeast extends RequirementAbstract
{
// -------------------------------------------- //
// SERIALIZABLE
// -------------------------------------------- //
private static final long serialVersionUID = 1L;
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private final Rank rank;
public Rank getRank() { return this.rank; }
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
public static ReqRankIsAtLeast get(Rank rank) { return new ReqRankIsAtLeast(rank); }
private ReqRankIsAtLeast(Rank rank) { this.rank = rank; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public boolean apply(CommandSender sender, MassiveCommand command)
{
if (MUtil.isntSender(sender)) return false;
MPlayer mplayer = MPlayer.get(sender);
return mplayer.getRank().isAtLeast(this.getRank());
}
@Override
public String createErrorMessage(CommandSender sender, MassiveCommand command)
{
return Txt.parse("<b>You must be <h>%s <b>or higher to %s.", rank.getName(), getDesc(command));
}
}

View File

@@ -0,0 +1,46 @@
package com.massivecraft.factions.cmd.type;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.MPerm;
import com.massivecraft.factions.entity.Rank;
import com.massivecraft.massivecore.command.type.TypeAbstractChoice;
import java.util.Collections;
public class TypeMPermable extends TypeAbstractChoice<MPerm.MPermable>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static TypeMPermable i = new TypeMPermable();
public static TypeMPermable get() { return i; }
private TypeMPermable()
{
super(Rank.class);
this.faction = null;
this.setAll(Collections.emptyList());
}
public static TypeMPermable get(Faction faction) { return new TypeMPermable(faction); }
public TypeMPermable(Faction faction)
{
super(MPerm.MPermable.class);
if (faction == null) throw new NullPointerException("faction");
this.faction = faction;
var permables = MPerm.getPermables(faction);
this.setAll(permables);
}
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private final Faction faction;
public Faction getFaction() { return this.faction; }
}

View File

@@ -1,128 +1,44 @@
package com.massivecraft.factions.cmd.type;
import com.massivecraft.factions.Rel;
import com.massivecraft.massivecore.collections.MassiveMap;
import com.massivecraft.massivecore.collections.MassiveSet;
import com.massivecraft.massivecore.command.type.enumeration.TypeEnum;
import com.massivecraft.massivecore.util.MUtil;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.Rank;
import com.massivecraft.massivecore.command.type.TypeAbstractChoice;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class TypeRank extends TypeEnum<Rel>
public class TypeRank extends TypeAbstractChoice<Rank>
{
// -------------------------------------------- //
// CONSTANTS
// -------------------------------------------- //
public static final Set<String> NAMES_PROMOTE = new MassiveSet<>(
"Promote",
"+",
"Plus",
"Up"
);
public static final Set<String> NAMES_DEMOTE = new MassiveSet<>(
"Demote",
"-",
"Minus",
"Down"
);
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
// Because of the caching in TypeAbstractChoice, we want only one of each instance.
// Null instance, doesn't allow promote and demote.
private static final TypeRank i = new TypeRank(null);
private static TypeRank i = new TypeRank();
public static TypeRank get() { return i; }
// Cached instances, does allow promote and demote.
private static final Map<Rel, TypeRank> instances;
static
private TypeRank()
{
Map<Rel, TypeRank> result = new MassiveMap<>();
for (Rel rel : Rel.values())
{
if ( ! rel.isRank()) continue;
result.put(rel, new TypeRank(rel));
}
result.put(null, i);
instances = Collections.unmodifiableMap(result);
super(Rank.class);
this.faction = null;
this.setAll(Collections.emptyList());
}
public static TypeRank get(Rel rank) { return instances.get(rank); }
// Constructor
public TypeRank(Rel rank)
public static TypeRank get(Faction faction) { return new TypeRank(faction); }
public TypeRank(Faction faction)
{
super(Rel.class);
if (rank != null && ! rank.isRank()) throw new IllegalArgumentException(rank + " is not a valid rank");
this.startRank = rank;
if (faction == null) throw new NullPointerException("faction");
this.faction = faction;
// Do setAll with only ranks.
List<Rel> all = MUtil.list(Rel.values());
for (Iterator<Rel> it = all.iterator(); it.hasNext(); )
{
if ( ! it.next().isRank()) it.remove();
}
this.setAll(all);
this.setAll(faction.getRanks().getAll());
}
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
// This must be final, for caching in TypeAbstractChoice to work.
private final Rel startRank;
public Rel getStartRank() { return this.startRank; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public String getName()
{
return "rank";
}
@Override
public String getNameInner(Rel value)
{
return value.getName();
}
@Override
public Set<String> getNamesInner(Rel value)
{
// Create
Set<String> ret = new MassiveSet<>();
// Fill Exact
ret.addAll(value.getNames());
// Fill Relative
Rel start = this.getStartRank();
if (start != null)
{
if (value == Rel.LEADER && start == Rel.OFFICER) ret.addAll(NAMES_PROMOTE);
if (value == Rel.OFFICER && start == Rel.MEMBER) ret.addAll(NAMES_PROMOTE);
if (value == Rel.OFFICER && start == Rel.LEADER) ret.addAll(NAMES_DEMOTE);
if (value == Rel.MEMBER && start == Rel.RECRUIT) ret.addAll(NAMES_PROMOTE);
if (value == Rel.MEMBER && start == Rel.OFFICER) ret.addAll(NAMES_DEMOTE);
if (value == Rel.RECRUIT && start == Rel.MEMBER) ret.addAll(NAMES_DEMOTE);
}
// Return
return ret;
}
private final Faction faction;
public Faction getFaction() { return this.faction; }
}