diff --git a/Common/src/main/java/dev/lukebemish/excavatedvariants/impl/ExcavatedVariantsClient.java b/Common/src/main/java/dev/lukebemish/excavatedvariants/impl/ExcavatedVariantsClient.java index a1adbd35..64e57267 100644 --- a/Common/src/main/java/dev/lukebemish/excavatedvariants/impl/ExcavatedVariantsClient.java +++ b/Common/src/main/java/dev/lukebemish/excavatedvariants/impl/ExcavatedVariantsClient.java @@ -10,6 +10,7 @@ import dev.lukebemish.dynamicassetgenerator.api.ResourceCache; import dev.lukebemish.dynamicassetgenerator.api.ResourceGenerationContext; import dev.lukebemish.dynamicassetgenerator.api.client.AssetResourceCache; +import dev.lukebemish.excavatedvariants.impl.client.ItemModelPlanner; import dev.lukebemish.excavatedvariants.impl.client.ResourceAssembler; import dev.lukebemish.excavatedvariants.impl.data.BaseOre; import dev.lukebemish.excavatedvariants.impl.data.BaseStone; @@ -51,13 +52,16 @@ public static void init() { )); List> toMake = new ArrayList<>(); + ItemModelPlanner planner = new ItemModelPlanner(); + ASSET_CACHE.planResetListener(planner); + ASSET_CACHE.planSource(planner); + for (Pair> p : ExcavatedVariants.oreStoneList) { var ore = p.getFirst(); for (BaseStone stone : p.getSecond()) { String fullId = stone.id + "_" + ore.id; toMake.add(new Pair<>(ore, stone)); - ASSET_CACHE.planSource(new ResourceLocation(ExcavatedVariants.MOD_ID, "models/item/" + fullId + ".json"), - (rl, context) -> JsonHelper.getItemModel(fullId)); + planner.add(fullId); LANG_BUILDER.add(fullId, stone, ore); } } diff --git a/Common/src/main/java/dev/lukebemish/excavatedvariants/impl/JsonHelper.java b/Common/src/main/java/dev/lukebemish/excavatedvariants/impl/JsonHelper.java deleted file mode 100644 index a02f3754..00000000 --- a/Common/src/main/java/dev/lukebemish/excavatedvariants/impl/JsonHelper.java +++ /dev/null @@ -1,18 +0,0 @@ -/* - * Copyright (C) 2023 Luke Bemish and contributors - * SPDX-License-Identifier: LGPL-3.0-or-later - */ - -package dev.lukebemish.excavatedvariants.impl; - -import java.io.ByteArrayInputStream; -import java.io.InputStream; - -import net.minecraft.server.packs.resources.IoSupplier; - -public class JsonHelper { - public static IoSupplier getItemModel(String id) { - String json = "{\"parent\": \"" + ExcavatedVariants.MOD_ID + ":" + "block/" + id + "_0\"}"; - return () -> new ByteArrayInputStream(json.getBytes()); - } -} diff --git a/Common/src/main/java/dev/lukebemish/excavatedvariants/impl/client/ItemModelPlanner.java b/Common/src/main/java/dev/lukebemish/excavatedvariants/impl/client/ItemModelPlanner.java new file mode 100644 index 00000000..0f12c364 --- /dev/null +++ b/Common/src/main/java/dev/lukebemish/excavatedvariants/impl/client/ItemModelPlanner.java @@ -0,0 +1,73 @@ +package dev.lukebemish.excavatedvariants.impl.client; + +import com.google.gson.JsonObject; +import dev.lukebemish.dynamicassetgenerator.api.PathAwareInputStreamSource; +import dev.lukebemish.dynamicassetgenerator.api.Resettable; +import dev.lukebemish.dynamicassetgenerator.api.ResourceGenerationContext; +import dev.lukebemish.dynamicassetgenerator.api.client.ClientPrePackRepository; +import dev.lukebemish.excavatedvariants.impl.ExcavatedVariants; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.packs.resources.IoSupplier; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +public class ItemModelPlanner implements PathAwareInputStreamSource, Resettable { + private final List ids = new ArrayList<>(); + + @Nullable + private JsonObject baseModel; + boolean tried = false; + + public void add(String id) { + ids.add(id); + } + + @Override + public @NotNull Set getLocations() { + return ids.stream().map(fullId -> new ResourceLocation(ExcavatedVariants.MOD_ID, "models/item/" + fullId + ".json")).collect(Collectors.toSet()); + } + + @Override + public @Nullable IoSupplier get(ResourceLocation outRl, ResourceGenerationContext context) { + try { + if (baseModel == null) { + setupBaseModel(); + } + } catch (IOException e) { + return null; + } + + return () -> { + var json = baseModel.deepCopy(); + json.addProperty("parent", ExcavatedVariants.MOD_ID + ":block/" + outRl.getPath().replace(".json", "").replace("models/item/", "") + "_0"); + return new ByteArrayInputStream(ExcavatedVariants.GSON_CONDENSED.toJson(json).getBytes()); + }; + } + + private synchronized void setupBaseModel() throws IOException { + try (var is = ClientPrePackRepository.getResource(new ResourceLocation("models/block/block.json"))) { + baseModel = ExcavatedVariants.GSON_CONDENSED.fromJson(new String(is.readAllBytes()), JsonObject.class); + } catch (Exception e) { + if (!tried) { + tried = true; + ExcavatedVariants.LOGGER.error("Failed to load base block model", e); + return; + } + throw new IOException(e); + } + } + + @Override + public void reset() { + baseModel = null; + tried = false; + } +} diff --git a/version.properties b/version.properties index e455e4cc..5525bc81 100644 --- a/version.properties +++ b/version.properties @@ -1,2 +1,2 @@ #Sat Sep 09 08:39:33 UTC 2023 -version=3.0.8 +version=3.0.9