package info.u_team.u_team_core.data;

import java.io.IOException;
import java.util.function.Consumer;
import java.util.stream.Stream;

import org.apache.logging.log4j.*;

import info.u_team.u_team_core.api.registry.IUArrayRegistryType;
import net.minecraft.block.Block;
import net.minecraft.data.*;
import net.minecraft.item.Item;
import net.minecraft.util.*;
import net.minecraftforge.client.model.generators.ItemModelProvider;
import net.minecraftforge.client.model.generators.ModelFile.UncheckedModelFile;

public abstract class CommonItemModelsProvider extends ItemModelProvider {
	
	protected final Marker marker;
	
	protected final GenerationData data;
	protected final String modid;
	protected final DataGenerator generator;
	
	public CommonItemModelsProvider(GenerationData data) {
		super(data.getGenerator(), data.getModid(), data.getExistingFileHelper());
		this.data = data;
		this.modid = data.getModid();
		this.generator = data.getGenerator();
		marker = MarkerManager.getMarker(getName());
	}
	
	@Override
	public void act(DirectoryCache cache) throws IOException {
		generatedModels.clear();
		registerModels();
		generatedModels.values().forEach(model -> {
			try {
				final ResourceLocation location = model.getLocation();
				CommonProvider.write(cache, model.toJson(), generator.getOutputFolder().resolve("assets/" + location.getNamespace() + "/models/" + location.getPath() + ".json"));
			} catch (final IOException ex) {
				CommonProvider.LOGGER.error(marker, "Could not write data.", ex);
			}
		});
	}
	
	@Override
	public String getName() {
		return "Item-Models";
	}
	
	// Item model methods
	
	protected void simpleGenerated(IItemProvider provider) {
		simpleParent(provider, "item/generated");
	}
	
	protected void simpleHandheld(IItemProvider provider) {
		simpleParent(provider, "item/handheld");
	}
	
	protected void simpleParent(IItemProvider provider, final String parent) {
		final String registryPath = provider.asItem().getRegistryName().getPath();
		getBuilder(registryPath).parent(new UncheckedModelFile(parent)).texture("layer0", "item/" + registryPath);
	}
	
	protected void simpleBlock(Block block) {
		final ResourceLocation registryName = block.getRegistryName();
		getBuilder(registryName.getPath()).parent(new UncheckedModelFile(new ResourceLocation(registryName.getNamespace(), "block/" + registryName.getPath())));
	}
	
	// Utility methods
	protected void iterateItems(IUArrayRegistryType<Item> type, Consumer<IItemProvider> item) {
		Stream.of(type.getArray()).forEach(item);
	}
	
	protected void iterateBlocks(IUArrayRegistryType<Block> type, Consumer<IItemProvider> item) {
		Stream.of(type.getArray()).forEach(item);
	}
	
	protected String getPath(IItemProvider provider) {
		return provider.asItem().getRegistryName().getPath();
	}
	
}

@@ -81,24 +81,15 @@
 til.
-function.Consume
+Iterato
 r;%0Ai
@@ -108,21 +108,25 @@
 til.
-stream.Stream
+function.Consumer
 ;%0A%0Ai
@@ -164,73 +164,8 @@
 *;%0A%0A
-import info.u_team.u_team_core.api.registry.IUArrayRegistryType;%0A
 impo
@@ -2349,37 +2349,40 @@
 ms(I
-UArrayRegistryType%3CItem%3E type
+terator%3C? extends Item%3E iterator
 , Co
@@ -2417,42 +2417,33 @@
 %7B%0A%09%09
-Stream.of(type.getArray()).forEach
+iterator.forEachRemaining
 (ite
@@ -2486,38 +2486,41 @@
 ks(I
-UArrayRegistryType%3CBlock%3E type
+terator%3C? extends Block%3E iterator
 , Co
@@ -2555,42 +2555,33 @@
 %7B%0A%09%09
-Stream.of(type.getArray()).forEach
+iterator.forEachRemaining
 (ite

Update iterator items / blocks methods