Get initial GUI working

This commit is contained in:
Aly 2021-02-04 22:22:03 -08:00
parent 79ffcfbd85
commit de97633434
10 changed files with 287 additions and 32 deletions

View file

@ -3,12 +3,12 @@ org.gradle.jvmargs=-Xmx1G
# Fabric Properties # Fabric Properties
# Check these on https://modmuss50.me/fabric.html # Check these on https://modmuss50.me/fabric.html
minecraft_version=1.16.4 minecraft_version=1.16.5
yarn_mappings=1.16.4+build.7 yarn_mappings=1.16.5+build.3
loader_version=0.10.8 loader_version=0.11.1
#Fabric api #Fabric api
fabric_version=0.28.1+1.16 fabric_version=0.30.0+1.16
loom_version=0.5-SNAPSHOT loom_version=0.5-SNAPSHOT
@ -18,5 +18,5 @@ org.gradle.jvmargs=-Xmx1G
archives_base_name = monadmachines archives_base_name = monadmachines
# Kotlin # Kotlin
kotlin_version=1.4.21 kotlin_version=1.4.30
fabric_kotlin_version=1.4.21+build.1 fabric_kotlin_version=1.4.30+build.2

View file

@ -1,23 +1,42 @@
package tf.bug.monadmachines package tf.bug.monadmachines
import net.fabricmc.api.EnvType
import net.fabricmc.api.Environment
import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder
import net.fabricmc.fabric.api.client.screenhandler.v1.ScreenRegistry
import net.fabricmc.fabric.api.screenhandler.v1.ScreenHandlerRegistry
import net.minecraft.entity.player.PlayerInventory
import net.minecraft.item.ItemGroup import net.minecraft.item.ItemGroup
import net.minecraft.item.ItemStack import net.minecraft.item.ItemStack
import net.minecraft.text.Text
import net.minecraft.util.Identifier import net.minecraft.util.Identifier
import net.minecraft.util.registry.Registry import net.minecraft.util.registry.Registry
import tf.bug.monadmachines.block.ProgramWorkstationBlock
import tf.bug.monadmachines.block.ProgramWorkstationScreen
import tf.bug.monadmachines.block.ProgramWorkstationScreenHandler
import tf.bug.monadmachines.item.MonadMachinesManualItem
import tf.bug.monadmachines.item.ProgramCardItem
val MONADMACHINES_ITEMGROUP: ItemGroup = FabricItemGroupBuilder.build( val MONADMACHINES_ITEMGROUP: ItemGroup = FabricItemGroupBuilder.build(
Identifier("monadmachines", "group") Identifier("monadmachines", "group")
) { ItemStack(ProgramWorkstation) } ) { ItemStack(ProgramWorkstationBlock) }
@Suppress("unused") @Suppress("unused")
fun init() { fun init() {
Registry.register(Registry.ITEM, MonadMachinesManual.id, MonadMachinesManual) Registry.register(Registry.ITEM, MonadMachinesManualItem.id, MonadMachinesManualItem)
Registry.register(Registry.BLOCK, ProgramWorkstation.id, ProgramWorkstation)
Registry.register(Registry.ITEM, ProgramWorkstation.id, ProgramWorkstation.item)
Registry.register(Registry.ITEM, ProgramCard.id, ProgramCard)
Registry.register(Registry.BLOCK, ProgramWorkstationBlock.id, ProgramWorkstationBlock)
Registry.register(Registry.ITEM, ProgramWorkstationBlock.id, ProgramWorkstationBlock.item)
Registry.register(Registry.BLOCK_ENTITY_TYPE, ProgramWorkstationBlock.id, ProgramWorkstationBlock.entityType)
Registry.register(Registry.ITEM, ProgramCardItem.id, ProgramCardItem)
}
@Suppress("unused")
@Environment(EnvType.CLIENT)
fun clientInit() {
ScreenRegistry.register(ProgramWorkstationScreenHandler.type) {
handler: ProgramWorkstationScreenHandler, inventory: PlayerInventory, title: Text ->
ProgramWorkstationScreen(handler, inventory, title)
}
} }

View file

@ -1,15 +0,0 @@
package tf.bug.monadmachines
import net.fabricmc.fabric.api.`object`.builder.v1.block.FabricBlockSettings
import net.minecraft.block.Block
import net.minecraft.block.Material
import net.minecraft.item.BlockItem
import net.minecraft.item.Item
import net.minecraft.util.Identifier
object ProgramWorkstation : Block(FabricBlockSettings.of(Material.METAL)) {
val id = Identifier("monadmachines", "program_workstation")
val item = BlockItem(this, Item.Settings().group(MONADMACHINES_ITEMGROUP))
}

View file

