From 2f60e601939eda129b58d8e891503c562f92e131 Mon Sep 17 00:00:00 2001 From: Algent Date: Tue, 1 Sep 2026 20:12:54 +0200 Subject: [PATCH] refactor: port face microblock factory --- JAVA_MIGRATION.md | 9 ++ JAVA_MIGRATION_DIVERGENCES.md | 48 +++++++++++ JAVA_MIGRATION_HANDOFF.md | 25 ++++-- JAVA_MIGRATION_MANUAL_CHECKS.md | 2 +- .../microblock/FaceMicroClass$.java | 62 ++++++++++++++ .../microblock/FaceMicroClass.java | 74 ++++++++++++++++ .../microblock/FaceMicroblock.scala | 85 ------------------- .../microblock/FaceMicroblockTraits.scala | 44 ++++++++++ .../microblock/FacePlacement$.java | 33 +++++++ .../codechicken/microblock/FacePlacement.java | 30 +++++++ .../microblock/HollowMicroblock.scala | 2 +- 11 files changed, 319 insertions(+), 95 deletions(-) create mode 100644 src/main/scala/codechicken/microblock/FaceMicroClass$.java create mode 100644 src/main/scala/codechicken/microblock/FaceMicroClass.java delete mode 100644 src/main/scala/codechicken/microblock/FaceMicroblock.scala create mode 100644 src/main/scala/codechicken/microblock/FaceMicroblockTraits.scala create mode 100644 src/main/scala/codechicken/microblock/FacePlacement$.java create mode 100644 src/main/scala/codechicken/microblock/FacePlacement.java diff --git a/JAVA_MIGRATION.md b/JAVA_MIGRATION.md index 8620f64..e7cad86 100644 --- a/JAVA_MIGRATION.md +++ b/JAVA_MIGRATION.md @@ -1155,3 +1155,12 @@ generated-trait compatibility work. Scala-runtime removal remains a later projec - A clean build passes all 244 plain-JVM tests and the Java 8 Forge server passes all 103 tests, including generated face/hollow parts and the external Scala microblock trait fixture. All eight retained public surfaces match the reference; only two private Scala iteration closures disappear. `microblock/FaceMicroblock.scala` is next. +- Characterized the face factory, placement singleton and both generated traits against untouched Scala. Two + plain-JVM cases freeze all eight retained public surfaces and every placement rule; one Forge case freezes factory + identity, all 42 populated bounds and generated face-part behavior. +- Ported the four concrete facade/companion types directly to Java while retaining `FaceMicroblock` and + `FaceMicroblockClient` unchanged in `FaceMicroblockTraits.scala`. All callable public descriptors match the + reference; the two private Scala bounds-initializer closures disappear. +- A clean build passes all 246 plain-JVM tests and the Java 8 Forge server passes all 104 tests. Recompiled Scala must + spell the Java array getter as `FaceMicroClass.aBounds()(index)`; existing binaries still link to the unchanged + `aBounds(): Cuboid6[]` descriptor. `microblock/CornerMicroblock.scala` is next. diff --git a/JAVA_MIGRATION_DIVERGENCES.md b/JAVA_MIGRATION_DIVERGENCES.md index aae2c70..ac14007 100644 --- a/JAVA_MIGRATION_DIVERGENCES.md +++ b/JAVA_MIGRATION_DIVERGENCES.md @@ -2749,3 +2749,51 @@ types are unchanged. separately compiled external Scala microblock trait. - Raw `javap` comparison confirms identical callable public names/descriptors for `Microblock`, `Microblock$`, all three traits and all three `$class` helpers; the retained class inventory is otherwise exact. + +## 2026-09-01 — FaceMicroblock factory Java port + +The face placement and factory facades/companions are now Java. The generated `FaceMicroblock` and +`FaceMicroblockClient` traits remain Scala in `FaceMicroblockTraits.scala`; translating them would widen the generator +work without helping this concrete-state port. + +### Observable behavior + +No known runtime divergence. Placement still selects the opposite face with `slot ^ 1`, expands only when the clicked +side is that opposite face, and uses the same face placement grid. Factory name, item slot, resistance, trait classes +and eager common-trait registration are unchanged. + +The bounds table remains a mutable 256-entry array. The same 42 entries (`size` 1 through 7, `side` 0 through 5) are +populated with the same rotations and cuboids; all other entries remain null. Generated parts still use the table +entry by identity, expose the same face slot and delegate solidity to their material. + +### ABI and reflection compatibility + +All eight retained facade, companion, trait and `$class` types keep their hierarchy and callable public +names/descriptors. The static `FaceMicroClass.aBounds(): Cuboid6[]` method used by WirelessRedstone and existing +ProjectRed binaries is unchanged, as are `FaceMicroClass.getClassId()` and both `MODULE$` singleton fields. + +Recompiled Scala source must write `FaceMicroClass.aBounds()(index)` rather than +`FaceMicroClass.aBounds(index)`, because the Java facade does not carry Scala property metadata. This is source-only; +existing bytecode already performs the unchanged zero-argument getter followed by an array load. The companion's +`baseTrait` generic signature is necessarily `Class` in Java rather than Scala's +`Class`; its raw JVM descriptor remains `()Ljava/lang/Class;`, and the static facade retains the +consumer-facing `Class` signature. + +Accepted classfile-only differences also include the ordinary Java companion class-initializer flag and removed +Scala signature/marker attributes. + +### Compiler artifacts + +`FaceMicroClass$$anonfun$1` and `FaceMicroClass$$anonfun$1$$anonfun$apply$mcVI$sp$1` disappear. Direct nested Java +loops need neither private bounds-initializer closure, and the downstream inventory contains no reference to them. +The eight supported runtime types remain. + +### Validation + +- `FaceMicroblockCharacterizationTest`: 2 plain-JVM tests, unchanged from the Scala baseline. +- Clean complete plain-JVM suite: 246 tests, 0 failures, 0 errors. +- Java 8 Forge dedicated-server suite: 104 tests, 0 failures, 0 errors, including all 42 bounds and a runtime-generated + face part. +- Raw `javap` comparison confirms identical callable public names/descriptors for both facades, both companions, both + traits and both `$class` helpers. +- Actual face rendering and highlight geometry remain covered by the existing manual checklist entries. diff --git a/JAVA_MIGRATION_HANDOFF.md b/JAVA_MIGRATION_HANDOFF.md index 4a6d671..915b343 100644 --- a/JAVA_MIGRATION_HANDOFF.md +++ b/JAVA_MIGRATION_HANDOFF.md @@ -12,8 +12,8 @@ what breaks, and what is left. The other documents hold the reasoning. | `JAVA_MIGRATION_MANUAL_CHECKS.md` | What no automated test can cover, and must be checked by hand in a client | | `JAVA_MIGRATION_PROFILE.md` | The focused baseline, first measured result, findings, and exact rerun command | -Branch: `algent/java`. Base: `master`. 136 commits including the API-cleanup plan and separate characterization and -port commits through the `Microblock` base. +Branch: `algent/java`. Base: `master`. 138 commits including the API-cleanup plan and separate characterization and +port commits through the face-microblock factory. ## The one rule that matters @@ -67,7 +67,7 @@ awk -F'"' '/ ... }` needs an explicit `AbstractFunction1`. +`Function1[_, BoxedUnit]`, so `operate { p => ... }` needs an explicit `AbstractFunction1`. A Java static array getter +also loses Scala property/indexing composition: `FaceMicroClass.aBounds(index)` becomes +`FaceMicroClass.aBounds()(index)` when recompiling, while existing bytecode remains compatible. **A companion object can be load-bearing without a single bytecode reference.** The inventory's 17 `MODULE$` list is not the whole test — check the reflective string constants too. `MultipartHelper$` is in neither the `MODULE$` list nor @@ -270,7 +272,7 @@ across the port. It is also where the two shim constraints in the gotchas list w `rayTraceAll`'s index production is now characterized, closing the gap the read-path cleanup left: the index written into `ExtendedMOP.data` is what `reduceMOP` hands back to every click, activate, harvest and pick block. -167 Java files, 16 Scala files, ~3,139 Scala lines left (non-blank; that is the metric this figure has always used). +171 Java files, 16 Scala files, ~3,111 Scala lines left (non-blank; that is the metric this figure has always used). ## What is left, and in what order @@ -490,9 +492,16 @@ and packet/update bytes. `MicroblockClient`, `CommonMicroblock` and `CommonMicro in `MicroblockTraits.scala`: this preserves their multiple-inheritance ABI and ProjectRed's existing Scala-trait generator path without an ASM change. The Forge suite still generates built-in parts and the external Scala fixture. -**Medium.** `FaceMicroblock.scala` is next. Apply the same narrow split: characterize its factory/facade, placement -singleton, bounds table and both traits, then port the concrete/singleton code while retaining generated traits unless -an exact Java representation is proven first. Do not combine it with another shape family. +The face factory and placement singleton are now four Java facade/companion types. Two plain-JVM cases freeze all +eight retained surfaces and placement behavior; one Forge case freezes the 256-entry table's exact 42 populated +bounds, factory identity and generated `FaceMicroblock`/`TFacePart` behavior. `FaceMicroblock` and +`FaceMicroblockClient` deliberately remain Scala in `FaceMicroblockTraits.scala`, preserving their exact generated +trait and `$class` surfaces. Existing binaries retain the same `aBounds(): Cuboid6[]` call; recompiled Scala source +must use `FaceMicroClass.aBounds()(index)`. Only the two private bounds-initializer closures disappear. + +**Medium.** `CornerMicroblock.scala` is next. Apply the same narrow split: characterize its factory/facade, placement +singleton, 56 populated bounds and generated trait, then port the concrete/singleton code while retaining the trait +unless an exact Java representation is proven first. Do not combine it with another shape family. **Phase 5 is complete.** There are no Scala files left in `multipart/scalatraits/`. The client pair required the first narrow generator relaxation: Java-trait parent linearization, explicit field-accessor recognition, and exclusion of diff --git a/JAVA_MIGRATION_MANUAL_CHECKS.md b/JAVA_MIGRATION_MANUAL_CHECKS.md index 273071f..6f67867 100644 --- a/JAVA_MIGRATION_MANUAL_CHECKS.md +++ b/JAVA_MIGRATION_MANUAL_CHECKS.md @@ -47,7 +47,7 @@ below its table. | Done | Check | Expected | From | | --- | --- | --- | --- | -| [ ] | Load the pack with ProjRed installed and place illumar lamp microblocks | The lamps generate, render and light correctly | `MicroblockGenerator.registerTrait` Scala trait path | +| [ ] | Load the pack with ProjRed installed and place illumar lamp microblocks | The lamps generate, use the correct face bounds, render and light correctly | `MicroblockGenerator.registerTrait` Scala trait path / `FaceMicroClass.aBounds` facade | | [ ] | Place and break OpenComputers, ProjectBlue and AE2 parts | No `NoSuchMethodError` or `AbstractMethodError` in the log | retained `$class` bridges | | [ ] | Load and render a schematic containing an ordered mixed multipart tile | Every part appears in the preview in the saved order; the integration does not silently disable itself | Schematica private registry-map reflection, `MicroblockClass.create`, and tile NBT reconstruction | | [ ] | Open a GuideNH scene containing a multipart tile and export its part/material data | The preview, material data and part statistics are complete | companion reflection, `MicroblockGenerator$.create`, `Microblock.microClass/material/shape`, `partList_$eq`, and `BlockMicroMaterial` mixin fields | diff --git a/src/main/scala/codechicken/microblock/FaceMicroClass$.java b/src/main/scala/codechicken/microblock/FaceMicroClass$.java new file mode 100644 index 0000000..3ab1ca2 --- /dev/null +++ b/src/main/scala/codechicken/microblock/FaceMicroClass$.java @@ -0,0 +1,62 @@ +package codechicken.microblock; + +import codechicken.lib.vec.Cuboid6; +import codechicken.lib.vec.Rotation; +import codechicken.lib.vec.Transformation; +import codechicken.lib.vec.Vector3; + +public final class FaceMicroClass$ extends CommonMicroClass { + + public static final FaceMicroClass$ MODULE$ = new FaceMicroClass$(); + + private Cuboid6[] aBounds = new Cuboid6[256]; + + private FaceMicroClass$() { + for (int side = 0; side < 6; side++) { + Transformation transform = Rotation.sideRotations[side].at(Vector3.center); + for (int thickness = 1; thickness < 8; thickness++) { + double distance = thickness / 8D; + aBounds[thickness << 4 | side] = new Cuboid6(0, 0, 0, 1, distance, 1).apply(transform); + } + } + } + + public Cuboid6[] aBounds() { + return aBounds; + } + + public void aBounds_$eq(Cuboid6[] bounds) { + aBounds = bounds; + } + + @Override + public String getName() { + return "mcr_face"; + } + + @Override + public int itemSlot() { + return 3; + } + + @Override + @SuppressWarnings({ "rawtypes", "unchecked" }) + public Class baseTrait() { + return (Class) FaceMicroblock.class; + } + + @Override + public Class clientTrait() { + return FaceMicroblockClient.class; + } + + @Override + public FacePlacement$ placementProperties() { + return FacePlacement$.MODULE$; + } + + @Override + public float getResistanceFactor() { + return 1; + } +} diff --git a/src/main/scala/codechicken/microblock/FaceMicroClass.java b/src/main/scala/codechicken/microblock/FaceMicroClass.java new file mode 100644 index 0000000..8df7879 --- /dev/null +++ b/src/main/scala/codechicken/microblock/FaceMicroClass.java @@ -0,0 +1,74 @@ +package codechicken.microblock; + +import net.minecraft.nbt.NBTTagCompound; + +import codechicken.lib.data.MCDataInput; +import codechicken.lib.vec.Cuboid6; + +public class FaceMicroClass { + + public static int baseTraitId() { + return FaceMicroClass$.MODULE$.baseTraitId(); + } + + public static int clientTraitId() { + return FaceMicroClass$.MODULE$.clientTraitId(); + } + + public static void register() { + FaceMicroClass$.MODULE$.register(); + } + + public static Microblock create(boolean client, int material) { + return FaceMicroClass$.MODULE$.create(client, material); + } + + public static Microblock createPart(String name, MCDataInput packet) { + return FaceMicroClass$.MODULE$.createPart(name, packet); + } + + public static Microblock createPart(String name, NBTTagCompound nbt) { + return FaceMicroClass$.MODULE$.createPart(name, nbt); + } + + public static int getClassId() { + return FaceMicroClass$.MODULE$.getClassId(); + } + + public static void register(int id) { + FaceMicroClass$.MODULE$.register(id); + } + + public static Cuboid6[] aBounds() { + return FaceMicroClass$.MODULE$.aBounds(); + } + + public static void aBounds_$eq(Cuboid6[] bounds) { + FaceMicroClass$.MODULE$.aBounds_$eq(bounds); + } + + public static String getName() { + return FaceMicroClass$.MODULE$.getName(); + } + + public static int itemSlot() { + return FaceMicroClass$.MODULE$.itemSlot(); + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + public static Class baseTrait() { + return (Class) FaceMicroClass$.MODULE$.baseTrait(); + } + + public static Class clientTrait() { + return FaceMicroClass$.MODULE$.clientTrait(); + } + + public static FacePlacement$ placementProperties() { + return FaceMicroClass$.MODULE$.placementProperties(); + } + + public static float getResistanceFactor() { + return FaceMicroClass$.MODULE$.getResistanceFactor(); + } +} diff --git a/src/main/scala/codechicken/microblock/FaceMicroblock.scala b/src/main/scala/codechicken/microblock/FaceMicroblock.scala deleted file mode 100644 index 4da99a5..0000000 --- a/src/main/scala/codechicken/microblock/FaceMicroblock.scala +++ /dev/null @@ -1,85 +0,0 @@ -package codechicken.microblock - -import codechicken.lib.vec.Cuboid6 -import codechicken.multipart.TFacePart -import codechicken.lib.vec.Vector3 -import codechicken.lib.vec.Rotation -import Rotation._ -import Vector3._ - -object FacePlacement extends PlacementProperties { - def microClass = FaceMicroClass - - def placementGrid = FacePlacementGrid$.MODULE$ - - def opposite(slot: Int, side: Int) = slot ^ 1 - - override def expand(slot: Int, side: Int) = sneakOpposite(slot, side) - - override def sneakOpposite(slot: Int, side: Int) = slot == (side ^ 1) -} - -class FaceMicroClass - -object FaceMicroClass extends CommonMicroClass { - var aBounds: Array[Cuboid6] = new Array(256) - - for (s <- 0 until 6) { - val transform = sideRotations(s).at(center) - for (t <- 1 until 8) { - val d = t / 8d - aBounds(t << 4 | s) = new Cuboid6(0, 0, 0, 1, d, 1).apply(transform) - } - } - - def getName = "mcr_face" - - def itemSlot = 3 - - def baseTrait = classOf[FaceMicroblock] - def clientTrait = classOf[FaceMicroblockClient] - - def placementProperties = FacePlacement - - def getResistanceFactor = 1 -} - -trait FaceMicroblockClient extends CommonMicroblockClient { - override def render(pos: Vector3, pass: Int) { - if (pass < 0) - MicroblockRender.renderCuboid(pos, getIMaterial, pass, getBounds, 0) - else if (isTransparent) - MicroblockRender.renderCuboid( - pos, - getIMaterial, - pass, - renderBounds, - renderMask - ) - else { - val mat = getIMaterial - MicroblockRender.renderCuboid( - pos, - mat, - pass, - renderBounds, - renderMask | 1 << getSlot - ) - MicroblockRender.renderCuboid( - pos, - mat, - pass, - Cuboid6.full, - ~(1 << getSlot) - ) - } - } -} - -trait FaceMicroblock extends CommonMicroblock with TFacePart { - def microClass = FaceMicroClass - - def getBounds = FaceMicroClass.aBounds(shape) - - override def solid(side: Int) = getIMaterial.isSolid -} diff --git a/src/main/scala/codechicken/microblock/FaceMicroblockTraits.scala b/src/main/scala/codechicken/microblock/FaceMicroblockTraits.scala new file mode 100644 index 0000000..6ef79e5 --- /dev/null +++ b/src/main/scala/codechicken/microblock/FaceMicroblockTraits.scala @@ -0,0 +1,44 @@ +package codechicken.microblock + +import codechicken.lib.vec.{Cuboid6, Vector3} +import codechicken.multipart.TFacePart + +trait FaceMicroblockClient extends CommonMicroblockClient { + override def render(pos: Vector3, pass: Int) { + if (pass < 0) + MicroblockRender.renderCuboid(pos, getIMaterial, pass, getBounds, 0) + else if (isTransparent) + MicroblockRender.renderCuboid( + pos, + getIMaterial, + pass, + renderBounds, + renderMask + ) + else { + val mat = getIMaterial + MicroblockRender.renderCuboid( + pos, + mat, + pass, + renderBounds, + renderMask | 1 << getSlot + ) + MicroblockRender.renderCuboid( + pos, + mat, + pass, + Cuboid6.full, + ~(1 << getSlot) + ) + } + } +} + +trait FaceMicroblock extends CommonMicroblock with TFacePart { + def microClass = FaceMicroClass$.MODULE$ + + def getBounds = FaceMicroClass.aBounds()(shape) + + override def solid(side: Int) = getIMaterial.isSolid +} diff --git a/src/main/scala/codechicken/microblock/FacePlacement$.java b/src/main/scala/codechicken/microblock/FacePlacement$.java new file mode 100644 index 0000000..130d34e --- /dev/null +++ b/src/main/scala/codechicken/microblock/FacePlacement$.java @@ -0,0 +1,33 @@ +package codechicken.microblock; + +public final class FacePlacement$ extends PlacementProperties { + + public static final FacePlacement$ MODULE$ = new FacePlacement$(); + + private FacePlacement$() {} + + @Override + public FaceMicroClass$ microClass() { + return FaceMicroClass$.MODULE$; + } + + @Override + public FacePlacementGrid$ placementGrid() { + return FacePlacementGrid$.MODULE$; + } + + @Override + public int opposite(int slot, int side) { + return slot ^ 1; + } + + @Override + public boolean expand(int slot, int side) { + return sneakOpposite(slot, side); + } + + @Override + public boolean sneakOpposite(int slot, int side) { + return slot == (side ^ 1); + } +} diff --git a/src/main/scala/codechicken/microblock/FacePlacement.java b/src/main/scala/codechicken/microblock/FacePlacement.java new file mode 100644 index 0000000..e5e3cfd --- /dev/null +++ b/src/main/scala/codechicken/microblock/FacePlacement.java @@ -0,0 +1,30 @@ +package codechicken.microblock; + +public final class FacePlacement { + + private FacePlacement() {} + + public static ExecutablePlacement customPlacement(MicroblockPlacement placement) { + return FacePlacement$.MODULE$.customPlacement(placement); + } + + public static FaceMicroClass$ microClass() { + return FacePlacement$.MODULE$.microClass(); + } + + public static FacePlacementGrid$ placementGrid() { + return FacePlacement$.MODULE$.placementGrid(); + } + + public static int opposite(int slot, int side) { + return FacePlacement$.MODULE$.opposite(slot, side); + } + + public static boolean expand(int slot, int side) { + return FacePlacement$.MODULE$.expand(slot, side); + } + + public static boolean sneakOpposite(int slot, int side) { + return FacePlacement$.MODULE$.sneakOpposite(slot, side); + } +} diff --git a/src/main/scala/codechicken/microblock/HollowMicroblock.scala b/src/main/scala/codechicken/microblock/HollowMicroblock.scala index 7308a49..6ce9c45 100644 --- a/src/main/scala/codechicken/microblock/HollowMicroblock.scala +++ b/src/main/scala/codechicken/microblock/HollowMicroblock.scala @@ -341,7 +341,7 @@ trait HollowMicroblock with TNormalOcclusion { def microClass = HollowMicroClass - def getBounds: Cuboid6 = FaceMicroClass.aBounds(shape) + def getBounds: Cuboid6 = FaceMicroClass.aBounds()(shape) // TNormalOcclusion is a Java interface, so its box test must be applied here rather than by the super chain. override def occlusionTest(npart: TMultiPart): Boolean =