2019-10-28 16:37:54 +00:00
|
|
|
package net.shadowfacts.phycon.item
|
|
|
|
|
2021-03-07 15:58:28 +00:00
|
|
|
import net.minecraft.block.BlockState
|
2021-03-04 03:29:19 +00:00
|
|
|
import net.minecraft.block.Blocks
|
2021-03-03 23:43:49 +00:00
|
|
|
import net.minecraft.entity.ItemEntity
|
2019-10-28 16:37:54 +00:00
|
|
|
import net.minecraft.item.Item
|
2021-03-03 23:43:49 +00:00
|
|
|
import net.minecraft.item.ItemStack
|
|
|
|
import net.minecraft.item.ItemUsageContext
|
2021-03-04 03:29:19 +00:00
|
|
|
import net.minecraft.sound.SoundCategory
|
|
|
|
import net.minecraft.sound.SoundEvents
|
2021-03-03 23:43:49 +00:00
|
|
|
import net.minecraft.util.ActionResult
|
2019-10-28 16:37:54 +00:00
|
|
|
import net.minecraft.util.Identifier
|
2021-03-07 15:38:22 +00:00
|
|
|
import net.minecraft.util.math.Vec3d
|
2019-10-28 16:37:54 +00:00
|
|
|
import net.shadowfacts.phycon.PhysicalConnectivity
|
2021-03-03 23:43:49 +00:00
|
|
|
import net.shadowfacts.phycon.block.DeviceBlock
|
2021-03-07 15:38:22 +00:00
|
|
|
import net.shadowfacts.phycon.block.FaceDeviceBlock
|
2021-03-14 19:48:09 +00:00
|
|
|
import net.shadowfacts.phycon.block.cable.CableBlock
|
2021-03-05 03:15:18 +00:00
|
|
|
import net.shadowfacts.phycon.init.PhyBlocks
|
2021-03-07 15:38:22 +00:00
|
|
|
import net.shadowfacts.phycon.util.containsInclusive
|
2019-10-28 16:37:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author shadowfacts
|
|
|
|
*/
|
2021-03-09 23:35:28 +00:00
|
|
|
class ScrewdriverItem: Item(Settings().maxCount(1)) {
|
2019-10-28 16:37:54 +00:00
|
|
|
companion object {
|
|
|
|
val ID = Identifier(PhysicalConnectivity.MODID, "screwdriver")
|
|
|
|
}
|
2021-03-03 23:43:49 +00:00
|
|
|
|
|
|
|
override fun useOnBlock(context: ItemUsageContext): ActionResult {
|
2021-03-07 15:58:28 +00:00
|
|
|
if (context.player?.isSneaking != true) return ActionResult.PASS
|
|
|
|
|
2021-03-03 23:43:49 +00:00
|
|
|
val state = context.world.getBlockState(context.blockPos)
|
|
|
|
val block = state.block
|
|
|
|
|
2021-03-07 15:58:28 +00:00
|
|
|
val newState =
|
|
|
|
when (block) {
|
|
|
|
is DeviceBlock<*> -> screwdriverDeviceBlock(context, state, block)
|
2021-03-14 19:48:09 +00:00
|
|
|
is CableBlock -> screwdriverCableBlock(context, state)
|
2021-03-07 15:58:28 +00:00
|
|
|
PhyBlocks.SWITCH -> screwdriverSwitchBlock(context)
|
|
|
|
else -> null
|
|
|
|
}
|
2021-03-05 03:15:18 +00:00
|
|
|
|
2021-03-07 15:58:28 +00:00
|
|
|
if (newState != null) {
|
|
|
|
context.world.setBlockState(context.blockPos, newState)
|
|
|
|
context.world.playSound(context.player, context.blockPos, SoundEvents.BLOCK_METAL_PLACE, SoundCategory.BLOCKS, 0.8f, 0.65f)
|
|
|
|
return ActionResult.SUCCESS
|
|
|
|
} else {
|
|
|
|
return ActionResult.PASS
|
|
|
|
}
|
|
|
|
}
|
2021-03-03 23:43:49 +00:00
|
|
|
|
2021-03-07 15:58:28 +00:00
|
|
|
private fun screwdriverDeviceBlock(context: ItemUsageContext, state: BlockState, block: DeviceBlock<*>): BlockState? {
|
|
|
|
if (!context.world.isClient) {
|
|
|
|
val be = block.getBlockEntity(context.world, context.blockPos)!!
|
2021-03-07 15:38:22 +00:00
|
|
|
|
2021-03-07 15:58:28 +00:00
|
|
|
val stack = ItemStack(block)
|
|
|
|
val beTag = stack.getOrCreateSubTag("BlockEntityTag")
|
|
|
|
be.toTag(beTag)
|
|
|
|
// remove x, y, z entries for stacking purposes
|
|
|
|
beTag.remove("x")
|
|
|
|
beTag.remove("y")
|
|
|
|
beTag.remove("z")
|
2021-03-07 15:38:22 +00:00
|
|
|
|
2021-03-07 15:58:28 +00:00
|
|
|
if (block === PhyBlocks.TERMINAL) {
|
|
|
|
// remove the terminal's internal buffer since it drops its items
|
|
|
|
beTag.remove("InternalBuffer")
|
2021-03-03 23:43:49 +00:00
|
|
|
}
|
|
|
|
|
2021-03-07 15:58:28 +00:00
|
|
|
val entity = ItemEntity(context.world, context.blockPos.x.toDouble(), context.blockPos.y.toDouble(), context.blockPos.z.toDouble(), stack)
|
|
|
|
context.world.spawnEntity(entity)
|
|
|
|
}
|
2021-03-04 03:29:19 +00:00
|
|
|
|
2021-03-07 15:58:28 +00:00
|
|
|
return if (block is FaceDeviceBlock<*>) {
|
|
|
|
screwdriverFaceDeviceBlock(context, state, block)
|
2021-03-03 23:43:49 +00:00
|
|
|
} else {
|
2021-03-07 15:58:28 +00:00
|
|
|
Blocks.AIR.defaultState
|
2021-03-03 23:43:49 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-07 15:58:28 +00:00
|
|
|
|
|
|
|
private fun screwdriverFaceDeviceBlock(context: ItemUsageContext, state: BlockState, block: FaceDeviceBlock<*>): BlockState? {
|
|
|
|
val hitInsideBlock = Vec3d(context.hitPos.x - context.blockPos.x, context.hitPos.y - context.blockPos.y, context.hitPos.z - context.blockPos.z)
|
|
|
|
val faceShape = block.faceShapes[state[FaceDeviceBlock.FACING]]!!
|
|
|
|
// if we hit the face part of block, leave the cable behind
|
|
|
|
if (faceShape.boundingBox.containsInclusive(hitInsideBlock)) {
|
2021-03-15 23:06:17 +00:00
|
|
|
val cableBlock = PhyBlocks.CABLES[state[FaceDeviceBlock.COLOR]]!!
|
|
|
|
return cableBlock.getInitialState(context.world, context.blockPos)
|
2021-03-07 15:58:28 +00:00
|
|
|
} else {
|
|
|
|
if (!context.world.isClient) {
|
2021-03-14 19:48:09 +00:00
|
|
|
val cable = ItemEntity(context.world, context.blockPos.x.toDouble(), context.blockPos.y.toDouble(), context.blockPos.z.toDouble(), ItemStack(state.block))
|
2021-03-07 15:58:28 +00:00
|
|
|
context.world.spawnEntity(cable)
|
|
|
|
}
|
|
|
|
return Blocks.AIR.defaultState
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 19:48:09 +00:00
|
|
|
private fun screwdriverCableBlock(context: ItemUsageContext, state: BlockState): BlockState? {
|
2021-03-07 15:58:28 +00:00
|
|
|
if (!context.world.isClient) {
|
2021-03-14 19:48:09 +00:00
|
|
|
val entity = ItemEntity(context.world, context.blockPos.x.toDouble(), context.blockPos.y.toDouble(), context.blockPos.z.toDouble(), ItemStack(state.block))
|
2021-03-07 15:58:28 +00:00
|
|
|
context.world.spawnEntity(entity)
|
|
|
|
}
|
|
|
|
return Blocks.AIR.defaultState
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun screwdriverSwitchBlock(context: ItemUsageContext): BlockState? {
|
|
|
|
if (!context.world.isClient) {
|
|
|
|
val entity = ItemEntity(context.world, context.blockPos.x.toDouble(), context.blockPos.y.toDouble(), context.blockPos.z.toDouble(), ItemStack(PhyBlocks.SWITCH))
|
|
|
|
context.world.spawnEntity(entity)
|
|
|
|
}
|
|
|
|
return Blocks.AIR.defaultState
|
|
|
|
}
|
|
|
|
|
2019-10-28 16:37:54 +00:00
|
|
|
}
|