@ -0,0 +1,80 @@
package tf.bug.monadmachines.block
import net.fabricmc.fabric.api.`object`.builder.v1.block.FabricBlockSettings
import net.minecraft.block.BlockRenderType
import net.minecraft.block.BlockState
import net.minecraft.block.BlockWithEntity
import net.minecraft.block.Material
import net.minecraft.block.entity.BlockEntity
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.item.BlockItem
import net.minecraft.item.Item
import net.minecraft.screen.ScreenHandler
import net.minecraft.util.ActionResult
import net.minecraft.util.Hand
import net.minecraft.util.Identifier
import net.minecraft.util.ItemScatterer
import net.minecraft.util.hit.BlockHitResult
import net.minecraft.util.math.BlockPos
import net.minecraft.world.BlockView
import net.minecraft.world.World
import tf.bug.monadmachines.MONADMACHINES_ITEMGROUP
object ProgramWorkstationBlock : BlockWithEntity(FabricBlockSettings.of(Material.METAL)) {
val id = Identifier("monadmachines", "program_workstation")
val item = BlockItem(this, Item.Settings().group(MONADMACHINES_ITEMGROUP))
val entityType = ProgramWorkstationBlockEntity.type
override fun createBlockEntity(world: BlockView?): BlockEntity {
return ProgramWorkstationBlockEntity()
}
override fun getRenderType(state: BlockState?): BlockRenderType {
return BlockRenderType.MODEL
}
override fun onUse(
state: BlockState,
world: World,
pos: BlockPos,
player: PlayerEntity?,
hand: Hand?,
hit: BlockHitResult?
): ActionResult {
if(!world.isClient) {
val screenHandlerFactory = state.createScreenHandlerFactory(world, pos)
if(screenHandlerFactory != null && player != null) {
player.openHandledScreen(screenHandlerFactory)
}
}
return ActionResult.SUCCESS
}
override fun onStateReplaced(
state: BlockState,
world: World,
pos: BlockPos,
newState: BlockState,
moved: Boolean
) {
if(state.block != newState.block) {
val blockEntity = world.getBlockEntity(pos)
if(blockEntity is ProgramWorkstationBlockEntity) {
ItemScatterer.spawn(world, pos, blockEntity)
world.updateComparators(pos, this)
}
super.onStateReplaced(state, world, pos, newState, moved)
}
}
override fun hasComparatorOutput(state: BlockState): Boolean {
return true
}
override fun getComparatorOutput(state: BlockState, world: World, pos: BlockPos): Int {
return ScreenHandler.calculateComparatorOutput(world.getBlockEntity(pos))
}
}

View file

@ -0,0 +1,93 @@
package tf.bug.monadmachines.block
import net.minecraft.block.BlockState
import net.minecraft.block.entity.BlockEntity
import net.minecraft.block.entity.BlockEntityType
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.entity.player.PlayerInventory
import net.minecraft.inventory.Inventories
import net.minecraft.inventory.Inventory
import net.minecraft.item.ItemStack
import net.minecraft.nbt.CompoundTag
import net.minecraft.screen.NamedScreenHandlerFactory
import net.minecraft.screen.ScreenHandler
import net.minecraft.text.Text
import net.minecraft.text.TranslatableText
import net.minecraft.util.collection.DefaultedList
import tf.bug.monadmachines.item.ProgramCardItem
class ProgramWorkstationBlockEntity : BlockEntity(type), NamedScreenHandlerFactory, Inventory {
private val card: DefaultedList<ItemStack> = DefaultedList.ofSize(1, ItemStack.EMPTY)
companion object {
val type: BlockEntityType<ProgramWorkstationBlockEntity> =
BlockEntityType.Builder.create({ ProgramWorkstationBlockEntity() }, ProgramWorkstationBlock).build(null)
}
override fun createMenu(syncId: Int, inv: PlayerInventory, player: PlayerEntity): ScreenHandler {
return ProgramWorkstationScreenHandler(syncId, inv, this)
}
override fun getDisplayName(): Text {
return TranslatableText(cachedState.block.translationKey)
}
override fun clear() {
card.clear()
}
override fun size(): Int {
return card.size
}
override fun isEmpty(): Boolean {
return card.isEmpty()
}
override fun getStack(slot: Int): ItemStack {
return card[slot]
}
override fun removeStack(slot: Int, amount: Int): ItemStack {
val result = Inventories.splitStack(card, slot, amount)
if (!result.isEmpty) {
markDirty()
}
return result
}
override fun removeStack(slot: Int): ItemStack {
return Inventories.removeStack(card, slot)
}
override fun setStack(slot: Int, stack: ItemStack) {
card[slot] = stack
if (stack.count > maxCountPerStack) {
stack.count = maxCountPerStack;
}
}
override fun canPlayerUse(player: PlayerEntity?): Boolean {
return true
}
override fun getMaxCountPerStack(): Int {
return 1
}
override fun isValid(slot: Int, stack: ItemStack): Boolean {
return slot == 0 && (stack.item is ProgramCardItem && stack.count <= 1)
}
override fun fromTag(state: BlockState, tag: CompoundTag) {
super.fromTag(state, tag)
Inventories.fromTag(tag, card)
}
override fun toTag(tag: CompoundTag): CompoundTag {
Inventories.toTag(tag, card)
return super.toTag(tag)
}
}

