|
| 1 | +package io.wispforest.owo.util; |
| 2 | + |
| 3 | +import net.minecraft.block.Block; |
| 4 | +import net.minecraft.item.Item; |
| 5 | +import net.minecraft.util.Identifier; |
| 6 | +import net.minecraft.util.registry.Registry; |
| 7 | + |
| 8 | +import java.util.*; |
| 9 | + |
| 10 | +/** |
| 11 | + * A simple utility for inserting values into Tags at runtime |
| 12 | + */ |
| 13 | +public class TagInjector { |
| 14 | + |
| 15 | + public static final HashMap<Identifier, Set<Identifier>> ADDITIIONS = new HashMap<>(); |
| 16 | + |
| 17 | + /** |
| 18 | + * Injects the given Identifiers into the given Tag. |
| 19 | + * If the Identifiers don't correspond to an object in the |
| 20 | + * relevant Registry, you <i>will</i> break the Tag. |
| 21 | + * If the Tag does not exist, it will be created. |
| 22 | + * |
| 23 | + * @param tag The tag to insert into, this could contain all kinds of values |
| 24 | + * @param values The values to insert |
| 25 | + */ |
| 26 | + public static void injectRaw(Identifier tag, Collection<Identifier> values) { |
| 27 | + ADDITIIONS.computeIfAbsent(tag, identifier -> new HashSet<>()).addAll(values); |
| 28 | + } |
| 29 | + |
| 30 | + public static void injectRaw(Identifier tag, Identifier... values) { |
| 31 | + injectRaw(tag, Arrays.asList(values)); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * A convenience method for directly inserting Blocks into the given Tag. |
| 36 | + * If any of Blocks are not registered yet, this <i>will</i> throw an |
| 37 | + * exception. |
| 38 | + */ |
| 39 | + public static void injectBlocks(Identifier tag, Collection<Block> values) { |
| 40 | + injectRaw(tag, values.stream().map(Registry.BLOCK::getId).toList()); |
| 41 | + } |
| 42 | + |
| 43 | + public static void injectBlocks(Identifier tag, Block... values) { |
| 44 | + injectRaw(tag, Arrays.stream(values).map(Registry.BLOCK::getId).toList()); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * A convenience method for directly inserting Items into the given Tag. |
| 49 | + * If any of Items are not registered yet, this <i>will</i> throw an |
| 50 | + * exception. |
| 51 | + */ |
| 52 | + public static void injectItems(Identifier tag, Collection<Item> values) { |
| 53 | + injectRaw(tag, values.stream().map(Registry.ITEM::getId).toList()); |
| 54 | + } |
| 55 | + |
| 56 | + public static void injectItems(Identifier tag, Item... values) { |
| 57 | + injectRaw(tag, Arrays.stream(values).map(Registry.ITEM::getId).toList()); |
| 58 | + } |
| 59 | + |
| 60 | +} |
0 commit comments