mirror of
https://github.com/GTNewHorizons/ForgeMultipart.git
synced 2026-11-27 23:45:52 +00:00
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.
This commit is contained in:
1 parent
8f73a693cc
commit
a2797c986b
8 files changed
+174
-72
No files matched your search
@@ -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.
|
||||
@@ -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<EntityPlayer, Object>` | `java.util.Map<EntityPlayer, Boolean>` |
|
||||
| `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.
|
||||
@@ -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'"' '/<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: **118 plain-JVM tests, 18 Forge server tests, all passing.**
|
||||
Current baseline: **120 plain-JVM tests, 21 Forge server tests, all passing.**
|
||||
|
||||
### ABI diff against the reference
|
||||
|
||||
@@ -155,6 +155,10 @@ proves the other branch did reach it. Use `LinkageError`, not the exact type —
|
||||
has no scope spanning a package and its siblings. `PacketScheduler.sendScheduled` hit this; `MultipartGenerator` and
|
||||
`MicroblockGenerator` will too.
|
||||
|
||||
**A Forge `FakePlayer` unlocks the server half of player-dependent code.**
|
||||
`FakePlayerFactory.getMinecraft(worldServer)` needs no connection and its world is not remote, which is the branch a
|
||||
server takes. It made `ControlKeyModifer.isControlDown` testable. The client branch of anything stays manual.
|
||||
|
||||
**Java 8 target.** No `List.of`, no `var`, no switch expressions in main or test sources.
|
||||
|
||||
**Two test classes sharing global registry state** must guard their registrations, and the registries' error paths call
|
||||
@@ -169,13 +173,14 @@ All eight load-bearing `$class` helpers from the inventory, both registries, and
|
||||
`MultiPartRegistry`, `TileMultipart`, `TMultiPart`, `TickScheduler`, `BlockMultipart`.
|
||||
|
||||
Plus the six marker interfaces: `TSlottedPart`, `IRandomDisplayTick`, `INeighborTileChange`, `TRandomUpdateTick`,
|
||||
`ISidedHollowConnect`, `IMicroMaterialRender`, plus `MultipartHelper`, `TileCache` and `PacketScheduler`.
|
||||
`ISidedHollowConnect`, `IMicroMaterialRender`, plus `MultipartHelper`, `TileCache`, `PacketScheduler` and the `ControlKeyModifer` pair.
|
||||
|
||||
68 Java files, 52 Scala files, ~6,527 Scala lines left (non-blank; that is the metric this figure has always used).
|
||||
70 Java files, 51 Scala files, ~6,471 Scala lines left (non-blank; that is the metric this figure has always used).
|
||||
|
||||
## What is left, and in what order
|
||||
|
||||
**Low risk, good next steps.** `ControlKeyModifier` and the two `package.scala` objects.
|
||||
**Low risk, good next steps.** The two `package.scala` objects. After those the low-risk list is empty and the
|
||||
next work is the medium group.
|
||||
|
||||
`IRedstonePart.scala` is misleadingly named and is **not** a marker-trait file. It holds six traits plus
|
||||
`RedstoneInteractions`, whose `MODULE$` is load-bearing, so it is its own piece of work at medium risk.
|
||||
|
||||
@@ -6,7 +6,7 @@ import net.minecraft.util.MovingObjectPosition
|
||||
import codechicken.lib.vec.Vector3
|
||||
import codechicken.lib.vec.BlockCoord
|
||||
import codechicken.lib.vec.Rotation
|
||||
import codechicken.multipart.ControlKeyModifer._
|
||||
import codechicken.multipart.ControlKeyModifer
|
||||
import net.minecraft.item.ItemStack
|
||||
import codechicken.multipart.TileMultipart
|
||||
import codechicken.lib.raytracer.ExtendedMOP
|
||||
@@ -85,7 +85,7 @@ class MicroblockPlacement(
|
||||
|
||||
val d = getHitDepth(vhit, hit.sideHit)
|
||||
val useOppMod = pp.sneakOpposite(slot, hit.sideHit)
|
||||
val oppMod = player.isControlDown
|
||||
val oppMod = ControlKeyModifer.isControlDown(player)
|
||||
val internal = d < 1 && htile != null
|
||||
val doExpand =
|
||||
internal && !gtile._2.asInstanceOf[
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package codechicken.multipart;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.settings.KeyBinding;
|
||||
|
||||
import org.lwjgl.input.Keyboard;
|
||||
|
||||
import codechicken.lib.packet.PacketCustom;
|
||||
import codechicken.multipart.handler.MultipartCPH;
|
||||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||
import cpw.mods.fml.common.gameevent.TickEvent.ClientTickEvent;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
/**
|
||||
* Key handler implementation. Client only: it extends a client class, so it must never be loaded on a server.
|
||||
* <p>
|
||||
* 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
* <p>
|
||||
* The misspelling in the class name is the published one and is kept deliberately.
|
||||
*/
|
||||
public final class ControlKeyModifer {
|
||||
|
||||
private static final Map<EntityPlayer, Boolean> 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<EntityPlayer, Boolean> 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;
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user