Add data constraints - closes #256

This commit is contained in:
Luck 2017-05-07 13:44:29 +01:00
parent 6c4fa65f91
commit 65af3d0f54
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
20 changed files with 216 additions and 100 deletions

View File

@ -29,14 +29,14 @@ import lombok.experimental.UtilityClass;
import com.google.common.base.Preconditions;
import me.lucko.luckperms.common.utils.ArgumentChecker;
import me.lucko.luckperms.common.constants.DataConstraints;
@UtilityClass
public class ApiUtils {
public static String checkUsername(String s) {
Preconditions.checkArgument(
!ArgumentChecker.checkUsername(s),
DataConstraints.PLAYER_USERNAME_TEST.test(s),
"Invalid username entry '" + s + "'. Usernames must be less than 16 chars and only contain 'a-z A-Z 1-9 _'."
);
return s;
@ -44,14 +44,14 @@ public class ApiUtils {
public static String checkName(String s) {
Preconditions.checkArgument(
!ArgumentChecker.checkName(s),
DataConstraints.GROUP_NAME_TEST.test(s),
"Invalid name entry '" + s + "'. Names must be less than 37 chars and only contain 'a-z A-Z 1-9'."
);
return s.toLowerCase();
}
public static long checkTime(long l) {
Preconditions.checkArgument(!ArgumentChecker.checkTime(l), "Unix time '" + l + "' is invalid, as it has already passed.");
Preconditions.checkArgument(DataConstraints.TIME_TEST.test(l), "Unix time '" + l + "' is invalid, as it has already passed.");
return l;
}

View File

@ -273,8 +273,8 @@ public class CommandManager {
return CommandResult.INVALID_ARGS;
}
if (e instanceof ArgumentUtils.InvalidServerException) {
Message.SERVER_INVALID_ENTRY.send(sender);
if (e instanceof ArgumentUtils.InvalidServerWorldException) {
Message.SERVER_WORLD_INVALID_ENTRY.send(sender);
return CommandResult.INVALID_ARGS;
}

View File

@ -172,8 +172,8 @@ public class SharedMainCommand<T extends PermissionHolder> extends SubCommand<T>
return CommandResult.INVALID_ARGS;
}
if (e instanceof ArgumentUtils.InvalidServerException) {
Message.SERVER_INVALID_ENTRY.send(sender);
if (e instanceof ArgumentUtils.InvalidServerWorldException) {
Message.SERVER_WORLD_INVALID_ENTRY.send(sender);
return CommandResult.INVALID_ARGS;
}

View File

@ -30,11 +30,11 @@ import me.lucko.luckperms.common.commands.Arg;
import me.lucko.luckperms.common.commands.CommandResult;
import me.lucko.luckperms.common.commands.abstraction.SingleCommand;
import me.lucko.luckperms.common.commands.sender.Sender;
import me.lucko.luckperms.common.constants.DataConstraints;
import me.lucko.luckperms.common.constants.Message;
import me.lucko.luckperms.common.constants.Permission;
import me.lucko.luckperms.common.data.LogEntry;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.utils.ArgumentChecker;
import me.lucko.luckperms.common.utils.Predicates;
import java.util.List;
@ -56,7 +56,7 @@ public class CreateGroup extends SingleCommand {
}
String groupName = args.get(0).toLowerCase();
if (ArgumentChecker.checkName(groupName)) {
if (!DataConstraints.GROUP_NAME_TEST.test(groupName)) {
Message.GROUP_INVALID_ENTRY.send(sender);
return CommandResult.INVALID_ARGS;
}

View File

@ -31,12 +31,12 @@ import me.lucko.luckperms.common.commands.CommandException;
import me.lucko.luckperms.common.commands.CommandResult;
import me.lucko.luckperms.common.commands.abstraction.SubCommand;
import me.lucko.luckperms.common.commands.sender.Sender;
import me.lucko.luckperms.common.constants.DataConstraints;
import me.lucko.luckperms.common.constants.Message;
import me.lucko.luckperms.common.constants.Permission;
import me.lucko.luckperms.common.core.model.Group;
import me.lucko.luckperms.common.data.LogEntry;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.utils.ArgumentChecker;
import me.lucko.luckperms.common.utils.Predicates;
import java.util.List;
@ -51,7 +51,7 @@ public class GroupClone extends SubCommand<Group> {
@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Group group, List<String> args, String label) throws CommandException {
String newGroupName = args.get(0).toLowerCase();
if (ArgumentChecker.checkName(newGroupName)) {
if (!DataConstraints.GROUP_NAME_TEST.test(newGroupName)) {
Message.GROUP_INVALID_ENTRY.send(sender);
return CommandResult.INVALID_ARGS;
}

View File

@ -32,12 +32,12 @@ import me.lucko.luckperms.common.commands.CommandException;
import me.lucko.luckperms.common.commands.CommandResult;
import me.lucko.luckperms.common.commands.abstraction.SubCommand;
import me.lucko.luckperms.common.commands.sender.Sender;
import me.lucko.luckperms.common.constants.DataConstraints;
import me.lucko.luckperms.common.constants.Message;
import me.lucko.luckperms.common.constants.Permission;
import me.lucko.luckperms.common.core.model.Group;
import me.lucko.luckperms.common.data.LogEntry;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.utils.ArgumentChecker;
import me.lucko.luckperms.common.utils.Predicates;
import java.util.List;
@ -52,7 +52,7 @@ public class GroupRename extends SubCommand<Group> {
@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Group group, List<String> args, String label) throws CommandException {
String newGroupName = args.get(0).toLowerCase();
if (ArgumentChecker.checkName(newGroupName)) {
if (!DataConstraints.GROUP_NAME_TEST.test(newGroupName)) {
Message.GROUP_INVALID_ENTRY.send(sender);
return CommandResult.INVALID_ARGS;
}

View File

@ -31,11 +31,11 @@ import me.lucko.luckperms.common.commands.CommandException;
import me.lucko.luckperms.common.commands.CommandResult;
import me.lucko.luckperms.common.commands.abstraction.SubCommand;
import me.lucko.luckperms.common.commands.sender.Sender;
import me.lucko.luckperms.common.constants.DataConstraints;
import me.lucko.luckperms.common.constants.Message;
import me.lucko.luckperms.common.constants.Permission;
import me.lucko.luckperms.common.data.Log;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.utils.ArgumentChecker;
import me.lucko.luckperms.common.utils.DateUtil;
import me.lucko.luckperms.common.utils.Predicates;
@ -68,7 +68,7 @@ public class LogGroupHistory extends SubCommand<Log> {
}
}
if (ArgumentChecker.checkName(group)) {
if (!DataConstraints.GROUP_NAME_TEST.test(group)) {
Message.GROUP_INVALID_ENTRY.send(sender);
return CommandResult.INVALID_ARGS;
}

View File

@ -31,11 +31,11 @@ import me.lucko.luckperms.common.commands.CommandException;
import me.lucko.luckperms.common.commands.CommandResult;
import me.lucko.luckperms.common.commands.abstraction.SubCommand;
import me.lucko.luckperms.common.commands.sender.Sender;
import me.lucko.luckperms.common.constants.DataConstraints;
import me.lucko.luckperms.common.constants.Message;
import me.lucko.luckperms.common.constants.Permission;
import me.lucko.luckperms.common.data.Log;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.utils.ArgumentChecker;
import me.lucko.luckperms.common.utils.DateUtil;
import me.lucko.luckperms.common.utils.Predicates;
@ -68,7 +68,7 @@ public class LogTrackHistory extends SubCommand<Log> {
}
}
if (ArgumentChecker.checkName(track)) {
if (!DataConstraints.TRACK_NAME_TEST.test(track)) {
Message.TRACK_INVALID_ENTRY.send(sender);
return CommandResult.INVALID_ARGS;
}

View File

@ -30,11 +30,11 @@ import me.lucko.luckperms.common.commands.Arg;
import me.lucko.luckperms.common.commands.CommandResult;
import me.lucko.luckperms.common.commands.abstraction.SingleCommand;
import me.lucko.luckperms.common.commands.sender.Sender;
import me.lucko.luckperms.common.constants.DataConstraints;
import me.lucko.luckperms.common.constants.Message;
import me.lucko.luckperms.common.constants.Permission;
import me.lucko.luckperms.common.data.LogEntry;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.utils.ArgumentChecker;
import me.lucko.luckperms.common.utils.Predicates;
import java.util.List;
@ -56,7 +56,7 @@ public class CreateTrack extends SingleCommand {
}
String trackName = args.get(0).toLowerCase();
if (ArgumentChecker.checkName(trackName)) {
if (!DataConstraints.TRACK_NAME_TEST.test(trackName)) {
Message.TRACK_INVALID_ENTRY.send(sender);
return CommandResult.INVALID_ARGS;
}

View File

@ -31,13 +31,13 @@ import me.lucko.luckperms.common.commands.CommandResult;
import me.lucko.luckperms.common.commands.abstraction.SubCommand;
import me.lucko.luckperms.common.commands.sender.Sender;
import me.lucko.luckperms.common.commands.utils.Util;
import me.lucko.luckperms.common.constants.DataConstraints;
import me.lucko.luckperms.common.constants.Message;
import me.lucko.luckperms.common.constants.Permission;
import me.lucko.luckperms.common.core.model.Group;
import me.lucko.luckperms.common.core.model.Track;
import me.lucko.luckperms.common.data.LogEntry;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.utils.ArgumentChecker;
import me.lucko.luckperms.common.utils.Predicates;
import me.lucko.luckperms.exceptions.ObjectAlreadyHasException;
@ -53,8 +53,7 @@ public class TrackAppend extends SubCommand<Track> {
@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Track track, List<String> args, String label) throws CommandException {
String groupName = args.get(0).toLowerCase();
if (ArgumentChecker.checkName(groupName)) {
if (!DataConstraints.GROUP_NAME_TEST.test(groupName)) {
sendDetailedUsage(sender, label);
return CommandResult.INVALID_ARGS;
}

View File

@ -31,12 +31,12 @@ import me.lucko.luckperms.common.commands.CommandException;
import me.lucko.luckperms.common.commands.CommandResult;
import me.lucko.luckperms.common.commands.abstraction.SubCommand;
import me.lucko.luckperms.common.commands.sender.Sender;
import me.lucko.luckperms.common.constants.DataConstraints;
import me.lucko.luckperms.common.constants.Message;
import me.lucko.luckperms.common.constants.Permission;
import me.lucko.luckperms.common.core.model.Track;
import me.lucko.luckperms.common.data.LogEntry;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.utils.ArgumentChecker;
import me.lucko.luckperms.common.utils.Predicates;
import java.util.List;
@ -51,7 +51,7 @@ public class TrackClone extends SubCommand<Track> {
@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Track track, List<String> args, String label) throws CommandException {
String newTrackName = args.get(0).toLowerCase();
if (ArgumentChecker.checkName(newTrackName)) {
if (!DataConstraints.TRACK_NAME_TEST.test(newTrackName)) {
Message.TRACK_INVALID_ENTRY.send(sender);
return CommandResult.INVALID_ARGS;
}

View File

@ -31,13 +31,13 @@ import me.lucko.luckperms.common.commands.CommandResult;
import me.lucko.luckperms.common.commands.abstraction.SubCommand;
import me.lucko.luckperms.common.commands.sender.Sender;
import me.lucko.luckperms.common.commands.utils.Util;
import me.lucko.luckperms.common.constants.DataConstraints;
import me.lucko.luckperms.common.constants.Message;
import me.lucko.luckperms.common.constants.Permission;
import me.lucko.luckperms.common.core.model.Group;
import me.lucko.luckperms.common.core.model.Track;
import me.lucko.luckperms.common.data.LogEntry;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.utils.ArgumentChecker;
import me.lucko.luckperms.common.utils.Predicates;
import me.lucko.luckperms.exceptions.ObjectAlreadyHasException;
@ -56,8 +56,7 @@ public class TrackInsert extends SubCommand<Track> {
@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Track track, List<String> args, String label) throws CommandException {
String groupName = args.get(0).toLowerCase();
if (ArgumentChecker.checkName(groupName)) {
if (!DataConstraints.GROUP_NAME_TEST.test(groupName)) {
sendDetailedUsage(sender, label);
return CommandResult.INVALID_ARGS;
}

View File

@ -31,12 +31,12 @@ import me.lucko.luckperms.common.commands.CommandResult;
import me.lucko.luckperms.common.commands.abstraction.SubCommand;
import me.lucko.luckperms.common.commands.sender.Sender;
import me.lucko.luckperms.common.commands.utils.Util;
import me.lucko.luckperms.common.constants.DataConstraints;
import me.lucko.luckperms.common.constants.Message;
import me.lucko.luckperms.common.constants.Permission;
import me.lucko.luckperms.common.core.model.Track;
import me.lucko.luckperms.common.data.LogEntry;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.utils.ArgumentChecker;
import me.lucko.luckperms.common.utils.Predicates;
import me.lucko.luckperms.exceptions.ObjectLacksException;
@ -52,8 +52,7 @@ public class TrackRemove extends SubCommand<Track> {
@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Track track, List<String> args, String label) throws CommandException {
String groupName = args.get(0).toLowerCase();
if (ArgumentChecker.checkName(groupName)) {
if (!DataConstraints.GROUP_NAME_TEST.test(groupName)) {
sendDetailedUsage(sender, label);
return CommandResult.INVALID_ARGS;
}

View File

@ -32,12 +32,12 @@ import me.lucko.luckperms.common.commands.CommandException;
import me.lucko.luckperms.common.commands.CommandResult;
import me.lucko.luckperms.common.commands.abstraction.SubCommand;
import me.lucko.luckperms.common.commands.sender.Sender;
import me.lucko.luckperms.common.constants.DataConstraints;
import me.lucko.luckperms.common.constants.Message;
import me.lucko.luckperms.common.constants.Permission;
import me.lucko.luckperms.common.core.model.Track;
import me.lucko.luckperms.common.data.LogEntry;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.utils.ArgumentChecker;
import me.lucko.luckperms.common.utils.Predicates;
import java.util.List;
@ -52,7 +52,7 @@ public class TrackRename extends SubCommand<Track> {
@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Track track, List<String> args, String label) throws CommandException {
String newTrackName = args.get(0).toLowerCase();
if (ArgumentChecker.checkName(newTrackName)) {
if (!DataConstraints.TRACK_NAME_TEST.test(newTrackName)) {
Message.TRACK_INVALID_ENTRY.send(sender);
return CommandResult.INVALID_ARGS;
}

View File

@ -34,6 +34,7 @@ import me.lucko.luckperms.common.commands.abstraction.SubCommand;
import me.lucko.luckperms.common.commands.sender.Sender;
import me.lucko.luckperms.common.commands.utils.ArgumentUtils;
import me.lucko.luckperms.common.commands.utils.Util;
import me.lucko.luckperms.common.constants.DataConstraints;
import me.lucko.luckperms.common.constants.Message;
import me.lucko.luckperms.common.constants.Permission;
import me.lucko.luckperms.common.core.NodeFactory;
@ -42,7 +43,6 @@ import me.lucko.luckperms.common.core.model.Track;
import me.lucko.luckperms.common.core.model.User;
import me.lucko.luckperms.common.data.LogEntry;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.utils.ArgumentChecker;
import me.lucko.luckperms.common.utils.Predicates;
import me.lucko.luckperms.exceptions.ObjectLacksException;
@ -63,7 +63,7 @@ public class UserDemote extends SubCommand<User> {
@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, User user, List<String> args, String label) throws CommandException {
final String trackName = args.get(0).toLowerCase();
if (ArgumentChecker.checkName(trackName)) {
if (!DataConstraints.TRACK_NAME_TEST.test(trackName)) {
Message.TRACK_INVALID_ENTRY.send(sender);
return CommandResult.INVALID_ARGS;
}

View File

@ -34,6 +34,7 @@ import me.lucko.luckperms.common.commands.abstraction.SubCommand;
import me.lucko.luckperms.common.commands.sender.Sender;
import me.lucko.luckperms.common.commands.utils.ArgumentUtils;
import me.lucko.luckperms.common.commands.utils.Util;
import me.lucko.luckperms.common.constants.DataConstraints;
import me.lucko.luckperms.common.constants.Message;
import me.lucko.luckperms.common.constants.Permission;
import me.lucko.luckperms.common.core.NodeFactory;
@ -42,7 +43,6 @@ import me.lucko.luckperms.common.core.model.Track;
import me.lucko.luckperms.common.core.model.User;
import me.lucko.luckperms.common.data.LogEntry;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.utils.ArgumentChecker;
import me.lucko.luckperms.common.utils.Predicates;
import me.lucko.luckperms.exceptions.ObjectLacksException;
@ -63,7 +63,7 @@ public class UserPromote extends SubCommand<User> {
@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, User user, List<String> args, String label) throws CommandException {
final String trackName = args.get(0).toLowerCase();
if (ArgumentChecker.checkName(trackName)) {
if (!DataConstraints.TRACK_NAME_TEST.test(trackName)) {
Message.TRACK_INVALID_ENTRY.send(sender);
return CommandResult.INVALID_ARGS;
}

View File

@ -28,12 +28,10 @@ package me.lucko.luckperms.common.commands.utils;
import lombok.AllArgsConstructor;
import lombok.Getter;
import com.google.common.base.Splitter;
import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.api.context.MutableContextSet;
import me.lucko.luckperms.common.commands.CommandException;
import me.lucko.luckperms.common.utils.ArgumentChecker;
import me.lucko.luckperms.common.constants.DataConstraints;
import me.lucko.luckperms.common.utils.DateUtil;
import java.util.ArrayList;
@ -44,7 +42,6 @@ import java.util.function.Function;
* Utility class to help process arguments, and throw checked exceptions if the arguments are invalid.
*/
public class ArgumentUtils {
private static final Splitter CONTEXT_SPLITTER = Splitter.on('=').limit(2).omitEmptyStrings();
public static final Function<String, String> WRAPPER = s -> s.contains(" ") ? "\"" + s + "\"" : s;
@ -84,7 +81,7 @@ public class ArgumentUtils {
public static String handleName(int index, List<String> args) throws ArgumentException {
String groupName = args.get(index).toLowerCase();
if (ArgumentChecker.checkName(groupName)) {
if (!DataConstraints.GROUP_NAME_TEST.test(groupName)) {
throw new DetailedUsageException();
}
return groupName;
@ -92,7 +89,7 @@ public class ArgumentUtils {
public static String handleNameWithSpace(int index, List<String> args) throws ArgumentException {
String groupName = args.get(index).toLowerCase();
if (ArgumentChecker.checkNameWithSpace(groupName)) {
if (!DataConstraints.GROUP_NAME_TEST_ALLOW_SPACE.test(groupName)) {
throw new DetailedUsageException();
}
return groupName;
@ -140,7 +137,7 @@ public class ArgumentUtils {
return args.size() > index ? args.get(index).toLowerCase() : null;
}
public static MutableContextSet handleContext(int fromIndex, List<String> args) {
public static MutableContextSet handleContext(int fromIndex, List<String> args) throws CommandException {
if (args.size() > fromIndex) {
MutableContextSet set = MutableContextSet.create();
@ -152,6 +149,14 @@ public class ArgumentUtils {
// one of the first two values, and doesn't have a key
if (i <= 1 && !pair.contains("=")) {
String key = i == 0 ? "server" : "world";
if (key.equals("server") && !DataConstraints.SERVER_NAME_TEST.test(pair)) {
throw new InvalidServerWorldException();
}
if (key.equals("world") && !DataConstraints.WORLD_NAME_TEST.test(pair)) {
throw new InvalidServerWorldException();
}
set.add(key, pair);
continue;
}
@ -171,6 +176,13 @@ public class ArgumentUtils {
continue;
}
if (key.equals("server") && !DataConstraints.SERVER_NAME_TEST.test(value)) {
throw new InvalidServerWorldException();
}
if (key.equals("world") && !DataConstraints.WORLD_NAME_TEST.test(value)) {
throw new InvalidServerWorldException();
}
set.add(key, value);
}
@ -250,7 +262,7 @@ public class ArgumentUtils {
public static class UseInheritException extends ArgumentException {
}
public static class InvalidServerException extends ArgumentException {
public static class InvalidServerWorldException extends ArgumentException {
}
public static class PastDateException extends ArgumentException {

View File

@ -0,0 +1,161 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.common.constants;
import lombok.experimental.UtilityClass;
import me.lucko.luckperms.common.utils.DateUtil;
import java.util.function.Predicate;
import java.util.regex.Pattern;
@UtilityClass
public class DataConstraints {
public static final Pattern RESERVED_CHARACTERS_PATTERN = Pattern.compile("[\\/\\$\\.]");
public static final int MAX_PERMISSION_LENGTH = 200;
public static final int MAX_TRACK_NAME_LENGTH = 36;
public static final int MAX_GROUP_NAME_LENGTH = 36;
public static final int MAX_PLAYER_USERNAME_LENGTH = 16;
public static final Pattern PLAYER_USERNAME_INVALID_CHAR_MATCHER = Pattern.compile("[^A-Za-z0-9_ ]");
public static final int MAX_SERVER_LENGTH = 36;
public static final int MAX_WORLD_LENGTH = 36;
public static final Predicate<String> PERMISSION_TEST = s -> {
if (s.length() <= 0 || s.length() > MAX_PERMISSION_LENGTH) {
return false;
}
return true;
};
public static final Predicate<String> PLAYER_USERNAME_TEST = s -> {
if (s.length() <= 0 || s.length() > MAX_PLAYER_USERNAME_LENGTH) {
return false;
}
if (PLAYER_USERNAME_INVALID_CHAR_MATCHER.matcher(s).find()) {
return false;
}
return true;
};
public static final Predicate<String> GROUP_NAME_TEST = s -> {
if (s.length() <= 0 || s.length() > MAX_GROUP_NAME_LENGTH) {
return false;
}
if (s.contains(" ")) {
return false;
}
if (RESERVED_CHARACTERS_PATTERN.matcher(s).find()) {
return false;
}
return true;
};
public static final Predicate<String> GROUP_NAME_TEST_ALLOW_SPACE = s -> {
if (s.length() <= 0 || s.length() > MAX_GROUP_NAME_LENGTH) {
return false;
}
if (RESERVED_CHARACTERS_PATTERN.matcher(s).find()) {
return false;
}
return true;
};
public static final Predicate<String> TRACK_NAME_TEST = s -> {
if (s.length() <= 0 || s.length() > MAX_TRACK_NAME_LENGTH) {
return false;
}
if (s.contains(" ")) {
return false;
}
if (RESERVED_CHARACTERS_PATTERN.matcher(s).find()) {
return false;
}
return true;
};
public static final Predicate<String> TRACK_NAME_TEST_ALLOW_SPACE = s -> {
if (s.length() <= 0 || s.length() > MAX_TRACK_NAME_LENGTH) {
return false;
}
if (RESERVED_CHARACTERS_PATTERN.matcher(s).find()) {
return false;
}
return true;
};
public static final Predicate<Long> TIME_TEST = DateUtil::shouldExpire;
public static final Predicate<String> SERVER_NAME_TEST = s -> {
if (s.length() <= 0 || s.length() > MAX_SERVER_LENGTH) {
return false;
}
if (s.contains(" ")) {
return false;
}
if (RESERVED_CHARACTERS_PATTERN.matcher(s).find()) {
return false;
}
return true;
};
public static final Predicate<String> WORLD_NAME_TEST = s -> {
if (s.length() <= 0 || s.length() > MAX_WORLD_LENGTH) {
return false;
}
if (s.contains(" ")) {
return false;
}
if (RESERVED_CHARACTERS_PATTERN.matcher(s).find()) {
return false;
}
return true;
};
}

View File

@ -93,7 +93,7 @@ public enum Message {
USER_INVALID_ENTRY("&d{0}&c is not a valid username/uuid.", true),
GROUP_INVALID_ENTRY("Group names can only contain alphanumeric characters.", true),
TRACK_INVALID_ENTRY("Track names can only contain alphanumeric characters.", true),
SERVER_INVALID_ENTRY("Server names can only contain alphanumeric characters.", true),
SERVER_WORLD_INVALID_ENTRY("Server/world names can only contain alphanumeric characters and cannot exceed 36 characters in length.", true),
USE_INHERIT_COMMAND("Use the 'parent add' and 'parent remove' commands instead of specifying the node.", true),

View File

@ -1,54 +0,0 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.common.utils;
import lombok.experimental.UtilityClass;
import me.lucko.luckperms.common.constants.Patterns;
/**
* Utility for checking arguments for consistency
*/
@UtilityClass
public class ArgumentChecker {
public static boolean checkUsername(String s) {
return (s.length() > 16 || Patterns.NON_USERNAME.matcher(s).find());
}
public static boolean checkName(String s) {
return (s.length() > 36 || Patterns.NON_ALPHA_NUMERIC.matcher(s).find());
}
public static boolean checkNameWithSpace(String s) {
return (s.length() > 36 || Patterns.NON_ALPHA_NUMERIC_SPACE.matcher(s).find());
}
public static boolean checkTime(long l) {
return DateUtil.shouldExpire(l);
}
}