package dev.isxander.yacl3.gui.utils; import net.minecraft.ResourceLocationException; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.Item; import net.minecraft.world.item.Items; import org.apache.commons.lang3.StringUtils; import org.jetbrains.annotations.NotNull; import java.util.Comparator; import java.util.HashSet; import java.util.Set; import java.util.function.Predicate; import java.util.stream.Stream; public final class ItemRegistryHelper { /** * Checks whether the given string is an identifier referring to a known item * * @param identifier Item identifier, either of the format "namespace:path" or "path". If no namespace is included, * the default vanilla namespace "minecraft" is used. * @return true if the identifier refers to a registered item, false otherwise */ public static boolean isRegisteredItem(String identifier) { try { ResourceLocation itemIdentifier = new ResourceLocation(identifier.toLowerCase()); return BuiltInRegistries.ITEM.containsKey(itemIdentifier); } catch (ResourceLocationException e) { return false; } } /** * Looks up the item of the given identifier string. * * @param identifier Item identifier, either of the format "namespace:path" or "path". If no namespace is included, * the default vanilla namespace "minecraft" is used. * @param defaultItem Fallback item that gets returned if the identifier does not name a registered item. * @return The item identified by the given string, or the fallback if the identifier is not known. */ public static Item getItemFromName(String identifier, Item defaultItem) { try { ResourceLocation itemIdentifier = new ResourceLocation(identifier.toLowerCase()); if (BuiltInRegistries.ITEM.containsKey(itemIdentifier)) { return BuiltInRegistries.ITEM.get(itemIdentifier); } } catch (ResourceLocationException ignored) { } return defaultItem; } /** * Looks up the item of the given identifier string. * * @param identifier Item identifier, either of the format "namespace:path" or "path". If no namespace is included, * the default vanilla namespace "minecraft" is used. * @return The item identified by the given string, or `Items.AIR` if the identifier is not known. */ public static Item getItemFromName(String identifier) { return getItemFromName(identifier, Items.AIR); } /** * Returns a list of item identifiers matching the given string. The value matches an identifier if: *