From 255b817c9265713ce372693b3a85ea1133681952 Mon Sep 17 00:00:00 2001 From: Algent Date: Wed, 14 Oct 2026 16:37:20 +0200 Subject: [PATCH] fix: re-resolve microblock icons after texture stitch loadIcons runs in TextureStitchEvent.Post and could cache icons before other mods finished theirs in the same event (Chisel CTMLib -v4 submaps), leaving microblocks with missing or previous-stitch textures. loadIcons now marks icons stale; the first icont() read after a stitch rebuilds them once. Icons set through icont_$eq are kept. Addon materials reading icont() get the fix without API changes. Fixes GTNH issue 27183 without relying on handler priority (upstream #63). --- docs/migration/DIVERGENCES.md | 1 + docs/migration/MANUAL_CHECKS.md | 1 + .../microblock/BlockMicroMaterial.java | 23 +++++++- ...lockMicroMaterialCharacterizationTest.java | 58 ++++++++++++++++++- 4 files changed, 81 insertions(+), 2 deletions(-) diff --git a/docs/migration/DIVERGENCES.md b/docs/migration/DIVERGENCES.md index 8dee5d9..e596e41 100644 --- a/docs/migration/DIVERGENCES.md +++ b/docs/migration/DIVERGENCES.md @@ -20,6 +20,7 @@ of the later upstream rename, not a divergence from the `1.7.12` reference. | Area | Difference and consequence | | --- | --- | | Raw microblock recipe material lookup | Skips `MissingMicroMaterial`, whose recovery item is stone, so sawing stone selects the real stone material. Existing missing-material microblocks retain their recovery behavior. This fixes a pre-existing Scala bug, also fixed upstream in PR #61. | +| `BlockMicroMaterial.icont()` after a texture stitch | `loadIcons` still builds icons eagerly, then marks them stale; the first `icont()` read after the stitch re-reads the block's icons once. Icons that another mod finishes in a later `TextureStitchEvent.Post` handler (Chisel's CTMLib `-v4` submaps) are picked up instead of rendering as missing or previous-stitch textures. Icons assigned through `icont_$eq` are kept. Upstream PR #63 targets the same bug by lowering the stitch handler to `EventPriority.LOW`; this port does not need that ordering. | | Client part additions | Reuses an existing multipart tile or creates one without converting the stale client block. Conversion and its outgoing packets remain server-side, avoiding duplicate parts and client disconnects. Matches upstream PR #62. | | `BlockMicroMaterial.createAndRegister(Block, Seq, String)` | Registers every supplied metadata value instead of only zero, including dark-oak logs and leaves and their legacy-name remaps. This fixes a pre-existing Scala bug; material IDs are rebuilt through the existing registry mapping. | | Chunk scheduled-tick restoration | Reactivates chunks with restored scheduled ticks so their callbacks resume at the saved deadlines. The Scala implementation restored entries without activating their chunk. NBT layout and duplicate-scheduling behavior are unchanged. | diff --git a/docs/migration/MANUAL_CHECKS.md b/docs/migration/MANUAL_CHECKS.md index bbbe9bf..4980aa1 100644 --- a/docs/migration/MANUAL_CHECKS.md +++ b/docs/migration/MANUAL_CHECKS.md @@ -29,6 +29,7 @@ Lamp Cover**, **Hollow Inverted White Lamp Cover**, **Inverted White Lamp Nook** | [ ] | Break a hollow cover | Forge Multipart **Hollow Stone Cover** | Destroy particles use the full block bounds, not the part bounds | `addDestroyEffects` scaleDensity false | | [ ] | Hover face, hollow, edge and corner microblock placement targets | Forge Multipart **Stone Cover**, **Hollow Stone Cover**, **Stone Strip** and **Stone Nook**; for a consumer override, hold a Stone Cover over ProjectRed **Framed Red Alloy Wire** to preview its jacket | The highlight and guide lines match each placement region, and a mod supplying its own highlight renderer overrides them | `PlacementGrid` / `MicroblockRender.renderHighlight` / `MicroblockEventHandler.drawBlockHighlight` / `MicroMaterialRegistry.renderHighlight` | | [ ] | Reload client resources after placing several microblock materials | Place **Stone**, **Glass**, **Grass** and ProjectRed **Inverted White Lamp Covers**, then press `F3+T` | The texture atlas reloads every material icon without missing textures | `MicroblockEventHandler.postTextureStitch` / `BlockMicroMaterial.loadIcons` | +| [ ] | Render microblocks of a block whose icons another mod finishes in `TextureStitchEvent.Post` | Saw Chisel **Stone Bricks** variants that use `-v4` textures (2x2 submaps) into covers; check them on world load and again after `F3+T` | Covers show the Chisel texture, never the missing texture, both times | `BlockMicroMaterial.icont` stale re-resolve (GTNH issue 27183) | | [ ] | Look at a **Stone Saw**, **Iron Saw** and **Diamond Saw** in inventory and in hand | All three items are supplied by Forge Multipart | The saw item renders with its custom transform | `ItemSaw` `IItemRenderer` | | [ ] | Place microblocks of several materials, including glass | Compare Forge Multipart **Stone Cover** and **Glass Cover** | Transparent materials render in the correct pass and are not opaque | `BlockMicroMaterial` / `IMicroMaterial.canRenderInPass` | | [ ] | Place grass and mycelium covers at several thicknesses and biomes | Forge Multipart **Grass Cover** and **Mycelium Cover**, preferably 1/8 and 1/2 thicknesses | Grass has an untinted base plus a tinted, height-aligned side overlay and tinted top; mycelium uses its top texture on horizontal faces and height-aligned side texture elsewhere | `GrassMicroMaterial` / `TopMicroMaterial` | diff --git a/src/main/scala/codechicken/microblock/BlockMicroMaterial.java b/src/main/scala/codechicken/microblock/BlockMicroMaterial.java index 35381d0..cec3b34 100644 --- a/src/main/scala/codechicken/microblock/BlockMicroMaterial.java +++ b/src/main/scala/codechicken/microblock/BlockMicroMaterial.java @@ -31,6 +31,10 @@ public class BlockMicroMaterial implements IMicroMaterial { @SideOnly(Side.CLIENT) private MultiIconTransformation icont; + /** Set by {@link #loadIcons()} so the next {@link #icont()} read re-resolves the block's icons. */ + @SideOnly(Side.CLIENT) + private volatile boolean iconsStale; + public BlockMicroMaterial(Block block, int meta) { this.block = block; this.meta = meta; @@ -111,23 +115,40 @@ public class BlockMicroMaterial implements IMicroMaterial { return blockKey; } + /** + * Returns the side icons. The first read after a texture stitch rebuilds them from the block: {@link #loadIcons()} + * runs inside {@code TextureStitchEvent.Post}, possibly before another mod's Post handler finishes its own icons + * (Chisel's CTMLib submaps create their sub-icons there), and the cached copy would otherwise keep missing or + * previous-stitch icons until the next stitch. + */ public MultiIconTransformation icont() { + if (iconsStale) { + icont = buildIcons(); + iconsStale = false; + } return icont; } + /** Explicitly assigned icons are kept as-is and are not re-resolved on the next {@link #icont()} read. */ public void icont_$eq(MultiIconTransformation icont) { this.icont = icont; + iconsStale = false; } @Override @SideOnly(Side.CLIENT) public void loadIcons() { + icont_$eq(buildIcons()); + iconsStale = true; + } + + private MultiIconTransformation buildIcons() { Block currentBlock = Block.getBlockFromName(blockKey()); IIcon[] icons = new IIcon[6]; for (int side = 0; side < icons.length; side++) { icons[side] = codechicken$microblock$BlockMicroMaterial$$safeIcon$1(currentBlock, side); } - icont_$eq(new MultiIconTransformation(icons)); + return new MultiIconTransformation(icons); } public final IIcon codechicken$microblock$BlockMicroMaterial$$safeIcon$1(Block block, int side) { diff --git a/src/test/java/codechicken/microblock/BlockMicroMaterialCharacterizationTest.java b/src/test/java/codechicken/microblock/BlockMicroMaterialCharacterizationTest.java index 24a5717..3f7abff 100644 --- a/src/test/java/codechicken/microblock/BlockMicroMaterialCharacterizationTest.java +++ b/src/test/java/codechicken/microblock/BlockMicroMaterialCharacterizationTest.java @@ -15,14 +15,17 @@ import java.lang.reflect.Proxy; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.TreeSet; import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.init.Blocks; import net.minecraft.util.IIcon; +import net.minecraft.util.RegistrySimple; import net.minecraft.world.IBlockAccess; import org.junit.jupiter.api.Test; @@ -166,7 +169,10 @@ class BlockMicroMaterialCharacterizationTest { assertField(BlockMicroMaterial.class, "blockKey", String.class, false, true); Field icons = assertField(BlockMicroMaterial.class, "icont", MultiIconTransformation.class, false, false); assertSideOnly(icons.getAnnotation(SideOnly.class)); - assertEquals(4, BlockMicroMaterial.class.getDeclaredFields().length); + Field stale = assertField(BlockMicroMaterial.class, "iconsStale", boolean.class, false, false); + assertSideOnly(stale.getAnnotation(SideOnly.class)); + assertTrue(Modifier.isVolatile(stale.getModifiers())); + assertEquals(5, BlockMicroMaterial.class.getDeclaredFields().length); assertClass(BlockMicroMaterial$.class, Object.class, true, MATERIAL_COMPANION_METHODS); assertModule(BlockMicroMaterial$.class, BlockMicroMaterial$.MODULE$); @@ -355,6 +361,56 @@ class BlockMicroMaterialCharacterizationTest { assertEquals(Arrays.asList(2, 23, 4, 23), reads); } + @Test + void firstIconReadAfterStitchPicksUpIconsFinishedByLaterPostHandlers() throws Exception { + IIcon early = stubIcon(); + IIcon late = stubIcon(); + AtomicReference current = new AtomicReference<>(early); + Block block = new Block(Material.rock) { + + @Override + public IIcon getIcon(int side, int metadata) { + return current.get(); + } + }; + String key = "fmptest:late_icon_block"; + Field objects = RegistrySimple.class.getDeclaredField("registryObjects"); + objects.setAccessible(true); + @SuppressWarnings("unchecked") + Map registry = (Map) objects.get(Block.blockRegistry); + registry.put(key, block); + try { + BlockMicroMaterial material = new BlockMicroMaterial(block, 0) { + + @Override + public String blockKey() { + return key; + } + }; + material.loadIcons(); + // Another mod's TextureStitchEvent.Post handler replaces the block's icon after FMP's ran. + current.set(late); + MultiIconTransformation resolved = material.icont(); + assertSame(late, resolved.icons[0]); + assertSame(late, resolved.icons[5]); + assertSame(resolved, material.icont(), "Icons are re-resolved once per stitch, not on every read"); + + MultiIconTransformation assigned = new MultiIconTransformation(early); + material.loadIcons(); + material.icont_$eq(assigned); + assertSame(assigned, material.icont(), "Explicitly assigned icons are not overwritten"); + } finally { + registry.remove(key); + } + } + + private static IIcon stubIcon() { + return (IIcon) Proxy.newProxyInstance( + IIcon.class.getClassLoader(), + new Class[] { IIcon.class }, + (proxy, method, arguments) -> method.getName().equals("toString") ? "stubIcon" : null); + } + @Test void overriddenBlockKeyIsReadBeforeLoadingIcons() { IllegalStateException failure = new IllegalStateException("block key unavailable");