diff --git a/src/main/java/org/embeddedt/modernfix/dynamicresources/ModelLocationCache.java b/src/main/java/org/embeddedt/modernfix/dynamicresources/ModelLocationCache.java index 25886e20..11938b3b 100644 --- a/src/main/java/org/embeddedt/modernfix/dynamicresources/ModelLocationCache.java +++ b/src/main/java/org/embeddedt/modernfix/dynamicresources/ModelLocationCache.java @@ -10,8 +10,10 @@ import net.minecraft.Util; import net.minecraft.client.renderer.block.BlockModelShaper; import net.minecraft.client.resources.model.ModelResourceLocation; import net.minecraft.core.Registry; +import net.minecraft.world.item.Item; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.state.BlockState; +import net.minecraftforge.registries.IRegistryDelegate; import java.util.ArrayList; import java.util.Collections; @@ -20,7 +22,7 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; public class ModelLocationCache { - private static final LoadingCache locationCache = CacheBuilder.newBuilder() + private static final LoadingCache blockLocationCache = CacheBuilder.newBuilder() .maximumSize(10000) .build(new CacheLoader() { @Override @@ -29,9 +31,26 @@ public class ModelLocationCache { } }); + private static final LoadingCache itemLocationCache = CacheBuilder.newBuilder() + .maximumSize(10000) + .build(new CacheLoader() { + @Override + public ModelResourceLocation load(Item key) throws Exception { + return new ModelResourceLocation(Registry.ITEM.getKey(key), "inventory"); + } + }); + public static ModelResourceLocation get(BlockState state) { try { - return locationCache.get(state); + return blockLocationCache.get(state); + } catch(ExecutionException e) { + throw new RuntimeException(e.getCause()); + } + } + + public static ModelResourceLocation get(Item item) { + try { + return itemLocationCache.get(item); } catch(ExecutionException e) { throw new RuntimeException(e.getCause()); } diff --git a/src/main/java/org/embeddedt/modernfix/mixin/perf/dynamic_resources/ItemModelShaperMixin.java b/src/main/java/org/embeddedt/modernfix/mixin/perf/dynamic_resources/ItemModelShaperMixin.java index 0ca70841..86b708bd 100644 --- a/src/main/java/org/embeddedt/modernfix/mixin/perf/dynamic_resources/ItemModelShaperMixin.java +++ b/src/main/java/org/embeddedt/modernfix/mixin/perf/dynamic_resources/ItemModelShaperMixin.java @@ -7,6 +7,7 @@ import net.minecraft.client.resources.model.ModelResourceLocation; import net.minecraft.world.item.Item; import net.minecraftforge.client.ItemModelMesherForge; import net.minecraftforge.registries.IRegistryDelegate; +import org.embeddedt.modernfix.dynamicresources.ModelLocationCache; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Overwrite; @@ -22,6 +23,8 @@ public abstract class ItemModelShaperMixin extends ItemModelShaper { super(arg); } + private static final ModelResourceLocation SENTINEL = new ModelResourceLocation("modernfix", "sentinel"); + /** * @reason Get the stored location for that item and meta, and get the model * from that location from the model manager. @@ -29,7 +32,11 @@ public abstract class ItemModelShaperMixin extends ItemModelShaper { @Overwrite @Override public BakedModel getItemModel(Item item) { - ModelResourceLocation map = locations.get(item.delegate); + ModelResourceLocation map = locations.getOrDefault(item.delegate, SENTINEL); + if(map == SENTINEL) { + /* generate the appropriate location from our cache */ + map = ModelLocationCache.get(item); + } return map == null ? null : getModelManager().getModel(map); } diff --git a/src/main/java/org/embeddedt/modernfix/mixin/perf/dynamic_resources/ItemRendererMixin.java b/src/main/java/org/embeddedt/modernfix/mixin/perf/dynamic_resources/ItemRendererMixin.java new file mode 100644 index 00000000..3e47df2d --- /dev/null +++ b/src/main/java/org/embeddedt/modernfix/mixin/perf/dynamic_resources/ItemRendererMixin.java @@ -0,0 +1,20 @@ +package org.embeddedt.modernfix.mixin.perf.dynamic_resources; + +import net.minecraft.client.renderer.ItemModelShaper; +import net.minecraft.client.renderer.entity.ItemRenderer; +import net.minecraft.client.resources.model.ModelResourceLocation; +import net.minecraft.world.item.Item; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + +@Mixin(ItemRenderer.class) +public class ItemRendererMixin { + /** + * Don't waste space putting all these locations into the cache, compute them on demand later. + */ + @Redirect(method = "", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/ItemModelShaper;register(Lnet/minecraft/world/item/Item;Lnet/minecraft/client/resources/model/ModelResourceLocation;)V")) + private void skipDefaultRegistration(ItemModelShaper shaper, Item item, ModelResourceLocation mrl) { + + } +} diff --git a/src/main/resources/modernfix.mixins.json b/src/main/resources/modernfix.mixins.json index f8320632..71141447 100644 --- a/src/main/resources/modernfix.mixins.json +++ b/src/main/resources/modernfix.mixins.json @@ -92,6 +92,7 @@ "perf.dynamic_resources.BlockElementFaceDeserializerMixin", "perf.dynamic_resources.BlockModelShaperMixin", "perf.dynamic_resources.ItemModelShaperMixin", + "perf.dynamic_resources.ItemRendererMixin", "perf.dynamic_resources.ModelBakeryMixin", "perf.dynamic_resources.ae2.RegistrationMixin", "perf.dynamic_resources.ctm.TextureMetadataHandlerMixin",