All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good
72 lines
2.4 KiB
Java
72 lines
2.4 KiB
Java
package net.knarcraft.blacksmith.util;
|
|
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.Material;
|
|
import org.bukkit.NamespacedKey;
|
|
import org.bukkit.Registry;
|
|
import org.bukkit.enchantments.Enchantment;
|
|
import org.jetbrains.annotations.NotNull;
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
/**
|
|
* A helper class for parsing input into proper object types
|
|
*/
|
|
public final class InputParsingHelper {
|
|
|
|
private InputParsingHelper() {
|
|
|
|
}
|
|
|
|
/**
|
|
* Gets whether the input is an "empty" value treated as null
|
|
*
|
|
* @param input <p>The input to check</p>
|
|
* @return <p>True if the value is empty</p>
|
|
*/
|
|
public static boolean isEmpty(@Nullable String input) {
|
|
return input == null || input.equalsIgnoreCase("null") || input.equals("\"\"") ||
|
|
input.isBlank() || input.equals("-1");
|
|
}
|
|
|
|
/**
|
|
* Tries to find the material matching the given input string
|
|
*
|
|
* @param input <p>The string to match to a material</p>
|
|
* @return <p>The material matching the string, or null if not found</p>
|
|
*/
|
|
public static @Nullable Material matchMaterial(@NotNull String input) {
|
|
return Material.matchMaterial(input.replace("-", "_"));
|
|
}
|
|
|
|
/**
|
|
* Tries to find the enchantment matching the given input string
|
|
*
|
|
* @param input <p>The string to match to an enchantment</p>
|
|
* @return <p>The enchantment matching the string, or null if not found</p>
|
|
*/
|
|
public static @Nullable Enchantment matchEnchantment(@NotNull String input) {
|
|
try {
|
|
Registry<Enchantment> enchantments = Bukkit.getRegistry(Enchantment.class);
|
|
if (enchantments == null) {
|
|
throw new RuntimeException("Unable to get enchantment registry");
|
|
}
|
|
return enchantments.get(NamespacedKey.minecraft(
|
|
input.replace("-", "_").replace(" ", "_").toLowerCase()));
|
|
} catch (IllegalArgumentException exception) {
|
|
//Invalid characters, such as : will normally throw an illegal argument exception
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Converts a material name like "*helmet" into a regular expression ".*HELMET"
|
|
*
|
|
* @param input <p>The input to RegExIfy</p>
|
|
* @return <p>The converted input</p>
|
|
*/
|
|
public static @NotNull String regExIfy(@NotNull String input) {
|
|
return input.replace("*", ".*").toUpperCase().replace("-", "_");
|
|
}
|
|
|
|
}
|