Skip to content

Commit

Permalink
Fix race condition between material render info loading and tool/part…
Browse files Browse the repository at this point in the history
… model loading

Requires loading in the "wrong" spot. Will be so happy when texture atlases are auto-stiched
  • Loading branch information
KnightMiner committed Mar 16, 2024
1 parent 2a3fdf0 commit 500d984
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import lombok.extern.log4j.Log4j2;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.Resource;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraftforge.client.event.RegisterClientReloadListenersEvent;
import net.minecraftforge.client.event.ModelEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.fml.ModLoader;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import slimeknights.mantle.data.gson.ResourceLocationSerializer;
import slimeknights.mantle.data.listener.IEarlySafeManagerReloadListener;
import slimeknights.mantle.util.JsonHelper;
Expand Down Expand Up @@ -53,8 +57,15 @@ public class MaterialRenderInfoLoader implements IEarlySafeManagerReloadListener
/**
* Called on mod construct to register the resource listener
*/
public static void addResourceListener(RegisterClientReloadListenersEvent manager) {
manager.registerReloadListener(INSTANCE);
public static void init() {
// bit of a hack: instead of registering our resource listener to the list as we should, we use the additional model registration event
// we do this as we need to guarantee we run before models are baked, which happens in the first stage of listeners in the bakery constructor
// the other option would be to wait until the atlas stitch event, though that would make it more difficult to know which sprites we need
FMLJavaModLoadingContext.get().getModEventBus().addListener(EventPriority.NORMAL, false, ModelEvent.RegisterAdditional.class, event -> {
if(ModLoader.isLoadingStateValid()) {
INSTANCE.onReloadSafe(Minecraft.getInstance().getResourceManager());
}
});
}

/** Map of all loaded materials */
Expand Down Expand Up @@ -90,7 +101,6 @@ public Optional<MaterialRenderInfo> getRenderInfo(MaterialVariantId variantId) {
@Override
public void onReloadSafe(ResourceManager manager) {
// first, we need to fetch all relevant JSON files
int trim = FOLDER.length() + 1;
Map<MaterialVariantId,MaterialRenderInfo> map = new HashMap<>();
for(Entry<ResourceLocation, Resource> entry : manager.listResources(FOLDER, (loc) -> loc.getPath().endsWith(".json")).entrySet()) {
// clean up ID by trimming off the extension and folder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import slimeknights.tconstruct.TConstruct;
import slimeknights.tconstruct.common.ClientEventBase;
import slimeknights.tconstruct.library.client.book.TinkerBook;
import slimeknights.tconstruct.library.client.materials.MaterialRenderInfoLoader;
import slimeknights.tconstruct.library.utils.DomainDisplayName;
import slimeknights.tconstruct.shared.block.ClearStainedGlassBlock;
import slimeknights.tconstruct.shared.block.ClearStainedGlassBlock.GlassColor;
Expand All @@ -31,7 +30,6 @@
public class CommonsClientEvents extends ClientEventBase {
@SubscribeEvent
static void addResourceListeners(RegisterClientReloadListenersEvent event) {
MaterialRenderInfoLoader.addResourceListener(event);
DomainDisplayName.addResourceListener(event);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import slimeknights.tconstruct.library.client.data.spritetransformer.IColorMapping;
import slimeknights.tconstruct.library.client.data.spritetransformer.ISpriteTransformer;
import slimeknights.tconstruct.library.client.data.spritetransformer.RecolorSpriteTransformer;
import slimeknights.tconstruct.library.client.materials.MaterialRenderInfoLoader;
import slimeknights.tconstruct.library.client.modifiers.ModifierIconManager;
import slimeknights.tconstruct.tables.client.PatternGuiTextureLoader;

Expand All @@ -51,6 +52,7 @@ public static void onConstruct() {
// needs to register listeners early enough for minecraft to load
PatternGuiTextureLoader.init();
ModifierIconManager.init();
MaterialRenderInfoLoader.init();

// add the recipe cache invalidator to the client
Consumer<RecipesUpdatedEvent> recipesUpdated = event -> RecipeCacheInvalidator.reload(true);
Expand Down

0 comments on commit 500d984

Please sign in to comment.