mirror of
https://github.com/GTNewHorizons/ForgeMultipart.git
synced 2026-11-28 16:06:17 +00:00
docs: cover custom Java tile traits
This commit is contained in:
1 parent
d98e48ff03
commit
bd8e473c7f
9 files changed
+250
-37
No files matched your search
+5
-3
@@ -27,6 +27,7 @@ Existing reflective binaries remain supported until consumer release and pack ad
|
||||
| Read a microblock material's block and metadata | `BlockMicroMaterial.block()`, `meta()` — [typed GuideNH query, identity and overrides](api/MATERIAL_ACCESS.md) |
|
||||
| Create a microblock with the requested material and side | `MicroblockGenerator.create(MicroblockClass, int, boolean)` — [construction, material traits and GuideNH migration](api/MICROBLOCK_CREATION.md) |
|
||||
| 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) |
|
||||
| Add material-specific behavior to generated microblocks | `registerTrait(String)` and `IGeneratedMaterial` — [illuminated Java extension, compilation and side contracts](api/MICROBLOCK_EXTENSIONS.md) |
|
||||
| Read/index/search a tile's parts | `jPartList()` — [part collection ownership and order](api/PART_TRAVERSAL.md#collection-ownership-and-ordering) |
|
||||
@@ -55,7 +56,7 @@ compile classpath for overload resolution, as the [loading guide](api/PART_LOADI
|
||||
| Find, place and remove parts | [TileMultipart](../src/main/scala/codechicken/multipart/TileMultipart.java): `getTile`, `canPlacePart`, `addPart`, `remPart`. Retain the tile returned by changes because generated capabilities can replace the instance |
|
||||
| Construct a server composite tile from parts, or restore saved multipart NBT | [MultipartHelper](../src/main/scala/codechicken/multipart/MultipartHelper.java): `createTileFromParts(Iterable)`, `createTileFromNBT(World, NBTTagCompound)` |
|
||||
| Register microblock materials | [MicroMaterialRegistry](../src/main/scala/codechicken/microblock/MicroMaterialRegistry.java) and [BlockMicroMaterial](../src/main/scala/codechicken/microblock/BlockMicroMaterial.java) |
|
||||
| Register generated tile traits or pass-through interfaces | [MultipartGenerator](../src/main/scala/codechicken/multipart/MultipartGenerator.java); see [safe capability access](api/TILE_TRAIT_ACCESS.md) and the [extension constraints](../JAVA_MIGRATION_HANDOFF.md#retained-compiler-constraints) before migrating custom traits |
|
||||
| Register generated tile traits or pass-through interfaces | [MultipartGenerator](../src/main/scala/codechicken/multipart/MultipartGenerator.java); see [custom tile-trait authoring](api/CUSTOM_TILE_TRAITS.md) and [safe capability access](api/TILE_TRAIT_ACCESS.md) |
|
||||
|
||||
New API examples use Java 8 language/library features. The source path `src/main/scala` also contains Java classes
|
||||
because of the current joint-compilation layout; it does not imply that those APIs require Scala source in a consumer.
|
||||
@@ -113,8 +114,9 @@ An [illuminated Java microblock extension](api/MICROBLOCK_EXTENSIONS.md) now cov
|
||||
light aggregation and halo geometry, with compiling examples and Forge coverage. Its physical-client rendering and
|
||||
ProjectRed adoption remain open. [Converter registration and lifecycle](api/BLOCK_CONVERTERS.md) are documented and
|
||||
tested. [Stable tile capability access](api/TILE_TRAIT_ACCESS.md) covers safe Java calls, ProjectRed redstone queries
|
||||
and OpenComputers slot refresh. Custom tile-trait authoring and the remaining audited reflection replacements are
|
||||
still pending. ProjectRed's existing Scala traits remain supported until adoption.
|
||||
and OpenComputers slot refresh. [Custom Java tile-trait authoring](api/CUSTOM_TILE_TRAITS.md) covers registration,
|
||||
stable capabilities, lifecycle/state rules and transformer constraints with generated Forge coverage. The remaining
|
||||
audited reflection replacements are still pending. ProjectRed's existing Scala traits remain supported until adoption.
|
||||
|
||||
All ten entries in the plan's Phase 9.1 API table have Java replacements. That table is a bounded list of signatures;
|
||||
the broader API, extension and consumer adoption work above remains open.
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
# Custom generated tile traits from Java
|
||||
|
||||
[API index](../API.md) · [Stable capability access](TILE_TRAIT_ACCESS.md) · [Composite generation](COMPOSITE_GENERATION.md)
|
||||
|
||||
A custom tile trait adds behavior to every generated `TileMultipart` that contains a part implementing a chosen marker
|
||||
interface. Use it for tile-wide aggregation or lifecycle behavior. If the tile only needs to expose one interface and
|
||||
forward its methods to one implementing part, use `MultipartGenerator.registerPassThroughInterface` instead; FMP
|
||||
already generates that forwarding and rejects a second implementor through occlusion.
|
||||
|
||||
The complete compiling example has four small types:
|
||||
|
||||
- [CustomTileTraitPart.java](../../src/functionalTest/java/codechicken/multipart/examples/CustomTileTraitPart.java) is
|
||||
the marker and contribution contract implemented by requesting parts.
|
||||
- [CustomTileTraitCapability.java](../../src/functionalTest/java/codechicken/multipart/examples/CustomTileTraitCapability.java)
|
||||
is the stable interface used by callers.
|
||||
- [CustomTileTrait.java](../../src/functionalTest/java/codechicken/multipart/examples/CustomTileTrait.java) is the raw
|
||||
input class FMP transforms, plus its ordinary helper.
|
||||
- [CustomTileTraitExample.java](../../src/functionalTest/java/codechicken/multipart/examples/CustomTileTraitExample.java)
|
||||
registers and accesses the capability.
|
||||
|
||||
These files live in the functional-test source set and are examples, not classes shipped in the production artifact.
|
||||
|
||||
## Registration and selection
|
||||
|
||||
Register during common mod initialization, before FMP first inspects any part class implementing the marker:
|
||||
|
||||
```java
|
||||
private static final String TRAIT = "example.multipart.ContributionTileTrait";
|
||||
|
||||
public static void register() {
|
||||
MultipartGenerator.registerTrait(ContributionPart.class.getName(), TRAIT);
|
||||
}
|
||||
```
|
||||
|
||||
Keep the trait name as a string literal. `ContributionTileTrait.class.getName()` loads the untransformed class before
|
||||
FMP can rewrite it. The marker is an ordinary interface, so its class literal is safe. Names may contain dots or
|
||||
slashes.
|
||||
|
||||
The two-argument overload installs the same trait on client and server generated tiles. Use
|
||||
`registerTrait(marker, clientTrait, serverTrait)` when the sides differ; either trait name may be null. The order is
|
||||
client first, then server. Register a parent Java trait before a child trait.
|
||||
|
||||
Registration is global for the running game. A second mapping for the same marker and side logs an error and leaves
|
||||
the first mapping active. Registration loads and validates the trait bytes, so missing or invalid traits fail at that
|
||||
point. It does not register a part type, construct a tile, load parts or bind ownership.
|
||||
|
||||
When FMP first sees a concrete part class, it records every registered marker that class implements. Composite classes
|
||||
are then cached by the exact selected trait set and side. Registering after that inspection is too late for the cached
|
||||
part class.
|
||||
|
||||
## Trait and stable capability
|
||||
|
||||
The transformed input is a top-level class extending `TileMultipart`. It may be abstract and needs a no-argument
|
||||
constructor. Put the API that other code calls on a separate ordinary interface:
|
||||
|
||||
```java
|
||||
public interface ContributionTileCapability {
|
||||
int totalContribution();
|
||||
}
|
||||
|
||||
public abstract class ContributionTileTrait extends TileMultipart
|
||||
implements ContributionTileCapability {
|
||||
|
||||
@Override
|
||||
public int totalContribution() {
|
||||
return ContributionTileTraitAccess.total(this);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
At runtime FMP rewrites `ContributionTileTrait` into an interface and copies its method bodies into a generated helper.
|
||||
Calling through the untransformed class can therefore emit invalid class method or field bytecode. Callers use the
|
||||
stable capability instead:
|
||||
|
||||
```java
|
||||
int total = tile instanceof ContributionTileCapability
|
||||
? ((ContributionTileCapability) tile).totalContribution()
|
||||
: 0;
|
||||
```
|
||||
|
||||
Keep the absent-capability behavior in the caller-facing helper. The example returns zero for null and ordinary tiles.
|
||||
Adding or removing a requesting part may replace the tile with another generated class, so retain the tile returned by
|
||||
FMP's normal placement and removal APIs.
|
||||
|
||||
## Transformer-safe method bodies
|
||||
|
||||
The current Java trait transformer supports the example's small delegating method. Keep substantial work in an
|
||||
ordinary top-level helper outside the transformed class:
|
||||
|
||||
```java
|
||||
final class ContributionTileTraitAccess {
|
||||
static int total(Object receiver) {
|
||||
TileMultipart tile = (TileMultipart) receiver;
|
||||
int total = 0;
|
||||
for (TMultiPart part : tile.jPartList()) {
|
||||
if (part instanceof ContributionPart) {
|
||||
total += ((ContributionPart) part).contribution();
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The helper parameter must be `Object`. If it is typed as the raw trait, javac can remove the cast to `TileMultipart`;
|
||||
after transformation the verifier then sees an interface receiver where the bytecode expected a class. The helper is
|
||||
also where inherited field/method access and field access on method arguments belong, because the transformer can
|
||||
otherwise mistake those reads for trait-owned state.
|
||||
|
||||
Keep the transformed class free of nested, local and anonymous classes, lambdas and compiler-generated inner-class
|
||||
helpers. Primitive-array allocation is also unsupported in a trait body. Put those operations in the ordinary helper.
|
||||
Client-only members may use Forge's `@SideOnly(Side.CLIENT)`; FMP removes opposite-side members before collecting the
|
||||
trait. Test the resulting generated class on each selected side.
|
||||
|
||||
## State and lifecycle
|
||||
|
||||
Prefer derived behavior when it is cheap enough. The example sums the current `jPartList()` and owns no tile state,
|
||||
which avoids NBT, packet, invalidation and transition rules.
|
||||
|
||||
If a trait declares fields, FMP generates `copyFrom(TileMultipart)` when the trait does not declare one. It copies each
|
||||
non-transient field only when the source tile has the same trait; transient fields are excluded. Persistent or
|
||||
client-synchronized state still needs explicit NBT/description handling. A custom `copyFrom` replaces the generated
|
||||
field copier, so it must preserve the normal superclass chain and define behavior for sources without the trait.
|
||||
|
||||
Cached aggregates must also follow every lifecycle path that can change them. Override the relevant `bindPart`,
|
||||
`partRemoved` and `clearParts` hooks, call `super` in the established order, and cover tile promotion/demotion and
|
||||
loading with a focused Forge test. Do not use a custom cache unless measurement shows the derived scan is too costly.
|
||||
|
||||
`MultipartGenerator.generateCompositeTile` only chooses or constructs the empty composite class. If using that advanced
|
||||
API directly, prepare world/position or saved state and then call `loadPartList`; ordinary placement performs the
|
||||
required transition itself. See the [staged generation guide](COMPOSITE_GENERATION.md) for ownership and failure rules.
|
||||
|
||||
## Validation and adoption
|
||||
|
||||
[CustomTileTraitFunctionalTest.java](../../src/functionalTest/java/codechicken/multipart/test/CustomTileTraitFunctionalTest.java)
|
||||
registers the real example during Forge initialization. It checks server and client selections, transformed capability
|
||||
dispatch, mixed-part aggregation, part binding, exact tile reuse and generated-class caching. The four example sources
|
||||
also compile as Java 8 against the packaged dev artifact without Scala.
|
||||
|
||||
No supplied active consumer directly registers a custom `MultipartGenerator` tile trait; their current generated-tile
|
||||
extensions use built-in traits or pass-through interfaces. This guide establishes the supported Java extension pattern
|
||||
and does not count any consumer as migrated. Existing Scala trait inputs and binary contracts remain supported until
|
||||
their release and target-pack adoption gates pass.
|
||||
@@ -101,16 +101,13 @@ capabilities do not append such caches; it is not a general refresh for inventor
|
||||
`loadPartList` reconstruction would run broader cache, binding and notification behavior and is not equivalent.
|
||||
|
||||
For custom generated tiles, keep consumer-callable methods on an ordinary Java capability interface and use the
|
||||
stable base for existing tile hooks. Register raw trait inputs by name before class loading, during initialization.
|
||||
Pass-through registration is sufficient when a tile should forward one interface to one part; it rejects a second
|
||||
implementor through occlusion checks. Writing custom aggregation/lifecycle traits still needs the compiler's
|
||||
[Java trait restrictions](../../JAVA_MIGRATION_HANDOFF.md#retained-compiler-constraints), including helper placement
|
||||
and inherited access. The [microblock extension](MICROBLOCK_EXTENSIONS.md) demonstrates those helper constraints,
|
||||
but is not a complete custom tile-trait authoring example.
|
||||
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
|
||||
a tile should only forward one interface to one implementing part.
|
||||
|
||||
No transformed compile-stub artifact is introduced here: redstone already has a stable interface and slot refresh is
|
||||
available on the stable base tile. Assess remaining trait-only requirements individually before adding build
|
||||
machinery. Custom tile-trait authoring and other audited private/reflection contracts remain open.
|
||||
machinery. Other audited private/reflection contracts remain open.
|
||||
|
||||
## Validation
|
||||
|
||||
@@ -122,5 +119,6 @@ entry with the frozen consumer sequence, including equality, current-mask rebind
|
||||
storage. Packaged-jar compilation and bytecode inspection verify that the example uses stable owners without Scala,
|
||||
reflection or raw trait-class references. Existing binary APIs remain callable; `refreshPartSlots` is additive.
|
||||
|
||||
This is consumer source-access coverage. It does not validate physical-client trait selection/rendering, supply all
|
||||
custom extension examples, or establish that any consumer has released and adopted a migration.
|
||||
This is consumer source-access coverage. It does not validate physical-client rendering or establish that any consumer
|
||||
has released and adopted a migration. Generated custom tile traits have separate both-side coverage in the
|
||||
[authoring guide](CUSTOM_TILE_TRAITS.md#validation-and-adoption).
|
||||
@@ -2157,3 +2157,24 @@ differences belong in the [divergence ledger](../../JAVA_MIGRATION_DIVERGENCES.m
|
||||
without Scala/reflection/raw-trait references. Evidence: `run/migration-slot-refresh-reference/`.
|
||||
- Consumer source/release/adoption remain pending. Next bounded task: a custom Java tile-trait authoring example with
|
||||
generated Forge coverage; remaining audited reflection replacements and physical-client checks stay separate.
|
||||
|
||||
### 2026-09-08 — Custom Java tile-trait authoring
|
||||
|
||||
- Committed baseline `d98e48f` before production documentation changes. Its Forge example registers a top-level Java
|
||||
trait by name during initialization and verifies server/client selection, stable capability dispatch, mixed-part
|
||||
aggregation, part binding, exact tile reuse and generated-class caching.
|
||||
- Added the [custom tile-trait guide](../api/CUSTOM_TILE_TRAITS.md) and expanded both `registerTrait` Javadocs. The
|
||||
four-type pattern separates the requesting marker, consumer capability, transformed input and ordinary helper. It
|
||||
records class-loading order, side-specific registration, first-mapping behavior, state/lifecycle ownership and the
|
||||
current transformer constraints. The example uses a derived aggregate instead of adding a cache.
|
||||
- No supplied active consumer directly registers a custom multipart tile trait. Current consumers use built-in traits,
|
||||
pass-through interfaces or ProjectRed's distinct microblock-trait path. Reference checkouts remain unchanged and no
|
||||
migration/release/adoption is claimed.
|
||||
- Normal and clean formatting/checkstyle/build/Forge validation passes with **573 JVM / 287 Forge** cases, zero
|
||||
failures/errors/skips. All **573 archived JVM / 287 archived Forge** callers pass. The example targets Java 8 against
|
||||
the dev artifact without Scala or reflection and calls only the stable capability/base API.
|
||||
- All **444** production classes, **17** ScalaSignature payloads and **3,763** packaged methods remain. All **134**
|
||||
generated dumps retain executable equivalence; only source-line metadata moves in the facade class. Evidence:
|
||||
`run/migration-custom-tile-trait-reference/`.
|
||||
- Next bounded task: expose supported button-orientation customization for Et Futurum, then cover Iguana's saw-strength
|
||||
customization. Consumer adoption, physical-client/full-pack checks and measured performance remain separate gates.
|
||||
Reference in new issue
Block a user