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.*;
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<ResourceLocation, ITag.Builder> tagToBuilder = Maps.newLinkedHashMap();
	
	public CommonTagsProvider(GenerationData data, IForgeRegistry<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 -> tagToBuilder.containsKey(id) ? Tag.func_241284_a_() : null, id -> registry.getValue(id)).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.func_232965_c_();
			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> func_240522_a_(ITag.INamedTag<T> p_240522_1_) {
		ITag.Builder itag$builder = this.func_240525_b_(p_240522_1_);
		return new TagsProvider.Builder<>(itag$builder, this.registry, modid);
	}
	
	protected ITag.Builder func_240525_b_(ITag.INamedTag<T> p_240525_1_) {
		return this.tagToBuilder.computeIfAbsent(p_240525_1_.func_230234_a_(), (p_240526_0_) -> {
			return new ITag.Builder();
		});
	}
	
	protected Tag.Builder<T> getBuilder(Tag<T> tag) {
		final Optional<Tag.Builder<T>> optional = tagToBuilder.entrySet().stream().filter(entry -> entry.getKey().getId().equals(tag.getId())).findAny().map(Entry::getValue);
		if (optional.isPresent()) {
			return optional.get();
		} else {
			return tagToBuilder.computeIfAbsent(tag, otherTag -> Tag.Builder.create());
		}
	}
}
@@ -111,36 +111,8 @@
 .*;%0A
-import java.util.Map.Entry;%0A
 impo
@@ -345,21 +345,21 @@
 raft
-forge
+.util
 .registr
 ies.
@@ -358,13 +358,18 @@
 istr
-ies.*
+y.Registry
 ;%0A%0Ap
@@ -413,39 +413,8 @@
 er%3CT
- extends IForgeRegistryEntry%3CT%3E
 %3E ex
@@ -455,22 +455,16 @@
 d final 
-IForge
 Registry
@@ -624,14 +624,8 @@
 ta, 
-IForge
 Regi
@@ -1029,16 +1029,29 @@
 alue(id)
+.orElse(null)
 ).collec
@@ -1656,22 +1656,18 @@
 %3CT%3E 
-func_240522_a_
+getBuilder
 (ITa
@@ -1685,25 +1685,23 @@
 %3CT%3E 
-p_240522_1_) %7B%0A%09%09
+tag) %7B%0A%09%09final 
 ITag
@@ -1709,22 +1709,20 @@
 Builder 
-i
 tag
-$b
+B
 uilder =
@@ -1726,39 +1726,25 @@
 r = 
-this.func_240525_b_(p_240522_1_
+getTagBuilder(tag
 );%0A%09
@@ -1782,22 +1782,20 @@
 r%3C%3E(
-i
 tag
-$b
+B
 uilder, 
 this
@@ -1790,21 +1790,16 @@
 uilder, 
-this.
 registry
@@ -1841,22 +1841,21 @@
 der 
-func_240525_b_
+getTagBuilder
 (ITa
@@ -1869,27 +1869,19 @@
 dTag%3CT%3E 
-p_240525_1_
+tag
 ) %7B%0A%09%09re
@@ -1923,19 +1923,11 @@
 ent(
-p_240525_1_
+tag
 .fun
@@ -1945,437 +1945,40 @@
 (), 
-(p_240526_0_) -%3E %7B%0A%09%09%09return new ITag.Builder();%0A%09%09%7D);%0A%09%7D%0A%09%0A%09protected Tag.Builder%3CT%3E getBuilder(Tag%3CT%3E tag) %7B%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);%0A%09%09if (optional.isPresent()) %7B%0A%09%09%09return optional.get();%0A%09%09%7D else %7B%0A%09%09%09return tagToBuilder.computeIfAbsent(tag, otherTag -%3E Tag.Builder.create());%0A%09%09%7D
+location -%3E new ITag.Builder());
 %0A%09%7D%0A

Fix common tags provider