diff --git a/JAVA_MIGRATION.md b/JAVA_MIGRATION.md index 12575c7..f4ee39e 100644 --- a/JAVA_MIGRATION.md +++ b/JAVA_MIGRATION.md @@ -793,6 +793,14 @@ generated-trait compatibility work. Scala-runtime removal remains a later projec - Ported all five emitted packet-handler classes to Java and changed the two Scala proxy registrations to pass the companion singletons explicitly. Every callable public member and the emitted class list match the reference; the registry channel and wire format are unchanged. -- All 171 plain-JVM tests pass. The Java 8 Forge source set and new handshake case compile, but this checkout's server - run stopped before mod loading because the Minecraft EULA has not been accepted; the last completed Forge baseline - remains 86 tests until that user-controlled prerequisite is satisfied. +- All 171 plain-JVM tests pass. After the local EULA was accepted, all 87 Java 8 Forge dedicated-server tests pass, + including the new registry handshake. +- Characterized `MultipartSaveLoad` against untouched Scala. Three plain-JVM cases freeze the static facade, + load-bearing companion, private fields and exact dummy class shape. Four Forge cases freeze ProjectRed-style binary + linkage, both reflected vanilla maps, converter precedence/deletion and saved multipart reconstruction. +- Ported the singleton to a Java facade/companion pair and kept the dummy as the static facade member + `MultipartSaveLoad.TileNBTContainer`, which emits the literal `MultipartSaveLoad$TileNBTContainer` binary name. + Nesting it under the companion would emit the wrong double dollar. Every callable public member matches the + reference. Only the unreferenced compiler-generated `$$anonfun$1` closure disappeared. +- All 174 plain-JVM tests and all 91 Java 8 Forge dedicated-server tests pass. The next medium-risk target is + `MissingMicroMaterial.scala`; its real icon/render paths remain client-manual work. diff --git a/JAVA_MIGRATION_DIVERGENCES.md b/JAVA_MIGRATION_DIVERGENCES.md index fb497a9..106880d 100644 --- a/JAVA_MIGRATION_DIVERGENCES.md +++ b/JAVA_MIGRATION_DIVERGENCES.md @@ -1999,5 +1999,45 @@ target the same classes, fields, method names and descriptors. - Complete plain-JVM suite: 171 tests, 0 failures, 0 errors. - Raw `javap` comparison confirms identical callable public names/descriptors and interface order for all five types; the emitted class list is unchanged. -- The Java 8 Forge run is pending: `runFunctionalTestServer` stopped before mod loading because this checkout has not - accepted the Minecraft EULA. The last completed server baseline remains 86 passing tests. +- The deferred Java 8 Forge run now passes all 87 tests after the local EULA was accepted. + +## 2026-08-31 — MultipartSaveLoad Java port + +The chunk-load save/restore singleton is now a Java static facade plus a Java companion singleton. ProjectRed reads +`MultipartSaveLoad$.MODULE$` and calls `loadingWorld()`, so both forms remain rather than collapsing the object into a +single utility class. + +### Observable behavior + +No known runtime divergence. `hookLoader` still reflects Forge's runtime TileEntity name map and registers +`savedMultipart`; `registerTileClass` still updates the reverse map cached during singleton construction. Chunk loading +still records the world, reconstructs saved multipart dummies, uses the first matching converter, removes empty +conversions and leaves unclaimed tiles untouched. Replacements retain the old world, validate and replace the map +entry. + +### ABI and source compatibility + +`MultipartSaveLoad` retains its seven public static methods. `MultipartSaveLoad$` retains `MODULE$`, the seven matching +instance methods, and the exact private `converters`, `loadingWorld` and `classToNameMap` fields. The dummy remains the +public, non-final static member `MultipartSaveLoad.TileNBTContainer`, emitting the literal binary name +`MultipartSaveLoad$TileNBTContainer`. Placing it inside the companion would have incorrectly emitted a double dollar +and was rejected by the characterization test. + +All callable public names and descriptors match the Scala reference. The reference-only +`MultipartSaveLoad$$anonfun$1` closure class disappeared; it was implementation machinery and has no entry in +the downstream ABI inventory. Accepted classfile-only differences are removed Scala attributes, a private constructor +on the facade, extra private Java helpers, and the companion class initializer without Scala's `ACC_PUBLIC` flag. + +The frozen Scala consumer compiled against the reference continues to link and mutate the companion state. As with +other Java-authored Scala-style setters in this migration, newly recompiled Scala source should call +`loadingWorld_$eq` explicitly if property-assignment rewriting does not recognize the Java method. + +### Validation + +- `MultipartSaveLoadCharacterizationTest`: 3 plain-JVM tests, unchanged from the Scala baseline. +- `MultipartSaveLoadFunctionalTest`: 4 Forge tests, unchanged from the Scala baseline, including the frozen Scala + consumer. +- Complete plain-JVM suite: 174 tests, 0 failures, 0 errors. +- Java 8 Forge dedicated-server suite: 91 tests, 0 failures, 0 errors. +- Raw `javap` comparison confirms identical callable public names/descriptors for all three retained types. The sole + class-list removal is the unreferenced compiler closure above. diff --git a/JAVA_MIGRATION_HANDOFF.md b/JAVA_MIGRATION_HANDOFF.md index 895235d..66f3793 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`. 98 commits including the incremental-version build fix and separate -characterization and port commits for the ForgeMicroblock packet handlers. +Branch: `algent/java`. Base: `master`. 100 commits including the incremental-version build fix and separate +characterization and port commits for `MultipartSaveLoad`. ## The one rule that matters @@ -67,9 +67,8 @@ awk -F'"' '/> converters = new MutableList<>(); + private World loadingWorld; + private final Map, String> classToNameMap = getClassToNameMap(); + + private MultipartSaveLoad$() {} + + public MutableList> converters() { + return converters; + } + + public World loadingWorld() { + return loadingWorld; + } + + public void loadingWorld_$eq(World world) { + loadingWorld = world; + } + + public void hookLoader() { + Map> nameToClass = tileEntityMap("field_145855_i"); + nameToClass.put("savedMultipart", MultipartSaveLoad.TileNBTContainer.class); + } + + public void registerTileClass(Class tileClass) { + classToNameMap.put(tileClass, "savedMultipart"); + } + + public Map, String> getClassToNameMap() { + return tileEntityMap("field_145853_j"); + } + + @SuppressWarnings("unchecked") + private static Map tileEntityMap(String fieldName) { + try { + Field field = TileEntity.class.getDeclaredField( + new ObfMapping("net/minecraft/tileentity/TileEntity", fieldName, "Ljava/util/Map;") + .toRuntime().s_name); + field.setAccessible(true); + return (Map) field.get(null); + } catch (ReflectiveOperationException e) { + return MultipartSaveLoad$., RuntimeException>throwUnchecked(e); + } + } + + @SuppressWarnings("unchecked") + private static T throwUnchecked(Throwable throwable) throws E { + throw (E) throwable; + } + + @SuppressWarnings("unchecked") + public void loadTiles(Chunk chunk) { + loadingWorld = chunk.worldObj; + Iterator> iterator = ((Map) chunk.chunkTileEntityMap) + .entrySet().iterator(); + + while (iterator.hasNext()) { + Map.Entry entry = iterator.next(); + TileEntity oldTile = entry.getValue(); + TileMultipart replacement; + + if (oldTile instanceof MultipartSaveLoad.TileNBTContainer + && "savedMultipart".equals(((MultipartSaveLoad.TileNBTContainer) oldTile).tag().getString("id"))) { + replacement = TileMultipart.createFromNBT(((MultipartSaveLoad.TileNBTContainer) oldTile).tag()); + } else { + IPartTileConverter converter = findConverter(oldTile); + if (converter == null) { + continue; + } + + Iterable parts = converter.convert(oldTile); + replacement = parts.iterator().hasNext() ? MultipartHelper.createTileFromParts(parts) : null; + } + + if (replacement == null) { + iterator.remove(); + } else { + replacement.setWorldObj(oldTile.getWorldObj()); + replacement.validate(); + entry.setValue(replacement); + } + } + } + + private IPartTileConverter findConverter(TileEntity tile) { + scala.collection.Iterator> iterator = converters.iterator(); + while (iterator.hasNext()) { + IPartTileConverter converter = iterator.next(); + if (converter.canConvert(tile)) { + return converter; + } + } + return null; + } + +} diff --git a/src/main/scala/codechicken/multipart/handler/MultipartSaveLoad.java b/src/main/scala/codechicken/multipart/handler/MultipartSaveLoad.java new file mode 100644 index 0000000..149d1ec --- /dev/null +++ b/src/main/scala/codechicken/multipart/handler/MultipartSaveLoad.java @@ -0,0 +1,65 @@ +package codechicken.multipart.handler; + +import java.util.Map; + +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; +import net.minecraft.world.chunk.Chunk; + +import codechicken.multipart.MultipartHelper.IPartTileConverter; +import scala.collection.mutable.MutableList; + +/** Hack due to lack of TileEntityLoadEvent in Forge. */ +public final class MultipartSaveLoad { + + private MultipartSaveLoad() {} + + public static MutableList> converters() { + return MultipartSaveLoad$.MODULE$.converters(); + } + + public static World loadingWorld() { + return MultipartSaveLoad$.MODULE$.loadingWorld(); + } + + public static void loadingWorld_$eq(World world) { + MultipartSaveLoad$.MODULE$.loadingWorld_$eq(world); + } + + public static void hookLoader() { + MultipartSaveLoad$.MODULE$.hookLoader(); + } + + public static void registerTileClass(Class tileClass) { + MultipartSaveLoad$.MODULE$.registerTileClass(tileClass); + } + + public static Map, String> getClassToNameMap() { + return MultipartSaveLoad$.MODULE$.getClassToNameMap(); + } + + public static void loadTiles(Chunk chunk) { + MultipartSaveLoad$.MODULE$.loadTiles(chunk); + } + + /** Dummy registered tile that holds multipart NBT until its chunk finishes loading. */ + public static class TileNBTContainer extends TileEntity { + + private NBTTagCompound tag; + + public NBTTagCompound tag() { + return tag; + } + + public void tag_$eq(NBTTagCompound tag) { + this.tag = tag; + } + + @Override + public void readFromNBT(NBTTagCompound tag) { + super.readFromNBT(tag); + this.tag = tag; + } + } +} diff --git a/src/main/scala/codechicken/multipart/handler/MultipartSaveLoad.scala b/src/main/scala/codechicken/multipart/handler/MultipartSaveLoad.scala deleted file mode 100644 index 5fa299f..0000000 --- a/src/main/scala/codechicken/multipart/handler/MultipartSaveLoad.scala +++ /dev/null @@ -1,99 +0,0 @@ -package codechicken.multipart.handler - -import net.minecraft.tileentity.TileEntity -import net.minecraft.nbt.NBTTagCompound -import java.util.Map -import net.minecraft.world.chunk.Chunk -import net.minecraft.world.ChunkPosition -import codechicken.multipart.{MultipartHelper, TileMultipart} -import codechicken.lib.asm.ObfMapping -import net.minecraft.world.World -import scala.collection.mutable -import codechicken.multipart.MultipartHelper.IPartTileConverter -import scala.collection.JavaConversions._ - -/** Hack due to lack of TileEntityLoadEvent in forge - */ -object MultipartSaveLoad { - val converters = mutable.MutableList[IPartTileConverter[_]]() - var loadingWorld: World = _ - - class TileNBTContainer extends TileEntity { - var tag: NBTTagCompound = _ - - override def readFromNBT(t: NBTTagCompound) { - super.readFromNBT(t) - tag = t - } - } - - def hookLoader() { - val field = classOf[TileEntity].getDeclaredField( - new ObfMapping( - "net/minecraft/tileentity/TileEntity", - "field_145855_i", - "Ljava/util/Map;" - ).toRuntime.s_name - ) - field.setAccessible(true) - val map = field.get(null).asInstanceOf[Map[String, Class[_ <: TileEntity]]] - map.put("savedMultipart", classOf[TileNBTContainer]) - } - - private val classToNameMap = getClassToNameMap - - def registerTileClass(t: Class[_ <: TileEntity]) { - classToNameMap.put(t, "savedMultipart") - } - - def getClassToNameMap = { - val field = classOf[TileEntity].getDeclaredField( - new ObfMapping( - "net/minecraft/tileentity/TileEntity", - "field_145853_j", - "Ljava/util/Map;" - ).toRuntime.s_name - ) - field.setAccessible(true) - field.get(null).asInstanceOf[Map[Class[_ <: TileEntity], String]] - } - - def loadTiles(chunk: Chunk) { - loadingWorld = chunk.worldObj - val iterator = chunk.chunkTileEntityMap - .asInstanceOf[Map[ChunkPosition, TileEntity]] - .entrySet - .iterator - while (iterator.hasNext) { - val e = iterator.next - var next = false - val t = e.getValue match { - case t: TileNBTContainer if t.tag.getString("id") == "savedMultipart" => - TileMultipart.createFromNBT( - e.getValue.asInstanceOf[TileNBTContainer].tag - ) - case t => - converters.find(_.canConvert(t)) match { - case Some(c) => - val parts = c.convert(t) - if (!parts.isEmpty) - MultipartHelper.createTileFromParts(parts) - else - null - case _ => - next = true - null - } - } - - if (!next) { - if (t != null) { - t.setWorldObj(e.getValue.getWorldObj) - t.validate() - e.setValue(t) - } else - iterator.remove() - } - } - } -}