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

The input to check

* @return

True if the value is empty

*/ 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

The string to match to a material

* @return

The material matching the string, or null if not found

*/ 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

The string to match to an enchantment

* @return

The enchantment matching the string, or null if not found

*/ public static @Nullable Enchantment matchEnchantment(@NotNull String input) { try { Registry 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

The input to RegExIfy

* @return

The converted input

*/ public static @NotNull String regExIfy(@NotNull String input) { return input.replace("*", ".*").toUpperCase().replace("-", "_"); } }