Fix null and empty world name checking

This commit is contained in:
Ben Woo 2024-11-14 20:39:35 +08:00
parent b7d067123c
commit 30e8b93786

View File

@ -4,6 +4,7 @@ import java.io.File;
import java.util.Set;
import java.util.regex.Pattern;
import io.vavr.control.Option;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -45,13 +46,18 @@ public class WorldNameChecker {
*/
@NotNull
public NameStatus checkName(@Nullable String worldName) {
if (BLACKLIST_NAMES.contains(worldName)) {
return NameStatus.BLACKLISTED;
}
if (worldName == null || !WORLD_NAME_PATTERN.matcher(worldName).matches()) {
return NameStatus.INVALID_CHARS;
}
return NameStatus.VALID;
return Option.of(worldName).map(name -> {
if (name.isEmpty()) {
return NameStatus.EMPTY;
}
if (BLACKLIST_NAMES.contains(name)) {
return NameStatus.BLACKLISTED;
}
if (!WORLD_NAME_PATTERN.matcher(name).matches()) {
return NameStatus.INVALID_CHARS;
}
return NameStatus.VALID;
}).getOrElse(NameStatus.EMPTY);
}
/**
@ -142,6 +148,11 @@ public class WorldNameChecker {
*/
INVALID_CHARS,
/**
* Name string that is null or length 0.
*/
EMPTY,
/**
* Name not valid as it is deemed blacklisted.
*/