Re-add promote/demote options to rank set

This commit is contained in:
Magnus Ulf 2019-03-06 22:35:54 +01:00
parent abc27821f4
commit bcae649e0e
2 changed files with 72 additions and 6 deletions

View File

@ -105,7 +105,7 @@ public class CmdFactionsRankSet extends FactionsCommand
private void registerFields() throws MassiveException
{
// Getting the target and faction.
target = this.readArg(msender);
target = this.readArg();
targetFaction = target.getFaction();
@ -117,7 +117,7 @@ public class CmdFactionsRankSet extends FactionsCommand
factionChange = (endFaction != targetFaction);
// Rank if any passed.
TypeRank typeRank = new TypeRank(endFaction);
TypeRank typeRank = new TypeRank(endFaction, target.getRank());
rank = typeRank.read(this.argAt(1), sender);
}

View File

@ -2,9 +2,12 @@ package com.massivecraft.factions.cmd.type;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.Rank;
import com.massivecraft.massivecore.util.MUtil;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.collections.MassiveSet;
import org.bukkit.command.CommandSender;
import java.util.Collection;
import java.util.List;
import java.util.Set;
public class TypeRank extends TypeEntityInternalFaction<Rank>
@ -18,14 +21,28 @@ public class TypeRank extends TypeEntityInternalFaction<Rank>
private TypeRank()
{
super(Rank.class);
this.currentRank = null;
}
public static TypeRank get(Faction faction) { return new TypeRank(faction); }
public TypeRank(Faction faction)
@Deprecated public static TypeRank get(Faction faction) { return new TypeRank(faction, null); }
@Deprecated public TypeRank(Faction faction)
{
this(faction, null);
}
public static TypeRank get(Faction faction, Rank rank) { return new TypeRank(faction, rank); }
public TypeRank(Faction faction, Rank rank)
{
super(Rank.class, faction);
this.currentRank = rank;
// When setAll is done in the super constructor some optimisations are done
// which don't take the promote/demote thing into account.
this.setAll(this.getAll(faction));
}
private final Rank currentRank;
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@ -36,10 +53,59 @@ public class TypeRank extends TypeEntityInternalFaction<Rank>
return faction.getRanks().getAll();
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
List<String> ret = new MassiveList<>(super.getTabList(sender, arg));
ret.add("promote");
ret.add("demote");
return ret;
}
@Override
public Set<String> getNamesInner(Rank value)
{
return MUtil.set(value.getName(), value.getPrefix() + value.getName());
Set<String> names = new MassiveSet<>();
names.add(value.getName());
if (this.currentRank != null)
{
// You can't use "promote" to make someone leader.
Rank promote = getPromote(currentRank);
if (value == promote && !promote.isLeader()) names.add("promote");
if (value == getDemote(currentRank)) names.add("demote");
}
return names;
}
private static Rank getPromote(Rank rank)
{
Rank ret = null;
for (Rank r : rank.getFaction().getRanks().getAll())
{
if (rank == r) continue;
if (rank.isMoreThan(r)) continue;
if (ret != null && ret.isLessThan(r)) continue;
ret = r;
}
return ret;
}
private static Rank getDemote(Rank rank)
{
Rank ret = null;
for (Rank r : rank.getFaction().getRanks().getAll())
{
if (rank == r) continue;
if (rank.isLessThan(r)) continue;
if (ret != null && ret.isMoreThan(r)) continue;
ret = r;
}
return ret;
}
}