From a2797c986b2aef961da7609b0a560ad7496c53fd Mon Sep 17 00:00:00 2001 From: Algent Date: Sat, 15 Aug 2026 19:45:53 +0200 Subject: [PATCH] refactor: port ControlKeyModifer and ControlKeyHandler to Java isControlDown(EntityPlayer) and isClientPressing() keep their descriptors. map() became a java.util.Map and isClientPressing_$eq became setClientPressing. Dropped playerControlValue and ControlKeyValue. They existed only to give Scala player.isControlDown sugar through an implicit conversion, and the reference already shipped the explicit static 'for Java users'. MicroblockPlacement now calls that static directly. ControlKeyHandler was a Scala object extending KeyBinding, so the instance both the key registry and the event bus needed was ControlKeyHandler$.MODULE$. It is now an ordinary KeyBinding subclass with an INSTANCE constant, which also drops the twelve static forwarders Scala generated for inherited KeyBinding members. The misspelling in ControlKeyModifer is the published type name and is kept; only the file name, which said ControlKeyModifier.scala, is corrected. 120 plain-JVM tests and 21 Forge server tests pass, none of them changed by the port. --- JAVA_MIGRATION.md | 5 ++ JAVA_MIGRATION_DIVERGENCES.md | 59 +++++++++++++++++ JAVA_MIGRATION_HANDOFF.md | 15 +++-- .../microblock/MicroblockPlacement.scala | 4 +- .../multipart/ControlKeyHandler.java | 50 +++++++++++++++ .../multipart/ControlKeyModifer.java | 46 ++++++++++++++ .../multipart/ControlKeyModifier.scala | 63 ------------------- .../multipart/handler/proxies.scala | 4 +- 8 files changed, 174 insertions(+), 72 deletions(-) create mode 100644 src/main/scala/codechicken/multipart/ControlKeyHandler.java create mode 100644 src/main/scala/codechicken/multipart/ControlKeyModifer.java delete mode 100644 src/main/scala/codechicken/multipart/ControlKeyModifier.scala diff --git a/JAVA_MIGRATION.md b/JAVA_MIGRATION.md index aee2165..aae40c7 100644 --- a/JAVA_MIGRATION.md +++ b/JAVA_MIGRATION.md @@ -530,3 +530,8 @@ These ranges were formed before designing the characterization suite and should - Widened `sendScheduled` from `private[multipart]` to public. Its only caller is in `codechicken.multipart.handler`, and Java has no scope spanning a package and its siblings, so package-private would not compile. Expect this wherever `private[multipart]` crosses into the handler package. - Recorded that the whole subsystem has no implementors at all, in this codebase or in the pack. It is a third-party extension point, which is why every behavioral test had to construct its own part. - Confirmed the Forge server suite can drive the packet path without players connected. `MultipartSPH.getTileStream` only needs a non-remote world and a position, so a part bound to a tile with `setWorldObj` is enough to exercise `getWriteStream`. +- Ported `ControlKeyModifer` and `ControlKeyHandler`. `isControlDown(EntityPlayer)` and `isClientPressing()` keep their descriptors; `map()` became a `java.util.Map`, and the implicit-conversion pair `playerControlValue`/`ControlKeyValue` was dropped because it only ever existed to give Scala `player.isControlDown` sugar and the reference already shipped the explicit static "for Java users". +- Kept the misspelling in `ControlKeyModifer`. It is the published type name; only the file name, which said `ControlKeyModifier.scala`, was corrected. Renaming the type is an API change with no forcing reason and should be a deliberate decision rather than a side effect of a port. +- Converted the first Scala `object` that extends a class rather than being a plain singleton. `ControlKeyHandler` is now an ordinary `KeyBinding` subclass with an `INSTANCE` constant, which drops the twelve static forwarders Scala generated for inherited `KeyBinding` members along with the companion. The registration sites pass `ControlKeyHandler.INSTANCE`. +- Found that a Forge `FakePlayer` makes the server half of player-dependent code testable. `FakePlayerFactory.getMinecraft(worldServer)` yields a player whose world is not remote, which is exactly the branch the server takes, and it needs no connection. +- Recorded that the client half remains untestable in both harnesses. `isClientPressing` is only ever set from a client tick, so the key binding, the press/release packet and the client branch of `isControlDown` are all manual checks. diff --git a/JAVA_MIGRATION_DIVERGENCES.md b/JAVA_MIGRATION_DIVERGENCES.md index 0ce547c..f2c28fa 100644 --- a/JAVA_MIGRATION_DIVERGENCES.md +++ b/JAVA_MIGRATION_DIVERGENCES.md @@ -1069,3 +1069,62 @@ one. The two `PacketScheduler$$anonfun$` closure classes go with them; the Java abstract on `TScheduledPacketPart`, asserting the member set instead. Every behavioral assertion is unchanged. - What stays manual: the client-world guard. `schedulePacket` throws only when `world.isRemote`, which neither harness can produce — the plain JVM cannot build a `World` and the dedicated server is never remote. + +## 2026-08-15 — ControlKeyModifer and ControlKeyHandler Java port + +The placement-modifier key: a server-side map of which players hold it, and the client key binding that keeps that map +in sync. **Zero downstream references**, and not among guidenh's reflective names. + +The misspelling in `ControlKeyModifer` is the published name and is kept. The file was named `ControlKeyModifier.scala` +while the object inside it was `ControlKeyModifer`; only the file name is corrected. Renaming the type would be an API +change with no forcing reason, so it is left for a deliberate decision rather than smuggled into a port. + +### Observable behavior + +No known divergence. `isControlDown` still branches on whether the player's world is remote, still reports +`isClientPressing` on a client and the recorded map value on a server, and an unrecorded player still reads as not +holding. `ControlKeyHandler.tick` still fires only on a change, still checks the net handler before sending, and still +sets the client flag before writing packet 1. + +The reference's map was `HashMap().withDefaultValue(false)`; the port uses a plain `HashMap` and treats an absent key +as false at the single read site, which is the only place the default was ever observable. + +### Changed and removed API + +| Member | Reference | Port | +| --- | --- | --- | +| `isControlDown(EntityPlayer)` | static | **unchanged**, the documented Java entry point | +| `isClientPressing()` | static | unchanged | +| `isClientPressing_$eq(boolean)` | static | `setClientPressing(boolean)` | +| `map()` | `scala.collection.mutable.Map` | `java.util.Map` | +| `playerControlValue(EntityPlayer)` | static, returned `ControlKeyValue` | removed | + +Removed classes: `ControlKeyModifer$`, `ControlKeyModifer$ControlKeyValue` and `ControlKeyHandler$`. Nothing +references any of them in bytecode or by reflective name. + +`ControlKeyValue` and `playerControlValue` existed only to give Scala `player.isControlDown` sugar through an implicit +conversion. They are meaningless from Java, and the reference already shipped `isControlDown(EntityPlayer)` explicitly +"for Java users". The one in-repo user, `MicroblockPlacement`, now calls that static directly. + +### ControlKeyHandler is now an ordinary class with a singleton + +The reference was a Scala `object` extending `KeyBinding`, so the instance the key registry and the event bus both +needed was `ControlKeyHandler$.MODULE$`, and `ControlKeyHandler` was a class of static forwarders — including +forwarders for twelve inherited `KeyBinding` members. The port makes `ControlKeyHandler` itself the `KeyBinding` +subclass with a single `INSTANCE`, and `MultipartProxy_clientImpl` passes `ControlKeyHandler.INSTANCE`. + +Consequence to note: referencing `ControlKeyHandler` at all now loads `KeyBinding`, where before only the companion +did. Both are reached solely from the client proxy's `postInit`, so nothing changes in practice, and the class carries +`@SideOnly(Side.CLIENT)`. + +### Validation + +- Complete plain-JVM suite: 120 tests, 0 failures, 0 errors. +- Clean `spotlessApply checkstyleTest build`: passing. +- Java 8 Forge dedicated-server suite: 21 tests, 0 failures, 0 errors, 3 of them new. `isControlDown` needs a real + player to pick its branch, so they run against a Forge `FakePlayer` on a server world, which takes the server branch. +- Class list and members diffed against a reference jar built from the pre-port `src/main`. +- No characterization assertion changed with the port. +- What stays manual, and it is most of the client half: the key binding appearing in the controls screen, the packet + actually reaching the server on press and release, and `isControlDown` reporting the pressed state on a client world. + No harness has a client, so `isClientPressing` is never exercised. diff --git a/JAVA_MIGRATION_HANDOFF.md b/JAVA_MIGRATION_HANDOFF.md index d5b64f1..79d094c 100644 --- a/JAVA_MIGRATION_HANDOFF.md +++ b/JAVA_MIGRATION_HANDOFF.md @@ -10,7 +10,7 @@ what breaks, and what is left. The other documents hold the reasoning. | `JAVA_MIGRATION_DIVERGENCES.md` | Every intentional difference from the reference, one entry per port | | `JAVA_MIGRATION_MANUAL_CHECKS.md` | What no automated test can cover, and must be checked by hand in a client | -Branch: `algent/java`. Base: `master`. 48 commits so far. +Branch: `algent/java`. Base: `master`. 50 commits so far. ## The one rule that matters @@ -56,7 +56,7 @@ awk -F'"' '/ + * The reference was a Scala object, which is a singleton reached as {@code ControlKeyHandler$.MODULE$}. Java has no + * object sugar, so the single instance both the key registry and the event bus need is {@link #INSTANCE}. + */ +@SideOnly(Side.CLIENT) +public class ControlKeyHandler extends KeyBinding { + + public static final ControlKeyHandler INSTANCE = new ControlKeyHandler(); + + private boolean wasPressed; + + private ControlKeyHandler() { + super("key.control", Keyboard.KEY_NONE, "Forge Multipart"); + } + + public boolean wasPressed() { + return wasPressed; + } + + @SubscribeEvent + @SideOnly(Side.CLIENT) + public void tick(ClientTickEvent event) { + boolean pressed = getIsKeyPressed(); + if (pressed != wasPressed) { + wasPressed = pressed; + if (Minecraft.getMinecraft().getNetHandler() != null) { + ControlKeyModifer.setClientPressing(pressed); + PacketCustom packet = new PacketCustom(MultipartCPH.channel(), 1); + packet.writeBoolean(pressed); + packet.sendToServer(); + } + } + } +} diff --git a/src/main/scala/codechicken/multipart/ControlKeyModifer.java b/src/main/scala/codechicken/multipart/ControlKeyModifer.java new file mode 100644 index 0000000..092ee0a --- /dev/null +++ b/src/main/scala/codechicken/multipart/ControlKeyModifer.java @@ -0,0 +1,46 @@ +package codechicken.multipart; + +import java.util.HashMap; +import java.util.Map; + +import net.minecraft.entity.player.EntityPlayer; + +/** + * A class that maintains a map server<->client of which players are holding the control (or placement modifier + * key) much like sneaking. + *

