94 lines
2.7 KiB
Kotlin
94 lines
2.7 KiB
Kotlin
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)
|
|
}
|
|
|
|
}
|