Skip to content

Commit

Permalink
Implement dynamic model cache on vanilla ItemModelShaper
Browse files Browse the repository at this point in the history
  • Loading branch information
embeddedt committed Dec 15, 2023
1 parent 81836a8 commit 240fa4b
Showing 1 changed file with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraft.client.resources.model.ModelManager;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.world.item.Item;
import org.embeddedt.modernfix.dynamicresources.DynamicModelCache;
import org.embeddedt.modernfix.dynamicresources.ModelLocationCache;
import org.embeddedt.modernfix.util.DynamicInt2ObjectMap;
import org.spongepowered.asm.mixin.*;
Expand All @@ -31,6 +32,8 @@ public ItemModelShaperMixin() {

private static final ModelResourceLocation SENTINEL_VANILLA = new ModelResourceLocation("modernfix", "sentinel");

private final DynamicModelCache<Item> mfix$itemModelCache = new DynamicModelCache<>(k -> this.mfix$getModelForItem((Item)k), true);

@Inject(method = "<init>", at = @At("RETURN"))
private void replaceLocationMap(CallbackInfo ci) {
overrideLocationsVanilla = new HashMap<>();
Expand All @@ -47,15 +50,20 @@ private void replaceLocationMap(CallbackInfo ci) {
return map;
}


private BakedModel mfix$getModelForItem(Item item) {
ModelResourceLocation map = mfix$getLocation(item);
return map == null ? null : getModelManager().getModel(map);
}

/**
* @author embeddedt
* @reason Get the stored location for that item and meta, and get the model
* from that location from the model manager.
**/
@Overwrite
public BakedModel getItemModel(Item item) {
ModelResourceLocation map = mfix$getLocation(item);
return map == null ? null : getModelManager().getModel(map);
return this.mfix$itemModelCache.get(item);
}

/**
Expand All @@ -74,5 +82,7 @@ public void register(Item item, ModelResourceLocation location) {
* all models).
**/
@Overwrite
public void rebuildCache() {}
public void rebuildCache() {
this.mfix$itemModelCache.clear();
}
}

0 comments on commit 240fa4b

Please sign in to comment.