Skip to content

Commit

Permalink
WIP Model refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaMode committed Jul 26, 2024
1 parent 9a7f279 commit 33a13b3
Show file tree
Hide file tree
Showing 37 changed files with 659 additions and 453 deletions.
1 change: 0 additions & 1 deletion modules/model_builders/build.gradle

This file was deleted.

7 changes: 0 additions & 7 deletions modules/model_builders/src/main/resources/fabric.mod.json

This file was deleted.

1 change: 0 additions & 1 deletion modules/model_generators/build.gradle

This file was deleted.

This file was deleted.

7 changes: 0 additions & 7 deletions modules/model_generators/src/main/resources/fabric.mod.json

This file was deleted.

This file was deleted.

This file was deleted.

Empty file.

This file was deleted.

This file was deleted.

This file was deleted.

7 changes: 0 additions & 7 deletions modules/model_materials/src/main/resources/fabric.mod.json

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion modules/models/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
portingLib {
addModuleDependencies(["model_loader", "transfer"])
addModuleDependencies(["model_loader", "transfer", "data"])
enableTestMod()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.github.fabricators_of_create.porting_lib.models;

import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Codec;
import com.mojang.serialization.JsonOps;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import java.util.function.Function;
import net.minecraft.client.renderer.block.model.BlockElement;
import net.minecraft.client.renderer.block.model.BlockElementFace;
import org.jetbrains.annotations.Nullable;

/**
* Holds extra data that may be injected into a face.<p>
* Used by {@link ItemLayerModel}, {@link BlockElement} and {@link BlockElementFace}
*
* @param color Color in ARGB format
* @param blockLight Block Light for this face from 0-15 (inclusive)
* @param skyLight Sky Light for this face from 0-15 (inclusive)
* @param ambientOcclusion If this face has AO
*/
public record ExtraFaceData(int color, int blockLight, int skyLight, boolean ambientOcclusion) {

public static final ExtraFaceData DEFAULT = new ExtraFaceData(0xFFFFFFFF, 0, 0, true);

public static final Codec<Integer> COLOR = Codec.either(Codec.INT, Codec.STRING).xmap(
either -> either.map(Function.identity(), str -> (int) Long.parseLong(str, 16)),
color -> Either.right(Integer.toHexString(color)));

public static final Codec<ExtraFaceData> CODEC = RecordCodecBuilder.create(
builder -> builder
.group(
COLOR.optionalFieldOf("color", 0xFFFFFFFF).forGetter(ExtraFaceData::color),
Codec.intRange(0, 15).optionalFieldOf("block_light", 0).forGetter(ExtraFaceData::blockLight),
Codec.intRange(0, 15).optionalFieldOf("sky_light", 0).forGetter(ExtraFaceData::skyLight),
Codec.BOOL.optionalFieldOf("ambient_occlusion", true).forGetter(ExtraFaceData::ambientOcclusion))
.apply(builder, ExtraFaceData::new));
/**
* Parses an ExtraFaceData from JSON
*
* @param obj The JsonObject to parse from, weakly-typed to JsonElement to reduce logic complexity.
* @param fallback What to return if the first parameter is null.
* @return The parsed ExtraFaceData, or the fallback parameter if the first parmeter is null.
* @throws JsonParseException
*/
@Nullable
public static ExtraFaceData read(@Nullable JsonElement obj, @Nullable ExtraFaceData fallback) throws JsonParseException {
if (obj == null) {
return fallback;
}
return CODEC.parse(JsonOps.INSTANCE, obj).getOrThrow(JsonParseException::new);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package io.github.fabricators_of_create.porting_lib.models.builders;

import com.google.common.base.Preconditions;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.google.common.base.Preconditions;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import io.github.fabricators_of_create.porting_lib.core.PortingLib;
import io.github.fabricators_of_create.porting_lib.data.ExistingFileHelper;
import io.github.fabricators_of_create.porting_lib.models.generators.CustomLoaderBuilder;
Expand All @@ -24,7 +28,7 @@ public static <T extends ModelBuilder<T>> CompositeModelBuilder<T> begin(T paren
private final List<String> itemRenderOrder = new ArrayList<>();

protected CompositeModelBuilder(T parent, ExistingFileHelper existingFileHelper) {
super(PortingLib.id("composite"), parent, existingFileHelper);
super(PortingLib.id("composite"), parent, existingFileHelper, false);
}

public CompositeModelBuilder<T> child(String name, T modelBuilder) {
Expand All @@ -51,7 +55,7 @@ public JsonObject toJson(JsonObject json) {
json = super.toJson(json);

JsonObject children = new JsonObject();
for(Map.Entry<String, T> entry : childModels.entrySet()) {
for (Map.Entry<String, T> entry : childModels.entrySet()) {
children.add(entry.getKey(), entry.getValue().toJson());
}
json.add("children", children);
Expand Down
Loading

0 comments on commit 33a13b3

Please sign in to comment.