tech: fixed data reloads no longer clearing model caches after asset reload
@@ -6,6 +6,7 @@ import com.google.gson.JsonObject;
import net.minecraft.client.renderer.model.ItemCameraTransforms;
import net.minecraft.resources.IResourceManager;
import net.minecraftforge.client.model.IModelLoader;
+import net.minecraftforge.resource.IResourceType;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import se.mickelus.tetra.data.DataManager;
@@ -14,11 +15,13 @@ import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
+import java.util.function.Predicate;
public class ModularModelLoader implements IModelLoader<ModularItemModel> {
private static final Logger logger = LogManager.getLogger();
+ private static List<ModularItemModel> newModels = new LinkedList<>();
private static List<ModularItemModel> models = new LinkedList<>();
public ModularModelLoader() {
@@ -26,15 +29,27 @@ public class ModularModelLoader implements IModelLoader<ModularItemModel> {
DataManager.moduleData.onReload(ModularModelLoader::clearCaches);
}
+ /**
+ * Hack to shuffle models around since onResourceManagerReload is called after all models are loaded and there's no context available to tell them
+ * apart, so that override caches can be cleared when data is reloaded.
+ */
+ private static void shuffle() {
+ if (!newModels.isEmpty()) {
+ models = newModels;
+ newModels = new LinkedList<>();
+ }
+ }
+
public static void clearCaches() {
- logger.info("Clearing item model cache, let's get bakin'");
+ shuffle();
+ logger.info("Clearing model cache for {} items, let's get bakin'", models.size());
models.forEach(ModularItemModel::clearCache);
}
@Override
public void onResourceManagerReload(IResourceManager resourceManager) {
- logger.info("Clearing item models");
- models.clear();
+ logger.info("Reloading item models, old: {}, new: {}", models.size(), newModels.size());
+ shuffle();
}
@Override
@@ -46,12 +61,12 @@ public class ModularModelLoader implements IModelLoader<ModularItemModel> {
new TypeToken<Map<String, ItemCameraTransforms>>(){}.getType());
ModularItemModel model = new ModularItemModel(cameraTransforms, transformVariants);
- models.add(model);
+ newModels.add(model);
return model;
}
ModularItemModel model = new ModularItemModel(cameraTransforms);
- models.add(model);
+ newModels.add(model);
return model;
}
}