View file

@ -0,0 +1,26 @@
package tf.bug.monadmachines.block
import net.minecraft.client.gui.screen.ingame.HandledScreen
import net.minecraft.client.util.math.MatrixStack
import net.minecraft.entity.player.PlayerInventory
import net.minecraft.text.Text
class ProgramWorkstationScreen(handler: ProgramWorkstationScreenHandler, inventory: PlayerInventory, title: Text) : HandledScreen<ProgramWorkstationScreenHandler>(handler, inventory, title) {
override fun drawBackground(matrices: MatrixStack, delta: Float, mouseX: Int, mouseY: Int) {
// TODO
}
override fun render(matrices: MatrixStack, mouseX: Int, mouseY: Int, delta: Float) {
renderBackground(matrices)
super.render(matrices, mouseX, mouseY, delta)
drawMouseoverTooltip(matrices, mouseX, mouseY)
}
override fun init() {
super.init()
titleX = (backgroundWidth - textRenderer.getWidth(title)) / 2
}
}

View file

@ -0,0 +1,44 @@
package tf.bug.monadmachines.block
import net.fabricmc.fabric.api.screenhandler.v1.ScreenHandlerRegistry
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.entity.player.PlayerInventory
import net.minecraft.inventory.Inventory
import net.minecraft.inventory.SimpleInventory
import net.minecraft.screen.ScreenHandler
import net.minecraft.screen.ScreenHandlerType
import net.minecraft.screen.slot.Slot
class ProgramWorkstationScreenHandler(syncId: Int, playerInventory: PlayerInventory, inventory: Inventory) : ScreenHandler(type, syncId) {
private val inventory: Inventory
init {
checkSize(inventory, 1)
this.inventory = inventory
inventory.onOpen(playerInventory.player)
for(m in 0..2) {
for(l in 0..8) {
addSlot(Slot(playerInventory, l + m * 9 + 9, 8 + l * 18, 84 + m * 18))
}
}
}
companion object {
val type: ScreenHandlerType<ProgramWorkstationScreenHandler> =
ScreenHandlerRegistry.registerSimple(ProgramWorkstationBlock.id) { syncId: Int, playerInventory: PlayerInventory ->
ProgramWorkstationScreenHandler(
syncId,
playerInventory
)
}
}
constructor(syncId: Int, playerInventory: PlayerInventory) : this(syncId, playerInventory, SimpleInventory(1))
override fun canUse(player: PlayerEntity?): Boolean {
return this.inventory.canPlayerUse(player)
}
}

View file

@ -1,4 +1,4 @@
package tf.bug.monadmachines package tf.bug.monadmachines.item
import net.fabricmc.loader.api.FabricLoader import net.fabricmc.loader.api.FabricLoader
import net.minecraft.client.item.TooltipContext import net.minecraft.client.item.TooltipContext
@ -11,9 +11,10 @@ import net.minecraft.text.TextColor
import net.minecraft.text.TranslatableText import net.minecraft.text.TranslatableText
import net.minecraft.util.* import net.minecraft.util.*
import net.minecraft.world.World import net.minecraft.world.World
import tf.bug.monadmachines.MONADMACHINES_ITEMGROUP
import vazkii.patchouli.api.PatchouliAPI import vazkii.patchouli.api.PatchouliAPI
object MonadMachinesManual : Item(Settings().group(MONADMACHINES_ITEMGROUP)) { object MonadMachinesManualItem : Item(Settings().group(MONADMACHINES_ITEMGROUP)) {
val id = Identifier("monadmachines", "manual") val id = Identifier("monadmachines", "manual")

View file

@ -1,10 +1,11 @@
package tf.bug.monadmachines package tf.bug.monadmachines.item
import net.fabricmc.fabric.api.item.v1.FabricItemSettings import net.fabricmc.fabric.api.item.v1.FabricItemSettings
import net.minecraft.item.Item import net.minecraft.item.Item
import net.minecraft.util.Identifier import net.minecraft.util.Identifier
import tf.bug.monadmachines.MONADMACHINES_ITEMGROUP
object ProgramCard : Item(FabricItemSettings().group(MONADMACHINES_ITEMGROUP)) { object ProgramCardItem : Item(FabricItemSettings().group(MONADMACHINES_ITEMGROUP).maxCount(1)) {
val id = Identifier("monadmachines", "program_card") val id = Identifier("monadmachines", "program_card")

View file

@ -23,6 +23,12 @@
"adapter": "kotlin", "adapter": "kotlin",
"value": "tf.bug.monadmachines.MonadMachinesKt::init" "value": "tf.bug.monadmachines.MonadMachinesKt::init"
} }
],
"client": [
{
"adapter": "kotlin",
"value": "tf.bug.monadmachines.MonadMachinesKt::clientInit"
}
] ]
}, },
"mixins": [], "mixins": [],