Files
ForgeMultipart/src/main/scala/codechicken/microblock/MicroblockPlacement.scala
T
Algent a2797c986b 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.
2026-08-15 19:45:53 +02:00

183 lines
5.4 KiB
Scala

package codechicken.microblock
import net.minecraft.world.World
import net.minecraft.entity.player.EntityPlayer
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 net.minecraft.item.ItemStack
import codechicken.multipart.TileMultipart
import codechicken.lib.raytracer.ExtendedMOP
abstract class ExecutablePlacement(val pos: BlockCoord, val part: Microblock) {
def place(world: World, player: EntityPlayer, item: ItemStack)
def consume(world: World, player: EntityPlayer, item: ItemStack)
}
class AdditionPlacement($pos: BlockCoord, $part: Microblock)
extends ExecutablePlacement($pos, $part) {
def place(world: World, player: EntityPlayer, item: ItemStack) {
TileMultipart.addPart(world, pos, part)
}
def consume(world: World, player: EntityPlayer, item: ItemStack) {
item.stackSize -= 1
}
}
class ExpandingPlacement($pos: BlockCoord, $part: Microblock, opart: Microblock)
extends ExecutablePlacement($pos, $part) {
def place(world: World, player: EntityPlayer, item: ItemStack) {
opart.shape = part.shape
opart.tile.notifyPartChange(opart)
opart.sendShapeUpdate()
}
def consume(world: World, player: EntityPlayer, item: ItemStack) {
item.stackSize -= 1
}
}
abstract class PlacementProperties {
def opposite(slot: Int, side: Int): Int
def sneakOpposite(slot: Int, side: Int) = true
def expand(slot: Int, side: Int) = true
def microClass: MicroblockClass
def placementGrid: PlacementGrid
def customPlacement(pmt: MicroblockPlacement): ExecutablePlacement = null
}
object MicroblockPlacement {
def apply(
player: EntityPlayer,
hit: MovingObjectPosition,
size: Int,
material: Int,
checkMaterial: Boolean,
pp: PlacementProperties
): ExecutablePlacement =
new MicroblockPlacement(player, hit, size, material, checkMaterial, pp)()
}
class MicroblockPlacement(
val player: EntityPlayer,
val hit: MovingObjectPosition,
val size: Int,
val material: Int,
val checkMaterial: Boolean,
val pp: PlacementProperties
) {
val world = player.worldObj
val mcrClass = pp.microClass
val pos = new BlockCoord(hit.blockX, hit.blockY, hit.blockZ)
val vhit = new Vector3(hit.hitVec).add(-pos.x, -pos.y, -pos.z)
val gtile = TileMultipart.getOrConvertTile2(world, pos)
val htile = gtile._1
val slot = pp.placementGrid.getHitSlot(vhit, hit.sideHit)
val oslot = pp.opposite(slot, hit.sideHit)
val d = getHitDepth(vhit, hit.sideHit)
val useOppMod = pp.sneakOpposite(slot, hit.sideHit)
val oppMod = ControlKeyModifer.isControlDown(player)
val internal = d < 1 && htile != null
val doExpand =
internal && !gtile._2.asInstanceOf[
Boolean
] && !player.isSneaking && !(oppMod && useOppMod) && pp
.expand(slot, hit.sideHit)
val side = hit.sideHit
def apply(): ExecutablePlacement = {
val customPlacement = pp.customPlacement(this)
if (customPlacement != null)
return customPlacement
if (slot < 0)
return null
if (doExpand) {
val hpart = htile.partList.apply(ExtendedMOP.getData[(Int, _)](hit)._1)
if (hpart.getType == mcrClass.getName) {
val mpart = hpart.asInstanceOf[CommonMicroblock]
if (mpart.material == material && mpart.getSize + size < 8)
return expand(mpart)
}
}
if (internal) {
if (d < 0.5 || !useOppMod) {
val ret = internalPlacement(htile, slot)
if (ret != null) {
if (!useOppMod || !oppMod) return ret
else return internalPlacement(htile, oslot)
}
}
if (useOppMod && !oppMod)
return internalPlacement(htile, oslot)
else
return externalPlacement(slot)
}
if (!useOppMod || !oppMod)
return externalPlacement(slot)
else
return externalPlacement(oslot)
}
def expand(mpart: CommonMicroblock): ExecutablePlacement =
expand(mpart, create(mpart.getSize + size, mpart.getSlot, mpart.material))
def expand(mpart: Microblock, npart: Microblock): ExecutablePlacement = {
val pos = new BlockCoord(mpart.tile)
if (
TileMultipart.checkNoEntityCollision(world, pos, npart) && mpart.tile
.canReplacePart(mpart, npart)
)
return new ExpandingPlacement(pos, npart, mpart)
return null
}
def internalPlacement(htile: TileMultipart, slot: Int): ExecutablePlacement =
internalPlacement(htile, create(size, slot, material))
def internalPlacement(
htile: TileMultipart,
npart: Microblock
): ExecutablePlacement = {
val pos = new BlockCoord(htile)
if (
TileMultipart.checkNoEntityCollision(world, pos, npart) && htile
.canAddPart(npart)
)
return new AdditionPlacement(pos, npart)
return null
}
def externalPlacement(slot: Int): ExecutablePlacement = externalPlacement(
create(size, slot, material)
)
def externalPlacement(npart: Microblock): ExecutablePlacement = {
val pos = this.pos.copy().offset(side)
if (TileMultipart.canPlacePart(world, pos, npart))
return new AdditionPlacement(pos, npart)
return null
}
def getHitDepth(vhit: Vector3, side: Int): Double =
vhit.copy.scalarProject(Rotation.axes(side)) + (side % 2 ^ 1)
def create(size: Int, slot: Int, material: Int) = {
val part = mcrClass.create(world.isRemote, material)
part.setShape(size, slot)
part
}
}