Dynamically generate item model location cache

This commit is contained in:
embeddedt 2023-04-28 17:27:33 -04:00
parent 77e9309d2b
commit b9cb33b1ef
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
4 changed files with 50 additions and 3 deletions

View File

@ -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<BlockState, ModelResourceLocation> locationCache = CacheBuilder.newBuilder()
private static final LoadingCache<BlockState, ModelResourceLocation> blockLocationCache = CacheBuilder.newBuilder()
.maximumSize(10000)
.build(new CacheLoader<BlockState, ModelResourceLocation>() {
@Override
@ -29,9 +31,26 @@ public class ModelLocationCache {
}
});
private static final LoadingCache<Item, ModelResourceLocation> itemLocationCache = CacheBuilder.newBuilder()
.maximumSize(10000)
.build(new CacheLoader<Item, ModelResourceLocation>() {
@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());
}

View File

@ -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);
}

View File

@ -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 = "<init>", 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) {
}
}

View File

@ -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",