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.Collection;
import java.util.Collections; import java.util.Collections;
import com.massivecraft.massivecore.Named;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.MassiveException; import com.massivecraft.massivecore.MassiveException;
@ -17,6 +18,14 @@ public abstract class TypeNameAbstract extends TypeAbstract<String>
public boolean isStrict() { return this.strict; } public boolean isStrict() { return this.strict; }
public boolean isLenient() { return ! this.isStrict(); } 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 // CONSTRUCT
// -------------------------------------------- // // -------------------------------------------- //
@ -42,6 +51,18 @@ 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); 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; return arg;
} }
@ -51,11 +72,23 @@ public abstract class TypeNameAbstract extends TypeAbstract<String>
return Collections.emptyList(); return Collections.emptyList();
} }
// -------------------------------------------- //
// METHODS
// -------------------------------------------- //
public String getCurrentName(CommandSender sender)
{
Named named = this.getCurrent(sender);
if (named == null) return null;
return named.getName();
}
// -------------------------------------------- // // -------------------------------------------- //
// ABSTRACT // ABSTRACT
// -------------------------------------------- // // -------------------------------------------- //
public abstract String getCurrentName(CommandSender sender); public abstract Named getCurrent(CommandSender sender);
public abstract boolean isNameTaken(String name); public abstract boolean isNameTaken(String name);