diff --git a/src/main/kotlin/net/shadowfacts/phycon/init/PhyItems.kt b/src/main/kotlin/net/shadowfacts/phycon/init/PhyItems.kt index 760dc8a..7f89cc3 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/init/PhyItems.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/init/PhyItems.kt @@ -15,21 +15,22 @@ import net.shadowfacts.phycon.block.netswitch.SwitchBlock import net.shadowfacts.phycon.block.redstone_controller.RedstoneControllerBlock import net.shadowfacts.phycon.block.redstone_emitter.RedstoneEmitterBlock import net.shadowfacts.phycon.block.terminal.TerminalBlock +import net.shadowfacts.phycon.item.DeviceBlockItem /** * @author shadowfacts */ object PhyItems { - val INTERFACE = BlockItem(PhyBlocks.INTERFACE, Item.Settings()) - val TERMINAL = BlockItem(PhyBlocks.TERMINAL, Item.Settings()) + val INTERFACE = DeviceBlockItem(PhyBlocks.INTERFACE, Item.Settings()) + val TERMINAL = DeviceBlockItem(PhyBlocks.TERMINAL, Item.Settings()) val SWITCH = BlockItem(PhyBlocks.SWITCH, Item.Settings()) val CABLE = BlockItem(PhyBlocks.CABLE, Item.Settings()) - val EXTRACTOR = BlockItem(PhyBlocks.EXTRACTOR, Item.Settings()) - val INSERTER = BlockItem(PhyBlocks.INSERTER, Item.Settings()) - val MINER = BlockItem(PhyBlocks.MINER, Item.Settings()) - val REDSTONE_CONTROLLER = BlockItem(PhyBlocks.REDSTONE_CONTROLLER, Item.Settings()) - val REDSTONE_EMITTER = BlockItem(PhyBlocks.REDSTONE_EMITTER, Item.Settings()) + val EXTRACTOR = DeviceBlockItem(PhyBlocks.EXTRACTOR, Item.Settings()) + val INSERTER = DeviceBlockItem(PhyBlocks.INSERTER, Item.Settings()) + val MINER = DeviceBlockItem(PhyBlocks.MINER, Item.Settings()) + val REDSTONE_CONTROLLER = DeviceBlockItem(PhyBlocks.REDSTONE_CONTROLLER, Item.Settings()) + val REDSTONE_EMITTER = DeviceBlockItem(PhyBlocks.REDSTONE_EMITTER, Item.Settings()) val SCREWDRIVER = ScrewdriverItem() val CONSOLE = ConsoleItem() diff --git a/src/main/kotlin/net/shadowfacts/phycon/item/DeviceBlockItem.kt b/src/main/kotlin/net/shadowfacts/phycon/item/DeviceBlockItem.kt new file mode 100644 index 0000000..509197f --- /dev/null +++ b/src/main/kotlin/net/shadowfacts/phycon/item/DeviceBlockItem.kt @@ -0,0 +1,35 @@ +package net.shadowfacts.phycon.item + +import net.minecraft.client.item.TooltipContext +import net.minecraft.item.BlockItem +import net.minecraft.item.ItemStack +import net.minecraft.text.Text +import net.minecraft.world.World +import net.shadowfacts.phycon.api.util.IPAddress +import net.shadowfacts.phycon.block.DeviceBlock +import net.shadowfacts.phycon.util.text + +/** + * @author shadowfacts + */ +class DeviceBlockItem(block: DeviceBlock<*>, settings: Settings = Settings()): BlockItem(block, settings) { + + override fun appendTooltip(stack: ItemStack, world: World?, list: MutableList, context: TooltipContext) { + val beTag = stack.getSubTag("BlockEntityTag") + if (beTag != null) { + val ip = IPAddress(beTag.getInt("IPAddress")) + list.add(text { + withStyle(lightPurple) { + +translate("tooltip.phycon.device.configured") + } + +" (" + +translate("tooltip.phycon.device.ip") + withStyle(darkPurple) { + +ip.toString() + } + +")" + }) + } + } + +} diff --git a/src/main/kotlin/net/shadowfacts/phycon/item/ScrewdriverItem.kt b/src/main/kotlin/net/shadowfacts/phycon/item/ScrewdriverItem.kt index 87510b5..d12e16a 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/item/ScrewdriverItem.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/item/ScrewdriverItem.kt @@ -1,8 +1,13 @@ 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 @@ -11,4 +16,31 @@ 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 + } + } } diff --git a/src/main/kotlin/net/shadowfacts/phycon/util/textdsl.kt b/src/main/kotlin/net/shadowfacts/phycon/util/textdsl.kt new file mode 100644 index 0000000..e9c8e9b --- /dev/null +++ b/src/main/kotlin/net/shadowfacts/phycon/util/textdsl.kt @@ -0,0 +1,120 @@ +package net.shadowfacts.phycon.util + +import net.minecraft.text.* +import net.minecraft.util.Formatting +import java.util.* + +/** + * @author shadowfacts + */ +fun text(build: TextContext.() -> Unit): Text { + val context = TextContext() + context.build() + return context.toText() +} + +class TextContext( + private val texts: MutableList = mutableListOf(), + private val styles: Deque