component slots

This commit is contained in:
2026-04-19 12:50:13 +02:00
parent d3fb4a65cb
commit 74d1015bcd
29 changed files with 277 additions and 11 deletions

View File

@@ -22,6 +22,7 @@ import org.neoflock.neocomputers.block.NodeBlock
import org.neoflock.neocomputers.block.NodeBlockEntity
import org.neoflock.neocomputers.block.NodeSynchronizer
import org.neoflock.neocomputers.gui.render.ScreenRenderer
import org.neoflock.neocomputers.gui.widget.ComponentRoles
import org.neoflock.neocomputers.item.Items
import org.neoflock.neocomputers.item.Tabs
import org.neoflock.neocomputers.network.Networking
@@ -46,6 +47,7 @@ object NeoComputers {
BlockEntities.registerPowerBlocks()
Menus.MENUS.register()
Tabs.TABS.register()
ComponentRoles.mapDefaultTextures()
// i dont know why architectury wants two lambdas but whatever
EnvExecutor.runInEnv(Env.CLIENT) {{
ClientLifecycleEvent.CLIENT_SETUP.register {

View File

@@ -1,5 +1,6 @@
package org.neoflock.neocomputers.gui.menu;
import net.minecraft.world.Container
import net.minecraft.world.SimpleContainer
import net.minecraft.world.entity.player.Inventory
import org.neoflock.neocomputers.gui.menu.Menus;
@@ -7,18 +8,32 @@ import net.minecraft.world.entity.player.Player
import net.minecraft.world.inventory.AbstractContainerMenu
import net.minecraft.world.inventory.MenuType
import net.minecraft.world.item.ItemStack
import org.neoflock.neocomputers.gui.widget.ComponentRoles
import org.neoflock.neocomputers.gui.widget.ComponentSlot
import org.neoflock.neocomputers.gui.widget.ComponentSlotRequirement
import org.neoflock.neocomputers.gui.widget.DynamicSlot
import org.neoflock.neocomputers.utils.GenericContainerMenu
class CaseMenu : GenericContainerMenu {
constructor(i: Int, inv: Inventory) : this(i, inv, SimpleContainer(7))
constructor(i: Int, inv: Inventory) : super(Menus.CASE_MENU.get(), i, SimpleContainer(10)) {
open val eepromRequirement = ComponentSlotRequirement(1, ComponentRoles.FIRMWARE)
open val slotRequirements = listOf(
listOf(ComponentSlotRequirement(1, ComponentRoles.CARD), ComponentSlotRequirement(1, ComponentRoles.CARD)),
listOf(ComponentSlotRequirement(1, ComponentRoles.COMPUTE), ComponentSlotRequirement(1, ComponentRoles.MEMORY), ComponentSlotRequirement(1, ComponentRoles.MEMORY)),
listOf(ComponentSlotRequirement(1, ComponentRoles.STORAGE)),
)
constructor(i: Int, inv: Inventory, container: Container) : super(Menus.CASE_MENU.get(), i, container) {
this.addInventorySlots(inv, 8, 84)
for (col in 0..2) {
for (row in 0..2) {
var i = col*3+row
this.addSlot(DynamicSlot(this.container!!, i, 98+(col*22), 18*(row+1)-2))
this.addSlot(ComponentSlot(this.container!!, 0, 20, 34, eepromRequirement))
var i = 1
for ((col, slotCol) in slotRequirements.withIndex()) {
for ((row, slotReq) in slotCol.withIndex()) {
this.addSlot(ComponentSlot(this.container!!, i, 98+(col*22), 18*(row+1)-2, slotReq))
i++
}
}
// for (int col=1; col<4; col++) {
@@ -30,7 +45,4 @@ class CaseMenu : GenericContainerMenu {
// }
// }
}
override fun stillValid(player: Player) = true // TODO: implement this properly
override fun quickMoveStack(player: Player, i: Int): ItemStack = ItemStack.EMPTY // there's no container here anyways
}

View File

@@ -16,6 +16,8 @@ import org.neoflock.neocomputers.utils.GenericContainerScreen
class CaseScreen : GenericContainerScreen<CaseMenu> {
private val PCB: ResourceLocation = ResourceLocation.fromNamespaceAndPath(NeoComputers.MODID, "textures/gui/computer.png")
override fun shouldCenterTitle(): Boolean = false
constructor(abstractContainerMenu: CaseMenu, inventory: Inventory, component: Component) : super(abstractContainerMenu, inventory, component)
override fun renderBg(guiGraphics: GuiGraphics, f: Float, i: Int, j: Int) {

View File

@@ -0,0 +1,126 @@
package org.neoflock.neocomputers.gui.widget
import com.mojang.blaze3d.systems.RenderSystem
import net.minecraft.client.gui.GuiGraphics
import net.minecraft.client.renderer.GameRenderer
import net.minecraft.resources.ResourceLocation
import net.minecraft.world.Container
import net.minecraft.world.entity.player.Player
import net.minecraft.world.item.Item
import net.minecraft.world.item.ItemStack
import net.minecraft.world.level.ItemLike
import org.neoflock.neocomputers.NeoComputers
import org.neoflock.neocomputers.item.ComponentItem
// Sort of a mis-nomer, does not need to be associated with components specifically
object ComponentRoles {
// For card slots
val CARD = "card"
// For memory slots
val MEMORY = "memory"
// For storage slots, aside from EEPROMs and floppys
val STORAGE = "storage"
// For floppy drive slots
val FLOPPY = "floppy"
// For EEPROM slots
val FIRMWARE = "firmware"
// For CPU slot
val COMPUTE = "compute"
// For component bus
val BUS = "bus"
val TOOL = "tool"
val UPGRADE = "upgrade"
val CONTAINER = "container"
val TABLET = "tablet"
val RACK_MOUNTABLE = "rack"
val MISSING_ROLE_TEXTURE = ResourceLocation.fromNamespaceAndPath(NeoComputers.MODID, "textures/gui/slots/na.png")
val textureMap = mutableMapOf<String, ResourceLocation>()
fun mapTexture(role: String, texture: ResourceLocation) {
textureMap[role] = texture
}
fun getTextureFor(role: String) = textureMap[role] ?: MISSING_ROLE_TEXTURE
fun mapDefaultTextures() {
val core = mapOf(
CARD to "card",
BUS to "component_bus",
CONTAINER to "container",
COMPUTE to "cpu",
FIRMWARE to "eeprom",
FLOPPY to "floppy",
STORAGE to "hdd",
MEMORY to "memory",
TABLET to "tablet",
TOOL to "tool",
UPGRADE to "upgrade",
RACK_MOUNTABLE to "rack_mountable",
)
for((role, tex) in core) {
mapTexture(role, ResourceLocation.fromNamespaceAndPath(NeoComputers.MODID, "textures/gui/slots/$tex.png"))
}
}
}
data class ComponentSlotRequirement(val tier: Int, val role: String) {
fun allowsItemStack(itemStack: ItemStack): Boolean {
val item = itemStack.item
if(item !is ComponentItem) return false
if(tier > 0 && item.getComponentTier(itemStack) > tier) return false
return item.getComponentRoles(itemStack).contains(role)
}
}
// Tier 0 allows ALL tiers, making it completely untiered.
// Role determines what the role is.
class ComponentSlot(container: Container, slot: Int, x: Int, y: Int, val requirement: ComponentSlotRequirement): DynamicSlot(container, slot, x, y) {
override fun draw(graphics: GuiGraphics, relX: Int, relY: Int, mouseX: Int, mouseY: Int) {
super.draw(graphics, relX, relY, mouseX, mouseY)
if(!hasItem()) {
RenderSystem.enableBlend()
RenderSystem.setShaderTexture(0, ComponentRoles.getTextureFor(requirement.role))
RenderSystem.setShader { GameRenderer.getPositionTexShader() }
drawQuad(relX + x - 1, relY + y - 1, 18, 18, 0F, 0F, 15F, 15F)
if (requirement.tier > 0) {
RenderSystem.setShaderTexture(
0,
ResourceLocation.fromNamespaceAndPath(
NeoComputers.MODID,
"textures/gui/slots/tier${requirement.tier - 1}.png"
)
)
RenderSystem.setShader { GameRenderer.getPositionTexShader() }
drawQuad(relX + x - 1, relY + y - 1, 18, 18, 0F, 0F, 15F, 15F)
}
RenderSystem.disableBlend()
}
}
override fun mayPlace(itemStack: ItemStack): Boolean {
if(!requirement.allowsItemStack(itemStack)) return false
return super.mayPlace(itemStack)
}
override fun set(itemStack: ItemStack) {
super.set(itemStack)
val item = itemStack.item
if(item is ComponentItem) {
item.whenComponentPlaced(itemStack, requirement.role)
}
}
override fun onTake(player: Player, itemStack: ItemStack) {
val item = itemStack.item
if(item is ComponentItem) {
item.whenComponentTaken(itemStack, requirement.role)
}
super.onTake(player, itemStack)
}
override fun getMaxStackSize(): Int = 1
override fun getMaxStackSize(itemStack: ItemStack): Int = 1
}

View File

@@ -18,7 +18,7 @@ open class DynamicSlot(container: Container, slot: Int, x: Int, y: Int) : Slot(c
val BACKGROUND: ResourceLocation = ResourceLocation.fromNamespaceAndPath(NeoComputers.MODID, "textures/gui/slots/slot.png")
fun draw(graphics: GuiGraphics, relX: Int, relY: Int, mouseX: Int, mouseY: Int) {
open fun draw(graphics: GuiGraphics, relX: Int, relY: Int, mouseX: Int, mouseY: Int) {
RenderSystem.enableBlend() // background
RenderSystem.setShaderTexture(0, BACKGROUND)
RenderSystem.setShader { GameRenderer.getPositionTexShader() }
@@ -26,7 +26,7 @@ open class DynamicSlot(container: Container, slot: Int, x: Int, y: Int) : Slot(c
RenderSystem.disableBlend()
}
private fun drawQuad(x: Int, y: Int, width: Int, height: Int, u1: Float, v1: Float, u2: Float, v2: Float) {
fun drawQuad(x: Int, y: Int, width: Int, height: Int, u1: Float, v1: Float, u2: Float, v2: Float) {
var t: Tesselator = Tesselator.getInstance()
var builder: BufferBuilder = t.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX)

View File

@@ -0,0 +1,20 @@
package org.neoflock.neocomputers.item
import net.minecraft.world.item.ItemStack
import org.neoflock.neocomputers.network.Networking
// need not necessarily be just a component, can be upgrades as well
interface ComponentItem {
fun getComponentRoles(itemStack: ItemStack): Set<String>
fun getComponentTier(itemStack: ItemStack): Int
// Get machine properties they can influence
fun getMemoryCapacity(itemStack: ItemStack): Int
fun getComponentCapacity(itemStack: ItemStack): Int
fun whenComponentPlaced(itemStack: ItemStack, newRole: String)
fun whenComponentTaken(itemStack: ItemStack, previousRole: String)
// To node, if applicable
fun toComponentNode(itemStack: ItemStack): Networking.Node?
}

View File

@@ -7,4 +7,11 @@ import org.neoflock.neocomputers.NeoComputers
object Items {
val ITEMS: DeferredRegister<Item> = DeferredRegister.create(NeoComputers.MODID, Registries.ITEM)
val MEM0 = ITEMS.register("memory0") { MemoryTier1() }
val MEM1 = ITEMS.register("memory1") { MemoryTier1_5() }
val MEM2 = ITEMS.register("memory2") { MemoryTier2() }
val MEM3 = ITEMS.register("memory3") { MemoryTier2_5() }
val MEM4 = ITEMS.register("memory4") { MemoryTier3() }
val MEM5 = ITEMS.register("memory5") { MemoryTier3_5() }
}

View File

@@ -0,0 +1,45 @@
package org.neoflock.neocomputers.item
import net.minecraft.network.chat.Component
import net.minecraft.world.item.Item
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.TooltipFlag
import org.neoflock.neocomputers.gui.widget.ComponentRoles
import org.neoflock.neocomputers.network.Networking
import org.neoflock.neocomputers.utils.Formatting
open class MemoryItem(val tier: Int, val capacity: Int): Item(Item.Properties().`arch$tab`(Tabs.TAB)), ComponentItem {
override fun getComponentRoles(itemStack: ItemStack) = setOf(ComponentRoles.MEMORY)
override fun getComponentTier(itemStack: ItemStack): Int = tier
override fun getMemoryCapacity(itemStack: ItemStack): Int = capacity
override fun getComponentCapacity(itemStack: ItemStack): Int = 0
override fun whenComponentPlaced(itemStack: ItemStack, newRole: String) {}
override fun whenComponentTaken(itemStack: ItemStack, previousRole: String) {}
// no node for memory
override fun toComponentNode(itemStack: ItemStack): Networking.Node? = null
override fun appendHoverText(
itemStack: ItemStack,
tooltipContext: TooltipContext,
list: MutableList<Component?>,
tooltipFlag: TooltipFlag
) {
if(tooltipFlag.isAdvanced) {
list.addLast(Component.translatable("neocomputers.memory.capacity", Formatting.formatMemory(capacity.toLong())))
}
super.appendHoverText(itemStack, tooltipContext, list, tooltipFlag)
}
}
class MemoryTier1(): MemoryItem(1, 192 shl 10)
class MemoryTier1_5(): MemoryItem(1, 256 shl 10)
class MemoryTier2(): MemoryItem(2, 384 shl 10)
class MemoryTier2_5(): MemoryItem(2, 512 shl 10)
class MemoryTier3(): MemoryItem(3, 768 shl 10)
class MemoryTier3_5(): MemoryItem(3, 1 shl 20)

View File

@@ -0,0 +1,15 @@
package org.neoflock.neocomputers.utils
object Formatting {
fun formatMemory(size: Long, spacing: String = " "): String {
var unit = 0
val units = listOf("B", "KiB", "MiB", "GiB", "TiB", "PiB")
var num = size.toDouble()
while(unit < units.lastIndex && num >= 1024) {
num /= 1024
unit++
}
num = (num * 100).toInt().toDouble() / 100
return "$num$spacing${units[unit]}"
}
}