|
| 1 | +package org.embeddedt.modernfix.common.mixin.perf.model_optimizations; |
| 2 | + |
| 3 | +import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; |
| 4 | +import net.minecraft.client.renderer.block.model.MultiVariant; |
| 5 | +import net.minecraft.client.renderer.block.model.Variant; |
| 6 | +import net.minecraft.client.resources.model.UnbakedModel; |
| 7 | +import net.minecraft.resources.ResourceLocation; |
| 8 | +import org.embeddedt.modernfix.annotation.ClientOnlyMixin; |
| 9 | +import org.spongepowered.asm.mixin.Mixin; |
| 10 | +import org.spongepowered.asm.mixin.Overwrite; |
| 11 | +import org.spongepowered.asm.mixin.Shadow; |
| 12 | + |
| 13 | +import java.util.List; |
| 14 | +import java.util.function.Function; |
| 15 | + |
| 16 | +@Mixin(value = MultiVariant.class, priority = 700) |
| 17 | +@ClientOnlyMixin |
| 18 | +public abstract class MultiVariantMixin { |
| 19 | + @Shadow public abstract List<Variant> getVariants(); |
| 20 | + |
| 21 | + /** |
| 22 | + * @author embeddedt |
| 23 | + * @reason avoid streams, try to optimize for common case |
| 24 | + */ |
| 25 | + @Overwrite |
| 26 | + public void resolveParents(Function<ResourceLocation, UnbakedModel> modelGetter) { |
| 27 | + var variants = this.getVariants(); |
| 28 | + // There is usually only a single variant |
| 29 | + if (variants.size() == 1) { |
| 30 | + modelGetter.apply(variants.get(0).getModelLocation()).resolveParents(modelGetter); |
| 31 | + } else if(variants.size() > 1) { |
| 32 | + ObjectOpenHashSet<ResourceLocation> seenLocations = new ObjectOpenHashSet<>(variants.size()); |
| 33 | + for (var variant : variants) { |
| 34 | + var location = variant.getModelLocation(); |
| 35 | + if (seenLocations.add(location)) { |
| 36 | + modelGetter.apply(location).resolveParents(modelGetter); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | +} |
0 commit comments