Fix TypeRank

This commit is contained in:
BuildTools 2016-01-24 11:55:44 +01:00 committed by Olof Larsson
parent 58903a998e
commit 0937e12b21
2 changed files with 41 additions and 18 deletions

View File

@ -48,9 +48,6 @@ public class CmdFactionsRank extends FactionsCommand
private Rel targetRank = null; private Rel targetRank = null;
private Rel rank = null; private Rel rank = null;
// This one is permanent
private TypeRank rankReader = new TypeRank();
// -------------------------------------------- // // -------------------------------------------- //
// CONSTRUCT // CONSTRUCT
// -------------------------------------------- // // -------------------------------------------- //
@ -62,7 +59,7 @@ public class CmdFactionsRank extends FactionsCommand
// Parameters // Parameters
this.addParameter(TypeMPlayer.get(), "player"); this.addParameter(TypeMPlayer.get(), "player");
this.addParameter(rankReader, "action", "show"); this.addParameter(TypeRank.get(), "action", "show");
this.addParameter(TypeFaction.get(), "faction", "their"); this.addParameter(TypeFaction.get(), "faction", "their");
// Requirements // Requirements
@ -141,7 +138,7 @@ public class CmdFactionsRank extends FactionsCommand
// Rank if any passed. // Rank if any passed.
if (this.argIsSet(1)) if (this.argIsSet(1))
{ {
this.rankReader.setStartRank(targetRank); this.setParameterType(1, TypeRank.get(targetRank));
rank = this.readArg(); rank = this.readArg();
} }
@ -235,7 +232,7 @@ public class CmdFactionsRank extends FactionsCommand
// Don't change their rank to something they already are. // Don't change their rank to something they already are.
if (target.getRole() == rank) if (target.getRole() == rank)
{ {
throw new MassiveException().addMsg("%s <b>is already %s<b>.", target.describeTo(msender), rank.getColor() + rank.getDescPlayerOne()); throw new MassiveException().addMsg("%s <b>is already %s.", target.describeTo(msender), rank.getDescPlayerOne());
} }
} }

View File

@ -1,10 +1,16 @@
package com.massivecraft.factions.cmd.type; 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 java.util.Set;
import com.massivecraft.factions.Rel; import com.massivecraft.factions.Rel;
import com.massivecraft.massivecore.collections.MassiveMap;
import com.massivecraft.massivecore.collections.MassiveSet; import com.massivecraft.massivecore.collections.MassiveSet;
import com.massivecraft.massivecore.command.type.enumeration.TypeEnum; import com.massivecraft.massivecore.command.type.enumeration.TypeEnum;
import com.massivecraft.massivecore.util.MUtil;
public class TypeRank extends TypeEnum<Rel> public class TypeRank extends TypeEnum<Rel>
{ {
@ -29,31 +35,51 @@ public class TypeRank extends TypeEnum<Rel>
// -------------------------------------------- // // -------------------------------------------- //
// INSTANCE & CONSTRUCT // INSTANCE & CONSTRUCT
// -------------------------------------------- // // -------------------------------------------- //
// Because of the caching in TypeAbstractChoice, we want only one of each instance.
// Can be used to promote and demote. // Null instance, doesn't allow promote and demote.
public static TypeRank get(Rel rank) { return new TypeRank(rank); } 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<Rel, TypeRank> instances;
static
{
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);
}
public static TypeRank get(Rel rank) { return instances.get(rank); }
// Constructor
public TypeRank(Rel rank) public TypeRank(Rel rank)
{ {
super(Rel.class); super(Rel.class);
if (rank != null && ! rank.isRank()) throw new IllegalArgumentException(rank + " is not a valid rank"); if (rank != null && ! rank.isRank()) throw new IllegalArgumentException(rank + " is not a valid rank");
this.startRank = rank; this.startRank = rank;
}
// Can not be used to promote and demote. // Do setAll with only ranks.
private static TypeRank i = new TypeRank(); List<Rel> all = MUtil.list(Rel.values());
public static TypeRank get() { return i; } for (Iterator<Rel> it = all.iterator(); it.hasNext(); )
public TypeRank() {
{ if ( ! it.next().isRank()) it.remove();
this(null); }
this.setAll(all);
} }
// -------------------------------------------- // // -------------------------------------------- //
// FIELDS // 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 Rel getStartRank() { return this.startRank; }
public void setStartRank(Rel startRank) { this.startRank = startRank; }
// -------------------------------------------- // // -------------------------------------------- //
// OVERRIDE // OVERRIDE