package info.u_team.u_team_core.data;

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

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

import net.minecraft.data.*;
import net.minecraft.tags.*;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.registry.Registry;

public abstract class CommonTagsProvider<T> extends CommonProvider {
	
	protected final Registry<T> registry;
	protected final Map<ResourceLocation, ITag.Builder> tagToBuilder = Maps.newLinkedHashMap();
	
	public CommonTagsProvider(GenerationData data, Registry<T> registry) {
		super(data);
		this.registry = registry;
	}
	
	protected abstract void registerTags();
	
	@Override
	public void act(DirectoryCache cache) {
		tagToBuilder.clear();
		registerTags();
		
		tagToBuilder.forEach((location, builder) -> {
			final List<ITag.Proxy> list = builder.func_232963_b_(id -> Tag.func_241284_a_(), id -> registry.func_241873_b(id).orElse(null)).collect(Collectors.toList());
			if (!list.isEmpty()) {
				throw new IllegalArgumentException(String.format("Couldn't define tag %s as it is missing following references: %s", location, list.stream().map(Objects::toString).collect(Collectors.joining(","))));
			}
			final JsonObject object = builder.serialize();
			final Path path = makePath(location);
			try {
				write(cache, object, path);
			} catch (final IOException ex) {
				LOGGER.error(marker, "Could not write data.", ex);
			}
		});
	}
	
	protected abstract Path makePath(ResourceLocation location);
	
	protected TagsProvider.Builder<T> getBuilder(ITag.INamedTag<T> tag) {
		final ITag.Builder tagBuilder = getTagBuilder(tag);
		return new UniqueBuilder<>(tagBuilder, registry, modid);
	}
	
	protected ITag.Builder getTagBuilder(ITag.INamedTag<T> tag) {
		return this.tagToBuilder.computeIfAbsent(tag.getName(), location -> new ITag.Builder());
	}
	
	public static class UniqueBuilder<T> extends TagsProvider.Builder<T> {
		
		public UniqueBuilder(ITag.Builder builder, Registry<T> registry, String id) {
			super(builder, registry, id);
		}
		
	}
}
@@ -108,24 +108,62 @@
 va.util.*;%0D%0A
+import java.util.function.Predicate;%0D%0A
 import java.
@@ -322,12 +322,96 @@
 aft.
-tags
+data.TagsProvider.Builder;%0D%0Aimport net.minecraft.tags.*;%0D%0Aimport net.minecraft.tags.ITag
 .*;%0D
@@ -2184,16 +2184,59 @@
  %7B%0D%0A%09%09%0D%0A
+%09%09private final Registry%3CT%3E registry;%0D%0A%09%09%0D%0A
 %09%09public
@@ -2342,16 +2342,565 @@
 , id);%0D%0A
+%09%09%09this.registry = registry;%0D%0A%09%09%7D%0D%0A%09%09%0D%0A%09%09@Override%0D%0A%09%09public Builder%3CT%3E addItemEntry(T item) %7B%0D%0A%09%09%09final ResourceLocation location = registry.getKey(item);%0D%0A%09%09%09return super.addItemEntry(item);%0D%0A%09%09%7D%0D%0A%09%09%0D%0A%09%09private %3CC extends ITagEntry%3E void canAdd(Class%3CC%3E clazz, Predicate%3C? super C%3E predicate, Runnable add) %7B%0D%0A%09%09%09final boolean duplicate = getInternalBuilder().getProxyStream() //%0D%0A%09%09%09%09%09.map(Proxy::getEntry) //%0D%0A%09%09%09%09%09.filter(clazz::isInstance) //%0D%0A%09%09%09%09%09.map(clazz::cast) //%0D%0A%09%09%09%09%09.anyMatch(predicate);%0D%0A%09%09%09if (!duplicate) %7B%0D%0A%09%09%09%09add.run();%0D%0A%09%09%09%7D%0D%0A
 %09%09%7D%0D%0A%09%09%0D

Add duplicate method check