|
| 1 | +package doggytalents.client.data; |
| 2 | + |
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.OutputStreamWriter; |
| 6 | +import java.nio.charset.StandardCharsets; |
| 7 | +import java.nio.file.Path; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.Locale; |
| 10 | +import java.util.concurrent.CompletableFuture; |
| 11 | + |
| 12 | +import com.google.common.hash.Hashing; |
| 13 | +import com.google.common.hash.HashingOutputStream; |
| 14 | +import com.google.gson.JsonElement; |
| 15 | +import com.google.gson.stream.JsonWriter; |
| 16 | +import com.mojang.serialization.JsonOps; |
| 17 | + |
| 18 | +import doggytalents.api.anim.DogAnimation; |
| 19 | +import doggytalents.client.entity.model.animation.DTNAnimationCodec; |
| 20 | +import doggytalents.client.entity.model.animation.DTNAnimationLoader; |
| 21 | +import doggytalents.client.entity.model.animation.DogAnimationRegistry; |
| 22 | +import doggytalents.common.util.Util; |
| 23 | +import net.minecraft.core.HolderLookup; |
| 24 | +import net.minecraft.data.CachedOutput; |
| 25 | +import net.minecraft.data.DataProvider; |
| 26 | +import net.minecraft.data.PackOutput; |
| 27 | +import net.minecraft.data.PackOutput.Target; |
| 28 | +import net.minecraft.util.GsonHelper; |
| 29 | + |
| 30 | +public class DogAnimationJsonProvider implements DataProvider { |
| 31 | + |
| 32 | + private final PackOutput output; |
| 33 | + |
| 34 | + public DogAnimationJsonProvider(PackOutput output) { |
| 35 | + this.output = output; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public CompletableFuture<?> run(CachedOutput output) { |
| 40 | + var tasks = new ArrayList<CompletableFuture<?>>(); |
| 41 | + DogAnimationRegistry.init(); |
| 42 | + var path_prov = this.output.createPathProvider(Target.RESOURCE_PACK, |
| 43 | + DTNAnimationLoader.createRegistryPath()); |
| 44 | + for (var dog_anim : DogAnimation.values()) { |
| 45 | + var sequence = DogAnimationRegistry.getSequence(dog_anim); |
| 46 | + if (sequence == null) |
| 47 | + continue; |
| 48 | + var anim_json_optional = DTNAnimationCodec.CODEC.encodeStart(JsonOps.INSTANCE, sequence).result(); |
| 49 | + if (!anim_json_optional.isPresent()) |
| 50 | + continue; |
| 51 | + var anim_json = anim_json_optional.get(); |
| 52 | + var file_name = dog_anim.name().toLowerCase(Locale.ROOT); |
| 53 | + var save_path = path_prov.json(Util.getResource(file_name)); |
| 54 | + var save_future = |
| 55 | + saveStableNoIndent(output, anim_json, save_path); |
| 56 | + tasks.add(save_future); |
| 57 | + } |
| 58 | + return CompletableFuture.allOf(tasks.toArray(CompletableFuture[]::new)); |
| 59 | + } |
| 60 | + |
| 61 | + private static CompletableFuture<?> saveStableNoIndent(CachedOutput output, JsonElement json, Path path) { |
| 62 | + return CompletableFuture.runAsync(() -> { |
| 63 | + try { |
| 64 | + var content_bytes = new ByteArrayOutputStream(); |
| 65 | + var hashing_output = new HashingOutputStream(Hashing.sha1(), content_bytes); |
| 66 | + |
| 67 | + try (var writer = new JsonWriter( |
| 68 | + new OutputStreamWriter(hashing_output, StandardCharsets.UTF_8))) { |
| 69 | + |
| 70 | + writer.setSerializeNulls(false); |
| 71 | + GsonHelper.writeValue(writer, json, KEY_COMPARATOR); |
| 72 | + } |
| 73 | + |
| 74 | + output.writeIfNeeded(path, content_bytes.toByteArray(), hashing_output.hash()); |
| 75 | + } catch (IOException ioexception) { |
| 76 | + LOGGER.error("Failed to save file to {}", path, ioexception); |
| 77 | + } |
| 78 | + }, net.minecraft.Util.backgroundExecutor()); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public String getName() { |
| 83 | + return "DTN Animation Json Generator"; |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments