Improved TypeNameAbstract

This commit is contained in:
Olof Larsson 2016-10-23 15:20:33 +02:00
parent b1426a17b3
commit 04cdb09a1a
No known key found for this signature in database
GPG Key ID: BBEF14F97DA52474

View File

@ -3,6 +3,7 @@ package com.massivecraft.massivecore.command.type;
import java.util.Collection;
import java.util.Collections;
import com.massivecraft.massivecore.Named;
import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.MassiveException;
@ -16,7 +17,15 @@ public abstract class TypeNameAbstract extends TypeAbstract<String>
private final boolean strict;
public boolean isStrict() { return this.strict; }
public boolean isLenient() { return ! this.isStrict(); }
private Integer lengthMin = 1;
public Integer getLengthMin() { return this.lengthMin; }
public void setLengthMin(Integer lengthMin) { this.lengthMin = lengthMin; }
private Integer lengthMax = null;
public Integer getLengthMax() { return this.lengthMax; }
public void setLengthMax(Integer lengthMax) { this.lengthMax = lengthMax; }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
@ -42,20 +51,44 @@ public abstract class TypeNameAbstract extends TypeAbstract<String>
if (this.isNameTaken(arg)) throw new MassiveException().addMsg("<b>The name \"<h>%s<b>\" is already in use.",arg);
Integer lengthMin = this.getLengthMin();
if (lengthMin != null && arg.length() < lengthMin)
{
throw new MassiveException().addMsg("<b>The name must be at least <h>%d<b> characters.", lengthMin);
}
Integer lengthMax = this.getLengthMax();
if (lengthMax != null && arg.length() >lengthMax)
{
throw new MassiveException().addMsg("<b>The name must be at most <h>%d<b> characters.", lengthMax);
}
return arg;
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
return Collections.emptyList();
}
// -------------------------------------------- //
// METHODS
// -------------------------------------------- //
public String getCurrentName(CommandSender sender)
{
Named named = this.getCurrent(sender);
if (named == null) return null;
return named.getName();
}
// -------------------------------------------- //
// ABSTRACT
// -------------------------------------------- //
public abstract String getCurrentName(CommandSender sender);
public abstract Named getCurrent(CommandSender sender);
public abstract boolean isNameTaken(String name);