PhysicalConnectivity/src/main/kotlin/net/shadowfacts/phycon/item/ScrewdriverItem.kt

75 lines
2.6 KiB
Kotlin
Raw Normal View History

2019-10-28 16:37:54 +00:00
package net.shadowfacts.phycon.item
import net.minecraft.block.Blocks
import net.minecraft.entity.ItemEntity
2019-10-28 16:37:54 +00:00
import net.minecraft.item.Item
import net.minecraft.item.ItemStack
import net.minecraft.item.ItemUsageContext
import net.minecraft.sound.SoundCategory
import net.minecraft.sound.SoundEvents
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
import net.shadowfacts.phycon.block.DeviceBlock
2021-03-07 15:38:22 +00:00
import net.shadowfacts.phycon.block.FaceDeviceBlock
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
*/
class ScrewdriverItem: Item(Settings()) {
companion object {
val ID = Identifier(PhysicalConnectivity.MODID, "screwdriver")
}
override fun useOnBlock(context: ItemUsageContext): ActionResult {
val state = context.world.getBlockState(context.blockPos)
val block = state.block
if (block is DeviceBlock<*>) {
if (!context.world.isClient) {
val be = block.getBlockEntity(context.world, context.blockPos)!!
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")
if (block === PhyBlocks.TERMINAL) {
// remove the terminal's internal buffer since it drops its items
beTag.remove("InternalBuffer")
}
val entity = ItemEntity(context.world, context.blockPos.x.toDouble(), context.blockPos.y.toDouble(), context.blockPos.z.toDouble(), stack)
context.world.spawnEntity(entity)
2021-03-07 15:38:22 +00:00
var newState = Blocks.AIR.defaultState
if (block is FaceDeviceBlock<*>) {
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)) {
newState = PhyBlocks.CABLE.getInitialState(context.world, context.blockPos)
} else {
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.setBlockState(context.blockPos, newState, 3, 512)
}
context.world.playSound(context.player, context.blockPos, SoundEvents.BLOCK_METAL_PLACE, SoundCategory.BLOCKS, 0.8f, 0.65f)
return ActionResult.SUCCESS
} else {
return ActionResult.PASS
}
}
2019-10-28 16:37:54 +00:00
}