2019-10-28 16:37:54 +00:00
|
|
|
package net.shadowfacts.phycon.item
|
|
|
|
|
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
|
|
|
|
import net.shadowfacts.phycon.PhysicalConnectivity
|
2021-03-03 23:43:49 +00:00
|
|
|
import net.shadowfacts.phycon.block.DeviceBlock
|
2019-10-28 16:37:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author shadowfacts
|
|
|
|
*/
|
|
|
|
class ScrewdriverItem: Item(Settings()) {
|
|
|
|
companion object {
|
|
|
|
val ID = Identifier(PhysicalConnectivity.MODID, "screwdriver")
|
|
|
|
}
|
2021-03-03 23:43:49 +00:00
|
|
|
|
|
|
|
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")
|
2021-03-04 03:29:19 +00:00
|
|
|
be.toTag(beTag)
|
2021-03-03 23:43:49 +00:00
|
|
|
// remove x, y, z entries for stacking purposes
|
|
|
|
beTag.remove("x")
|
|
|
|
beTag.remove("y")
|
|
|
|
beTag.remove("z")
|
|
|
|
|
|
|
|
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
|
|
|
context.world.setBlockState(context.blockPos, Blocks.AIR.defaultState, 3, 512)
|
2021-03-03 23:43:49 +00:00
|
|
|
}
|
|
|
|
|
2021-03-04 03:29:19 +00:00
|
|
|
context.world.playSound(context.player, context.blockPos, SoundEvents.BLOCK_METAL_PLACE, SoundCategory.BLOCKS, 0.8f, 0.65f)
|
|
|
|
|
2021-03-03 23:43:49 +00:00
|
|
|
return ActionResult.SUCCESS
|
|
|
|
} else {
|
|
|
|
return ActionResult.PASS
|
|
|
|
}
|
|
|
|
}
|
2019-10-28 16:37:54 +00:00
|
|
|
}
|