From bcae649e0e4a21def3078653a4638df0348a86b3 Mon Sep 17 00:00:00 2001 From: Magnus Ulf Date: Wed, 6 Mar 2019 22:35:54 +0100 Subject: [PATCH] Re-add promote/demote options to rank set --- .../factions/cmd/CmdFactionsRankSet.java | 4 +- .../factions/cmd/type/TypeRank.java | 74 ++++++++++++++++++- 2 files changed, 72 insertions(+), 6 deletions(-) diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsRankSet.java b/src/com/massivecraft/factions/cmd/CmdFactionsRankSet.java index 4d58e3e1..95ecc156 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsRankSet.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsRankSet.java @@ -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); } diff --git a/src/com/massivecraft/factions/cmd/type/TypeRank.java b/src/com/massivecraft/factions/cmd/type/TypeRank.java index 1cf93e78..37d0369c 100644 --- a/src/com/massivecraft/factions/cmd/type/TypeRank.java +++ b/src/com/massivecraft/factions/cmd/type/TypeRank.java @@ -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 @@ -18,14 +21,28 @@ public class TypeRank extends TypeEntityInternalFaction 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 return faction.getRanks().getAll(); } + @Override + public Collection getTabList(CommandSender sender, String arg) + { + List ret = new MassiveList<>(super.getTabList(sender, arg)); + ret.add("promote"); + ret.add("demote"); + return ret; + } + @Override public Set getNamesInner(Rank value) { - return MUtil.set(value.getName(), value.getPrefix() + value.getName()); + Set 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; } }