mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2024-12-15 04:30: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:
parent
ecf880ed82
commit
9eabb29999
@ -28,8 +28,10 @@ import java.util.function.Function;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Combines several individual models together to form a turtle.
|
* 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;
|
private static final Transformation identity, flip;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
@ -42,7 +44,7 @@ public final class TurtleModelParts {
|
|||||||
flip = new Transformation(stack.last().pose());
|
flip = new Transformation(stack.last().pose());
|
||||||
}
|
}
|
||||||
|
|
||||||
public record Combination(
|
private record Combination(
|
||||||
boolean colour,
|
boolean colour,
|
||||||
@Nullable ITurtleUpgrade leftUpgrade,
|
@Nullable ITurtleUpgrade leftUpgrade,
|
||||||
@Nullable ITurtleUpgrade rightUpgrade,
|
@Nullable ITurtleUpgrade rightUpgrade,
|
||||||
@ -55,6 +57,7 @@ public final class TurtleModelParts {
|
|||||||
private final BakedModel familyModel;
|
private final BakedModel familyModel;
|
||||||
private final BakedModel colourModel;
|
private final BakedModel colourModel;
|
||||||
private final Function<TransformedModel, BakedModel> transformer;
|
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
|
* 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<>();
|
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.familyModel = familyModel;
|
||||||
this.colourModel = colourModel;
|
this.colourModel = colourModel;
|
||||||
this.transformer = x -> transformer.transform(x.getModel(), x.getMatrix());
|
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;
|
var christmas = Holiday.getCurrent() == Holiday.CHRISTMAS;
|
||||||
|
|
||||||
if (!(stack.getItem() instanceof TurtleItem turtle)) {
|
if (!(stack.getItem() instanceof TurtleItem turtle)) {
|
||||||
@ -85,7 +98,7 @@ public final class TurtleModelParts {
|
|||||||
return new Combination(colour != -1, leftUpgrade, rightUpgrade, overlay, christmas, flip);
|
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 mc = Minecraft.getInstance();
|
||||||
var modelManager = mc.getItemRenderer().getItemModelShaper().getModelManager();
|
var modelManager = mc.getItemRenderer().getItemModelShaper().getModelManager();
|
||||||
|
|
||||||
@ -109,7 +122,7 @@ public final class TurtleModelParts {
|
|||||||
return parts;
|
return parts;
|
||||||
}
|
}
|
||||||
|
|
||||||
public BakedModel transform(BakedModel model, Transformation transformation) {
|
private BakedModel transform(BakedModel model, Transformation transformation) {
|
||||||
if (transformation.equals(Transformation.identity())) return model;
|
if (transformation.equals(Transformation.identity())) return model;
|
||||||
return transformCache.computeIfAbsent(new TransformedModel(model, transformation), transformer);
|
return transformCache.computeIfAbsent(new TransformedModel(model, transformation), transformer);
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,10 @@ public class CompositeBakedModel extends CustomBakedModel {
|
|||||||
this.models = models;
|
this.models = models;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static BakedModel of(List<BakedModel> models) {
|
||||||
|
return models.size() == 1 ? models.get(0) : new CompositeBakedModel(models);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<BakedQuad> getQuads(@Nullable BlockState blockState, @Nullable Direction face, RandomSource rand) {
|
public List<BakedQuad> getQuads(@Nullable BlockState blockState, @Nullable Direction face, RandomSource rand) {
|
||||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||||
|
@ -14,8 +14,6 @@ import net.minecraft.world.entity.LivingEntity;
|
|||||||
import net.minecraft.world.item.ItemStack;
|
import net.minecraft.world.item.ItemStack;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
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.
|
* 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
|
* @see TurtleModelParts
|
||||||
*/
|
*/
|
||||||
public class TurtleModel extends ForwardingBakedModel {
|
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() {
|
private final ItemOverrides overrides = new ItemOverrides() {
|
||||||
@Override
|
@Override
|
||||||
public BakedModel resolve(BakedModel model, ItemStack stack, @Nullable ClientLevel level, @Nullable LivingEntity entity, int seed) {
|
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) {
|
public TurtleModel(BakedModel familyModel, BakedModel colourModel) {
|
||||||
wrapped = familyModel;
|
wrapped = familyModel;
|
||||||
parts = new TurtleModelParts(familyModel, colourModel, TransformedBakedModel::new);
|
parts = new TurtleModelParts<>(familyModel, colourModel, TransformedBakedModel::new, CompositeBakedModel::of);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ItemOverrides getOverrides() {
|
public ItemOverrides getOverrides() {
|
||||||
return overrides;
|
return overrides;
|
||||||
}
|
}
|
||||||
|
|
||||||
private BakedModel buildModel(TurtleModelParts.Combination combo) {
|
|
||||||
var models = parts.buildModel(combo);
|
|
||||||
return models.size() == 1 ? models.get(0) : new CompositeBakedModel(models);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -11,9 +11,8 @@ import net.minecraft.world.item.ItemDisplayContext;
|
|||||||
import net.minecraft.world.item.ItemStack;
|
import net.minecraft.world.item.ItemStack;
|
||||||
import net.minecraftforge.client.model.BakedModelWrapper;
|
import net.minecraftforge.client.model.BakedModelWrapper;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
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.
|
* 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
|
* @see TurtleModelParts
|
||||||
*/
|
*/
|
||||||
public class TurtleModel extends BakedModelWrapper<BakedModel> {
|
public class TurtleModel extends BakedModelWrapper<BakedModel> {
|
||||||
private final TurtleModelParts parts;
|
private final TurtleModelParts<List<BakedModel>> parts;
|
||||||
private final Map<TurtleModelParts.Combination, List<BakedModel>> cachedModels = new HashMap<>();
|
|
||||||
|
|
||||||
public TurtleModel(BakedModel familyModel, BakedModel colourModel) {
|
public TurtleModel(BakedModel familyModel, BakedModel colourModel) {
|
||||||
super(familyModel);
|
super(familyModel);
|
||||||
parts = new TurtleModelParts(familyModel, colourModel, TransformedBakedModel::new);
|
parts = new TurtleModelParts<>(familyModel, colourModel, TransformedBakedModel::new, Function.identity());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -37,6 +35,6 @@ public class TurtleModel extends BakedModelWrapper<BakedModel> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<BakedModel> getRenderPasses(ItemStack stack, boolean fabulous) {
|
public List<BakedModel> getRenderPasses(ItemStack stack, boolean fabulous) {
|
||||||
return cachedModels.computeIfAbsent(parts.getCombination(stack), parts::buildModel);
|
return parts.getModel(stack);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user