From 0937e12b210cc6ba4445c9c73cfde98ef2be147b Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sun, 24 Jan 2016 11:55:44 +0100 Subject: [PATCH] Fix TypeRank --- .../factions/cmd/CmdFactionsRank.java | 9 ++-- .../factions/cmd/type/TypeRank.java | 50 ++++++++++++++----- 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsRank.java b/src/com/massivecraft/factions/cmd/CmdFactionsRank.java index 83461b7c..02d0fb12 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsRank.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsRank.java @@ -48,9 +48,6 @@ public class CmdFactionsRank extends FactionsCommand private Rel targetRank = null; private Rel rank = null; - // This one is permanent - private TypeRank rankReader = new TypeRank(); - // -------------------------------------------- // // CONSTRUCT // -------------------------------------------- // @@ -62,7 +59,7 @@ public class CmdFactionsRank extends FactionsCommand // Parameters this.addParameter(TypeMPlayer.get(), "player"); - this.addParameter(rankReader, "action", "show"); + this.addParameter(TypeRank.get(), "action", "show"); this.addParameter(TypeFaction.get(), "faction", "their"); // Requirements @@ -141,7 +138,7 @@ public class CmdFactionsRank extends FactionsCommand // Rank if any passed. if (this.argIsSet(1)) { - this.rankReader.setStartRank(targetRank); + this.setParameterType(1, TypeRank.get(targetRank)); rank = this.readArg(); } @@ -235,7 +232,7 @@ public class CmdFactionsRank extends FactionsCommand // Don't change their rank to something they already are. if (target.getRole() == rank) { - throw new MassiveException().addMsg("%s is already %s.", target.describeTo(msender), rank.getColor() + rank.getDescPlayerOne()); + throw new MassiveException().addMsg("%s is already %s.", target.describeTo(msender), rank.getDescPlayerOne()); } } diff --git a/src/com/massivecraft/factions/cmd/type/TypeRank.java b/src/com/massivecraft/factions/cmd/type/TypeRank.java index 46ba3983..04617060 100644 --- a/src/com/massivecraft/factions/cmd/type/TypeRank.java +++ b/src/com/massivecraft/factions/cmd/type/TypeRank.java @@ -1,10 +1,16 @@ package com.massivecraft.factions.cmd.type; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Map; import java.util.Set; 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; public class TypeRank extends TypeEnum { @@ -29,31 +35,51 @@ public class TypeRank extends TypeEnum // -------------------------------------------- // // INSTANCE & CONSTRUCT // -------------------------------------------- // + // Because of the caching in TypeAbstractChoice, we want only one of each instance. - // Can be used to promote and demote. - public static TypeRank get(Rel rank) { return new TypeRank(rank); } + // Null instance, doesn't allow promote and demote. + private static final TypeRank i = new TypeRank(null); + public static TypeRank get() { return i; } + + // Cached instances, does allow promote and demote. + private static final Map instances; + static + { + Map 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); + } + public static TypeRank get(Rel rank) { return instances.get(rank); } + + // Constructor public TypeRank(Rel rank) { super(Rel.class); if (rank != null && ! rank.isRank()) throw new IllegalArgumentException(rank + " is not a valid rank"); this.startRank = rank; - } - - // Can not be used to promote and demote. - private static TypeRank i = new TypeRank(); - public static TypeRank get() { return i; } - public TypeRank() - { - this(null); + + // Do setAll with only ranks. + List all = MUtil.list(Rel.values()); + for (Iterator it = all.iterator(); it.hasNext(); ) + { + if ( ! it.next().isRank()) it.remove(); + } + + this.setAll(all); } // -------------------------------------------- // // FIELDS // -------------------------------------------- // - private Rel startRank; + // This must be final, for caching in TypeAbstractChoice to work. + private final Rel startRank; public Rel getStartRank() { return this.startRank; } - public void setStartRank(Rel startRank) { this.startRank = startRank; } // -------------------------------------------- // // OVERRIDE