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

47 lines
1.3 KiB
Kotlin

package net.shadowfacts.phycon.item
import net.minecraft.entity.ItemEntity
import net.minecraft.item.Item
import net.minecraft.item.ItemStack
import net.minecraft.item.ItemUsageContext
import net.minecraft.util.ActionResult
import net.minecraft.util.Identifier
import net.shadowfacts.phycon.PhysicalConnectivity
import net.shadowfacts.phycon.block.DeviceBlock
/**
* @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")
// remove x, y, z entries for stacking purposes
beTag.remove("x")
beTag.remove("y")
beTag.remove("z")
be.toTag(beTag)
val entity = ItemEntity(context.world, context.blockPos.x.toDouble(), context.blockPos.y.toDouble(), context.blockPos.z.toDouble(), stack)
context.world.spawnEntity(entity)
context.world.breakBlock(context.blockPos, false)
}
return ActionResult.SUCCESS
} else {
return ActionResult.PASS
}
}
}