Overview
The Valid class provides methods for:
- Validating objects and conditions
- Type checking (integers, decimals, UUIDs)
- Collection validation
- Range validation
- Equality comparisons
- List matching operations
All check methods throw FoException on failure, which is automatically logged.
Null checks
checkNotNull
Throws an exception if the object is null.
Valid.checkNotNull(player);
Valid.checkNotNull(config, "Configuration cannot be null!");
checkNotEmpty
Throws an exception if collection or string is null or empty.
value
Collection|Map|String
required
The value to check
Valid.checkNotEmpty(playerList, "Player list cannot be empty");
Valid.checkNotEmpty(command, "Command cannot be empty");
Valid.checkNotEmpty(settings, "Settings map is required");
Boolean checks
checkBoolean
Throws an exception if the expression is false.
Format replacements for the message
Valid.checkBoolean(player.hasPermission("admin"), "No permission!");
Valid.checkBoolean(amount > 0, "Amount must be positive, got %s", amount);
Type validation
isInteger
Checks if a string is a valid integer.
True if the string is a valid integer
if (Valid.isInteger(args[0])) {
int amount = Integer.parseInt(args[0]);
}
checkInteger
Throws exception if string is not a valid integer.
Valid.checkInteger(input, "Expected a number, got: " + input);
int number = Integer.parseInt(input);
isDecimal
Checks if a string is a valid decimal number.
if (Valid.isDecimal("3.14")) {
double pi = Double.parseDouble("3.14");
}
isNumber
Checks if string is a valid Java number (including hex, scientific notation, etc.).
boolean valid1 = Valid.isNumber("123"); // true
boolean valid2 = Valid.isNumber("3.14"); // true
boolean valid3 = Valid.isNumber("0xFF"); // true (hex)
boolean valid4 = Valid.isNumber("1.5e10"); // true (scientific)
isUUID
Checks if object is a valid UUID.
True if valid UUID or UUID string
if (Valid.isUUID(args[0])) {
UUID uuid = UUID.fromString(args[0]);
}
Empty checks
isNullOrEmpty
Checks if value is null or empty.
value
String|Collection|Object[]
required
The value to check
if (!Valid.isNullOrEmpty(playerName)) {
// Process name
}
if (Valid.isNullOrEmpty(items)) {
return; // No items to process
}
Range validation
isInRange
Checks if a value is within bounds (inclusive).
Minimum value (inclusive)
Maximum value (inclusive)
True if value is between min and max
int level = player.getLevel();
if (Valid.isInRange(level, 1, 100)) {
// Valid level
}
double damage = 15.5;
Valid.checkBoolean(
Valid.isInRange(damage, 0.0, 20.0),
"Damage must be between 0 and 20"
);
isFinite
Checks if all vector coordinates are finite numbers.
True if all coordinates are finite
Vector velocity = entity.getVelocity();
if (Valid.isFinite(velocity)) {
entity.setVelocity(velocity.multiply(2));
}
Equality checks
locationEquals
Compares two locations by world and block coordinates.
True if same world and block position
Location spawn = world.getSpawnLocation();
if (Valid.locationEquals(player.getLocation(), spawn)) {
player.sendMessage("You are at spawn!");
}
listEquals
Compares two lists (strips colors from strings before comparison).
List<String> list1 = Arrays.asList("&aHello", "&bWorld");
List<String> list2 = Arrays.asList("&cHello", "&dWorld");
if (Valid.listEquals(list1, list2)) {
// Content is the same (colors ignored)
}
colorlessEquals
Compares strings ignoring color codes.
first
String|List|String[]
required
First value
second
String|List|String[]
required
Second value
True if equal after stripping colors
boolean same = Valid.colorlessEquals("&aHello", "&cHello");
// Returns: true
List<String> a = Arrays.asList("&aTest");
List<String> b = Arrays.asList("&bTest");
boolean listsEqual = Valid.colorlessEquals(a, b);
// Returns: true
List matching
isInList
Checks if element is in list (case-insensitive).
List<String> commands = Arrays.asList("help", "info", "stats");
if (Valid.isInList(userInput, commands)) {
// Valid command
}
isInListStartsWith
Checks if any list element starts with the given string.
List<String> prefixes = Arrays.asList("admin_", "mod_", "helper_");
if (Valid.isInListStartsWith("admin_Steve", prefixes)) {
// Matches "admin_" prefix
}
isInListRegex
Checks if element matches any regex pattern in list.
List<String> patterns = Arrays.asList(
"player_.*",
"mob_[0-9]+",
"custom_.+"
);
if (Valid.isInListRegex("player_steve", patterns)) {
// Matches "player_.*" pattern
}
isInListEnum
Checks if string matches any enum constant name.
if (Valid.isInListEnum("DIAMOND", Material.values())) {
Material mat = Material.valueOf("DIAMOND");
}
Thread validation
checkSync
Throws exception if not on main thread.
public void modifyBlock(Block block) {
Valid.checkSync("Cannot modify blocks asynchronously!");
block.setType(Material.AIR);
}
checkAsync
Throws exception if on main thread.
public void queryDatabase() {
Valid.checkAsync("Cannot query database on main thread!");
// Database operations
}
Permission checks
checkPermission
Checks if sender has permission, sends message if not.
if (!Valid.checkPermission(player, "myplugin.admin")) {
return; // Message already sent
}
// Continue with admin action
Examples
Command argument validation
public void onCommand(CommandSender sender, String[] args) {
Valid.checkBoolean(args.length >= 2, "Usage: /command <player> <amount>");
String playerName = args[0];
Valid.checkNotEmpty(playerName, "Player name cannot be empty");
String amountStr = args[1];
Valid.checkInteger(amountStr, "Amount must be a number: " + amountStr);
int amount = Integer.parseInt(amountStr);
Valid.checkBoolean(
Valid.isInRange(amount, 1, 64),
"Amount must be between 1 and 64"
);
Player target = Bukkit.getPlayer(playerName);
Valid.checkNotNull(target, "Player not found: " + playerName);
// Execute command
}
Configuration validation
public void loadConfig() {
String message = config.getString("welcome-message");
Valid.checkNotEmpty(message, "Welcome message is required!");
int maxPlayers = config.getInt("max-players");
Valid.checkBoolean(
Valid.isInRange(maxPlayers, 1, 1000),
"Max players must be between 1 and 1000, got: " + maxPlayers
);
List<String> commands = config.getStringList("startup-commands");
Valid.checkNotEmpty(commands, "Startup commands list cannot be empty");
}
Safe null handling
public void teleportPlayer(Player player, Location location) {
Valid.checkNotNull(player, "Player cannot be null");
Valid.checkNotNull(location, "Location cannot be null");
Valid.checkNotNull(location.getWorld(), "World cannot be null");
Valid.checkBoolean(Valid.isFinite(location.toVector()), "Invalid coordinates");
Valid.checkSync("Cannot teleport player asynchronously");
player.teleport(location);
}