Properly discard caches when reloading models (#278)

* Properly discard caches when reloading models

* Singleton reloading multiple AutoRotatingModels
This commit is contained in:
Rongmario
2023-05-24 11:26:24 +01:00
committed by GitHub
parent b6de4ebf18
commit e2e5eed351
4 changed files with 55 additions and 5 deletions
@@ -45,7 +45,7 @@ import java.util.Map.Entry;
public class CableBusBakedModel implements IBakedModel {
private static final Map<CableBusRenderState, List<BakedQuad>> CABLE_MODEL_CACHE = new HashMap<>();
static final Map<CableBusRenderState, List<BakedQuad>> CABLE_MODEL_CACHE = new HashMap<>();
private final CableBuilder cableBuilder;
@@ -27,21 +27,26 @@ import com.google.common.collect.ImmutableMap;
import net.minecraft.client.renderer.block.model.IBakedModel;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.vertex.VertexFormat;
import net.minecraft.client.resources.IResourceManager;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.model.IModel;
import net.minecraftforge.client.model.ModelLoaderRegistry;
import net.minecraftforge.client.resource.IResourceType;
import net.minecraftforge.client.resource.ISelectiveResourceReloadListener;
import net.minecraftforge.client.resource.VanillaResourceType;
import net.minecraftforge.common.model.IModelState;
import net.minecraftforge.common.model.TRSRTransformation;
import java.util.Collection;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
/**
* The built-in model for the cable bus block.
*/
public class CableBusModel implements IModel {
public class CableBusModel implements IModel, ISelectiveResourceReloadListener {
private final PartModels partModels;
@@ -101,4 +106,12 @@ public class CableBusModel implements IModel {
public IModelState getDefaultState() {
return TRSRTransformation.identity();
}
@Override
public void onResourceManagerReload(IResourceManager resourceManager, Predicate<IResourceType> resourcePredicate) {
if (resourcePredicate.test(VanillaResourceType.MODELS)) {
CableBusBakedModel.CABLE_MODEL_CACHE.clear();
}
}
}
@@ -33,6 +33,8 @@ import net.minecraft.client.renderer.block.model.ItemOverrideList;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.vertex.VertexFormat;
import net.minecraft.client.renderer.vertex.VertexFormatElement;
import net.minecraft.client.resources.IResourceManager;
import net.minecraft.client.resources.IResourceManagerReloadListener;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.Vec3i;
import net.minecraftforge.client.model.pipeline.IVertexConsumer;
@@ -46,7 +48,7 @@ import java.util.ArrayList;
import java.util.List;
public class AutoRotatingModel implements IBakedModel {
public class AutoRotatingModel implements IBakedModel, IResourceManagerReloadListener {
private final IBakedModel parent;
private final LoadingCache<AutoRotatingCacheKey, List<BakedQuad>> quadCache;
@@ -152,6 +154,11 @@ public class AutoRotatingModel implements IBakedModel {
return this.quadCache.getUnchecked(key);
}
@Override
public void onResourceManagerReload(IResourceManager resourceManager) {
this.quadCache.invalidateAll();
}
public static class VertexRotator extends QuadGatheringTransformer {
private final FacingToRotation f2r;
private final EnumFacing face;