Add Console item

This commit is contained in:
Shadowfacts 2021-02-14 12:07:05 -05:00
parent 446a2a42c3
commit 584d8cd04e
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
6 changed files with 96 additions and 1 deletions

View File

@ -4,6 +4,7 @@ import net.minecraft.item.BlockItem
import net.minecraft.item.Item
import net.minecraft.util.Identifier
import net.minecraft.util.registry.Registry
import net.shadowfacts.phycon.item.ConsoleItem
import net.shadowfacts.phycon.item.ScrewdriverItem
import net.shadowfacts.phycon.network.block.cable.CableBlock
import net.shadowfacts.phycon.network.block.netinterface.InterfaceBlock
@ -25,6 +26,7 @@ object PhyItems {
val DEST = BlockItem(PhyBlocks.DEST , Item.Settings())
val SCREWDRIVER = ScrewdriverItem()
val CONSOLE = ConsoleItem()
fun init() {
register(InterfaceBlock.ID, INTERFACE)
@ -35,6 +37,7 @@ object PhyItems {
register(DestBlock.ID, DEST)
register(ScrewdriverItem.ID, SCREWDRIVER)
register(ConsoleItem.ID, CONSOLE)
}
private fun register(id: Identifier, item: Item) {

View File

@ -0,0 +1,47 @@
package net.shadowfacts.phycon.item
import net.fabricmc.api.EnvType
import net.fabricmc.api.Environment
import net.minecraft.client.MinecraftClient
import net.minecraft.item.Item
import net.minecraft.item.ItemUsageContext
import net.minecraft.text.LiteralText
import net.minecraft.util.ActionResult
import net.minecraft.util.Identifier
import net.shadowfacts.phycon.PhysicalConnectivity
import net.shadowfacts.phycon.network.DeviceBlock
import net.shadowfacts.phycon.network.DeviceBlockEntity
import net.shadowfacts.phycon.screen.DeviceConsoleScreen
/**
* @author shadowfacts
*/
class ConsoleItem: Item(Settings()) {
companion object {
val ID = Identifier(PhysicalConnectivity.MODID, "console")
}
override fun useOnBlock(context: ItemUsageContext): ActionResult {
val state = context.world.getBlockState(context.blockPos)
val block = state.block
if (block is DeviceBlock<*>) {
val be = block.getBlockEntity(context.world, context.blockPos)
if (be != null) {
if (context.world.isClient) {
this.openScreen(be)
} else {
be.sync()
}
return ActionResult.SUCCESS
}
}
return ActionResult.PASS
}
@Environment(EnvType.CLIENT)
private fun openScreen(be: DeviceBlockEntity) {
val screen = DeviceConsoleScreen(be)
MinecraftClient.getInstance().openScreen(screen)
}
}

View File

@ -0,0 +1,38 @@
package net.shadowfacts.phycon.screen
import net.minecraft.client.gui.screen.Screen
import net.minecraft.client.util.math.MatrixStack
import net.minecraft.text.TranslatableText
import net.shadowfacts.phycon.network.DeviceBlockEntity
import org.lwjgl.glfw.GLFW
/**
* @author shadowfacts
*/
class DeviceConsoleScreen(
val device: DeviceBlockEntity,
): Screen(TranslatableText("item.phycon.onsole")) {
override fun init() {
super.init()
}
override fun isPauseScreen() = false
override fun keyPressed(key: Int, j: Int, k: Int): Boolean {
if (key == GLFW.GLFW_KEY_E) {
onClose();
return true;
}
return super.keyPressed(key, j, k)
}
override fun render(matrixStack: MatrixStack, mouseX: Int, mouseY: Int, delta: Float) {
renderBackground(matrixStack)
super.render(matrixStack, mouseX, mouseY, delta)
drawCenteredString(matrixStack, textRenderer, device.macAddress.toString(), width / 2, height / 2 - 5, 0xffffff)
drawCenteredString(matrixStack, textRenderer, device.ipAddress.toString(), width / 2, height / 2 + 5, 0xffffff)
}
}

View File

@ -4,5 +4,6 @@
"block.phycon.terminal": "Terminal",
"block.phycon.cable": "Cable",
"item.phycon.screwdriver": "Screwdriver"
"item.phycon.screwdriver": "Screwdriver",
"item.phycon.console": "Console"
}

View File

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 913 B