package com.minelittlepony.unicopia.util; import java.util.Optional; import java.util.function.Predicate; import java.util.stream.Stream; import com.mojang.serialization.Lifecycle; import net.fabricmc.fabric.api.event.registry.FabricRegistryBuilder; import net.minecraft.registry.tag.TagKey; import net.minecraft.util.Identifier; import net.minecraft.util.Util; import net.minecraft.registry.*; import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.registry.entry.RegistryEntryList; import net.minecraft.registry.entry.RegistryEntryList.Named; import net.minecraft.world.World; public interface RegistryUtils { static Registry createSimple(Identifier id) { return FabricRegistryBuilder.from(new SimpleRegistry(RegistryKey.ofRegistry(id), Lifecycle.stable())).buildAndRegister(); } static Registry createDefaulted(Identifier id, String def) { return FabricRegistryBuilder.from(new SimpleDefaultedRegistry(def, RegistryKey.ofRegistry(id), Lifecycle.stable(), false)).buildAndRegister(); } static RegistryEntryList entriesForTag(World world, TagKey key) { return world.getRegistryManager().get(key.registry()).getOrCreateEntryList(key); } static Stream valuesForTag(World world, TagKey key) { return entriesForTag(world, key).stream().map(RegistryEntry::value); } static Optional pickRandom(World world, TagKey key) { return world.getRegistryManager().getOptional(key.registry()) .flatMap(registry -> registry.getEntryList(key)) .flatMap(entries -> entries.getRandom(world.random)) .map(RegistryEntry::value); } static Optional pickRandom(World world, TagKey key, Predicate filter) { return Util.getRandomOrEmpty(world.getRegistryManager().getOptional(key.registry()) .flatMap(registry -> registry.getEntryList(key)) .stream() .flatMap(Named::stream) .map(RegistryEntry::value) .filter(filter) .toList(), world.random); } static boolean isIn(World world, T obj, RegistryKey> registry, TagKey tag) { return world.getRegistryManager().get(registry).getEntry(obj).isIn(tag); } }