feat: add TileMultipart.refreshInventorySlots for AE2

AE2 casts the tile to the raw scalatraits TIInventoryTile to call
rebuildSlotMap after its cable bus inventory size changes. Give it a
supported entry point on TileMultipart instead.

- refreshInventorySlots is a no-op on base tiles; the generated inventory
  trait rebuilds its flattened slot mapping without rebinding parts
- rebuildSlotMap stays supported for released AE2 jars
- Functional tests cover resize, tiles without inventories, and a
  slotted plus inventory tile; smoke test freezes the new trait method
- Document it in the tile trait guide, API index and adoption ledger
This commit is contained in:
Algent committed 2026-10-16 15:17:42 +02:00
1 parent 7c6d4b9c88
commit 6ebec208df
8 files changed
+147 -7

No files matched your search

+5 -1
View File
@@ -38,6 +38,7 @@ Existing reflective binaries remain supported until consumer release and pack ad
| Call generated tile capabilities from Java | `TileMultipart`, `IRedstoneTile` and ordinary interfaces - [safe compilation and raw-trait pitfalls](api/TILE_TRAIT_ACCESS.md) |
| Add aggregate or lifecycle behavior to generated multipart tiles | `registerTrait(marker, trait)` - [custom Java tile traits, stable capabilities and transformer constraints](api/CUSTOM_TILE_TRAITS.md) |
| Refresh slots after a stored part changes shape | `refreshPartSlots(TMultiPart)` - [ownership, equality and notification responsibilities](api/TILE_TRAIT_ACCESS.md#refreshing-a-changed-slot-mask) |
| Refresh the tile inventory after a stored part changes its inventory size | `refreshInventorySlots()` - [mapping, scope and AE2 migration](api/TILE_TRAIT_ACCESS.md#refreshing-inventory-slots) |
| Add material-specific behavior to generated microblocks | `registerTrait(String)` and `IGeneratedMaterial` - [illuminated Java extension, compilation and side contracts](api/MICROBLOCK_EXTENSIONS.md) |
| Access built-in saws and change their cutting strength | `MicroblockProxy.sawStone()` and `ItemSaw.setHarvestLevel(int)` - [state, rendering and Iguana migration](api/SAW_STRENGTH.md) |
| Read/index/search a tile's parts | `jPartList()` - [part collection ownership and order](api/PART_TRAVERSAL.md#collection-ownership-and-ordering) |
@@ -93,7 +94,7 @@ The registry's matching companion methods carry the same internal marker. This i
not a claim that every other public member is a supported API. See [TileMultipart's Javadocs](../src/main/scala/codechicken/multipart/TileMultipart.java)
and [registry Javadocs](../src/main/scala/codechicken/microblock/MicroMaterialRegistry.java) for method-specific details.
Three advanced methods remain supported because consumers use them directly:
Four advanced methods remain supported because consumers use them directly:
- **`bindPart(part)`** updates capability caches through generated overrides. It does not insert the part, change
its tile binding or perform placement/notifications. The legacy OpenComputers integration clears a print part's old
@@ -103,6 +104,9 @@ Three advanced methods remain supported because consumers use them directly:
- **`refreshPartSlots(part)`** replaces OpenComputers' direct generated-array mutation. Generated slotted tiles clear
equal cached entries and invoke the virtual bind chain once; base tiles do nothing. It leaves ownership, storage,
validation and notifications to the caller. See the [slot-refresh contract](api/TILE_TRAIT_ACCESS.md#refreshing-a-changed-slot-mask).
- **`refreshInventorySlots()`** replaces AE2's direct `TIInventoryTile.rebuildSlotMap()` call. Generated inventory
tiles recompute their flattened slot mapping from the inventories they already track; other tiles do nothing. It
does not bind parts or notify. See [refreshing inventory slots](api/TILE_TRAIT_ACCESS.md#refreshing-inventory-slots).
- **`internalPartChange(part)`** sends local `onPartChanged` callbacks through the retained `operate` hook. The base
traversal captures list order, visits only parts still bound to the receiving tile, and excludes parts equal to the
changed part using `part.equals(p)`. Null broadcasts to all eligible parts. Detached or transferred parts are skipped.
+33 -1
View File
@@ -33,6 +33,7 @@ call their generated `$class` helpers as a new integration strategy. Reflection
| Offered redstone connections or masked power | `IRedstoneConnector` | `getConnectionMask(side)`, `weakPowerLevel(side, mask)`; also used by non-FMP connectors |
| Ordinary tile power queries | `TileMultipart` | `strongPowerLevel(side)`, `weakPowerLevel(side)`; preserve each method's side convention |
| Inventory or sided inventory | Minecraft `IInventory` / `ISidedInventory` | Use the interface implemented by the tile, rather than raw `JInventoryTile` or its internal caches |
| Inventory slots after a part's size changes | `TileMultipart` | `refreshInventorySlots()`; see [refreshing inventory slots](#refreshing-inventory-slots) |
| Fluid operations | Forge `IFluidHandler` | Use the interface rather than raw `TFluidHandlerTile` or `tankList` |
| A consumer-owned pass-through capability | The original ordinary Java interface | Register with `MultipartGenerator.registerPassThroughInterface`; generated calls forward to the implementing part |
@@ -96,9 +97,40 @@ Keep the tile/part's existing notification, sound, packet and scheduling sequenc
`bindPart` dispatches through every generated cache trait, so those hooks still run once. As before, some non-slot
caches append on bind and are not generally idempotent. This API matches OpenComputers' print part, whose additional
capabilities do not append such caches; it is not a general refresh for inventory/fluid capability changes. A whole
capabilities do not append such caches; it is not a general refresh for inventory/fluid capability changes (see
[refreshing inventory slots](#refreshing-inventory-slots)). A whole
`loadPartList` reconstruction would run broader cache, binding and notification behavior and is not equivalent.
## Refreshing inventory slots
A tile holding `IInventory` parts exposes one flattened inventory: each part's slots in part order. FMP caches that
mapping and rebuilds it when inventory parts are added, removed, loaded or copied. When a stored part changes its
own `getSizeInventory()`, call `TileMultipart.refreshInventorySlots()`. It replaces AE2's direct
`TIInventoryTile.rebuildSlotMap()` call and drops the `instanceof` check, because tiles without inventory parts do
nothing:
```java
tile.refreshInventorySlots();
notifyNeighbors();
```
The [compiling example](../../src/functionalTest/java/codechicken/multipart/examples/TileTraitAccessExample.java)
wraps the same call:
```java
TileTraitAccessExample.refreshInventory(tile);
```
It only recomputes the mapping from the inventories already tracked. It does not add or remove inventories, call
`bindPart`, touch other trait caches, move items, mark the tile dirty or notify neighbors. Keep the caller's existing
notifications afterward and call it on the owning world thread. Do not use `refreshPartSlots` for this: its
`bindPart` dispatch would track the inventory part a second time.
The old `TIInventoryTile.rebuildSlotMap()` call stays supported for released AE2 jars, and both calls do the same
rebuild.
## Custom generated tiles
For custom generated tiles, keep consumer-callable methods on an ordinary Java capability interface and use the
stable base for existing tile hooks. The [custom tile-trait guide](CUSTOM_TILE_TRAITS.md) provides the complete
registration, helper, state/lifecycle and access pattern. Pass-through registration remains the smaller choice when
+5 -3
View File
@@ -570,7 +570,8 @@ AE2's cable bus is a full multipart integration, not just visual compatibility.
iterator.
- Occlusion is checked both directly with `NormalOcclusionTest` and by asking `tile.canAddPart` with an AE2
`NormallyOccludedPart` probe.
- When a generated tile implements `TIInventoryTile`, AE2 calls `rebuildSlotMap()` after changes.
- When a generated tile implements `TIInventoryTile`, AE2 calls `rebuildSlotMap()` from `notifyNeighbors()`, after
its cable bus inventory size may have changed. `TileMultipart.refreshInventorySlots()` is the supported replacement.
- AE2's Waila path and AE2 Fluid Craft load `TileMultipart` by name to register multipart HUD providers.
- Since `4548d13f0` (2026-09-12), `CableBusPart.bind(TileMultipart)` calls `super.bind` then refreshes hosted
parts through `CableBusContainer.updatePartHostInfo`. Host changes and world join also refresh those references.
@@ -868,7 +869,7 @@ behavior:
This ledger tracks migration of a specific legacy contract, not completion of an entire consumer's migration.
Factory registration/lookup, material enumeration, tile collection/traversal access, tile loading/storage assignment,
client preview reconstruction, tile occlusion and box queries, render-ID accessors, named tile conversion results and
the struck-part index are implemented on
the struck-part index and inventory slot refresh are implemented on
`algent/java`. The [API index](../API.md) links the guides and compiling Java examples. These additions are not yet
tied to a released minimum dependency version. Unlisted contracts remain governed by the inventories above.
@@ -877,6 +878,7 @@ tied to a released minimum dependency version. Unlisted contracts remain governe
| `ButtonPart.sideMetaMap[side ^ 1]` live inverse lookup | ProjectRed `e173952e96a4` and separate preparatory migration checkout: `illumination/items.scala`, `ItemPartButtonCommons.newPart` | `ButtonPart.metaForSide(side ^ 1)`; [lookup contract](../api/BUTTON_ORIENTATIONS.md#reading-orientation-metadata) | FMP accessor/example and regression coverage added; consumer replacement requires a rebuilt FMP artifact; preparatory migration is not upstream adoption | Retain the array until ProjectRed and Et Futurum migrations are released/adopted and a fresh scan clears legacy use |
| `TRedstoneTile.openConnections(int)` | ProjectRed `e173952e96a4`: `transmission/redwires.scala` | Existing stable `IRedstoneTile.openConnections(int)` with the same mask/rotation logic; [guide](../api/TILE_TRAIT_ACCESS.md) | FMP source-access example and Forge coverage complete; consumer source patch/release pending | Retain the old runtime interface descriptor until adoption |
| `TSlottedTile.v_partMap()` array mutation | OpenComputers `2c00f79be24b`: `PrintPart.toggleState` | `TileMultipart.refreshPartSlots(part)`; [equality, cache and notification contract](../api/TILE_TRAIT_ACCESS.md#refreshing-a-changed-slot-mask) | FMP API/example complete; consumer source patch/release pending, preserving validation and following sound/notify/packet/schedule order | Keep the live array/accessor ABI until released pack adoption; do not replace it with reflection or a whole-tile reload |
| `TIInventoryTile.rebuildSlotMap()` raw trait call | AE2 `c690fbc273be`: `appeng/fmp/CableBusPart.notifyNeighbors` | `TileMultipart.refreshInventorySlots()`, a no-op without inventory parts, so the `instanceof` guard can go; [mapping and scope](../api/TILE_TRAIT_ACCESS.md#refreshing-inventory-slots) | FMP API, example and regression coverage complete; consumer source patch and release pending, keeping its neighbor notification afterward | Keep the `TIInventoryTile` interface and `rebuildSlotMap` until released pack adoption |
| External Scala `LightMicroblock` registered through `MicroblockGenerator.registerTrait` | ProjectRed `e173952e96a4`: `illumination/lightmicroblocks.scala` | Java source trait registered by name, `IGeneratedMaterial`, typed sibling traversal and halo helper; [example](../api/MICROBLOCK_EXTENSIONS.md) | FMP example tested; consumer rewrite/release, existing config/halo wiring and physical-client checks pending | Retain external Scala trait ingestion until released-consumer adoption and internal Scala-trait removal |
| `MultiPartRegistry$.registerParts(IPartFactory2, Seq)` and existing array calls | ProjectRed `e173952e96a4`: transmission, expansion and fabrication proxies | `MultiPartRegistry.registerPartFactory` with existing `IPartFactory2` methods and unchanged IDs | Source patch and release pending; Boolean-factory proxies separately need the two-method adapter described in the guide | No migrated pack version verified; retain all registration bridges and external trait support |
| `MultiPartRegistry$.registerParts(Function2, Seq)` | ForgeRelocationFMP `49a810b8c63b`: proxy init registering `rfmp_frame` | `IPartFactory2` creating a fresh `FramePart` on both paths, registered through `registerPartFactory` | Source patch and release pending; preserve `rfmp_frame` and keep converter/pass-through registration | No migrated pack version verified; retain the function companion descriptor |
@@ -988,7 +990,7 @@ separate legacy/extension contracts.
[converter guide](../api/BLOCK_CONVERTERS.md) covers lifecycle; actual inventory/network transfers need integration checks.
- Raw trait classes become interfaces at runtime. ProjectRed should query `IRedstoneTile.openConnections` while
preserving rotation/masks; OpenComputers should use `refreshPartSlots` while preserving validation and subsequent
sound/notify/packet/schedule order. Keep their legacy runtime interfaces until adoption. See
sound/notify/packet/schedule order; AE2 should call `refreshInventorySlots` before its neighbor notification. Keep their legacy runtime interfaces until adoption. See
[stable tile access](../api/TILE_TRAIT_ACCESS.md).
- The [custom tile-trait guide](../api/CUSTOM_TILE_TRAITS.md) covers a top-level Java input, separate marker/capability
interfaces and ordinary helpers. Stateful extensions still own copy, persistence, synchronization and lifecycle
@@ -18,4 +18,9 @@ public final class TileTraitAccessExample {
public static void refreshSlots(TileMultipart tile, TMultiPart part) {
tile.refreshPartSlots(part);
}
/** Call after a stored inventory part changes its size, before notifying neighbors; no-op without inventories. */
public static void refreshInventory(TileMultipart tile) {
tile.refreshInventorySlots();
}
}
@@ -445,6 +445,7 @@ class ForgeEnvironmentSmokeTest {
"openInventory()V",
"partRemoved(Lcodechicken/multipart/TMultiPart;I)V",
"rebuildSlotMap()V",
"refreshInventorySlots()V",
"setInventorySlotContents(ILnet/minecraft/item/ItemStack;)V",
"slotMap()[Lscala/Tuple2;",
"slotMap_$eq([Lscala/Tuple2;)V",
@@ -4,6 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -20,10 +21,13 @@ import net.minecraft.item.ItemStack;
import org.junit.jupiter.api.Test;
import codechicken.multipart.TMultiPart;
import codechicken.multipart.TSlottedPart;
import codechicken.multipart.TileMultipart;
import codechicken.multipart.asm.MultipartMixinFactory;
import codechicken.multipart.examples.TileTraitAccessExample;
import codechicken.multipart.scalatraits.JInventoryTile;
import codechicken.multipart.scalatraits.TIInventoryTile;
import codechicken.multipart.scalatraits.TSlottedTile;
import scala.Tuple2;
import scala.collection.immutable.Nil$;
@@ -130,6 +134,61 @@ class TIInventoryTileFunctionalTest {
assertEquals(2, ((Number) api.slotMap()[2]._2()).intValue());
}
@Test
void supportedRefreshRebuildsTheMapAfterInventorySizeChanges() {
TileMultipart tile = newInventoryTile();
InventoryPart first = new InventoryPart("first", 1);
InventoryPart resized = new InventoryPart("resized", 1);
tile.addPart_do(first);
tile.addPart_do(resized);
TIInventoryTile inventory = inventory(tile);
assertEquals(2, inventory.getSizeInventory());
resized.resize(3);
assertEquals(2, inventory.getSizeInventory(), "FMP cannot see a size change on its own");
TileTraitAccessExample.refreshInventory(tile);
assertEquals(4, inventory.getSizeInventory());
assertEquals(Arrays.asList(first, resized), inventory.invList());
assertSame(first, inventory.slotMap()[0]._1());
assertSame(resized, inventory.slotMap()[3]._1());
assertEquals(2, ((Number) inventory.slotMap()[3]._2()).intValue());
assertEquals(Arrays.asList(first, resized), tile.jPartList());
}
@Test
void supportedRefreshDoesNothingWithoutTheInventoryCapability() {
TileMultipart plain = new TileMultipart();
PlainPart part = new PlainPart();
plain.addPart_do(part);
plain.refreshInventorySlots();
assertEquals(Arrays.asList(part), plain.jPartList());
TileMultipart slotted = newTile(TSlottedTile.class);
SlottedInventoryPart slottedPart = new SlottedInventoryPart(1 << 2);
slotted.addPart_do(slottedPart);
slotted.refreshInventorySlots();
assertFalse(slotted instanceof TIInventoryTile);
assertSame(slottedPart, slotted.partMap(2));
assertEquals(Arrays.asList(slottedPart), slotted.jPartList());
}
@Test
void supportedRefreshLeavesOtherTraitCachesAlone() {
TileMultipart tile = newTile(JInventoryTile.class, TSlottedTile.class);
SlottedInventoryPart part = new SlottedInventoryPart(1 << 2);
tile.addPart_do(part);
TIInventoryTile inventory = inventory(tile);
((InventoryPart) part).resize(4);
tile.refreshInventorySlots();
assertEquals(Arrays.asList(part), inventory.invList(), "no inventory is added again");
assertEquals(4, inventory.getSizeInventory());
assertSame(part, tile.partMap(2));
assertNull(tile.partMap(3));
}
@Test
void rebuildingQueriesEachInventorySizeOncePerPassAndPreservesSlotOrder() {
TIInventoryTile api = inventory(newInventoryTile());
@@ -209,9 +268,14 @@ class TIInventoryTileFunctionalTest {
}
private static TileMultipart newInventoryTile() {
int traitId = MultipartMixinFactory.getId(JInventoryTile.class.getName().replace('.', '/'));
return newTile(JInventoryTile.class);
}
private static TileMultipart newTile(Class<?>... traitInputs) {
BitSet traits = new BitSet();
traits.set(traitId);
for (Class<?> input : traitInputs) {
traits.set(MultipartMixinFactory.getId(input.getName().replace('.', '/')));
}
return (TileMultipart) MultipartMixinFactory.construct(traits, Nil$.MODULE$);
}
@@ -327,6 +391,21 @@ class TIInventoryTileFunctionalTest {
}
}
private static final class SlottedInventoryPart extends InventoryPart implements TSlottedPart {
private final int slotMask;
private SlottedInventoryPart(int slotMask) {
super("slotted", 1);
this.slotMask = slotMask;
}
@Override
public int getSlotMask() {
return slotMask;
}
}
private static final class SidedInventoryPart extends InventoryPart implements ISidedInventory {
private final int[] accessibleSlots;
@@ -572,6 +572,18 @@ public class TileMultipart extends TileEntity implements IChunkLoadTile {
*/
public void refreshPartSlots(TMultiPart part) {}
/**
* Recomputes the generated inventory's flattened slot mapping after an inventory part already stored in this tile
* changes its {@link net.minecraft.inventory.IInventory#getSizeInventory()}. Tiles holding an {@code IInventory}
* part rebuild the mapping in part order; a tile without the inventory capability does nothing.
*
* <p>
* Adding, removing and loading parts already rebuild the mapping; call this only for a size change FMP cannot see.
* It does not add or remove inventories, move items, mark the tile dirty or notify neighbors. Callers keep their
* own notifications afterward. Replaces direct calls to the generated {@code TIInventoryTile.rebuildSlotMap()}.
*/
public void refreshInventorySlots() {}
/** Internal FMP placement callback for trait overrides; do not call directly from consumers. */
public void partAdded(TMultiPart part) {}
@@ -96,6 +96,11 @@ public class JInventoryTile extends TileMultipart implements TIInventoryTile {
slotMap = (Tuple2<IInventory, Object>[]) new Tuple2[0];
}
@Override
public void refreshInventorySlots() {
rebuildSlotMap();
}
@Override
@SuppressWarnings("unchecked")
public void rebuildSlotMap() {