1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-11-01 06:03:00 +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
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);
}