package net.minecraft.world.entity.variant; import java.util.Optional; import net.minecraft.core.Holder; import net.minecraft.core.Registry; import net.minecraft.core.RegistryAccess; import net.minecraft.nbt.CompoundTag; import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; public class VariantUtils { public static final String TAG_VARIANT = "variant"; public static Holder getDefaultOrAny(RegistryAccess registryAccess, ResourceKey key) { Registry registry = registryAccess.lookupOrThrow(key.registryKey()); return (Holder)registry.get(key).or(registry::getAny).orElseThrow(); } public static Holder getAny(RegistryAccess registryAccess, ResourceKey> registryKey) { return (Holder)registryAccess.lookupOrThrow(registryKey).getAny().orElseThrow(); } public static void writeVariant(CompoundTag tag, Holder variant) { variant.unwrapKey().ifPresent(resourceKey -> tag.store("variant", ResourceLocation.CODEC, resourceKey.location())); } public static Optional> readVariant(CompoundTag tag, RegistryAccess registryAccess, ResourceKey> registryKey) { return tag.read("variant", ResourceLocation.CODEC).map(resourceLocation -> ResourceKey.create(registryKey, resourceLocation)).flatMap(registryAccess::get); } }