package info.u_team.u_team_core.data;

import java.io.*;
import java.nio.file.*;
import java.util.Objects;

import org.apache.logging.log4j.*;

import com.google.gson.*;
import com.google.gson.internal.Streams;
import com.google.gson.stream.JsonWriter;

import net.minecraft.data.*;

public abstract class CommonProvider implements IDataProvider {
	
	protected static final Logger LOGGER = LogManager.getLogger("DataGenerator");
	protected static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
	
	protected final Marker marker;
	
	protected final DataGenerator generator;
	protected final Path path;
	
	public CommonProvider(DataGenerator generator) {
		this.generator = generator;
		this.path = resolvePath(generator.getOutputFolder());
		marker = MarkerManager.getMarker(getName());
	}
	
	protected abstract Path resolvePath(Path outputFolder);
	
	protected Path resolveAssets(Path outputFolder, String modid) {
		return outputFolder.resolve("assets").resolve(modid);
	}
	
	protected Path resolveData(Path outputFolder, String modid) {
		return outputFolder.resolve("data").resolve(modid);
	}
	
	protected void write(DirectoryCache cache, JsonElement element, Path path) throws IOException {
		final StringWriter writer = new StringWriter();
		try {
			final JsonWriter jsonWriter = GSON.newJsonWriter(Streams.writerForAppendable(writer));
			jsonWriter.setIndent("	"); // Use tab instead of two spaces
			GSON.toJson(element, jsonWriter);
		} catch (IOException e) {
			throw new JsonIOException(e);
		}
		write(cache, writer.toString(), path);
	}
	
	protected void write(DirectoryCache cache, String string, Path path) throws IOException {
		final String hash = HASH_FUNCTION.hashUnencodedChars(string).toString();
		if (!Objects.equals(cache.getPreviousHash(path), hash) || !Files.exists(path)) {
			Files.createDirectories(path.getParent());
			try (final BufferedWriter writer = Files.newBufferedWriter(path)) {
				writer.write(string);
			}
		}
		cache.recordHash(path, hash);
	}
}

@@ -190,68 +190,73 @@
 son.
-internal.Streams;%0Aimport com.google.gson.stream.JsonWriter;%0A
+stream.JsonWriter;%0A%0Aimport info.u_team.u_team_core.util.GsonUtil;
 %0Aimp
@@ -1205,32 +1205,37 @@
 IOException %7B%0A%09%09
+try (
 final StringWrit
@@ -1269,17 +1269,13 @@
 r();
-%0A%09%09try %7B%0A
+ //%0A%09
 %09%09%09f
@@ -1308,161 +1308,125 @@
  = G
-SON.newJsonWriter(Streams.writerForAppendable(writer));%0A%09%09%09jsonWriter.setIndent(%22%09%22); // Use tab instead of two spaces%0A%09%09%09GSON.toJson(element, jsonWriter
+sonUtil.createTabWriter(GSON, writer)) %7B%0A%09%09%09GSON.toJson(element, jsonWriter);%0A%09%09%09write(cache, writer.toString(), path
 );%0A%09
@@ -1493,49 +1493,8 @@
 %09%09%7D%0A
-%09%09write(cache, writer.toString(), path);%0A
 %09%7D%0A%09

Make the writer better with try with resources