|
| 1 | +package com.provismet.CombatPlusCore.utility; |
| 2 | + |
| 3 | +import com.provismet.datagen.CombatPlusCore.provider.CPCEnchantmentProvider; |
| 4 | +import net.minecraft.block.Block; |
| 5 | +import net.minecraft.enchantment.Enchantment; |
| 6 | +import net.minecraft.entity.damage.DamageType; |
| 7 | +import net.minecraft.item.Item; |
| 8 | +import net.minecraft.registry.DynamicRegistryManager; |
| 9 | +import net.minecraft.registry.Registerable; |
| 10 | +import net.minecraft.registry.RegistryEntryLookup; |
| 11 | +import net.minecraft.registry.RegistryKey; |
| 12 | +import net.minecraft.registry.RegistryKeys; |
| 13 | +import net.minecraft.registry.RegistryWrapper; |
| 14 | +import net.minecraft.registry.entry.RegistryEntry; |
| 15 | +import net.minecraft.util.Identifier; |
| 16 | + |
| 17 | +import java.util.Optional; |
| 18 | + |
| 19 | +/** |
| 20 | + * Utility class for linking a registry key to an enchantment builder. |
| 21 | + * <p> |
| 22 | + * The EnchantmentContainer allows the enchantment builder to be reused between data-gen and in bootstrapping. |
| 23 | + * <p> |
| 24 | + * The EnchantmentContainer can be fed directly to the {@link CPCEnchantmentProvider.EnchantmentBuilder} during |
| 25 | + * data generation. |
| 26 | + * |
| 27 | + * @see RegistryKey |
| 28 | + * @see Enchantment.Builder |
| 29 | + * @see CPCEnchantmentProvider |
| 30 | + */ |
| 31 | +public class EnchantmentContainer { |
| 32 | + private final RegistryKey<Enchantment> key; |
| 33 | + private final BuilderBuilder internalBuilder; |
| 34 | + |
| 35 | + public EnchantmentContainer (Identifier id, BuilderBuilder builder) { |
| 36 | + this.key = RegistryKey.of(RegistryKeys.ENCHANTMENT, id); |
| 37 | + this.internalBuilder = builder; |
| 38 | + } |
| 39 | + |
| 40 | + public RegistryKey<Enchantment> getKey () { |
| 41 | + return this.key; |
| 42 | + } |
| 43 | + |
| 44 | + public RegistryEntry<Enchantment> getEntryOrThrow (DynamicRegistryManager manager) { |
| 45 | + Optional<RegistryEntry.Reference<Enchantment>> reference = manager.get(RegistryKeys.ENCHANTMENT).getEntry(this.key); |
| 46 | + return reference.orElseThrow(); |
| 47 | + } |
| 48 | + |
| 49 | + public RegistryEntry<Enchantment> getEntryOrThrow (RegistryWrapper.WrapperLookup registryLookup) { |
| 50 | + Optional<RegistryEntry.Reference<Enchantment>> reference = registryLookup.getWrapperOrThrow(RegistryKeys.ENCHANTMENT).getOptional(this.key); |
| 51 | + return reference.orElseThrow(); |
| 52 | + } |
| 53 | + |
| 54 | + public Enchantment.Builder getBuilder (Registerable<Enchantment> registerable) { |
| 55 | + RegistryEntryLookup<Item> itemLookup = registerable.getRegistryLookup(RegistryKeys.ITEM); |
| 56 | + RegistryEntryLookup<Enchantment> enchantmentLookup = registerable.getRegistryLookup(RegistryKeys.ENCHANTMENT); |
| 57 | + RegistryEntryLookup<DamageType> damageLookup = registerable.getRegistryLookup(RegistryKeys.DAMAGE_TYPE); |
| 58 | + RegistryEntryLookup<Block> blockLookup = registerable.getRegistryLookup(RegistryKeys.BLOCK); |
| 59 | + return this.getBuilder(itemLookup, enchantmentLookup, damageLookup, blockLookup); |
| 60 | + } |
| 61 | + |
| 62 | + public Enchantment.Builder getBuilder (CPCEnchantmentProvider.EnchantmentBuilder enchantmentBuilder) { |
| 63 | + return this.getBuilder(enchantmentBuilder.itemLookup, enchantmentBuilder.enchantmentLookup, enchantmentBuilder.damageTypeLookup, enchantmentBuilder.blockLookup); |
| 64 | + } |
| 65 | + |
| 66 | + public Enchantment.Builder getBuilder (RegistryEntryLookup<Item> itemLookup, RegistryEntryLookup<Enchantment> enchantmentLookup, RegistryEntryLookup<DamageType> damageLookup, RegistryEntryLookup<Block> blockLookup) { |
| 67 | + return this.internalBuilder.create(itemLookup, enchantmentLookup, damageLookup, blockLookup); |
| 68 | + } |
| 69 | + |
| 70 | + @FunctionalInterface |
| 71 | + public interface BuilderBuilder { |
| 72 | + Enchantment.Builder create (RegistryEntryLookup<Item> itemLookup, RegistryEntryLookup<Enchantment> enchantmentLookup, RegistryEntryLookup<DamageType> damageLookup, RegistryEntryLookup<Block> blockLookup); |
| 73 | + } |
| 74 | +} |
0 commit comments