categories not an enum, so its easier to extend. Validators can modify value.

This commit is contained in:
gnembon committed 2019-06-11 12:27:26 -03:00
1 parent 92f40c36b8
commit f05c661fbf
7 files changed
+65 -54

No files matched your search

+12 -10
View File
@@ -11,40 +11,42 @@ public abstract class Validator<T> {
* Validate the new value of a rule
* @return true if valid, false if new rule invalid.
*/
abstract boolean validate(ServerCommandSource source, ParsedRule<T> currentRule, T newValue, String string);
abstract T validate(ServerCommandSource source, ParsedRule<T> currentRule, T newValue, String string);
public static class POSITIVE_NUMBER<T extends Number> extends Validator<T> {
@Override
public boolean validate(ServerCommandSource source, ParsedRule<T> currentRule, T newValue, String string) {
return newValue.doubleValue() > 0;
public T validate(ServerCommandSource source, ParsedRule<T> currentRule, T newValue, String string) {
return newValue.doubleValue() > 0 ? newValue : null;
}
}
public static class _COMMAND<T> extends Validator<T> {
@Override
public boolean validate(ServerCommandSource source, ParsedRule<T> currentRule, T newValue, String string) {
public T validate(ServerCommandSource source, ParsedRule<T> currentRule, T newValue, String string) {
if (CarpetServer.settingsManager != null)
CarpetServer.settingsManager.notifyPlayersCommandsChanged();
return true;
return newValue;
}
}
public static class WIP<T> extends Validator<T> {
@Override
public boolean validate(ServerCommandSource source, ParsedRule<T> currentRule, T newValue, String string) {
public T validate(ServerCommandSource source, ParsedRule<T> currentRule, T newValue, String string) {
if (source != null)
try
{
Messenger.m(source, "r Work in progress - limited or no functionality available for "+currentRule.name);
}
catch (NullPointerException ignored) { }
return true;
return newValue;
}
}
public static class _STRICT<T> extends Validator<T> {
@Override
public boolean validate(ServerCommandSource source, ParsedRule<T> currentRule, T newValue, String string) {
public T validate(ServerCommandSource source, ParsedRule<T> currentRule, T newValue, String string) {
if (!currentRule.options.contains(string.toLowerCase(Locale.ROOT)))
{
if (source != null)
@@ -55,9 +57,9 @@ public abstract class Validator<T> {
catch (NullPointerException ignored)
{
}
return false;
return null;
}
return true;
return newValue;
}
}
}