refactor: port multipart save/load

This commit is contained in:
Algent committed 2026-08-31 14:36:02 +02:00
1 parent 3cdc62455c
commit 386c3b2cef
6 files changed
+254 -118

No files matched your search

+11 -3
View File
@@ -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.
+42 -2
View File
@@ -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.
+18 -14
View File
@@ -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'"' '/<testsuite /{t+=$4;f+=$8;e+=$10} END{print "tests="t" failures="f" e
grep -o 'tests="[0-9]*" skipped="[0-9]*" failures="[0-9]*" errors="[0-9]*"' run/server/junit-out/TEST-*.xml
```
Current baseline: **171 plain-JVM tests passing.** The last completed Forge server run has **86 passing tests**; the
new handshake test compiles, but this checkout currently stops before mod loading because its Minecraft EULA has not
been accepted.
Current baseline: **174 plain-JVM tests and 91 Java 8 Forge dedicated-server tests passing.** The ignored local server
EULA is accepted in this checkout.
### ABI diff against the reference
@@ -244,7 +243,7 @@ All eight load-bearing `$class` helpers from the inventory, both registries, and
`TRedstoneTile`, `TTileChangeTile`, `TFluidHandlerTile`, `TIInventoryTile`/`JInventoryTile`, `TileMultipartClient`, and
`TRandomDisplayTickTile` Java-trait ports, plus `MultipartCompatiblity`/`MCPCCompatModule`, `MultipartMod`,
`MultipartEventHandler`, `MicroblockMod`, `MicroblockEventHandler`, and the complete `MicroblockPH`/
`MicroblockCPH`/`MicroblockSPH` packet-handler unit.
`MicroblockCPH`/`MicroblockSPH` packet-handler unit, plus `MultipartSaveLoad`.
Plus the six marker interfaces: `TSlottedPart`, `IRandomDisplayTick`, `INeighborTileChange`, `TRandomUpdateTick`,
`ISidedHollowConnect`, `IMicroMaterialRender`, plus `MultipartHelper`, `TileCache`, `PacketScheduler` and the `ControlKeyModifer` pair.
@@ -259,7 +258,7 @@ across the port. It is also where the two shim constraints in the gotchas list w
`rayTraceAll`'s index production is now characterized, closing the gap the read-path cleanup left: the index written
into `ExtendedMOP.data` is what `reduceMOP` hands back to every click, activate, harvest and pick block.
108 Java files, 32 Scala files, ~5,008 Scala lines left (non-blank; that is the metric this figure has always used).
110 Java files, 31 Scala files, ~4,919 Scala lines left (non-blank; that is the metric this figure has always used).
## What is left, and in what order
@@ -369,8 +368,14 @@ dedicated server while the companion still reaches event-bus registration.
The ForgeMicroblock packet-handler unit is now Java. `MicroblockPH` keeps its channel accessor; both static facades,
both companion singletons and their three CodeChickenLib packet interfaces retain their exact public descriptors.
Seven plain-JVM tests freeze the raw ABI, integrated-server skip, ordered disconnect, unknown-type failure and no-op
server callback. The Forge handshake test freezes the exact channel, type and material-ID payload; it compiles but
still needs the EULA-gated server run in this checkout.
server callback. The Forge handshake test freezes the exact channel, type and material-ID payload and passes on the
Java 8 dedicated server.
`MultipartSaveLoad` is now Java. Its static facade, load-bearing companion singleton, private state fields and literal
`MultipartSaveLoad$TileNBTContainer` binary name retain their exact public shape. A frozen Scala consumer exercises
ProjectRed's `MODULE$`/`loadingWorld` linkage, and dedicated Forge tests cover reflective vanilla map registration,
converter precedence/deletion and saved multipart reconstruction. Only the compiler-generated `$$anonfun$1`
class disappeared; the downstream inventory contains no reference to it.
**Medium.** The `handler` packages on both sides, `ItemMicroPart`,
`MicroblockPlacement`, `PlacementGrids`, `BlockMicroMaterial`, `MissingMicroMaterial`, `ConfigContent`,
@@ -380,11 +385,10 @@ still needs the EULA-gated server run in this checkout.
narrow generator relaxation: Java-trait parent linearization, explicit field-accessor recognition, and exclusion of
transient runtime caches from generated copying.
Take `multipart/handler/MultipartSaveLoad.scala` next once the Forge harness can run; at 89 nonblank lines it is the
smallest remaining handler unit. Its `MultipartSaveLoad$.MODULE$` and `loadingWorld()` accessor are load-bearing for
ProjectRed, so freeze the facade/companion shape, mutable converter-list identity, `TileNBTContainer`, reflective map
hooks and all replace/remove branches before changing it. Existing helper and event-handler Forge tests cover several
paths but are not a substitute for a dedicated untouched-Scala characterization commit.
Take `microblock/MissingMicroMaterial.scala` next. At 48 nonblank lines it is the smallest remaining non-ASM unit in
the medium-risk queue, and existing registry tests already cover much of its inherited behavior. Before changing it,
freeze the exact facade/companion surface, `IMicroMaterial` method descriptors, client-only method stripping and the
singleton registered by `DefaultContent`; keep actual icon/render behavior on the client manual checklist.
**Phase 6/7, last.** `Microblock` and the microblock shape hierarchy, `MicroblockGenerator`, `MultipartGenerator`, and
all of `multipart/asm/`. The ASM subsystem should be last; freeze generated-class fixtures before touching it.
@@ -0,0 +1,118 @@
package codechicken.multipart.handler;
import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.Map;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.ChunkPosition;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import codechicken.lib.asm.ObfMapping;
import codechicken.multipart.MultipartHelper;
import codechicken.multipart.MultipartHelper.IPartTileConverter;
import codechicken.multipart.TMultiPart;
import codechicken.multipart.TileMultipart;
import scala.collection.mutable.MutableList;
/** Scala companion singleton retained for binary compatibility with compiled Scala consumers. */
public final class MultipartSaveLoad$ {
public static final MultipartSaveLoad$ MODULE$ = new MultipartSaveLoad$();
private final MutableList<IPartTileConverter<?>> converters = new MutableList<>();
private World loadingWorld;
private final Map<Class<? extends TileEntity>, String> classToNameMap = getClassToNameMap();
private MultipartSaveLoad$() {}
public MutableList<IPartTileConverter<?>> converters() {
return converters;
}
public World loadingWorld() {
return loadingWorld;
}
public void loadingWorld_$eq(World world) {
loadingWorld = world;
}
public void hookLoader() {
Map<String, Class<? extends TileEntity>> nameToClass = tileEntityMap("field_145855_i");
nameToClass.put("savedMultipart", MultipartSaveLoad.TileNBTContainer.class);
}
public void registerTileClass(Class<? extends TileEntity> tileClass) {
classToNameMap.put(tileClass, "savedMultipart");
}
public Map<Class<? extends TileEntity>, String> getClassToNameMap() {
return tileEntityMap("field_145853_j");
}
@SuppressWarnings("unchecked")
private static <K, V> Map<K, V> 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<K, V>) field.get(null);
} catch (ReflectiveOperationException e) {
return MultipartSaveLoad$.<Map<K, V>, RuntimeException>throwUnchecked(e);
}
}
@SuppressWarnings("unchecked")
private static <T, E extends Throwable> T throwUnchecked(Throwable throwable) throws E {
throw (E) throwable;
}
@SuppressWarnings("unchecked")
public void loadTiles(Chunk chunk) {
loadingWorld = chunk.worldObj;
Iterator<Map.Entry<ChunkPosition, TileEntity>> iterator = ((Map<ChunkPosition, TileEntity>) chunk.chunkTileEntityMap)
.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<ChunkPosition, TileEntity> 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<TMultiPart> 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<IPartTileConverter<?>> iterator = converters.iterator();
while (iterator.hasNext()) {
IPartTileConverter<?> converter = iterator.next();
if (converter.canConvert(tile)) {
return converter;
}
}
return null;
}
}
@@ -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<IPartTileConverter<?>> 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<? extends TileEntity> tileClass) {
MultipartSaveLoad$.MODULE$.registerTileClass(tileClass);
}
public static Map<Class<? extends TileEntity>, 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;
}
}
}
@@ -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()
}
}
}
}