package info.u_team.u_team_core.data;

import java.io.IOException;
import java.nio.file.Path;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import com.google.common.collect.Maps;
import com.google.gson.JsonObject;

import net.minecraft.data.DirectoryCache;
import net.minecraft.tags.*;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.registries.*;

public abstract class CommonTagsProvider<T extends IForgeRegistryEntry<T>> extends CommonProvider {
	
	protected final IForgeRegistry<T> registry;
	protected final Map<Tag<T>, Tag.Builder<T>> tagToBuilder = Maps.newLinkedHashMap();
	
	protected CommonTagsProvider(GenerationData data, IForgeRegistry<T> registry) {
		super(data);
		this.registry = registry;
	}
	
	protected abstract void registerTags();
	
	@Override
	public void act(DirectoryCache cache) {
		tagToBuilder.clear();
		registerTags();
		
		final TagCollection<T> collection = new TagCollection<>(id -> Optional.empty(), "", false, "generated");
		final Map<ResourceLocation, Tag.Builder<T>> map = tagToBuilder.entrySet().stream().collect(Collectors.toMap(entry -> entry.getKey().getId(), Entry::getValue));
		
		collection.registerAll(map);
		collection.getTagMap().forEach((location, tag) -> {
			final JsonObject object = tag.serialize(registry::getKey);
			final Path path = makePath(location);
			try {
				write(cache, object, path);
			} catch (final IOException ex) {
				LOGGER.error(marker, "Could not write data.", ex);
			}
			
		});
		setCollection(collection);
	}
	
	protected abstract void setCollection(TagCollection<T> collection);
	
	protected abstract Path makePath(ResourceLocation location);
	
	protected Tag.Builder<T> getBuilder(Tag<T> tag) {
		return tagToBuilder.computeIfAbsent(tag, otherTag -> Tag.Builder.create());
	}
}
@@ -1788,16 +1788,257 @@
 tag) %7B%0D%0A
+%09%09final Optional%3CTag.Builder%3CT%3E%3E optional = tagToBuilder.entrySet().stream().filter(entry -%3E entry.getKey().getId().equals(tag.getId())).findAny().map(Entry::getValue);%0D%0A%09%09if (optional.isPresent()) %7B%0D%0A%09%09%09return optional.get();%0D%0A%09%09%7D else %7B%0D%0A%09
 %09%09return
@@ -2104,17 +2104,22 @@
 create());%0D%0A
+%09%09%7D%0D%0A
 %09%7D%0D%0A%7D

Fix crash when a tag with the same id but a diffrent wrapper instance is used to register to the builder