Allow screwdrivering cables and switches

This commit is contained in:
Shadowfacts 2021-03-07 10:58:28 -05:00
parent 9acceeae3c
commit e6faa34355
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 71 additions and 35 deletions

View File

@ -1,5 +1,6 @@
package net.shadowfacts.phycon.item package net.shadowfacts.phycon.item
import net.minecraft.block.BlockState
import net.minecraft.block.Blocks import net.minecraft.block.Blocks
import net.minecraft.entity.ItemEntity import net.minecraft.entity.ItemEntity
import net.minecraft.item.Item import net.minecraft.item.Item
@ -25,9 +26,29 @@ class ScrewdriverItem: Item(Settings()) {
} }
override fun useOnBlock(context: ItemUsageContext): ActionResult { override fun useOnBlock(context: ItemUsageContext): ActionResult {
if (context.player?.isSneaking != true) return ActionResult.PASS
val state = context.world.getBlockState(context.blockPos) val state = context.world.getBlockState(context.blockPos)
val block = state.block val block = state.block
if (block is DeviceBlock<*>) {
val newState =
when (block) {
is DeviceBlock<*> -> screwdriverDeviceBlock(context, state, block)
PhyBlocks.CABLE -> screwdriverCableBlock(context)
PhyBlocks.SWITCH -> screwdriverSwitchBlock(context)
else -> null
}
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
}
}
private fun screwdriverDeviceBlock(context: ItemUsageContext, state: BlockState, block: DeviceBlock<*>): BlockState? {
if (!context.world.isClient) { if (!context.world.isClient) {
val be = block.getBlockEntity(context.world, context.blockPos)!! val be = block.getBlockEntity(context.world, context.blockPos)!!
@ -46,29 +67,44 @@ class ScrewdriverItem: Item(Settings()) {
val entity = ItemEntity(context.world, context.blockPos.x.toDouble(), context.blockPos.y.toDouble(), context.blockPos.z.toDouble(), stack) val entity = ItemEntity(context.world, context.blockPos.x.toDouble(), context.blockPos.y.toDouble(), context.blockPos.z.toDouble(), stack)
context.world.spawnEntity(entity) context.world.spawnEntity(entity)
}
var newState = Blocks.AIR.defaultState return if (block is FaceDeviceBlock<*>) {
screwdriverFaceDeviceBlock(context, state, block)
} else {
Blocks.AIR.defaultState
}
}
if (block is FaceDeviceBlock<*>) { 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 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]]!! val faceShape = block.faceShapes[state[FaceDeviceBlock.FACING]]!!
// if we hit the face part of block, leave the cable behind // if we hit the face part of block, leave the cable behind
if (faceShape.boundingBox.containsInclusive(hitInsideBlock)) { if (faceShape.boundingBox.containsInclusive(hitInsideBlock)) {
newState = PhyBlocks.CABLE.getInitialState(context.world, context.blockPos) return PhyBlocks.CABLE.getInitialState(context.world, context.blockPos)
} else { } else {
if (!context.world.isClient) {
val cable = ItemEntity(context.world, context.blockPos.x.toDouble(), context.blockPos.y.toDouble(), context.blockPos.z.toDouble(), ItemStack(PhyBlocks.CABLE)) val cable = ItemEntity(context.world, context.blockPos.x.toDouble(), context.blockPos.y.toDouble(), context.blockPos.z.toDouble(), ItemStack(PhyBlocks.CABLE))
context.world.spawnEntity(cable) context.world.spawnEntity(cable)
} }
return Blocks.AIR.defaultState
}
} }
context.world.setBlockState(context.blockPos, newState, 3, 512) private fun screwdriverCableBlock(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.CABLE))
context.world.spawnEntity(entity)
}
return Blocks.AIR.defaultState
} }
context.world.playSound(context.player, context.blockPos, SoundEvents.BLOCK_METAL_PLACE, SoundCategory.BLOCKS, 0.8f, 0.65f) 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
}
return ActionResult.SUCCESS
} else {
return ActionResult.PASS
}
}
} }