Add using screwdriver to dismantle devices

This commit is contained in:
Shadowfacts 2021-03-03 18:43:49 -05:00
parent ca090d0924
commit 5b314120a7
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
6 changed files with 205 additions and 8 deletions

View File

@ -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()

View File

@ -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<Text>, 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()
}
+")"
})
}
}
}

View File

@ -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
}
}
}

View File

@ -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<Text> = mutableListOf(),
private val styles: Deque<Style> = LinkedList()
) {
fun toText(): MutableText {
val text = LiteralText("")
texts.forEach(text::append)
return text
}
operator fun String.unaryPlus() {
+LiteralText(this)
}
operator fun MutableText.unaryPlus() {
if (styles.isNotEmpty()) {
texts.add(this.setStyle(styles.peek()))
} else {
texts.add(this)
}
}
fun translate(key: String, vararg parameters: Any): TranslatableText {
return TranslatableText(key, *parameters)
}
fun withStyle(style: Style, build: TextContext.() -> Unit) {
styles.push(style)
build()
styles.pop()
}
fun withStyle(style: Style.() -> Style, build: TextContext.() -> Unit) {
withStyle(Style.EMPTY.style(), build)
}
fun withStyle(formatting: Formatting, build: TextContext.() -> Unit) {
withStyle(Style.EMPTY.withFormatting(formatting), build)
}
val bold = Formatting.BOLD
fun Style.bold(): Style = withBold(true)
val italic = Formatting.ITALIC
fun Style.italic(): Style = withItalic(true)
val strikethrough = Formatting.STRIKETHROUGH
fun Style.strikethrough(): Style = withFormatting(strikethrough)
val underline = Formatting.UNDERLINE
fun Style.underline(): Style = withFormatting(underline)
val obfuscated = Formatting.OBFUSCATED
fun Style.obfuscated(): Style = withFormatting(obfuscated)
val black = Formatting.BLACK
fun Style.black(): Style = withFormatting(black)
val darkBlue = Formatting.DARK_BLUE
fun Style.darkBlue(): Style = withFormatting(darkBlue)
val darkGreen = Formatting.DARK_GREEN
fun Style.darkGreen(): Style = withFormatting(darkGreen)
val darkAqua = Formatting.DARK_AQUA
fun Style.darkAqua(): Style = withFormatting(darkAqua)
val darkRed = Formatting.DARK_RED
fun Style.darkRed(): Style = withFormatting(darkRed)
val darkPurple = Formatting.DARK_PURPLE
fun Style.darkPurple(): Style = withFormatting(darkPurple)
val gold = Formatting.GOLD
fun Style.gold(): Style = withFormatting(gold)
val gray = Formatting.GRAY
fun Style.gray(): Style = withFormatting(gray)
val darkGray = Formatting.DARK_GRAY
fun Style.darkGray(): Style = withFormatting(darkGray)
val blue = Formatting.BLUE
fun Style.blue(): Style = withFormatting(blue)
val green = Formatting.GREEN
fun Style.green(): Style = withFormatting(green)
val aqua = Formatting.RED
fun Style.aqua(): Style = withFormatting(aqua)
val red = Formatting.RED
fun Style.red(): Style = withFormatting(red)
val lightPurple = Formatting.LIGHT_PURPLE
fun Style.lightPurple(): Style = withFormatting(lightPurple)
val yellow = Formatting.YELLOW
fun Style.yellow(): Style = withFormatting(yellow)
val white = Formatting.WHITE
fun Style.white(): Style = withFormatting(white)
}

View File

@ -30,5 +30,8 @@
"gui.phycon.activation_mode.managed": "Managed",
"gui.phycon.emitter.count": "%d Item(s)",
"gui.phycon.miner_mode.automatic": "Automatic",
"gui.phycon.miner_mode.on_demand": "On Demand"
"gui.phycon.miner_mode.on_demand": "On Demand",
"tooltip.phycon.device.configured": "Configured",
"tooltip.phycon.device.ip": "IP: "
}

View File

@ -0,0 +1,6 @@
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "phycon:item/screwdriver"
}
}