From 471c767d336e9822f4b6d40cceffb834a313d07e Mon Sep 17 00:00:00 2001 From: Algent Date: Mon, 31 Aug 2026 13:51:22 +0200 Subject: [PATCH] refactor: port microblock packet handlers Preserve the registry channel, singleton ABI, client skip, and handshake payload. --- JAVA_MIGRATION.md | 14 +++- JAVA_MIGRATION_DIVERGENCES.md | 44 ++++++++++++ JAVA_MIGRATION_HANDOFF.md | 32 ++++++--- .../microblock/handler/MicroblockCPH$.java | 41 +++++++++++ .../microblock/handler/MicroblockCPH.java | 23 ++++++ .../microblock/handler/MicroblockPH.java | 10 +++ .../microblock/handler/MicroblockSPH$.java | 27 +++++++ .../microblock/handler/MicroblockSPH.java | 24 +++++++ .../microblock/handler/packethandlers.scala | 72 ------------------- .../microblock/handler/proxies.scala | 9 ++- 10 files changed, 209 insertions(+), 87 deletions(-) create mode 100644 src/main/scala/codechicken/microblock/handler/MicroblockCPH$.java create mode 100644 src/main/scala/codechicken/microblock/handler/MicroblockCPH.java create mode 100644 src/main/scala/codechicken/microblock/handler/MicroblockPH.java create mode 100644 src/main/scala/codechicken/microblock/handler/MicroblockSPH$.java create mode 100644 src/main/scala/codechicken/microblock/handler/MicroblockSPH.java delete mode 100644 src/main/scala/codechicken/microblock/handler/packethandlers.scala diff --git a/JAVA_MIGRATION.md b/JAVA_MIGRATION.md index b620d24..12575c7 100644 --- a/JAVA_MIGRATION.md +++ b/JAVA_MIGRATION.md @@ -30,7 +30,9 @@ and lifecycle rewriting, `TRedstoneTile` removed the measured redstone allocatio state plus inherited access, `TFluidHandlerTile` preserved ordered Forge fluid distribution, and `TIInventoryTile`/`JInventoryTile` preserved AE2's direct rebuild linkage plus flattened sided inventory routing. `TileMultipartClient` and `TRandomDisplayTickTile` complete the built-in trait queue with their render-cache and random -display-tick behavior intact. All retain their exact runtime interfaces. See `JAVA_MIGRATION_PROFILE.md`. +display-tick behavior intact. The first handler wave now also includes both mod entry points, both event handlers and +the ForgeMicroblock registry packet handlers. All retain their exact runtime interfaces. See +`JAVA_MIGRATION_PROFILE.md`. That audit found and this branch has now repaired one existing regression: Schematica 1.12.6 reflects the original private `MultiPartRegistry$` field `codechicken$multipart$MultiPartRegistry$$typeMap` and casts it to a Scala mutable map. The @@ -784,3 +786,13 @@ generated-trait compatibility work. Scala-runtime removal remains a later projec unchanged; their actual rendering remains on the client manual checklist. - All 164 plain-JVM and all 86 Java 8 Forge dedicated-server tests pass. This event-only adapter is not a meaningful focused performance target; `microblock/handler/packethandlers.scala` is next. +- Characterized the ForgeMicroblock packet-handler unit against untouched Scala. Seven plain-JVM cases freeze the + shared channel base, both facade/companion surfaces and exact packet interfaces, the integrated-server registry + skip, ordered missing-material disconnect, unknown-type `MatchError` and no-op server callback. One Forge case + freezes the handshake channel, type and complete material-ID payload. +- 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. diff --git a/JAVA_MIGRATION_DIVERGENCES.md b/JAVA_MIGRATION_DIVERGENCES.md index b7968e1..fb497a9 100644 --- a/JAVA_MIGRATION_DIVERGENCES.md +++ b/JAVA_MIGRATION_DIVERGENCES.md @@ -1957,3 +1957,47 @@ constructor. Neither changes runtime annotations or the callable public surface. - Raw `javap` comparison confirms identical callable public members, reflection confirms identical annotations, and the emitted class list is unchanged. - Texture stitching and placement-highlight rendering require a client and remain on the manual checklist. + +## 2026-08-31 — ForgeMicroblock packet-handler Java port + +The shared packet-handler base and both client/server facade-companion pairs are now Java. Scala proxy code registers +`MicroblockCPH$.MODULE$` and `MicroblockSPH$.MODULE$` explicitly, preserving the singleton objects CodeChickenLib +receives. + +### Observable behavior + +No known runtime divergence. The registry channel remains `ForgeMicroblock`. Client packet type 1 still skips the +material-map read in an integrated single-player game, avoiding mutation of the registry shared with the server. A +multiplayer client still reads the complete map and disconnects with translation key `microblock.missing` plus the +ordered, comma-separated missing names. Unknown client packet types still throw `scala.MatchError`. + +The server packet callback remains a no-op. The handshake still sends packet type 1 on `ForgeMicroblock`, followed by +the material count and every registered material name in ID order. + +### ABI and source compatibility + +`MicroblockPH` retains its public constructor and `registryChannel()` accessor. `MicroblockCPH` and `MicroblockSPH` +retain their exact static forwarders. Their companion classes retain `MODULE$`, extend `MicroblockPH`, implement the +same CodeChickenLib packet interfaces in the same order, and expose the same instance methods. No audited shipping +consumer references these types. + +Both jars emit exactly `MicroblockPH.class`, `MicroblockCPH.class`, `MicroblockCPH$.class`, `MicroblockSPH.class` and +`MicroblockSPH$.class`. Accepted classfile-only differences are the removed Scala signature/marker attributes, private +constructors on the static facades, and companion class initializers without Scala's `ACC_PUBLIC` flag. A class +initializer is invoked only by the JVM and cannot be named by caller bytecode. + +Recompiled in-repo Scala now passes `MicroblockCPH$.MODULE$` and `MicroblockSPH$.MODULE$` where the object values were +previously implicit, and calls the Java no-arg channel accessors with parentheses. Compiled external callers still +target the same classes, fields, method names and descriptors. + +### Validation + +- `MicroblockPacketHandlerCharacterizationTest` and `MicroblockPacketHandlerBehaviorTest`: 7 plain-JVM tests, + unchanged from the Scala baseline. +- `MicroblockPacketHandlerFunctionalTest`: 1 Forge handshake test, unchanged from the Scala baseline and compiled into + the functional-test jar. +- 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. diff --git a/JAVA_MIGRATION_HANDOFF.md b/JAVA_MIGRATION_HANDOFF.md index 156724a..895235d 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`. 96 commits including the incremental-version build fix and separate -characterization and port commits for `MicroblockEventHandler`. +Branch: `algent/java`. Base: `master`. 98 commits including the incremental-version build fix and separate +characterization and port commits for the ForgeMicroblock packet handlers. ## The one rule that matters @@ -67,7 +67,9 @@ awk -F'"' '/ missing = MicroMaterialRegistry.readIDMap(packet); + if (!missing.isEmpty()) { + netHandler.handleDisconnect( + new S40PacketDisconnect( + new ChatComponentTranslation("microblock.missing", String.join(", ", missing)))); + } + } +} diff --git a/src/main/scala/codechicken/microblock/handler/MicroblockCPH.java b/src/main/scala/codechicken/microblock/handler/MicroblockCPH.java new file mode 100644 index 0000000..0056550 --- /dev/null +++ b/src/main/scala/codechicken/microblock/handler/MicroblockCPH.java @@ -0,0 +1,23 @@ +package codechicken.microblock.handler; + +import net.minecraft.client.Minecraft; +import net.minecraft.network.play.INetHandlerPlayClient; + +import codechicken.lib.packet.PacketCustom; + +public final class MicroblockCPH { + + private MicroblockCPH() {} + + public static String registryChannel() { + return MicroblockCPH$.MODULE$.registryChannel(); + } + + public static void handlePacket(PacketCustom packet, Minecraft minecraft, INetHandlerPlayClient netHandler) { + MicroblockCPH$.MODULE$.handlePacket(packet, minecraft, netHandler); + } + + public static void handleMaterialRegistration(PacketCustom packet, INetHandlerPlayClient netHandler) { + MicroblockCPH$.MODULE$.handleMaterialRegistration(packet, netHandler); + } +} diff --git a/src/main/scala/codechicken/microblock/handler/MicroblockPH.java b/src/main/scala/codechicken/microblock/handler/MicroblockPH.java new file mode 100644 index 0000000..c980f80 --- /dev/null +++ b/src/main/scala/codechicken/microblock/handler/MicroblockPH.java @@ -0,0 +1,10 @@ +package codechicken.microblock.handler; + +public class MicroblockPH { + + private final String registryChannel = "ForgeMicroblock"; + + public String registryChannel() { + return registryChannel; + } +} diff --git a/src/main/scala/codechicken/microblock/handler/MicroblockSPH$.java b/src/main/scala/codechicken/microblock/handler/MicroblockSPH$.java new file mode 100644 index 0000000..b5d39f9 --- /dev/null +++ b/src/main/scala/codechicken/microblock/handler/MicroblockSPH$.java @@ -0,0 +1,27 @@ +package codechicken.microblock.handler; + +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.network.NetHandlerPlayServer; +import net.minecraft.network.play.INetHandlerPlayServer; + +import codechicken.lib.packet.PacketCustom; +import codechicken.lib.packet.PacketCustom.IHandshakeHandler; +import codechicken.lib.packet.PacketCustom.IServerPacketHandler; +import codechicken.microblock.MicroMaterialRegistry; + +public final class MicroblockSPH$ extends MicroblockPH implements IServerPacketHandler, IHandshakeHandler { + + public static final MicroblockSPH$ MODULE$ = new MicroblockSPH$(); + + private MicroblockSPH$() {} + + @Override + public void handlePacket(PacketCustom packet, EntityPlayerMP sender, INetHandlerPlayServer netHandler) {} + + @Override + public void handshakeRecieved(NetHandlerPlayServer netHandler) { + PacketCustom packet = new PacketCustom(registryChannel(), 1); + MicroMaterialRegistry.writeIDMap(packet); + netHandler.sendPacket(packet.toPacket()); + } +} diff --git a/src/main/scala/codechicken/microblock/handler/MicroblockSPH.java b/src/main/scala/codechicken/microblock/handler/MicroblockSPH.java new file mode 100644 index 0000000..0ed757e --- /dev/null +++ b/src/main/scala/codechicken/microblock/handler/MicroblockSPH.java @@ -0,0 +1,24 @@ +package codechicken.microblock.handler; + +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.network.NetHandlerPlayServer; +import net.minecraft.network.play.INetHandlerPlayServer; + +import codechicken.lib.packet.PacketCustom; + +public final class MicroblockSPH { + + private MicroblockSPH() {} + + public static String registryChannel() { + return MicroblockSPH$.MODULE$.registryChannel(); + } + + public static void handlePacket(PacketCustom packet, EntityPlayerMP sender, INetHandlerPlayServer netHandler) { + MicroblockSPH$.MODULE$.handlePacket(packet, sender, netHandler); + } + + public static void handshakeRecieved(NetHandlerPlayServer netHandler) { + MicroblockSPH$.MODULE$.handshakeRecieved(netHandler); + } +} diff --git a/src/main/scala/codechicken/microblock/handler/packethandlers.scala b/src/main/scala/codechicken/microblock/handler/packethandlers.scala deleted file mode 100644 index cda3a40..0000000 --- a/src/main/scala/codechicken/microblock/handler/packethandlers.scala +++ /dev/null @@ -1,72 +0,0 @@ -package codechicken.microblock.handler - -import codechicken.lib.packet.PacketCustom.{ - IHandshakeHandler, - IClientPacketHandler, - IServerPacketHandler -} -import codechicken.lib.packet.PacketCustom -import net.minecraft.client.Minecraft -import codechicken.microblock.MicroMaterialRegistry -import net.minecraft.entity.player.EntityPlayerMP -import net.minecraft.network.play.{INetHandlerPlayServer, INetHandlerPlayClient} -import net.minecraft.network.play.server.S40PacketDisconnect -import net.minecraft.util.ChatComponentTranslation -import net.minecraft.network.NetHandlerPlayServer - -class MicroblockPH { - val registryChannel = - "ForgeMicroblock" // Must use the 250 system for ID registry as the NetworkMod idMap hasn't been properly initialized from the server yet. -} - -object MicroblockCPH extends MicroblockPH with IClientPacketHandler { - def handlePacket( - packet: PacketCustom, - mc: Minecraft, - netHandler: INetHandlerPlayClient - ) { - packet.getType match { - // In singleplayer the integrated server and this client share the one - // static registry, so the id-map packet is redundant. Skipping it avoids - // rebuilding (clearing) the shared maps on the netty thread while the - // server thread reads them - the race that turned microblocks into Blood - // Rune slabs. - case 1 => - if (!mc.isSingleplayer) - handleMaterialRegistration(packet, netHandler) - } - } - - def handleMaterialRegistration( - packet: PacketCustom, - netHandler: INetHandlerPlayClient - ) { - val missing = MicroMaterialRegistry.readIDMap(packet) - if (!missing.isEmpty) - netHandler.handleDisconnect( - new S40PacketDisconnect( - new ChatComponentTranslation( - "microblock.missing", - String.join(", ", missing) - ) - ) - ) - } -} - -object MicroblockSPH - extends MicroblockPH - with IServerPacketHandler - with IHandshakeHandler { - def handlePacket( - packet: PacketCustom, - sender: EntityPlayerMP, - netHandler: INetHandlerPlayServer - ) {} - - def handshakeRecieved(netHandler: NetHandlerPlayServer) { - val packet = new PacketCustom(registryChannel, 1) - MicroMaterialRegistry.writeIDMap(packet) - netHandler.sendPacket(packet.toPacket) - } -} diff --git a/src/main/scala/codechicken/microblock/handler/proxies.scala b/src/main/scala/codechicken/microblock/handler/proxies.scala index bf3f1b9..7b926d5 100644 --- a/src/main/scala/codechicken/microblock/handler/proxies.scala +++ b/src/main/scala/codechicken/microblock/handler/proxies.scala @@ -108,8 +108,8 @@ class MicroblockProxy_serverImpl { def postInit() { MicroMaterialRegistry.calcMaxCuttingStrength() PacketCustom.assignHandshakeHandler( - MicroblockSPH.registryChannel, - MicroblockSPH + MicroblockSPH.registryChannel(), + MicroblockSPH$.MODULE$ ) if (Loader.isModLoaded("postea")) { PosteaCompat.registerTransformers() @@ -126,7 +126,10 @@ class MicroblockProxy_clientImpl extends MicroblockProxy_serverImpl { super.postInit() MicroMaterialRegistry.loadIcons() MinecraftForgeClient.registerItemRenderer(itemMicro, ItemMicroPartRenderer) - PacketCustom.assignHandler(MicroblockCPH.registryChannel, MicroblockCPH) + PacketCustom.assignHandler( + MicroblockCPH.registryChannel(), + MicroblockCPH$.MODULE$ + ) if (Loader.isModLoaded("angelica")) MicroblockMod.angelicaCompat_$eq(new AngelicaCompat)