refactor: port TItemMultiPart to Java

Replaces ItemMultiPart.scala with Java implementations of JItemMultiPart,
TItemMultiPart and the TItemMultiPart$class bridge. The interface is
descriptor-identical to the reference, JItemMultiPart keeps its
constructor and both instance methods and gains only additive statics, and
the bridge keeps all three public statics for ProjRed, its only consumer.
The emitted class-file set for this area is unchanged.

This is the first converted trait that extends a Minecraft class rather
than TMultiPart, so the linearization check ran against Item. The same
split applied: getHitDepth became a real default because Item does not
declare it, while onItemUse stayed abstract because Item does and a
default would silently lose to the vanilla implementation. The rule
generalises to the implementor's actual superclass.

The bridge's getHitDepth ignores its part argument and calls the static
directly, matching the reference whose trait body never used this. The
onItemUse path still dispatches getHitDepth through the interface, so an
implementor's override is honoured.

Validated with 57 plain-JVM tests, a clean build, and the Java 8 Forge
dedicated-server suite.
This commit is contained in:
Algent committed 2026-08-14 21:25:57 +02:00
1 parent 6813144d84
commit f2f1870931
6 files changed
+171 -68

No files matched your search

@@ -1,68 +0,0 @@
package codechicken.multipart
import net.minecraft.item.Item
import codechicken.lib.vec.Vector3
import codechicken.lib.vec.Rotation
import net.minecraft.item.ItemStack
import net.minecraft.entity.player.EntityPlayer
import net.minecraft.world.World
import codechicken.lib.vec.BlockCoord
/** Java class implementation
*/
abstract class JItemMultiPart extends Item with TItemMultiPart
/** Simple multipart item class for easy placement. Simply override the newPart
* function and it the part will be added to the block space if it passes the
* occlusion tests.
*/
trait TItemMultiPart extends Item {
def getHitDepth(vhit: Vector3, side: Int): Double =
vhit.copy.scalarProject(Rotation.axes(side)) + (side % 2 ^ 1)
override def onItemUse(
item: ItemStack,
player: EntityPlayer,
world: World,
x: Int,
y: Int,
z: Int,
side: Int,
hitX: Float,
hitY: Float,
hitZ: Float
): Boolean = {
val pos = new BlockCoord(x, y, z)
val vhit = new Vector3(hitX, hitY, hitZ)
val d = getHitDepth(vhit, side)
def place(): Boolean = {
val part = newPart(item, player, world, pos, side, vhit)
if (part == null || !TileMultipart.canPlacePart(world, pos, part))
return false
if (!world.isRemote)
TileMultipart.addPart(world, pos, part)
if (!player.capabilities.isCreativeMode)
item.stackSize -= 1
return true
}
if (d < 1 && place())
return true
pos.offset(side)
return place()
}
/** Create a new part based on the placement information parameters.
*/
def newPart(
item: ItemStack,
player: EntityPlayer,
world: World,
pos: BlockCoord,
side: Int,
vhit: Vector3
): TMultiPart
}
@@ -0,0 +1,58 @@
package codechicken.multipart;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import codechicken.lib.vec.BlockCoord;
import codechicken.lib.vec.Rotation;
import codechicken.lib.vec.Vector3;
/** Java class implementation of {@link TItemMultiPart}, and the canonical home of its behavior. */
public abstract class JItemMultiPart extends Item implements TItemMultiPart {
@Override
public double getHitDepth(Vector3 vhit, int side) {
return hitDepth(vhit, side);
}
@Override
public boolean onItemUse(ItemStack item, EntityPlayer player, World world, int x, int y, int z, int side,
float hitX, float hitY, float hitZ) {
return onItemUse(this, item, player, world, x, y, z, side, hitX, hitY, hitZ);
}
public static double hitDepth(Vector3 vhit, int side) {
return vhit.copy().scalarProject(Rotation.axes[side]) + (side % 2 ^ 1);
}
public static boolean onItemUse(TItemMultiPart part, ItemStack item, EntityPlayer player, World world, int x, int y,
int z, int side, float hitX, float hitY, float hitZ) {
BlockCoord pos = new BlockCoord(x, y, z);
Vector3 vhit = new Vector3(hitX, hitY, hitZ);
if (part.getHitDepth(vhit, side) < 1 && place(part, item, player, world, pos, side, vhit)) {
return true;
}
pos.offset(side);
return place(part, item, player, world, pos, side, vhit);
}
private static boolean place(TItemMultiPart part, ItemStack item, EntityPlayer player, World world, BlockCoord pos,
int side, Vector3 vhit) {
TMultiPart newPart = part.newPart(item, player, world, pos, side, vhit);
if (newPart == null || !TileMultipart.canPlacePart(world, pos, newPart)) {
return false;
}
if (!world.isRemote) {
TileMultipart.addPart(world, pos, newPart);
}
if (!player.capabilities.isCreativeMode) {
item.stackSize -= 1;
}
return true;
}
}
@@ -0,0 +1,28 @@
package codechicken.multipart;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import codechicken.lib.vec.Vector3;
/**
* Binary bridge for Scala implementations compiled against the original trait. Their forwarders call these statics
* directly, so the bodies cannot delegate back to the instance methods without recursing.
*/
@Deprecated
public abstract class TItemMultiPart$class {
private TItemMultiPart$class() {}
public static double getHitDepth(TItemMultiPart part, Vector3 vhit, int side) {
return JItemMultiPart.hitDepth(vhit, side);
}
public static boolean onItemUse(TItemMultiPart part, ItemStack item, EntityPlayer player, World world, int x, int y,
int z, int side, float hitX, float hitY, float hitZ) {
return JItemMultiPart.onItemUse(part, item, player, world, x, y, z, side, hitX, hitY, hitZ);
}
public static void $init$(TItemMultiPart part) {}
}
@@ -0,0 +1,29 @@
package codechicken.multipart;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import codechicken.lib.vec.BlockCoord;
import codechicken.lib.vec.Vector3;
/**
* Simple multipart item interface for easy placement. Override newPart and the part will be added to the block space if
* it passes the occlusion tests.
* <p>
* Implementors must extend {@link net.minecraft.item.Item} and override onItemUse themselves, delegating to
* {@link JItemMultiPart#onItemUse}, as {@link JItemMultiPart} does. Item declares onItemUse and a superclass method
* beats an interface default on the JVM, so declaring it default here would silently lose.
*/
public interface TItemMultiPart {
default double getHitDepth(Vector3 vhit, int side) {
return JItemMultiPart.hitDepth(vhit, side);
}
boolean onItemUse(ItemStack item, EntityPlayer player, World world, int x, int y, int z, int side, float hitX,
float hitY, float hitZ);
/** Create a new part based on the placement information parameters. */
TMultiPart newPart(ItemStack item, EntityPlayer player, World world, BlockCoord pos, int side, Vector3 vhit);
}