1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-15 20:50:29 +00:00

Move the model cache inside TurtleModelParts

This removes a tiny bit of duplication (at the cost of mode code), but
makes the interface more intuitive, as there's no bouncing between
getCombination -> cache -> buildModel.
This commit is contained in:
Jonathan Coates 2023-07-01 18:26:38 +01:00
parent ecf880ed82
commit 9eabb29999
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
4 changed files with 30 additions and 23 deletions

View File

@ -28,8 +28,10 @@ import java.util.function.Function;
/**
* Combines several individual models together to form a turtle.
*
* @param <T> The type of the resulting "baked model".
*/
public final class TurtleModelParts {
public final class TurtleModelParts<T> {
private static final Transformation identity, flip;
static {
@ -42,7 +44,7 @@ public final class TurtleModelParts {
flip = new Transformation(stack.last().pose());
}
public record Combination(
private record Combination(
boolean colour,
@Nullable ITurtleUpgrade leftUpgrade,
@Nullable ITurtleUpgrade rightUpgrade,
@ -55,6 +57,7 @@ public final class TurtleModelParts {
private final BakedModel familyModel;
private final BakedModel colourModel;
private final Function<TransformedModel, BakedModel> transformer;
private final Function<Combination, T> buildModel;
/**
* A cache of {@link TransformedModel} to the transformed {@link BakedModel}. This helps us pool the transformed
@ -62,13 +65,23 @@ public final class TurtleModelParts {
*/
private final Map<TransformedModel, BakedModel> transformCache = new HashMap<>();
public TurtleModelParts(BakedModel familyModel, BakedModel colourModel, ModelTransformer transformer) {
/**
* A cache of {@link Combination}s to the combined model.
*/
private final Map<Combination, T> modelCache = new HashMap<>();
public TurtleModelParts(BakedModel familyModel, BakedModel colourModel, ModelTransformer transformer, Function<List<BakedModel>, T> combineModel) {
this.familyModel = familyModel;
this.colourModel = colourModel;
this.transformer = x -> transformer.transform(x.getModel(), x.getMatrix());
buildModel = x -> combineModel.apply(buildModel(x));
}
public Combination getCombination(ItemStack stack) {
public T getModel(ItemStack stack) {
return modelCache.computeIfAbsent(getCombination(stack), buildModel);
}
private Combination getCombination(ItemStack stack) {
var christmas = Holiday.getCurrent() == Holiday.CHRISTMAS;
if (!(stack.getItem() instanceof TurtleItem turtle)) {
@ -85,7 +98,7 @@ public final class TurtleModelParts {
return new Combination(colour != -1, leftUpgrade, rightUpgrade, overlay, christmas, flip);
}
public List<BakedModel> buildModel(Combination combo) {
private List<BakedModel> buildModel(Combination combo) {
var mc = Minecraft.getInstance();
var modelManager = mc.getItemRenderer().getItemModelShaper().getModelManager();
@ -109,7 +122,7 @@ public final class TurtleModelParts {
return parts;
}
public BakedModel transform(BakedModel model, Transformation transformation) {
private BakedModel transform(BakedModel model, Transformation transformation) {
if (transformation.equals(Transformation.identity())) return model;
return transformCache.computeIfAbsent(new TransformedModel(model, transformation), transformer);
}

View File

@ -25,6 +25,10 @@ public class CompositeBakedModel extends CustomBakedModel {
this.models = models;
}
public static BakedModel of(List<BakedModel> models) {
return models.size() == 1 ? models.get(0) : new CompositeBakedModel(models);
}
@Override
public List<BakedQuad> getQuads(@Nullable BlockState blockState, @Nullable Direction face, RandomSource rand) {
@SuppressWarnings({ "unchecked", "rawtypes" })

View File

@ -14,8 +14,6 @@ import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
/**
* The custom model for turtle items, which renders tools and overlays as part of the model.
@ -23,28 +21,22 @@ import java.util.Map;
* @see TurtleModelParts
*/
public class TurtleModel extends ForwardingBakedModel {
private final TurtleModelParts parts;
private final TurtleModelParts<BakedModel> parts;
private final Map<TurtleModelParts.Combination, BakedModel> cachedModels = new HashMap<>();
private final ItemOverrides overrides = new ItemOverrides() {
@Override
public BakedModel resolve(BakedModel model, ItemStack stack, @Nullable ClientLevel level, @Nullable LivingEntity entity, int seed) {
return cachedModels.computeIfAbsent(parts.getCombination(stack), TurtleModel.this::buildModel);
return parts.getModel(stack);
}
};
public TurtleModel(BakedModel familyModel, BakedModel colourModel) {
wrapped = familyModel;
parts = new TurtleModelParts(familyModel, colourModel, TransformedBakedModel::new);
parts = new TurtleModelParts<>(familyModel, colourModel, TransformedBakedModel::new, CompositeBakedModel::of);
}
@Override
public ItemOverrides getOverrides() {
return overrides;
}
private BakedModel buildModel(TurtleModelParts.Combination combo) {
var models = parts.buildModel(combo);
return models.size() == 1 ? models.get(0) : new CompositeBakedModel(models);
}
}

View File

@ -11,9 +11,8 @@ import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.client.model.BakedModelWrapper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
/**
* The custom model for turtle items, which renders tools and overlays as part of the model.
@ -21,12 +20,11 @@ import java.util.Map;
* @see TurtleModelParts
*/
public class TurtleModel extends BakedModelWrapper<BakedModel> {
private final TurtleModelParts parts;
private final Map<TurtleModelParts.Combination, List<BakedModel>> cachedModels = new HashMap<>();
private final TurtleModelParts<List<BakedModel>> parts;
public TurtleModel(BakedModel familyModel, BakedModel colourModel) {
super(familyModel);
parts = new TurtleModelParts(familyModel, colourModel, TransformedBakedModel::new);
parts = new TurtleModelParts<>(familyModel, colourModel, TransformedBakedModel::new, Function.identity());
}
@Override
@ -37,6 +35,6 @@ public class TurtleModel extends BakedModelWrapper<BakedModel> {
@Override
public List<BakedModel> getRenderPasses(ItemStack stack, boolean fabulous) {
return cachedModels.computeIfAbsent(parts.getCombination(stack), parts::buildModel);
return parts.getModel(stack);
}
}