package net.shadowfacts.phycon.item import net.minecraft.block.BlockState import net.minecraft.item.ItemPlacementContext import net.minecraft.util.math.Direction import net.shadowfacts.phycon.block.FaceDeviceBlock import net.shadowfacts.phycon.block.cable.CableBlock import net.shadowfacts.phycon.init.PhyBlocks import net.shadowfacts.phycon.util.CableConnection /** * @author shadowfacts */ class FaceDeviceBlockItem(block: FaceDeviceBlock<*>, settings: Settings = Settings()): DeviceBlockItem(block, settings) { override fun getPlacementState(context: ItemPlacementContext): BlockState? { val hitState = context.world.getBlockState(context.blockPos) if (hitState.block is CableBlock) { val hitBlockEdge = context.hitPos.getComponentAlongAxis(context.side.axis) % 1 == 0.0 val placementSide = if (hitBlockEdge) context.side.opposite else context.side if (hitState[CableBlock.CONNECTIONS[placementSide]] != CableConnection.ON) { var connection = FaceDeviceBlock.FaceCableConnection.NONE for (dir in Direction.values()) { if (hitState[CableBlock.CONNECTIONS[dir]] == CableConnection.ON) { connection = FaceDeviceBlock.FaceCableConnection.from(dir) break } } return block.defaultState .with(FaceDeviceBlock.FACING, placementSide) .with(FaceDeviceBlock.CABLE_CONNECTION, connection) } } return null } }