From c20b6bf9d9a05ea3399600344c05a6ee2d338ee1 Mon Sep 17 00:00:00 2001 From: Algent Date: Tue, 1 Sep 2026 10:06:34 +0200 Subject: [PATCH] refactor: port microblock placement to Java --- JAVA_MIGRATION.md | 8 + JAVA_MIGRATION_DIVERGENCES.md | 40 +++ JAVA_MIGRATION_HANDOFF.md | 27 +- .../microblock/AdditionPlacement.java | 25 ++ .../microblock/ExecutablePlacement.java | 30 +++ .../microblock/ExpandingPlacement.java | 29 +++ .../microblock/MicroblockPlacement$.java | 16 ++ .../microblock/MicroblockPlacement.java | 238 ++++++++++++++++++ .../microblock/MicroblockPlacement.scala | 182 -------------- .../microblock/MicroblockRender.scala | 2 +- .../microblock/PlacementProperties.java | 22 ++ 11 files changed, 426 insertions(+), 193 deletions(-) create mode 100644 src/main/scala/codechicken/microblock/AdditionPlacement.java create mode 100644 src/main/scala/codechicken/microblock/ExecutablePlacement.java create mode 100644 src/main/scala/codechicken/microblock/ExpandingPlacement.java create mode 100644 src/main/scala/codechicken/microblock/MicroblockPlacement$.java create mode 100644 src/main/scala/codechicken/microblock/MicroblockPlacement.java delete mode 100644 src/main/scala/codechicken/microblock/MicroblockPlacement.scala create mode 100644 src/main/scala/codechicken/microblock/PlacementProperties.java diff --git a/JAVA_MIGRATION.md b/JAVA_MIGRATION.md index 118187c..62ce02c 100644 --- a/JAVA_MIGRATION.md +++ b/JAVA_MIGRATION.md @@ -1061,3 +1061,11 @@ generated-trait compatibility work. Scala-runtime removal remains a later projec - A clean build passes all 208 plain-JVM tests and the Java 8 Forge server passes all 98 tests. The jar retains exactly the four public ItemMicroPart types; three unreferenced Scala iteration closures disappear. `microblock/MicroblockPlacement.scala` is next. +- Characterized `MicroblockPlacement` against untouched Scala. Three plain-JVM cases freeze the exact six-type + hierarchy, constructors, fields, callable descriptors, companion and defaults. Four Forge cases freeze external + placement, internal/opposite-slot selection, in-place expansion, custom-placement precedence and consumption. +- Ported the six retained types directly to Java. The only Scala source caller now names + `MicroblockPlacement$.MODULE$` explicitly; no runtime class was added or removed and every callable public member + matches the reference. +- A clean build passes all 211 plain-JVM tests and the Java 8 Forge server passes all 102 tests. + `microblock/PlacementGrids.scala` is next. diff --git a/JAVA_MIGRATION_DIVERGENCES.md b/JAVA_MIGRATION_DIVERGENCES.md index 420025b..0ef12e1 100644 --- a/JAVA_MIGRATION_DIVERGENCES.md +++ b/JAVA_MIGRATION_DIVERGENCES.md @@ -2359,3 +2359,43 @@ downstream inventory contains no reference to those compiler artifacts. names/descriptors. - Creative listing, localized names, successful placement/consumption/sound and real client rendering remain on the manual checklist. + +## 2026-09-01 — MicroblockPlacement Java port + +The placement decision engine, its executable result hierarchy, property base and companion singleton are now six +Java types. The item and highlight renderer continue to call `MicroblockPlacement$.MODULE$`, preserving the singleton +entry point exposed by the Scala implementation. + +### Observable behavior + +No known runtime divergence. Custom placement still wins before a rejected grid slot. Expansion still requires an +internal, non-converted tile hit without sneaking or the opposite modifier, then checks part type, material, combined +size, entity collision and replacement compatibility. Successful expansion mutates the original part's shape in +place, notifies its tile and sends the shape update. Internal placement retains the same shallow-hit and control-key +slot selection; external placement retains the same side offset. Addition and expansion each consume one item when +their caller requests consumption. + +The Java implementation uses direct conditionals and standard null-safe equality. No focused performance claim is +made because placement is an interactive path rather than a sustained server loop. + +### ABI and source compatibility + +The retained runtime types are exactly `ExecutablePlacement`, `AdditionPlacement`, `ExpandingPlacement`, +`PlacementProperties`, `MicroblockPlacement`, and `MicroblockPlacement$`. Their hierarchy, constructors, stored-field +names/types/modifiers, `MODULE$`, and every callable public name and descriptor match the Scala reference. No anonymous +or closure classes existed in the reference unit, so the emitted class list is unchanged. + +The remaining Scala highlight renderer now spells the companion call as `MicroblockPlacement$.MODULE$.apply(...)`; +Scala's object-application sugar does not recognize a Java-authored companion. Accepted classfile-only differences are +removed Scala signature/marker attributes, a private companion constructor and the companion class initializer without +Scala's `ACC_PUBLIC` flag. + +### Validation + +- `MicroblockPlacementCharacterizationTest`: 3 plain-JVM tests, unchanged from the Scala baseline. +- `MicroblockPlacementFunctionalTest`: 4 Forge tests, unchanged from the Scala baseline. +- Clean complete plain-JVM suite: 211 tests, 0 failures, 0 errors. +- Java 8 Forge dedicated-server suite: 102 tests, 0 failures, 0 errors. +- Clean reference/port jars contain the same six runtime classes, and raw `javap` comparison confirms identical + callable public names/descriptors. +- Full item-use sound plus client highlight/control-key feedback remain on the manual checklist. diff --git a/JAVA_MIGRATION_HANDOFF.md b/JAVA_MIGRATION_HANDOFF.md index 4a3ce6d..0a1118a 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`. 115 commits including the API-cleanup plan and separate characterization and -port commits through `ItemMicroPart`. +Branch: `algent/java`. Base: `master`. 118 commits including the API-cleanup plan and separate characterization and +port commits through `MicroblockPlacement`. ## The one rule that matters @@ -67,7 +67,7 @@ awk -F'"' '/ gtile; + private final TileMultipart htile; + private final int slot; + private final int oslot; + private final double d; + private final boolean useOppMod; + private final boolean oppMod; + private final boolean internal; + private final boolean doExpand; + private final int side; + + public MicroblockPlacement(EntityPlayer player, MovingObjectPosition hit, int size, int material, + boolean checkMaterial, PlacementProperties pp) { + this.player = player; + this.hit = hit; + this.size = size; + this.material = material; + this.checkMaterial = checkMaterial; + this.pp = pp; + world = player.worldObj; + mcrClass = pp.microClass(); + pos = new BlockCoord(hit.blockX, hit.blockY, hit.blockZ); + vhit = new Vector3(hit.hitVec).add(-pos.x, -pos.y, -pos.z); + gtile = TileMultipart.getOrConvertTile2(world, pos); + htile = gtile._1(); + slot = pp.placementGrid().getHitSlot(vhit, hit.sideHit); + oslot = pp.opposite(slot, hit.sideHit); + d = getHitDepth(vhit, hit.sideHit); + useOppMod = pp.sneakOpposite(slot, hit.sideHit); + oppMod = ControlKeyModifer.isControlDown(player); + internal = d < 1 && htile != null; + doExpand = internal && !(Boolean) gtile._2() + && !player.isSneaking() + && !(oppMod && useOppMod) + && pp.expand(slot, hit.sideHit); + side = hit.sideHit; + } + + public EntityPlayer player() { + return player; + } + + public MovingObjectPosition hit() { + return hit; + } + + public int size() { + return size; + } + + public int material() { + return material; + } + + public boolean checkMaterial() { + return checkMaterial; + } + + public PlacementProperties pp() { + return pp; + } + + public World world() { + return world; + } + + public MicroblockClass mcrClass() { + return mcrClass; + } + + public BlockCoord pos() { + return pos; + } + + public Vector3 vhit() { + return vhit; + } + + public Tuple2 gtile() { + return gtile; + } + + public TileMultipart htile() { + return htile; + } + + public int slot() { + return slot; + } + + public int oslot() { + return oslot; + } + + public double d() { + return d; + } + + public boolean useOppMod() { + return useOppMod; + } + + public boolean oppMod() { + return oppMod; + } + + public boolean internal() { + return internal; + } + + public boolean doExpand() { + return doExpand; + } + + public int side() { + return side; + } + + public ExecutablePlacement apply() { + ExecutablePlacement customPlacement = pp.customPlacement(this); + if (customPlacement != null) { + return customPlacement; + } + if (slot < 0) { + return null; + } + + if (doExpand) { + Tuple2 data = ExtendedMOP.getData(hit); + TMultiPart hitPart = htile.partList().apply((Integer) data._1()); + if (Objects.equals(hitPart.getType(), mcrClass.getName())) { + CommonMicroblock commonPart = (CommonMicroblock) hitPart; + Microblock microPart = (Microblock) hitPart; + if (microPart.material() == material && microPart.getSize() + size < 8) { + return expand(commonPart); + } + } + } + + if (internal) { + if (d < 0.5 || !useOppMod) { + ExecutablePlacement placement = internalPlacement(htile, slot); + if (placement != null) { + if (!useOppMod || !oppMod) { + return placement; + } + return internalPlacement(htile, oslot); + } + } + if (useOppMod && !oppMod) { + return internalPlacement(htile, oslot); + } + return externalPlacement(slot); + } + + if (!useOppMod || !oppMod) { + return externalPlacement(slot); + } + return externalPlacement(oslot); + } + + public ExecutablePlacement expand(CommonMicroblock part) { + Microblock microPart = (Microblock) part; + return expand(microPart, create(microPart.getSize() + size, part.getSlot(), microPart.material())); + } + + public ExecutablePlacement expand(Microblock part, Microblock newPart) { + BlockCoord partPos = new BlockCoord(part.tile()); + if (TileMultipart.checkNoEntityCollision(world, partPos, newPart) + && part.tile().canReplacePart(part, newPart)) { + return new ExpandingPlacement(partPos, newPart, part); + } + return null; + } + + public ExecutablePlacement internalPlacement(TileMultipart tile, int targetSlot) { + return internalPlacement(tile, create(size, targetSlot, material)); + } + + public ExecutablePlacement internalPlacement(TileMultipart tile, Microblock newPart) { + BlockCoord tilePos = new BlockCoord(tile); + if (TileMultipart.checkNoEntityCollision(world, tilePos, newPart) && tile.canAddPart(newPart)) { + return new AdditionPlacement(tilePos, newPart); + } + return null; + } + + public ExecutablePlacement externalPlacement(int targetSlot) { + return externalPlacement(create(size, targetSlot, material)); + } + + public ExecutablePlacement externalPlacement(Microblock newPart) { + BlockCoord targetPos = pos.copy().offset(side); + if (TileMultipart.canPlacePart(world, targetPos, newPart)) { + return new AdditionPlacement(targetPos, newPart); + } + return null; + } + + public double getHitDepth(Vector3 localHit, int hitSide) { + return localHit.copy().scalarProject(Rotation.axes[hitSide]) + (hitSide % 2 ^ 1); + } + + public Microblock create(int partSize, int partSlot, int partMaterial) { + Microblock part = mcrClass.create(world.isRemote, partMaterial); + part.setShape(partSize, partSlot); + return part; + } +} diff --git a/src/main/scala/codechicken/microblock/MicroblockPlacement.scala b/src/main/scala/codechicken/microblock/MicroblockPlacement.scala deleted file mode 100644 index ea68a7b..0000000 --- a/src/main/scala/codechicken/microblock/MicroblockPlacement.scala +++ /dev/null @@ -1,182 +0,0 @@ -package codechicken.microblock - -import net.minecraft.world.World -import net.minecraft.entity.player.EntityPlayer -import net.minecraft.util.MovingObjectPosition -import codechicken.lib.vec.Vector3 -import codechicken.lib.vec.BlockCoord -import codechicken.lib.vec.Rotation -import codechicken.multipart.ControlKeyModifer -import net.minecraft.item.ItemStack -import codechicken.multipart.TileMultipart -import codechicken.lib.raytracer.ExtendedMOP - -abstract class ExecutablePlacement(val pos: BlockCoord, val part: Microblock) { - def place(world: World, player: EntityPlayer, item: ItemStack) - def consume(world: World, player: EntityPlayer, item: ItemStack) -} - -class AdditionPlacement($pos: BlockCoord, $part: Microblock) - extends ExecutablePlacement($pos, $part) { - def place(world: World, player: EntityPlayer, item: ItemStack) { - TileMultipart.addPart(world, pos, part) - } - - def consume(world: World, player: EntityPlayer, item: ItemStack) { - item.stackSize -= 1 - } -} - -class ExpandingPlacement($pos: BlockCoord, $part: Microblock, opart: Microblock) - extends ExecutablePlacement($pos, $part) { - def place(world: World, player: EntityPlayer, item: ItemStack) { - opart.shape = part.shape - opart.tile.notifyPartChange(opart) - opart.sendShapeUpdate() - } - - def consume(world: World, player: EntityPlayer, item: ItemStack) { - item.stackSize -= 1 - } -} - -abstract class PlacementProperties { - def opposite(slot: Int, side: Int): Int - - def sneakOpposite(slot: Int, side: Int) = true - - def expand(slot: Int, side: Int) = true - - def microClass: MicroblockClass - - def placementGrid: PlacementGrid - - def customPlacement(pmt: MicroblockPlacement): ExecutablePlacement = null -} - -object MicroblockPlacement { - def apply( - player: EntityPlayer, - hit: MovingObjectPosition, - size: Int, - material: Int, - checkMaterial: Boolean, - pp: PlacementProperties - ): ExecutablePlacement = - new MicroblockPlacement(player, hit, size, material, checkMaterial, pp)() -} - -class MicroblockPlacement( - val player: EntityPlayer, - val hit: MovingObjectPosition, - val size: Int, - val material: Int, - val checkMaterial: Boolean, - val pp: PlacementProperties -) { - val world = player.worldObj - val mcrClass = pp.microClass - val pos = new BlockCoord(hit.blockX, hit.blockY, hit.blockZ) - val vhit = new Vector3(hit.hitVec).add(-pos.x, -pos.y, -pos.z) - val gtile = TileMultipart.getOrConvertTile2(world, pos) - val htile = gtile._1 - val slot = pp.placementGrid.getHitSlot(vhit, hit.sideHit) - val oslot = pp.opposite(slot, hit.sideHit) - - val d = getHitDepth(vhit, hit.sideHit) - val useOppMod = pp.sneakOpposite(slot, hit.sideHit) - val oppMod = ControlKeyModifer.isControlDown(player) - val internal = d < 1 && htile != null - val doExpand = - internal && !gtile._2.asInstanceOf[ - Boolean - ] && !player.isSneaking && !(oppMod && useOppMod) && pp - .expand(slot, hit.sideHit) - val side = hit.sideHit - - def apply(): ExecutablePlacement = { - val customPlacement = pp.customPlacement(this) - if (customPlacement != null) - return customPlacement - - if (slot < 0) - return null - - if (doExpand) { - val hpart = htile.partList.apply(ExtendedMOP.getData[(Int, _)](hit)._1) - if (hpart.getType == mcrClass.getName) { - val mpart = hpart.asInstanceOf[CommonMicroblock] - if (mpart.material == material && mpart.getSize + size < 8) - return expand(mpart) - } - } - - if (internal) { - if (d < 0.5 || !useOppMod) { - val ret = internalPlacement(htile, slot) - if (ret != null) { - if (!useOppMod || !oppMod) return ret - else return internalPlacement(htile, oslot) - } - } - if (useOppMod && !oppMod) - return internalPlacement(htile, oslot) - else - return externalPlacement(slot) - } - - if (!useOppMod || !oppMod) - return externalPlacement(slot) - else - return externalPlacement(oslot) - } - - def expand(mpart: CommonMicroblock): ExecutablePlacement = - expand(mpart, create(mpart.getSize + size, mpart.getSlot, mpart.material)) - - def expand(mpart: Microblock, npart: Microblock): ExecutablePlacement = { - val pos = new BlockCoord(mpart.tile) - if ( - TileMultipart.checkNoEntityCollision(world, pos, npart) && mpart.tile - .canReplacePart(mpart, npart) - ) - return new ExpandingPlacement(pos, npart, mpart) - return null - } - - def internalPlacement(htile: TileMultipart, slot: Int): ExecutablePlacement = - internalPlacement(htile, create(size, slot, material)) - - def internalPlacement( - htile: TileMultipart, - npart: Microblock - ): ExecutablePlacement = { - val pos = new BlockCoord(htile) - if ( - TileMultipart.checkNoEntityCollision(world, pos, npart) && htile - .canAddPart(npart) - ) - return new AdditionPlacement(pos, npart) - return null - } - - def externalPlacement(slot: Int): ExecutablePlacement = externalPlacement( - create(size, slot, material) - ) - - def externalPlacement(npart: Microblock): ExecutablePlacement = { - val pos = this.pos.copy().offset(side) - if (TileMultipart.canPlacePart(world, pos, npart)) - return new AdditionPlacement(pos, npart) - return null - } - - def getHitDepth(vhit: Vector3, side: Int): Double = - vhit.copy.scalarProject(Rotation.axes(side)) + (side % 2 ^ 1) - - def create(size: Int, slot: Int, material: Int) = { - val part = mcrClass.create(world.isRemote, material) - part.setShape(size, slot) - part - } -} diff --git a/src/main/scala/codechicken/microblock/MicroblockRender.scala b/src/main/scala/codechicken/microblock/MicroblockRender.scala index 14899aa..84941fe 100644 --- a/src/main/scala/codechicken/microblock/MicroblockRender.scala +++ b/src/main/scala/codechicken/microblock/MicroblockRender.scala @@ -29,7 +29,7 @@ object MicroblockRender { mcrClass.placementProperties.placementGrid .render(new Vector3(hit.hitVec), hit.sideHit) - val placement = MicroblockPlacement( + val placement = MicroblockPlacement$.MODULE$.apply( player, hit, size, diff --git a/src/main/scala/codechicken/microblock/PlacementProperties.java b/src/main/scala/codechicken/microblock/PlacementProperties.java new file mode 100644 index 0000000..236aaaf --- /dev/null +++ b/src/main/scala/codechicken/microblock/PlacementProperties.java @@ -0,0 +1,22 @@ +package codechicken.microblock; + +public abstract class PlacementProperties { + + public abstract int opposite(int slot, int side); + + public boolean sneakOpposite(int slot, int side) { + return true; + } + + public boolean expand(int slot, int side) { + return true; + } + + public abstract MicroblockClass microClass(); + + public abstract PlacementGrid placementGrid(); + + public ExecutablePlacement customPlacement(MicroblockPlacement placement) { + return null; + } +}