From fa675676ae62d3c79f54c737017f2a71bcfdfc1f Mon Sep 17 00:00:00 2001 From: Algent Date: Tue, 1 Sep 2026 19:54:41 +0200 Subject: [PATCH] refactor: port microblock base to Java --- JAVA_MIGRATION.md | 10 + JAVA_MIGRATION_DIVERGENCES.md | 47 ++++ JAVA_MIGRATION_HANDOFF.md | 25 ++- JAVA_MIGRATION_MANUAL_CHECKS.md | 8 +- .../microblock/CornerMicroblock.scala | 3 +- .../microblock/EdgeMicroblock.scala | 7 +- .../codechicken/microblock/Microblock$.java | 12 ++ .../codechicken/microblock/Microblock.java | 181 ++++++++++++++++ .../codechicken/microblock/Microblock.scala | 201 ------------------ .../microblock/MicroblockTraits.scala | 76 +++++++ 10 files changed, 353 insertions(+), 217 deletions(-) create mode 100644 src/main/scala/codechicken/microblock/Microblock$.java create mode 100644 src/main/scala/codechicken/microblock/Microblock.java delete mode 100644 src/main/scala/codechicken/microblock/Microblock.scala create mode 100644 src/main/scala/codechicken/microblock/MicroblockTraits.scala diff --git a/JAVA_MIGRATION.md b/JAVA_MIGRATION.md index f2b3f84..8620f64 100644 --- a/JAVA_MIGRATION.md +++ b/JAVA_MIGRATION.md @@ -1145,3 +1145,13 @@ generated-trait compatibility work. Scala-runtime removal remains a later projec - 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. +- Characterized the `Microblock` base, its default-argument companion and all three mixin traits against untouched + Scala. Three plain-JVM cases freeze the eight retained type surfaces, fields, constructor/default, signed shape + packing, material delegation, item conversion, description/update bytes and core NBT. +- Ported `Microblock` and `Microblock$` directly to Java while moving `MicroblockClient`, `CommonMicroblock` and + `CommonMicroblockClient` unchanged to `MicroblockTraits.scala`. Keeping those load-bearing Scala traits avoids a + Java single-inheritance workaround and preserves the existing ProjectRed generator path. Three remaining Scala + assignments now call the same public field setters explicitly. +- 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. diff --git a/JAVA_MIGRATION_DIVERGENCES.md b/JAVA_MIGRATION_DIVERGENCES.md index 406a04a..aae2c70 100644 --- a/JAVA_MIGRATION_DIVERGENCES.md +++ b/JAVA_MIGRATION_DIVERGENCES.md @@ -2702,3 +2702,50 @@ No runtime class is added or removed; both clean jars contain the same three sup 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. + +## 2026-09-01 — Microblock base Java port + +The shared abstract microblock base and its generated default-argument companion are now Java. The three mixin traits +from the same source unit remain Scala in `MicroblockTraits.scala`; translating them would require either changing +their multiple-inheritance shape or widening the mixin generator, neither of which is needed to port the base. + +### Observable behavior + +No known gameplay divergence. Material and shape state, signed-byte shape packing, cuboid routing, type/strength/light/ +transparency/resistance delegation, drop denominations, pick-item preference, description and update bytes, tile +notification order, and `shape`/`material` NBT remain unchanged. + +`getDrops()` now returns a directly populated `ArrayList` instead of the Java view of a Scala `ListBuffer`. Both are +mutable Java `List` implementations with the same ordered contents; no audited consumer depends on the concrete list +class. Direct `4, 2, 1` loops also replace Scala collection traversal and `pickItem`'s non-local-return exception. + +### ABI and reflection compatibility + +`Microblock` remains a public abstract subclass of `TMultiPart` implementing `TCuboidPart`, with exactly one public +`(int)` constructor, the same private mutable `material: int` and `shape: byte` fields, and every callable public name +and descriptor retained. `Microblock$`, `MODULE$`, the constructor-default method and its static forwarder also remain. +GuideNH's reflected `microClass()`, `material()` and `shape()` members and Schematica's `Microblock` type identity are +therefore unchanged. + +`MicroblockClient`, `CommonMicroblock`, `CommonMicroblockClient` and their three `$class` helpers remain Scala and +retain their exact interface hierarchy and public descriptors. The three remaining Scala source assignments in the +corner/edge units now name `shape_$eq(byte)` explicitly because Java classfiles do not carry Scala source-property +metadata; the emitted call and runtime behavior are the same. + +Accepted classfile-only differences are removed Scala signature/marker attributes from `Microblock`, Java's ordinary +companion class-initializer flag, and the traits' updated source-file marker. + +### Compiler artifacts + +`Microblock$$anonfun$getDrops$1` and `Microblock$$anonfun$pickItem$1` disappear. The direct Java loops need neither +private Scala closure; the downstream inventory contains no reference to them. The other eight supported runtime +types are unchanged. + +### Validation + +- `MicroblockCharacterizationTest`: 3 plain-JVM tests, unchanged from the Scala baseline. +- Clean complete plain-JVM suite: 244 tests, 0 failures, 0 errors. +- Java 8 Forge dedicated-server suite: 103 tests, 0 failures, 0 errors, including generated face/hollow parts and a + 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. diff --git a/JAVA_MIGRATION_HANDOFF.md b/JAVA_MIGRATION_HANDOFF.md index fb6f2ef..4a6d671 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`. 134 commits including the API-cleanup plan and separate characterization and -port commits through `MicroblockClass`. +Branch: `algent/java`. Base: `master`. 136 commits including the API-cleanup plan and separate characterization and +port commits through the `Microblock` base. ## The one rule that matters @@ -67,7 +67,7 @@ awk -F'"' '/ getSubParts() { + return JCuboidPart.subParts(this); + } + + @Override + public Iterable getCollisionBoxes() { + return JCuboidPart.collisionBoxes(this); + } + + @Override + public void drawBreaking(RenderBlocks renderBlocks) { + JCuboidPart.renderBreaking(this, renderBlocks); + } + + public abstract MicroblockClass microClass(); + + @Override + public String getType() { + return microClass().getName(); + } + + @Override + public float getStrength(MovingObjectPosition hit, EntityPlayer player) { + MicroMaterialRegistry.IMicroMaterial microMaterial = getIMaterial(); + return microMaterial == null ? super.getStrength(hit, player) : microMaterial.getStrength(player); + } + + @Override + public boolean doesTick() { + return false; + } + + public int getSize() { + return shape >> 4; + } + + public int getShape() { + return shape & 0xF; + } + + public void setShape(int size, int slot) { + shape = (byte) (size << 4 | slot); + } + + public int getMaterial() { + return material; + } + + public MicroMaterialRegistry.IMicroMaterial getIMaterial() { + return MicroMaterialRegistry.getMaterial(material); + } + + public abstract int itemClassID(); + + @Override + public List getDrops() { + int remaining = getSize(); + List items = new ArrayList<>(3); + for (int size = 4; size > 0; size /= 2) { + int amount = remaining / size; + remaining -= amount * size; + if (amount > 0) { + items.add( + ItemMicroPart.create( + amount, + size | itemClassID() << 8, + MicroMaterialRegistry.materialName(material))); + } + } + return items; + } + + @Override + public ItemStack pickItem(MovingObjectPosition hit) { + int size = getSize(); + for (int itemSize = 4; itemSize > 0; itemSize /= 2) { + if (size % itemSize == 0 && size / itemSize >= 1) { + return ItemMicroPart + .create(itemSize | itemClassID() << 8, MicroMaterialRegistry.materialName(material)); + } + } + return null; + } + + @Override + public void writeDesc(MCDataOutput packet) { + MicroMaterialRegistry.writeMaterialID(packet, material); + packet.writeByte(shape); + } + + @Override + public void readDesc(MCDataInput packet) { + shape = packet.readByte(); + } + + public void sendShapeUpdate() { + getWriteStream().writeByte(shape); + } + + @Override + public void read(MCDataInput packet) { + super.read(packet); + tile().notifyPartChange(this); + } + + @Override + public void save(NBTTagCompound tag) { + tag.setByte("shape", shape); + tag.setString("material", MicroMaterialRegistry.materialName(material)); + } + + @Override + public void load(NBTTagCompound tag) { + shape = tag.getByte("shape"); + material = MicroMaterialRegistry.materialID(tag.getString("material")); + } + + public boolean isTransparent() { + return getIMaterial().isTransparent(); + } + + @Override + public int getLightValue() { + return getIMaterial().getLightValue(); + } + + @Override + public float explosionResistance(Entity entity) { + return getIMaterial().explosionResistance(entity) * microClass().getResistanceFactor(); + } +} diff --git a/src/main/scala/codechicken/microblock/Microblock.scala b/src/main/scala/codechicken/microblock/Microblock.scala deleted file mode 100644 index 2067d72..0000000 --- a/src/main/scala/codechicken/microblock/Microblock.scala +++ /dev/null @@ -1,201 +0,0 @@ -package codechicken.microblock - -import net.minecraft.client.renderer.RenderBlocks -import net.minecraft.nbt.NBTTagCompound -import codechicken.lib.lighting.LightMatrix -import codechicken.microblock.MicroMaterialRegistry._ -import codechicken.lib.vec.BlockCoord -import org.lwjgl.opengl.GL11 -import codechicken.lib.render.CCRenderState -import net.minecraft.world.World -import codechicken.lib.vec.Cuboid6 -import codechicken.lib.vec.Vector3 -import codechicken.multipart.TIconHitEffects -import codechicken.multipart.IconHitEffects -import net.minecraft.client.particle.EffectRenderer -import net.minecraft.util.IIcon -import net.minecraft.block.Block -import codechicken.multipart.TCuboidPart -import codechicken.multipart.JCuboidPart -import codechicken.multipart.TMultiPart -import net.minecraft.util.MovingObjectPosition -import net.minecraft.entity.player.EntityPlayer -import codechicken.multipart.JPartialOcclusion -import codechicken.lib.render.Vertex5 -import net.minecraft.item.ItemStack -import scala.collection.mutable.ListBuffer -import codechicken.lib.data.MCDataOutput -import codechicken.lib.data.MCDataInput -import codechicken.lib.render.RenderUtils -import codechicken.lib.render.IFaceRenderer -import codechicken.multipart.TSlottedPart -import scala.collection.JavaConversions._ -import codechicken.lib.render.TextureUtils -import net.minecraft.entity.Entity -import net.minecraft.init.Blocks -import cpw.mods.fml.relauncher.{Side, SideOnly} -import codechicken.lib.render.BlockRenderer.BlockFace - -abstract class Microblock(var material: Int = 0) - extends TMultiPart - with TCuboidPart { - var shape: Byte = 0 - - // TCuboidPart is a Java interface, so these must be declared explicitly or TMultiPart's empty versions win. - override def getSubParts = JCuboidPart.subParts(this) - - override def getCollisionBoxes = JCuboidPart.collisionBoxes(this) - - override def drawBreaking(renderBlocks: RenderBlocks) = - JCuboidPart.renderBreaking(this, renderBlocks) - - def microClass: MicroblockClass - - def getType = microClass.getName - - override def getStrength(hit: MovingObjectPosition, player: EntityPlayer) = - getIMaterial match { - case null => super.getStrength(hit, player) - case mat => mat.getStrength(player) - } - - override def doesTick = false - - def getSize = shape >> 4 - - def getShape = shape & 0xf - - def setShape(size: Int, slot: Int) = shape = (size << 4 | slot).toByte - - def getMaterial = material - - def getIMaterial = MicroMaterialRegistry.getMaterial(material) - - def itemClassID: Int - - override def getDrops = { - var size = getSize - val items = ListBuffer[ItemStack]() - for (s <- Seq(4, 2, 1)) { - val m = size / s - size -= m * s - if (m > 0) - items += ItemMicroPart.create( - m, - s | itemClassID << 8, - MicroMaterialRegistry.materialName(material) - ) - } - items - } - - override def pickItem(hit: MovingObjectPosition): ItemStack = { - val size = getSize - for (s <- Seq(4, 2, 1)) - if (size % s == 0 && size / s >= 1) - return ItemMicroPart.create( - s | itemClassID << 8, - MicroMaterialRegistry.materialName(material) - ) - - return null // unreachable - } - - override def writeDesc(packet: MCDataOutput) { - writeMaterialID(packet, material) - packet.writeByte(shape) - } - - override def readDesc(packet: MCDataInput) { - shape = packet.readByte - } - - def sendShapeUpdate() { - getWriteStream.writeByte(shape) - } - - override def read(packet: MCDataInput) { - super.read(packet) - tile.notifyPartChange(this) - } - - override def save(tag: NBTTagCompound) { - tag.setByte("shape", shape) - tag.setString("material", materialName(material)) - } - - override def load(tag: NBTTagCompound) { - shape = tag.getByte("shape") - material = materialID(tag.getString("material")) - } - - def isTransparent = getIMaterial.isTransparent - - override def getLightValue = getIMaterial.getLightValue - - override def explosionResistance(entity: Entity) = - getIMaterial.explosionResistance(entity) * microClass.getResistanceFactor -} - -trait MicroblockClient - extends Microblock - with TIconHitEffects - with IMicroMaterialRender { - def getBrokenIcon(side: Int) = getIMaterial match { - case null => Blocks.stone.getIcon(0, 0) - case mat => mat.getBreakingIcon(side) - } - - // TIconHitEffects is a Java interface, so these must be declared explicitly or TMultiPart's empty versions win. - override def addHitEffects( - hit: MovingObjectPosition, - effectRenderer: EffectRenderer - ) = IconHitEffects.addHitEffects(this, hit, effectRenderer) - - override def addDestroyEffects(effectRenderer: EffectRenderer) = - IconHitEffects.addDestroyEffects(this, effectRenderer) - - override def renderStatic(pos: Vector3, pass: Int) = { - if (getIMaterial.canRenderInPass(pass)) { - render(pos, pass) - true - } else - false - } - - def render(pos: Vector3, pass: Int) - - override def getRenderBounds = getBounds -} - -trait CommonMicroblockClient - extends CommonMicroblock - with MicroblockClient - with TMicroOcclusionClient { - def render(pos: Vector3, pass: Int) { - if (pass < 0) - MicroblockRender.renderCuboid(pos, getIMaterial, pass, getBounds, 0) - else - MicroblockRender.renderCuboid( - pos, - getIMaterial, - pass, - renderBounds, - renderMask - ) - } -} - -trait CommonMicroblock - extends Microblock - with JPartialOcclusion - with TMicroOcclusion - with TSlottedPart { - def microClass: CommonMicroClass - - def getSlot = getShape - def getSlotMask = 1 << getSlot - def getPartialOcclusionBoxes = Seq(getBounds) - - override def itemClassID = microClass.getClassId -} diff --git a/src/main/scala/codechicken/microblock/MicroblockTraits.scala b/src/main/scala/codechicken/microblock/MicroblockTraits.scala new file mode 100644 index 0000000..a1614af --- /dev/null +++ b/src/main/scala/codechicken/microblock/MicroblockTraits.scala @@ -0,0 +1,76 @@ +package codechicken.microblock + +import codechicken.lib.vec.Vector3 +import codechicken.multipart.{ + IconHitEffects, + JPartialOcclusion, + TIconHitEffects, + TSlottedPart +} +import net.minecraft.client.particle.EffectRenderer +import net.minecraft.init.Blocks +import net.minecraft.util.MovingObjectPosition +import scala.collection.JavaConversions._ + +trait MicroblockClient + extends Microblock + with TIconHitEffects + with IMicroMaterialRender { + def getBrokenIcon(side: Int) = getIMaterial match { + case null => Blocks.stone.getIcon(0, 0) + case mat => mat.getBreakingIcon(side) + } + + // TIconHitEffects is a Java interface, so these must be declared explicitly or TMultiPart's empty versions win. + override def addHitEffects( + hit: MovingObjectPosition, + effectRenderer: EffectRenderer + ) = IconHitEffects.addHitEffects(this, hit, effectRenderer) + + override def addDestroyEffects(effectRenderer: EffectRenderer) = + IconHitEffects.addDestroyEffects(this, effectRenderer) + + override def renderStatic(pos: Vector3, pass: Int) = { + if (getIMaterial.canRenderInPass(pass)) { + render(pos, pass) + true + } else + false + } + + def render(pos: Vector3, pass: Int) + + override def getRenderBounds = getBounds +} + +trait CommonMicroblockClient + extends CommonMicroblock + with MicroblockClient + with TMicroOcclusionClient { + def render(pos: Vector3, pass: Int) { + if (pass < 0) + MicroblockRender.renderCuboid(pos, getIMaterial, pass, getBounds, 0) + else + MicroblockRender.renderCuboid( + pos, + getIMaterial, + pass, + renderBounds, + renderMask + ) + } +} + +trait CommonMicroblock + extends Microblock + with JPartialOcclusion + with TMicroOcclusion + with TSlottedPart { + def microClass: CommonMicroClass + + def getSlot = getShape + def getSlotMask = 1 << getSlot + def getPartialOcclusionBoxes = Seq(getBounds) + + override def itemClassID = microClass.getClassId +}