+ * The misspelling in the class name is the published one and is kept deliberately. + */ +public final class ControlKeyModifer { + + private static final Map map = new HashMap<>(); + + private static boolean clientPressing; + + private ControlKeyModifer() {} + + /** Players the server has been told are holding the key. Absent means not holding. */ + public static Map map() { + return map; + } + + public static boolean isClientPressing() { + return clientPressing; + } + + public static void setClientPressing(boolean pressing) { + clientPressing = pressing; + } + + /** + * On a client world this reports what this client is pressing; on a server world it reports what the given player + * last told the server. + */ + public static boolean isControlDown(EntityPlayer p) { + if (p.worldObj.isRemote) { + return clientPressing; + } + Boolean pressed = map.get(p); + return pressed != null && pressed; + } +} diff --git a/src/main/scala/codechicken/multipart/ControlKeyModifier.scala b/src/main/scala/codechicken/multipart/ControlKeyModifier.scala deleted file mode 100644 index 00f1131..0000000 --- a/src/main/scala/codechicken/multipart/ControlKeyModifier.scala +++ /dev/null @@ -1,63 +0,0 @@ -package codechicken.multipart - -import net.minecraft.client.settings.KeyBinding -import org.lwjgl.input.Keyboard -import net.minecraft.client.Minecraft -import codechicken.lib.packet.PacketCustom -import codechicken.multipart.handler.MultipartCPH -import scala.collection.mutable.HashMap -import net.minecraft.entity.player.EntityPlayer -import cpw.mods.fml.common.eventhandler.SubscribeEvent -import cpw.mods.fml.relauncher.{Side, SideOnly} -import cpw.mods.fml.common.gameevent.TickEvent.ClientTickEvent - -/** A class that maintains a map server<->client of which players are holding - * the control (or placement modifier key) much like sneaking. - */ -object ControlKeyModifer { - implicit def playerControlValue(p: EntityPlayer) = new ControlKeyValue(p) - - class ControlKeyValue(p: EntityPlayer) { - def isControlDown = { - if (p.worldObj.isRemote) { - isClientPressing - } else { - map(p) - } - } - } - - val map = HashMap[EntityPlayer, Boolean]().withDefaultValue(false) - var isClientPressing = false; - - /** Implicit static for Java users. - */ - def isControlDown(p: EntityPlayer) = p.isControlDown -} - -/** Key Handler implementation - */ -object ControlKeyHandler - extends KeyBinding( - "key.control", - Keyboard.KEY_NONE, - "Forge Multipart" - ) { - import ControlKeyModifer._ - var wasPressed = false - - @SubscribeEvent - @SideOnly(Side.CLIENT) - def tick(event: ClientTickEvent) { - val pressed = getIsKeyPressed - if (pressed != wasPressed) { - wasPressed = pressed - if (Minecraft.getMinecraft.getNetHandler != null) { - isClientPressing = pressed - val packet = new PacketCustom(MultipartCPH.channel, 1) - packet.writeBoolean(pressed) - packet.sendToServer() - } - } - } -} diff --git a/src/main/scala/codechicken/multipart/handler/proxies.scala b/src/main/scala/codechicken/multipart/handler/proxies.scala index 45c1c4a..c68ed57 100644 --- a/src/main/scala/codechicken/multipart/handler/proxies.scala +++ b/src/main/scala/codechicken/multipart/handler/proxies.scala @@ -105,8 +105,8 @@ class MultipartProxy_clientImpl extends MultipartProxy_serverImpl { PacketCustom.assignHandler(MultipartCPH.channel, MultipartCPH) PacketCustom.assignHandler(MultipartCPH.registryChannel, MultipartCPH) - FMLCommonHandler.instance().bus().register(ControlKeyHandler) - ClientRegistry.registerKeyBinding(ControlKeyHandler) + FMLCommonHandler.instance().bus().register(ControlKeyHandler.INSTANCE) + ClientRegistry.registerKeyBinding(ControlKeyHandler.INSTANCE) } @SideOnly(Side.CLIENT)