From 7172af45731b2c96718adb5e9144cb9c883d02bb Mon Sep 17 00:00:00 2001 From: Algent Date: Tue, 1 Sep 2026 19:34:51 +0200 Subject: [PATCH] refactor: port microblock classes to Java --- JAVA_MIGRATION.md | 10 +++ JAVA_MIGRATION_DIVERGENCES.md | 45 ++++++++++++ JAVA_MIGRATION_HANDOFF.md | 21 +++--- JAVA_MIGRATION_MANUAL_CHECKS.md | 4 +- .../microblock/CommonMicroClass$.java | 30 ++++++++ .../microblock/CommonMicroClass.java | 32 +++++++++ .../microblock/MicroblockClass.java | 68 +++++++++++++++++++ .../microblock/MicroblockClass.scala | 63 ----------------- 8 files changed, 200 insertions(+), 73 deletions(-) create mode 100644 src/main/scala/codechicken/microblock/CommonMicroClass$.java create mode 100644 src/main/scala/codechicken/microblock/CommonMicroClass.java create mode 100644 src/main/scala/codechicken/microblock/MicroblockClass.java delete mode 100644 src/main/scala/codechicken/microblock/MicroblockClass.scala diff --git a/JAVA_MIGRATION.md b/JAVA_MIGRATION.md index 40afec1..f2b3f84 100644 --- a/JAVA_MIGRATION.md +++ b/JAVA_MIGRATION.md @@ -1135,3 +1135,13 @@ generated-trait compatibility work. Scala-runtime removal remains a later projec anonymous/closure classes. - A clean build passes all 238 plain-JVM tests and the Java 8 Forge server passes all 103 tests. `microblock/MicroblockClass.scala` is next. +- Characterized `MicroblockClass`, `CommonMicroClass` and its companion against untouched Scala. Three plain-JVM + cases freeze the exact hierarchy, constructors, public descriptors, private fields, side annotations, registry + semantics and generator call descriptors. Constructor execution remains a Forge-only boundary because generator + initialization requires the launch class loader. +- Ported all three retained types directly to Java. Eager base-trait registration, synchronized one-time client-trait + registration, part-factory registration order, class IDs, duplicate rejection and both create paths are unchanged. + The GuideNH-pinned `MicroblockGenerator$.create(MicroblockClass, int, boolean)` descriptor remains exact. +- A clean build passes all 241 plain-JVM tests and the Java 8 Forge server passes all 103 tests. The clean reference and + port jars contain the same three runtime types and every callable public descriptor matches. `microblock/Microblock.scala` + is the next deliberately high-risk target. diff --git a/JAVA_MIGRATION_DIVERGENCES.md b/JAVA_MIGRATION_DIVERGENCES.md index 36fc1ff..406a04a 100644 --- a/JAVA_MIGRATION_DIVERGENCES.md +++ b/JAVA_MIGRATION_DIVERGENCES.md @@ -2657,3 +2657,48 @@ references to them. - Clean reference/port jars retain the same two supported types, and raw `javap` comparison confirms identical callable public names/descriptors and retained field shape. - Actual item, material-face, placement-grid and highlight OpenGL output remains on the manual checklist. + +## 2026-09-01 — MicroblockClass Java port + +The abstract microblock factory, common factory base and common-factory registry singleton are now Java. + +### Observable behavior + +No known runtime divergence. Construction still registers the common trait eagerly. The client trait ID remains a +synchronized, one-time lazy value backed by the same volatile bitmap shape, so a dedicated server does not initialize +the client trait. `register(int)` still publishes the multipart factory before assigning and publishing the common +class ID. Packet creation still passes `client = true`; NBT creation still passes `client = false`. + +The common-class registry remains a fixed 256-entry array indexed by the signed high byte of the material ID. A +duplicate registration still throws with the same message and leaves the first entry installed. + +### ABI and reflection compatibility + +The retained runtime types are exactly `MicroblockClass`, `CommonMicroClass`, and `CommonMicroClass$`. Their hierarchy, +constructors, callable public names/descriptors, private fields and `@SideOnly` annotations match the Scala reference. +The compiler-generated covariant `createPart` bridges retain the `TMultiPart` descriptors expected by +`IPartFactory2`. + +This identity is load-bearing: Schematica calls `MicroblockClass.create(boolean, int)` reflectively, while GuideNH +matches `MicroblockGenerator$.create(MicroblockClass, int, boolean)` by exact parameter type. Both descriptors are +unchanged. The `CommonMicroClass` static facade and `CommonMicroClass$.MODULE$` companion also remain available to +downstream Scala and Java consumers. + +Accepted classfile-only differences are removed Scala signature/marker attributes, removal of the private Scala +`classId` accessor methods, and the companion class initializer without Scala's `ACC_PUBLIC` flag. The exact private +`clientTraitId$lzycompute` method, `clientTraitId` field and `bitmap$0` field are deliberately retained. + +### Compiler artifacts + +No runtime class is added or removed; both clean jars contain the same three supported types. + +### Validation + +- `MicroblockClassCharacterizationTest`: 3 plain-JVM tests, unchanged from the Scala baseline. +- Clean complete plain-JVM suite: 241 tests, 0 failures, 0 errors. +- Java 8 Forge dedicated-server suite: 103 tests, 0 failures, 0 errors, including all five built-in factories and + runtime-generated microblock classes. +- Clean reference/port jars retain the same three types, and raw `javap` comparison confirms identical callable public + names/descriptors and retained field/annotation shapes. +- The real constructor cannot be initialized under the plain-JVM application class loader; exact generator calls are + therefore frozen at the bytecode boundary and exercised end to end by the Forge suite. diff --git a/JAVA_MIGRATION_HANDOFF.md b/JAVA_MIGRATION_HANDOFF.md index 9376c40..fb6f2ef 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`. 132 commits including the API-cleanup plan and separate characterization and -port commits through `MicroblockRender`. +Branch: `algent/java`. Base: `master`. 134 commits including the API-cleanup plan and separate characterization and +port commits through `MicroblockClass`. ## The one rule that matters @@ -67,7 +67,7 @@ awk -F'"' '/> 8]; + } + + public void registerMicroClass(CommonMicroClass microClass, int id) { + if (classes[id] != null) { + throw new IllegalArgumentException( + "Microblock class id " + id + + " is already taken by " + + classes[id].getName() + + " when adding " + + microClass.getName()); + } + classes[id] = microClass; + } +} diff --git a/src/main/scala/codechicken/microblock/CommonMicroClass.java b/src/main/scala/codechicken/microblock/CommonMicroClass.java new file mode 100644 index 0000000..9b1c4ca --- /dev/null +++ b/src/main/scala/codechicken/microblock/CommonMicroClass.java @@ -0,0 +1,32 @@ +package codechicken.microblock; + +public abstract class CommonMicroClass extends MicroblockClass { + + private int classId; + + public static CommonMicroClass[] classes() { + return CommonMicroClass$.MODULE$.classes(); + } + + public static CommonMicroClass getMicroClass(int modelId) { + return CommonMicroClass$.MODULE$.getMicroClass(modelId); + } + + public static void registerMicroClass(CommonMicroClass microClass, int id) { + CommonMicroClass$.MODULE$.registerMicroClass(microClass, id); + } + + public int getClassId() { + return classId; + } + + public abstract int itemSlot(); + + public abstract PlacementProperties placementProperties(); + + public void register(int id) { + register(); + classId = id; + CommonMicroClass$.MODULE$.registerMicroClass(this, id); + } +} diff --git a/src/main/scala/codechicken/microblock/MicroblockClass.java b/src/main/scala/codechicken/microblock/MicroblockClass.java new file mode 100644 index 0000000..652a143 --- /dev/null +++ b/src/main/scala/codechicken/microblock/MicroblockClass.java @@ -0,0 +1,68 @@ +package codechicken.microblock; + +import net.minecraft.nbt.NBTTagCompound; + +import codechicken.lib.data.MCDataInput; +import codechicken.multipart.MultiPartRegistry; +import codechicken.multipart.MultiPartRegistry.IPartFactory2; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; + +public abstract class MicroblockClass implements IPartFactory2 { + + private final int baseTraitId; + + @SideOnly(Side.CLIENT) + private int clientTraitId; + + private volatile boolean bitmap$0; + + public MicroblockClass() { + baseTraitId = MicroblockGenerator$.MODULE$.registerTrait(baseTrait()); + } + + public abstract String getName(); + + public abstract Class baseTrait(); + + @SideOnly(Side.CLIENT) + public abstract Class clientTrait(); + + public abstract float getResistanceFactor(); + + public int baseTraitId() { + return baseTraitId; + } + + public int clientTraitId() { + return bitmap$0 ? clientTraitId : clientTraitId$lzycompute(); + } + + private int clientTraitId$lzycompute() { + synchronized (this) { + if (!bitmap$0) { + clientTraitId = MicroblockGenerator$.MODULE$.registerTrait(clientTrait()); + bitmap$0 = true; + } + return clientTraitId; + } + } + + public void register() { + MultiPartRegistry.registerParts(this, getName()); + } + + public Microblock create(boolean client, int material) { + return MicroblockGenerator$.MODULE$.create(this, material, client); + } + + @Override + public Microblock createPart(String name, MCDataInput packet) { + return create(true, MicroMaterialRegistry.readMaterialID(packet)); + } + + @Override + public Microblock createPart(String name, NBTTagCompound nbt) { + return create(false, MicroMaterialRegistry.materialID(nbt.getString("material"))); + } +} diff --git a/src/main/scala/codechicken/microblock/MicroblockClass.scala b/src/main/scala/codechicken/microblock/MicroblockClass.scala deleted file mode 100644 index eebb9a1..0000000 --- a/src/main/scala/codechicken/microblock/MicroblockClass.scala +++ /dev/null @@ -1,63 +0,0 @@ -package codechicken.microblock - -import codechicken.multipart.MultiPartRegistry -import codechicken.multipart.MultiPartRegistry.IPartFactory2 -import net.minecraft.nbt.NBTTagCompound -import codechicken.lib.data.MCDataInput -import cpw.mods.fml.relauncher.{Side, SideOnly} - -abstract class MicroblockClass extends IPartFactory2 { - def getName: String - def baseTrait: Class[_ <: Microblock] - @SideOnly(Side.CLIENT) - def clientTrait: Class[_ <: MicroblockClient] - def getResistanceFactor: Float - - val baseTraitId = MicroblockGenerator.registerTrait(baseTrait) - @SideOnly(Side.CLIENT) - lazy val clientTraitId = MicroblockGenerator.registerTrait(clientTrait) - - def register() { - MultiPartRegistry.registerParts(this, getName) - } - - def create(client: Boolean, material: Int) = - MicroblockGenerator.create(this, material, client) - override def createPart(name: String, packet: MCDataInput) = - create(true, MicroMaterialRegistry.readMaterialID(packet)) - override def createPart(name: String, nbt: NBTTagCompound) = - create(false, MicroMaterialRegistry.materialID(nbt.getString("material"))) -} - -/** Microblocks with corresponding items - */ -abstract class CommonMicroClass extends MicroblockClass { - private var classId: Int = _ - - def getClassId = classId - def itemSlot: Int - def placementProperties: PlacementProperties - - def register(id: Int) { - register() - classId = id - CommonMicroClass.registerMicroClass(this, id) - } -} - -object CommonMicroClass { - val classes = new Array[CommonMicroClass](256) - - def getMicroClass(modelId: Int) = classes(modelId >> 8) - - def registerMicroClass(mcrClass: CommonMicroClass, id: Int) { - if (classes(id) != null) - throw new IllegalArgumentException( - "Microblock class id " + id + " is already taken by " + classes( - id - ).getName + " when adding " + mcrClass.getName - ) - - classes(id) = mcrClass - } -}