From 0f84ff203e2a77b562694fdaf35012551a41a837 Mon Sep 17 00:00:00 2001 From: Algent Date: Tue, 1 Sep 2026 00:08:20 +0200 Subject: [PATCH] refactor: port multipart proxy to java --- JAVA_MIGRATION.md | 12 ++ JAVA_MIGRATION_DIVERGENCES.md | 49 +++++++ JAVA_MIGRATION_HANDOFF.md | 26 ++-- JAVA_MIGRATION_MANUAL_CHECKS.md | 1 + .../multipart/MultipartGenerator.scala | 4 +- .../multipart/handler/MultipartProxy$.java | 23 ++++ .../multipart/handler/MultipartProxy.java | 69 ++++++++++ .../handler/MultipartProxy_clientImpl.java | 34 +++++ .../handler/MultipartProxy_serverImpl.java | 104 ++++++++++++++ .../multipart/handler/packethandlers.scala | 1 - .../multipart/handler/proxies.scala | 127 ------------------ 11 files changed, 311 insertions(+), 139 deletions(-) create mode 100644 src/main/scala/codechicken/multipart/handler/MultipartProxy$.java create mode 100644 src/main/scala/codechicken/multipart/handler/MultipartProxy.java create mode 100644 src/main/scala/codechicken/multipart/handler/MultipartProxy_clientImpl.java create mode 100644 src/main/scala/codechicken/multipart/handler/MultipartProxy_serverImpl.java delete mode 100644 src/main/scala/codechicken/multipart/handler/proxies.scala diff --git a/JAVA_MIGRATION.md b/JAVA_MIGRATION.md index 525b197..b74f7ea 100644 --- a/JAVA_MIGRATION.md +++ b/JAVA_MIGRATION.md @@ -1022,3 +1022,15 @@ generated-trait compatibility work. Scala-runtime removal remains a later projec Extra Utilities is the sole exception and is constrained rather than fixed. The pattern is a three-step ratchet: FMP adds a supported equivalent (additive, safe now), the consumer is patched and released, then FMP drops the private shape in a release allowed to break that ABI. + +### 2026-09-01 + +- Characterized `MultipartProxy` against untouched Scala. Four plain-JVM cases freeze the four-type hierarchy, exact + facade/companion surfaces, mutable singleton accessors, client-only boundaries and both chunk-index conversions. One + Forge case freezes dedicated-server stripping, virtual fallback to the server implementation and initialized proxy + state. +- Ported the hierarchy and facade to four Java types without changing their emitted names or callable public + descriptors. Common generated-tile registration now names `MultipartProxy$.MODULE$` explicitly so it still reaches + the inherited server method after Forge strips the client override and its static forwarder. +- All 187 plain-JVM and all 96 Java 8 Forge dedicated-server tests pass. Startup registration and two bit-packing + helpers are not a meaningful focused performance target; `microblock/handler/proxies.scala` is next. diff --git a/JAVA_MIGRATION_DIVERGENCES.md b/JAVA_MIGRATION_DIVERGENCES.md index a6f1fe2..7b1c8ad 100644 --- a/JAVA_MIGRATION_DIVERGENCES.md +++ b/JAVA_MIGRATION_DIVERGENCES.md @@ -2156,3 +2156,52 @@ methods remain common-side and therefore survive dedicated-server stripping, mat - Java 8 Forge dedicated-server suite: 95 tests, 0 failures, 0 errors. - A clean build followed by raw `javap` comparison confirms identical callable public names/descriptors and emitted classes. The clean step is material: Zinc otherwise retained and packaged the deleted Scala class files. + +## 2026-09-01 — MultipartProxy Java port + +The multipart server/client proxy hierarchy is now Java, including the static facade and load-bearing companion +singleton. ProjectRed's direct `MultipartProxy$.MODULE$` block access and BuildCraftCompat/Iguana Tweaks' static +`MultipartProxy.block()`/`config()` calls therefore keep the same owners and descriptors. + +### Observable behavior + +No known runtime divergence. Pre-init still records the logger, opens `multipart.cfg`, registers the same eight +generated-tile traits in the same order, registers and resolves the multipart block, then hooks saved-tile loading. +Post-init still registers the same event-handler singleton on both buses, assigns both server packet handlers, starts +the tick scheduler and chunk-load hook, and loads compatibility hooks in the same order. + +Client post-init still follows common post-init with the multipart block renderer, both client packet channels, and +the control-key event/key-binding registrations. Generated tile classes are still registered for save/load before a +client renderer is bound. The two chunk-index conversions retain their exact bit packing, including signed input. + +On a dedicated server Forge strips `postInit` and `onTileClassBuilt` from `MultipartProxy_clientImpl` and strips their +static facade forwarders. `MultipartProxy$.MODULE$` then resolves both calls to `MultipartProxy_serverImpl`, exactly as +the Scala hierarchy did. The common `MultipartGenerator` callback now spells that companion call explicitly; calling +the stripped facade would fail after the Java port even though its raw dev-jar ABI is correct. + +### ABI, annotations and source compatibility + +All four emitted types remain: `MultipartProxy_serverImpl`, `MultipartProxy_clientImpl`, `MultipartProxy`, and +`MultipartProxy$`. The inheritance and final flags are unchanged. The server implementation retains its exact ten +public methods and three private fields; the client implementation declares only its two overrides; the facade keeps +all twelve public static forwarders; and the companion declares only `MODULE$` plus both instance index conversions. +Every callable public descriptor matches the reference. + +Only the two client overrides and their facade forwarders retain `@SideOnly(CLIENT)`; no class is side-only. The +facade and companion still share mutable block/config/logger state while separately constructed server proxies remain +independent. + +Accepted classfile-only differences are removed Scala signature/marker attributes, a private constructor on the +static facade, and a companion class initializer without Scala's `ACC_PUBLIC` flag. Both jars emit exactly the same +four proxy classes. An unused Scala wildcard import was removed from the packet-handler source. + +### Validation + +- `MultipartProxyCharacterizationTest`: 4 plain-JVM tests, unchanged from the Scala baseline. +- `MultipartProxyFunctionalTest`: 1 Forge test, unchanged from the Scala baseline. +- Complete plain-JVM suite: 187 tests, 0 failures, 0 errors. +- Java 8 Forge dedicated-server suite: 96 tests, 0 failures, 0 errors. +- A clean build followed by raw `javap` comparison confirms identical emitted classes, hierarchy, fields, callable + public names/descriptors and client-only method annotations. +- Client renderer, packet, key-binding and generated-tile renderer registration require a client and remain on the + manual checklist. diff --git a/JAVA_MIGRATION_HANDOFF.md b/JAVA_MIGRATION_HANDOFF.md index bec4375..39a90d0 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`. 106 commits including the incremental-version build fix and separate -characterization and port commits for `GrassMicroMaterial`/`TopMicroMaterial`. +Branch: `algent/java`. Base: `master`. 109 commits including the API-cleanup plan and separate characterization and +port commits for `MultipartProxy`. ## The one rule that matters @@ -67,7 +67,7 @@ awk -F'"' '/> 8 & 0xFF, + chunk.chunkZPos << 4 | (index & 0xF0) >> 4); + } + + public int indexInChunk(BlockCoord pos) { + return pos.x & 0xF | pos.y << 8 | (pos.z & 0xF) << 4; + } +} diff --git a/src/main/scala/codechicken/multipart/handler/MultipartProxy.java b/src/main/scala/codechicken/multipart/handler/MultipartProxy.java new file mode 100644 index 0000000..2d4f6c7 --- /dev/null +++ b/src/main/scala/codechicken/multipart/handler/MultipartProxy.java @@ -0,0 +1,69 @@ +package codechicken.multipart.handler; + +import java.io.File; + +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.ChunkCoordIntPair; + +import org.apache.logging.log4j.Logger; + +import codechicken.lib.config.ConfigFile; +import codechicken.lib.vec.BlockCoord; +import codechicken.multipart.BlockMultipart; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; + +public final class MultipartProxy { + + private MultipartProxy() {} + + public static BlockMultipart block() { + return MultipartProxy$.MODULE$.block(); + } + + public static void block_$eq(BlockMultipart block) { + MultipartProxy$.MODULE$.block_$eq(block); + } + + public static ConfigFile config() { + return MultipartProxy$.MODULE$.config(); + } + + public static void config_$eq(ConfigFile config) { + MultipartProxy$.MODULE$.config_$eq(config); + } + + public static Logger logger() { + return MultipartProxy$.MODULE$.logger(); + } + + public static void logger_$eq(Logger logger) { + MultipartProxy$.MODULE$.logger_$eq(logger); + } + + public static void preInit(File cfgdir, Logger logger) { + MultipartProxy$.MODULE$.preInit(cfgdir, logger); + } + + public static void init() { + MultipartProxy$.MODULE$.init(); + } + + @SideOnly(Side.CLIENT) + public static void postInit() { + MultipartProxy$.MODULE$.postInit(); + } + + @SideOnly(Side.CLIENT) + public static void onTileClassBuilt(Class type) { + MultipartProxy$.MODULE$.onTileClassBuilt(type); + } + + public static BlockCoord indexInChunk(ChunkCoordIntPair chunk, int index) { + return MultipartProxy$.MODULE$.indexInChunk(chunk, index); + } + + public static int indexInChunk(BlockCoord pos) { + return MultipartProxy$.MODULE$.indexInChunk(pos); + } +} diff --git a/src/main/scala/codechicken/multipart/handler/MultipartProxy_clientImpl.java b/src/main/scala/codechicken/multipart/handler/MultipartProxy_clientImpl.java new file mode 100644 index 0000000..34a852d --- /dev/null +++ b/src/main/scala/codechicken/multipart/handler/MultipartProxy_clientImpl.java @@ -0,0 +1,34 @@ +package codechicken.multipart.handler; + +import net.minecraft.tileentity.TileEntity; + +import codechicken.lib.packet.PacketCustom; +import codechicken.multipart.ControlKeyHandler; +import codechicken.multipart.MultipartRenderer$; +import cpw.mods.fml.client.registry.ClientRegistry; +import cpw.mods.fml.client.registry.RenderingRegistry; +import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; + +public class MultipartProxy_clientImpl extends MultipartProxy_serverImpl { + + @Override + @SideOnly(Side.CLIENT) + public void postInit() { + super.postInit(); + RenderingRegistry.registerBlockHandler(MultipartRenderer$.MODULE$); + PacketCustom.assignHandler(MultipartCPH$.MODULE$.channel(), MultipartCPH$.MODULE$); + PacketCustom.assignHandler(MultipartCPH$.MODULE$.registryChannel(), MultipartCPH$.MODULE$); + + FMLCommonHandler.instance().bus().register(ControlKeyHandler.INSTANCE); + ClientRegistry.registerKeyBinding(ControlKeyHandler.INSTANCE); + } + + @Override + @SideOnly(Side.CLIENT) + public void onTileClassBuilt(Class type) { + super.onTileClassBuilt(type); + ClientRegistry.bindTileEntitySpecialRenderer(type, MultipartRenderer$.MODULE$); + } +} diff --git a/src/main/scala/codechicken/multipart/handler/MultipartProxy_serverImpl.java b/src/main/scala/codechicken/multipart/handler/MultipartProxy_serverImpl.java new file mode 100644 index 0000000..b873229 --- /dev/null +++ b/src/main/scala/codechicken/multipart/handler/MultipartProxy_serverImpl.java @@ -0,0 +1,104 @@ +package codechicken.multipart.handler; + +import java.io.File; + +import net.minecraft.block.Block; +import net.minecraft.tileentity.TileEntity; +import net.minecraftforge.common.MinecraftForge; + +import org.apache.logging.log4j.Logger; + +import codechicken.lib.config.ConfigFile; +import codechicken.lib.packet.PacketCustom; +import codechicken.lib.world.TileChunkLoadHook; +import codechicken.lib.world.WorldExtensionManager; +import codechicken.multipart.BlockMultipart; +import codechicken.multipart.MultipartGenerator; +import codechicken.multipart.TickScheduler$; +import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.common.registry.GameRegistry; + +public class MultipartProxy_serverImpl { + + private BlockMultipart block; + private ConfigFile config; + private Logger logger; + + public BlockMultipart block() { + return block; + } + + public void block_$eq(BlockMultipart block) { + this.block = block; + } + + public ConfigFile config() { + return config; + } + + public void config_$eq(ConfigFile config) { + this.config = config; + } + + public Logger logger() { + return logger; + } + + public void logger_$eq(Logger logger) { + this.logger = logger; + } + + public void preInit(File cfgdir, Logger logger) { + this.logger = logger; + config = new ConfigFile(new File(cfgdir, "multipart.cfg")).setComment("Multipart API config file"); + + MultipartGenerator + .registerTrait("codechicken.multipart.TSlottedPart", "codechicken.multipart.scalatraits.TSlottedTile"); + MultipartGenerator.registerTrait( + "net.minecraftforge.fluids.IFluidHandler", + "codechicken.multipart.scalatraits.TFluidHandlerTile"); + MultipartGenerator.registerTrait( + "net.minecraft.inventory.IInventory", + "codechicken.multipart.scalatraits.JInventoryTile"); + MultipartGenerator.registerTrait( + "net.minecraft.inventory.ISidedInventory", + "codechicken.multipart.scalatraits.JInventoryTile"); + MultipartGenerator.registerTrait( + "codechicken.multipart.JPartialOcclusion", + "codechicken.multipart.scalatraits.TPartialOcclusionTile"); + MultipartGenerator.registerTrait( + "codechicken.multipart.IRedstonePart", + "codechicken.multipart.scalatraits.TRedstoneTile"); + MultipartGenerator.registerTrait( + "codechicken.multipart.IRandomDisplayTick", + "codechicken.multipart.scalatraits.TRandomDisplayTickTile", + null); + MultipartGenerator.registerTrait( + "codechicken.multipart.INeighborTileChange", + null, + "codechicken.multipart.scalatraits.TTileChangeTile"); + + GameRegistry.registerBlock(new BlockMultipart().setBlockName("multipart"), null, "block"); + block = (BlockMultipart) Block.blockRegistry.getObject("ForgeMultipart:block"); + + MultipartSaveLoad.hookLoader(); + } + + public void init() {} + + public void postInit() { + FMLCommonHandler.instance().bus().register(MultipartEventHandler$.MODULE$); + MinecraftForge.EVENT_BUS.register(MultipartEventHandler$.MODULE$); + PacketCustom.assignHandler(MultipartSPH$.MODULE$.channel(), MultipartSPH$.MODULE$); + PacketCustom.assignHandshakeHandler(MultipartSPH$.MODULE$.registryChannel(), MultipartSPH$.MODULE$); + + WorldExtensionManager.registerWorldExtension(TickScheduler$.MODULE$); + TileChunkLoadHook.init(); + + MultipartCompatiblity.load(); + } + + public void onTileClassBuilt(Class type) { + MultipartSaveLoad.registerTileClass(type); + } +} diff --git a/src/main/scala/codechicken/multipart/handler/packethandlers.scala b/src/main/scala/codechicken/multipart/handler/packethandlers.scala index 7a436f3..6ff2464 100644 --- a/src/main/scala/codechicken/multipart/handler/packethandlers.scala +++ b/src/main/scala/codechicken/multipart/handler/packethandlers.scala @@ -23,7 +23,6 @@ import codechicken.lib.data.MCDataOutputWrapper import java.io.DataOutputStream import java.io.ByteArrayOutputStream import net.minecraft.world.ChunkCoordIntPair -import MultipartProxy._ import codechicken.multipart.PacketScheduler import java.util.LinkedList import scala.collection.JavaConversions._ diff --git a/src/main/scala/codechicken/multipart/handler/proxies.scala b/src/main/scala/codechicken/multipart/handler/proxies.scala deleted file mode 100644 index 6e4738d..0000000 --- a/src/main/scala/codechicken/multipart/handler/proxies.scala +++ /dev/null @@ -1,127 +0,0 @@ -package codechicken.multipart.handler - -import codechicken.multipart._ -import cpw.mods.fml.client.registry.RenderingRegistry -import cpw.mods.fml.client.registry.ClientRegistry -import net.minecraft.tileentity.TileEntity -import codechicken.lib.config.ConfigFile -import java.io.File -import net.minecraftforge.common.MinecraftForge -import cpw.mods.fml.relauncher.SideOnly -import cpw.mods.fml.relauncher.Side -import codechicken.lib.packet.PacketCustom -import net.minecraft.world.ChunkCoordIntPair -import codechicken.lib.vec.BlockCoord -import codechicken.lib.world.{TileChunkLoadHook, WorldExtensionManager} -import cpw.mods.fml.common.FMLCommonHandler -import cpw.mods.fml.common.registry.GameRegistry -import net.minecraft.block.Block -import org.apache.logging.log4j.Logger - -class MultipartProxy_serverImpl { - var block: BlockMultipart = _ - var config: ConfigFile = _ - var logger: Logger = _ - - def preInit(cfgdir: File, logger: Logger) { - this.logger = logger - config = new ConfigFile(new File(cfgdir, "multipart.cfg")) - .setComment("Multipart API config file") - - MultipartGenerator.registerTrait( - "codechicken.multipart.TSlottedPart", - "codechicken.multipart.scalatraits.TSlottedTile" - ) - MultipartGenerator.registerTrait( - "net.minecraftforge.fluids.IFluidHandler", - "codechicken.multipart.scalatraits.TFluidHandlerTile" - ) - MultipartGenerator.registerTrait( - "net.minecraft.inventory.IInventory", - "codechicken.multipart.scalatraits.JInventoryTile" - ) - MultipartGenerator.registerTrait( - "net.minecraft.inventory.ISidedInventory", - "codechicken.multipart.scalatraits.JInventoryTile" - ) - MultipartGenerator.registerTrait( - "codechicken.multipart.JPartialOcclusion", - "codechicken.multipart.scalatraits.TPartialOcclusionTile" - ) - MultipartGenerator.registerTrait( - "codechicken.multipart.IRedstonePart", - "codechicken.multipart.scalatraits.TRedstoneTile" - ) - MultipartGenerator.registerTrait( - "codechicken.multipart.IRandomDisplayTick", - "codechicken.multipart.scalatraits.TRandomDisplayTickTile", - null - ) - MultipartGenerator.registerTrait( - "codechicken.multipart.INeighborTileChange", - null, - "codechicken.multipart.scalatraits.TTileChangeTile" - ) - - GameRegistry.registerBlock( - new BlockMultipart().setBlockName("multipart"), - null, - "block" - ) - block = Block.blockRegistry - .getObject("ForgeMultipart:block") - .asInstanceOf[BlockMultipart] - - MultipartSaveLoad.hookLoader() - } - - def init() {} - - def postInit() { - FMLCommonHandler.instance().bus().register(MultipartEventHandler$.MODULE$) - MinecraftForge.EVENT_BUS.register(MultipartEventHandler$.MODULE$) - PacketCustom.assignHandler(MultipartSPH.channel, MultipartSPH) - PacketCustom.assignHandshakeHandler( - MultipartSPH.registryChannel, - MultipartSPH - ) - - WorldExtensionManager.registerWorldExtension(TickScheduler$.MODULE$) - TileChunkLoadHook.init() - - MultipartCompatiblity.load() - } - - def onTileClassBuilt(t: Class[_ <: TileEntity]) { - MultipartSaveLoad.registerTileClass(t) - } -} - -class MultipartProxy_clientImpl extends MultipartProxy_serverImpl { - @SideOnly(Side.CLIENT) - override def postInit() { - super.postInit() - RenderingRegistry.registerBlockHandler(MultipartRenderer$.MODULE$) - PacketCustom.assignHandler(MultipartCPH.channel, MultipartCPH) - PacketCustom.assignHandler(MultipartCPH.registryChannel, MultipartCPH) - - FMLCommonHandler.instance().bus().register(ControlKeyHandler.INSTANCE) - ClientRegistry.registerKeyBinding(ControlKeyHandler.INSTANCE) - } - - @SideOnly(Side.CLIENT) - override def onTileClassBuilt(t: Class[_ <: TileEntity]) { - super.onTileClassBuilt(t) - ClientRegistry.bindTileEntitySpecialRenderer(t, MultipartRenderer$.MODULE$) - } -} - -object MultipartProxy extends MultipartProxy_clientImpl { - def indexInChunk(cc: ChunkCoordIntPair, i: Int) = new BlockCoord( - cc.chunkXPos << 4 | i & 0xf, - (i >> 8) & 0xff, - cc.chunkZPos << 4 | (i & 0xf0) >> 4 - ) - def indexInChunk(pos: BlockCoord) = - pos.x & 0xf | pos.y << 8 | (pos.z & 0xf) << 4 -}