From 2c610bdb48f0092ff5828a5327d4e3e303918976 Mon Sep 17 00:00:00 2001 From: Algent Date: Fri, 14 Aug 2026 18:02:03 +0200 Subject: [PATCH] refactor: port IDWriter to Java Keep Scala function descriptors as deprecated bridges while adding primitive Java entry points. --- JAVA_MIGRATION.md | 5 +- JAVA_MIGRATION_DIVERGENCES.md | 27 +++++ .../java/codechicken/multipart/IDWriter.java | 98 +++++++++++++++++++ .../codechicken/multipart/IDWriter.scala | 26 ----- 4 files changed, 129 insertions(+), 27 deletions(-) create mode 100644 JAVA_MIGRATION_DIVERGENCES.md create mode 100644 src/main/java/codechicken/multipart/IDWriter.java delete mode 100644 src/main/scala/codechicken/multipart/IDWriter.scala diff --git a/JAVA_MIGRATION.md b/JAVA_MIGRATION.md index 9da9ef8..7e5b0bb 100644 --- a/JAVA_MIGRATION.md +++ b/JAVA_MIGRATION.md @@ -218,7 +218,7 @@ If a characterization test captures a confirmed bug that the port intentionally - [x] Write the first characterization suite against the untouched Scala implementation. - [ ] Create a remaining manual compatibility checklist for rendering, input, and other behavior that cannot be asserted reliably by the automated harness. - [ ] Capture representative CPU and allocation profiles before optimization work. -- [ ] Start a divergence log; it should initially be empty. +- [x] Start a divergence log. Exit condition: there is a reproducible behavior and ABI baseline, the test layers run in CI or an equivalent repeatable command, and the initial suite passes against the Scala implementation. @@ -421,3 +421,6 @@ These ranges were formed before designing the characterization suite and should - Confirmed that normal occlusion returns `true` when parts may coexist, despite the source comment saying “true if the test fails.” Face contact and overlaps no larger than CodeChickenLib's `1e-5` intersection tolerance are accepted; larger overlaps reject placement. Preserve the behavior and correct the misleading documentation separately when this area is ported. - Added nine plain-JVM characterization cases for `PartialOcclusionTest`: its fixed 8³ grid and x-major indexing, part-ID encoding, half-voxel coordinate rounding, required visibility, complete-occlusion exemption, cross-part overlap, same-part box overlap, unfilled entries, and the public `JPartialOcclusion` overload. - Confirmed that every non-exempt part needs at least one exclusively owned voxel. Any second write to an occupied voxel permanently changes it to `-1`, including overlap between two boxes supplied by the same part; this can make that part fail the test. Preserve this exact dynamic-tile behavior during translation unless it is changed later as an explicitly documented bug fix. +- Completed the first behavior-preserving production conversion by replacing `IDWriter.scala` with `IDWriter.java`. All eight existing encoding cases, all 25 plain-JVM tests, the clean build, and both Java 8 Forge server checks pass unchanged. +- Preserved the four legacy Scala function accessor descriptors as deprecated binary bridges and added direct Java `write(MCDataOutput, int)` and `read(MCDataInput)` methods. Recompiled Scala registry callers use the new primitive methods because Scala property auto-application does not apply to accessors declared in Java. +- Removed the six `IDWriter$$anonfun$setMax$*` compiler artifacts from the packaged jar. Their disappearance and replacement with Java anonymous helper classes is recorded in `JAVA_MIGRATION_DIVERGENCES.md`; the supported `IDWriter` descriptors remain link-compatible. diff --git a/JAVA_MIGRATION_DIVERGENCES.md b/JAVA_MIGRATION_DIVERGENCES.md new file mode 100644 index 0000000..779a908 --- /dev/null +++ b/JAVA_MIGRATION_DIVERGENCES.md @@ -0,0 +1,27 @@ +# Java migration divergence log + +This log records intentional differences from the `1.7.12` reference implementation. Behavioral changes must be isolated from behavior-preserving conversions and justified here. + +## 2026-08-14 — IDWriter Java pilot + +### Observable behavior + +No known behavior divergence. Byte, unsigned-short, and integer carrier selection and encoding remain identical, including the reference behavior that negative maximum values select the byte carrier. + +### Supported JVM API + +- Preserved `write()`, `write_$eq(Function2)`, `read()`, `read_$eq(Function1)`, `setMax(int)`, and the public constructor with their reference JVM descriptors. +- Marked the four Scala function property methods deprecated. +- Added `write(MCDataOutput, int)` and `read(MCDataInput)` as the Java-native API. Recompiled Scala callers also resolve to these methods. + +### Compiler artifacts + +Accepted divergence: the six public-but-generated `IDWriter$$anonfun$setMax$1` through `$6` classes are removed and replaced by private Java anonymous helper classes. They represented closure implementation details, were not named by source callers, and are not retained as supported API. + +### Validation + +- Existing `IDWriterCharacterizationTest`: 8 tests, 0 failures, 0 errors. +- Complete plain-JVM suite: 25 tests, 0 failures, 0 errors. +- Clean `checkstyleTest build`: passing. +- Java 8 Forge dedicated-server suite: 2 tests, 0 failures, 0 errors. +- `IDWriter` class-file version: 52 (Java 8). diff --git a/src/main/java/codechicken/multipart/IDWriter.java b/src/main/java/codechicken/multipart/IDWriter.java new file mode 100644 index 0000000..a50d230 --- /dev/null +++ b/src/main/java/codechicken/multipart/IDWriter.java @@ -0,0 +1,98 @@ +package codechicken.multipart; + +import codechicken.lib.data.MCDataInput; +import codechicken.lib.data.MCDataOutput; +import scala.Function1; +import scala.Function2; +import scala.runtime.AbstractFunction1; +import scala.runtime.AbstractFunction2; +import scala.runtime.BoxedUnit; + +/** Class for reading and writing IDs, widening the carrier data type as necessary. */ +public class IDWriter { + + private Function2 write; + private Function1 read; + + @Deprecated + public Function2 write() { + return write; + } + + @Deprecated + public void write_$eq(Function2 write) { + this.write = write; + } + + @Deprecated + public Function1 read() { + return read; + } + + @Deprecated + public void read_$eq(Function1 read) { + this.read = read; + } + + public void write(MCDataOutput data, int id) { + write.apply(data, id); + } + + public int read(MCDataInput data) { + return (Integer) read.apply(data); + } + + public void setMax(int value) { + // The reference Scala implementation widens negative values as signed values. + long maximum = value; + if (maximum > 0xffffL) { + write_$eq(new AbstractFunction2() { + + @Override + public BoxedUnit apply(MCDataOutput data, Object id) { + data.writeInt((Integer) id); + return BoxedUnit.UNIT; + } + }); + read_$eq(new AbstractFunction1() { + + @Override + public Object apply(MCDataInput data) { + return data.readInt(); + } + }); + } else if (maximum > 0xffL) { + write_$eq(new AbstractFunction2() { + + @Override + public BoxedUnit apply(MCDataOutput data, Object id) { + data.writeShort((Integer) id); + return BoxedUnit.UNIT; + } + }); + read_$eq(new AbstractFunction1() { + + @Override + public Object apply(MCDataInput data) { + return data.readUShort(); + } + }); + } else { + write_$eq(new AbstractFunction2() { + + @Override + public BoxedUnit apply(MCDataOutput data, Object id) { + data.writeByte((Integer) id); + return BoxedUnit.UNIT; + } + }); + read_$eq(new AbstractFunction1() { + + @Override + public Object apply(MCDataInput data) { + return (int) data.readUByte(); + } + }); + } + } +} diff --git a/src/main/scala/codechicken/multipart/IDWriter.scala b/src/main/scala/codechicken/multipart/IDWriter.scala deleted file mode 100644 index a420aea..0000000 --- a/src/main/scala/codechicken/multipart/IDWriter.scala +++ /dev/null @@ -1,26 +0,0 @@ -package codechicken.multipart - -import codechicken.lib.data.MCDataOutput -import codechicken.lib.data.MCDataInput - -/** Class for reading and writing ids, widening the carrier data type as - * necessary - */ -class IDWriter { - var write: (MCDataOutput, Int) => Unit = _ - var read: (MCDataInput) => Int = _ - - def setMax(i: Int) { - val l = i.toLong & 0xffffffff - if (l > 0xffff) { - write = (data, i) => data.writeInt(i) - read = (data) => data.readInt() - } else if (l > 0xff) { - write = (data, i) => data.writeShort(i) - read = (data) => data.readUShort() - } else { - write = (data, i) => data.writeByte(i) - read = (data) => data.readUByte() - } - } -}