Skip to content

Commit

Permalink
Fix issue with models that do not parent block/block
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebemish committed Mar 8, 2024
1 parent 19cb18a commit 8387f54
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,13 +52,16 @@ public static void init() {
));
List<Pair<BaseOre, BaseStone>> toMake = new ArrayList<>();

ItemModelPlanner planner = new ItemModelPlanner();
ASSET_CACHE.planResetListener(planner);
ASSET_CACHE.planSource(planner);

for (Pair<BaseOre, HashSet<BaseStone>> 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);
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<String> ids = new ArrayList<>();

@Nullable
private JsonObject baseModel;
boolean tried = false;

public void add(String id) {
ids.add(id);
}

@Override
public @NotNull Set<ResourceLocation> getLocations() {
return ids.stream().map(fullId -> new ResourceLocation(ExcavatedVariants.MOD_ID, "models/item/" + fullId + ".json")).collect(Collectors.toSet());
}

@Override
public @Nullable IoSupplier<InputStream> 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;
}
}
2 changes: 1 addition & 1 deletion version.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#Sat Sep 09 08:39:33 UTC 2023
version=3.0.8
version=3.0.9

0 comments on commit 8387f54

Please sign in to comment.