Allow TypeNameAbstract to detect disallowed characters

This commit is contained in:
TheComputerGeek2 2017-05-22 11:22:35 -07:00 committed by Olof Larsson
parent e9b62e3fbc
commit 6ea17bb720

View File

@ -2,10 +2,13 @@ package com.massivecraft.massivecore.command.type;
import com.massivecraft.massivecore.MassiveException; import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.Named; import com.massivecraft.massivecore.Named;
import com.massivecraft.massivecore.collections.MassiveSet;
import com.massivecraft.massivecore.util.Txt;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Set;
public abstract class TypeNameAbstract extends TypeAbstract<String> public abstract class TypeNameAbstract extends TypeAbstract<String>
{ {
@ -62,6 +65,20 @@ public abstract class TypeNameAbstract extends TypeAbstract<String>
throw new MassiveException().addMsg("<b>The name must be at most <h>%d<b> characters.", lengthMax); throw new MassiveException().addMsg("<b>The name must be at most <h>%d<b> characters.", lengthMax);
} }
Set<Character> disallowed = new MassiveSet<>();
for (char character : arg.toCharArray())
{
if (!this.isCharacterAllowed(character)) disallowed.add(character);
}
// We found some disallowed characters
if (!disallowed.isEmpty())
{
String characterViolations = Txt.implode(disallowed, "");
String pluralityResolution = disallowed.size() == 1 ? " is" : "s are";
throw new MassiveException().addMsg("<b>The following character%s not allowed: <h>%s<b>.", pluralityResolution, characterViolations);
}
return arg; return arg;
} }
@ -71,7 +88,6 @@ public abstract class TypeNameAbstract extends TypeAbstract<String>
return Collections.emptyList(); return Collections.emptyList();
} }
// -------------------------------------------- // // -------------------------------------------- //
// METHODS // METHODS
// -------------------------------------------- // // -------------------------------------------- //
@ -83,6 +99,9 @@ public abstract class TypeNameAbstract extends TypeAbstract<String>
return named.getName(); return named.getName();
} }
// Override this if you want to specify what characters may be used
public boolean isCharacterAllowed(char character) { return true; }
// -------------------------------------------- // // -------------------------------------------- //
// ABSTRACT // ABSTRACT
// -------------------------------------------- // // -------------------------------------------- //