diff --git a/common/src/main/java/me/lucko/luckperms/common/api/ApiUtils.java b/common/src/main/java/me/lucko/luckperms/common/api/ApiUtils.java index f09d1de2c..b8d849e7e 100644 --- a/common/src/main/java/me/lucko/luckperms/common/api/ApiUtils.java +++ b/common/src/main/java/me/lucko/luckperms/common/api/ApiUtils.java @@ -27,19 +27,24 @@ package me.lucko.luckperms.common.api; import com.google.common.base.Preconditions; +import me.lucko.luckperms.common.config.ConfigKeys; +import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.storage.misc.DataConstraints; +import java.util.function.Predicate; + public final class ApiUtils { - public static String checkUsername(String s) { + public static String checkUsername(String s, LuckPermsPlugin plugin) { if (s == null) { return null; } - Preconditions.checkArgument( - 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 _'." - ); + Predicate test = plugin.getConfiguration().get(ConfigKeys.ALLOW_INVALID_USERNAMES) ? + DataConstraints.PLAYER_USERNAME_TEST_LENIENT : + DataConstraints.PLAYER_USERNAME_TEST; + + Preconditions.checkArgument(test.test(s), "Invalid username entry: " + s); return s; } @@ -48,10 +53,7 @@ public final class ApiUtils { return null; } - Preconditions.checkArgument( - 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'." - ); + Preconditions.checkArgument(DataConstraints.GROUP_NAME_TEST.test(s), "Invalid name entry: " + s); return s.toLowerCase(); } diff --git a/common/src/main/java/me/lucko/luckperms/common/api/implementation/ApiLog.java b/common/src/main/java/me/lucko/luckperms/common/api/implementation/ApiLog.java index c5fc522a2..a3993f11b 100644 --- a/common/src/main/java/me/lucko/luckperms/common/api/implementation/ApiLog.java +++ b/common/src/main/java/me/lucko/luckperms/common/api/implementation/ApiLog.java @@ -27,6 +27,7 @@ package me.lucko.luckperms.common.api.implementation; import me.lucko.luckperms.api.LogEntry; import me.lucko.luckperms.common.actionlog.Log; +import me.lucko.luckperms.common.api.ApiUtils; import org.checkerframework.checker.nullness.qual.NonNull; @@ -34,8 +35,6 @@ import java.util.Objects; import java.util.SortedSet; import java.util.UUID; -import static me.lucko.luckperms.common.api.ApiUtils.checkName; - @SuppressWarnings("unchecked") public class ApiLog implements me.lucko.luckperms.api.Log { private final Log handle; @@ -64,12 +63,12 @@ public class ApiLog implements me.lucko.luckperms.api.Log { @Override public @NonNull SortedSet getGroupHistory(@NonNull String name) { Objects.requireNonNull(name, "name"); - return (SortedSet) this.handle.getGroupHistory(checkName(name)); + return (SortedSet) this.handle.getGroupHistory(ApiUtils.checkName(name)); } @Override public @NonNull SortedSet getTrackHistory(@NonNull String name) { Objects.requireNonNull(name, "name"); - return (SortedSet) this.handle.getTrackHistory(checkName(name)); + return (SortedSet) this.handle.getTrackHistory(ApiUtils.checkName(name)); } } diff --git a/common/src/main/java/me/lucko/luckperms/common/api/implementation/ApiStorage.java b/common/src/main/java/me/lucko/luckperms/common/api/implementation/ApiStorage.java index 3eeafa3fb..88db9f3f4 100644 --- a/common/src/main/java/me/lucko/luckperms/common/api/implementation/ApiStorage.java +++ b/common/src/main/java/me/lucko/luckperms/common/api/implementation/ApiStorage.java @@ -33,6 +33,7 @@ import me.lucko.luckperms.api.Track; import me.lucko.luckperms.api.User; import me.lucko.luckperms.api.event.cause.CreationCause; import me.lucko.luckperms.api.event.cause.DeletionCause; +import me.lucko.luckperms.common.api.ApiUtils; import me.lucko.luckperms.common.bulkupdate.comparison.Constraint; import me.lucko.luckperms.common.bulkupdate.comparison.StandardComparison; import me.lucko.luckperms.common.node.factory.NodeFactory; @@ -50,9 +51,6 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executor; import java.util.function.Function; -import static me.lucko.luckperms.common.api.ApiUtils.checkName; -import static me.lucko.luckperms.common.api.ApiUtils.checkUsername; - public class ApiStorage implements me.lucko.luckperms.api.Storage { private static final Function CONSUME_EXCEPTION = throwable -> { throwable.printStackTrace(); @@ -114,7 +112,7 @@ public class ApiStorage implements me.lucko.luckperms.api.Storage { @Override public @NonNull CompletableFuture loadUser(@NonNull UUID uuid, String username) { Objects.requireNonNull(uuid, "uuid"); - username = checkUsername(username); + username = ApiUtils.checkUsername(username, this.plugin); if (this.plugin.getUserManager().getIfLoaded(uuid) == null) { this.plugin.getUserManager().getHouseKeeper().registerApiUsage(uuid); @@ -147,7 +145,7 @@ public class ApiStorage implements me.lucko.luckperms.api.Storage { @Override public @NonNull CompletableFuture createAndLoadGroup(@NonNull String name) { Objects.requireNonNull(name, "name"); - return this.handle.createAndLoadGroup(checkName(name), CreationCause.API) + return this.handle.createAndLoadGroup(ApiUtils.checkName(name), CreationCause.API) .thenApply(r -> true) .exceptionally(consumeExceptionToFalse()); } @@ -155,7 +153,7 @@ public class ApiStorage implements me.lucko.luckperms.api.Storage { @Override public @NonNull CompletableFuture loadGroup(@NonNull String name) { Objects.requireNonNull(name, "name"); - return this.handle.loadGroup(checkName(name)) + return this.handle.loadGroup(ApiUtils.checkName(name)) .thenApply(Optional::isPresent) .exceptionally(consumeExceptionToFalse()); } @@ -195,7 +193,7 @@ public class ApiStorage implements me.lucko.luckperms.api.Storage { @Override public @NonNull CompletableFuture createAndLoadTrack(@NonNull String name) { Objects.requireNonNull(name, "name"); - return this.handle.createAndLoadTrack(checkName(name), CreationCause.API) + return this.handle.createAndLoadTrack(ApiUtils.checkName(name), CreationCause.API) .thenApply(r -> true) .exceptionally(consumeExceptionToFalse()); } @@ -203,7 +201,7 @@ public class ApiStorage implements me.lucko.luckperms.api.Storage { @Override public @NonNull CompletableFuture loadTrack(@NonNull String name) { Objects.requireNonNull(name, "name"); - return this.handle.loadTrack(checkName(name)) + return this.handle.loadTrack(ApiUtils.checkName(name)) .thenApply(Optional::isPresent) .exceptionally(consumeExceptionToFalse()); } @@ -235,7 +233,7 @@ public class ApiStorage implements me.lucko.luckperms.api.Storage { public @NonNull CompletableFuture saveUUIDData(@NonNull String username, @NonNull UUID uuid) { Objects.requireNonNull(username, "username"); Objects.requireNonNull(uuid, "uuid"); - return this.handle.savePlayerData(uuid, checkUsername(username)) + return this.handle.savePlayerData(uuid, ApiUtils.checkUsername(username, this.plugin)) .thenApply(r -> true) .exceptionally(consumeExceptionToFalse()); } @@ -243,7 +241,7 @@ public class ApiStorage implements me.lucko.luckperms.api.Storage { @Override public @NonNull CompletableFuture getUUID(@NonNull String username) { Objects.requireNonNull(username, "username"); - return this.handle.getPlayerUuid(checkUsername(username)); + return this.handle.getPlayerUuid(ApiUtils.checkUsername(username, this.plugin)); } @Override diff --git a/common/src/main/java/me/lucko/luckperms/common/api/implementation/ApiUserManager.java b/common/src/main/java/me/lucko/luckperms/common/api/implementation/ApiUserManager.java index b5c59be6a..a30a62649 100644 --- a/common/src/main/java/me/lucko/luckperms/common/api/implementation/ApiUserManager.java +++ b/common/src/main/java/me/lucko/luckperms/common/api/implementation/ApiUserManager.java @@ -61,7 +61,7 @@ public class ApiUserManager extends ApiAbstractManager loadUser(@NonNull UUID uuid, @Nullable String username) { Objects.requireNonNull(uuid, "uuid"); - ApiUtils.checkUsername(username); + ApiUtils.checkUsername(username, this.plugin); if (this.plugin.getUserManager().getIfLoaded(uuid) == null) { this.plugin.getUserManager().getHouseKeeper().registerApiUsage(uuid);