Update grill to use SlottedContainer
This commit is contained in:
parent
abf74e4a2b
commit
d2ff43946e
@ -24,7 +24,10 @@ import ru.dbotthepony.mc.otm.block.decorative.GrillBlock
|
||||
import ru.dbotthepony.mc.otm.block.entity.ExperienceStorage
|
||||
import ru.dbotthepony.mc.otm.block.entity.MatteryBlockEntity
|
||||
import ru.dbotthepony.mc.otm.config.MachinesConfig
|
||||
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
||||
import ru.dbotthepony.mc.otm.container.set
|
||||
import ru.dbotthepony.mc.otm.container.slotted.AutomationFilters
|
||||
import ru.dbotthepony.mc.otm.container.slotted.ContainerSlot
|
||||
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
|
||||
import ru.dbotthepony.mc.otm.core.TextComponent
|
||||
import ru.dbotthepony.mc.otm.core.get
|
||||
import ru.dbotthepony.mc.otm.core.isNotEmpty
|
||||
@ -38,43 +41,39 @@ import ru.dbotthepony.mc.otm.menu.decorative.GrillMenu
|
||||
import ru.dbotthepony.mc.otm.registry.game.MBlockEntities
|
||||
|
||||
class GrillBlockEntity(blockPos: BlockPos, blockState: BlockState) : MatteryBlockEntity(MBlockEntities.GRILL, blockPos, blockState), MenuProvider, IBlockWithCustomName {
|
||||
val fuelSlot = object : MatteryContainer(this@GrillBlockEntity::markDirtyFast, 1) {
|
||||
override fun getMaxStackSize(): Int {
|
||||
return 4
|
||||
}
|
||||
}
|
||||
val fuelSlot = SlottedContainer.simple(1, FUEL_SLOT_PROVIDER, ::markDirtyFast)
|
||||
|
||||
val inputSlots = object : MatteryContainer(this@GrillBlockEntity::markDirtyFast, SLOTS) {
|
||||
override fun getMaxStackSize(): Int {
|
||||
return 1
|
||||
}
|
||||
private inner class InputSlot(container: SlottedContainer, slot: Int) : ContainerSlot(container, slot) {
|
||||
override val maxStackSize: Int
|
||||
get() = 1
|
||||
|
||||
private fun clearSlot(slot: Int) {
|
||||
|
||||
private fun clearSlot() {
|
||||
inputProgress[slot] = 0
|
||||
outputs[slot] = null
|
||||
activeSlots.rem(slot)
|
||||
}
|
||||
|
||||
override fun setChanged(slot: Int, new: ItemStack, old: ItemStack) {
|
||||
super.setChanged(slot, new, old)
|
||||
override fun notifyChanged(old: ItemStack) {
|
||||
super.notifyChanged(old)
|
||||
|
||||
if (new.isEmpty || new.count > 1) {
|
||||
clearSlot(slot)
|
||||
if (item.isEmpty || item.count > 1) {
|
||||
clearSlot()
|
||||
} else {
|
||||
val level = level
|
||||
|
||||
if (level == null) {
|
||||
clearSlot(slot)
|
||||
clearSlot()
|
||||
return
|
||||
}
|
||||
|
||||
val input = SingleRecipeInput(new)
|
||||
val input = SingleRecipeInput(item)
|
||||
val result = level.recipeManager
|
||||
.byType(RecipeType.SMOKING)
|
||||
.firstOrNull { it.value.matches(input, level) }
|
||||
|
||||
if (result == null) {
|
||||
clearSlot(slot)
|
||||
clearSlot()
|
||||
} else {
|
||||
if (outputs[slot] != result.value)
|
||||
inputProgress[slot] = 0
|
||||
@ -86,10 +85,11 @@ class GrillBlockEntity(blockPos: BlockPos, blockState: BlockState) : MatteryBloc
|
||||
}
|
||||
}
|
||||
|
||||
val inputSlots = SlottedContainer.simple(SLOTS, ::InputSlot, ::markDirtyFast)
|
||||
override var customDisplayName: Component? = null
|
||||
|
||||
override fun createMenu(p_39954_: Int, p_39955_: Inventory, p_39956_: Player): AbstractContainerMenu {
|
||||
return GrillMenu(p_39954_, p_39955_, this)
|
||||
override fun createMenu(containerID: Int, inventory: Inventory, player: Player): AbstractContainerMenu {
|
||||
return GrillMenu(containerID, inventory, this)
|
||||
}
|
||||
|
||||
override fun getDisplayName(): Component {
|
||||
@ -205,5 +205,6 @@ class GrillBlockEntity(blockPos: BlockPos, blockState: BlockState) : MatteryBloc
|
||||
const val SLOTS = 6
|
||||
|
||||
private val progressCodec = Codec.INT.minRange(0)
|
||||
val FUEL_SLOT_PROVIDER = ContainerSlot.Simple(maxStackSize = 4, filter = AutomationFilters.CHEMICAL_FUEL)
|
||||
}
|
||||
}
|
||||
|
@ -21,6 +21,11 @@ import java.util.function.Predicate
|
||||
* and actual implementations of this interface are likely to provide efficient method implementations in place of derived/emulated ones.
|
||||
*/
|
||||
interface IEnhancedContainer : IContainer, RecipeInput, Iterable<ItemStack> {
|
||||
// provide non-ambiguous "get" operator
|
||||
operator fun get(slot: Int): ItemStack {
|
||||
return getItem(slot)
|
||||
}
|
||||
|
||||
fun containerSlot(slot: Int): IContainerSlot {
|
||||
return IContainerSlot.Simple(slot, this)
|
||||
}
|
||||
|
@ -392,6 +392,17 @@ class SlottedContainer(
|
||||
.build()
|
||||
}
|
||||
|
||||
fun simple(size: Int, provider: SlotProvider<*>): SlottedContainer {
|
||||
return Builder().add(size, provider).build()
|
||||
}
|
||||
|
||||
fun simple(size: Int, provider: SlotProvider<*>, listener: Runnable): SlottedContainer {
|
||||
return Builder()
|
||||
.add(size, provider)
|
||||
.onChanged(listener)
|
||||
.build()
|
||||
}
|
||||
|
||||
fun filtered(size: Int): SlottedContainer {
|
||||
return Builder().add(size, ::FilteredContainerSlot).build()
|
||||
}
|
||||
|
@ -11,6 +11,8 @@ import net.minecraft.world.item.crafting.SingleRecipeInput
|
||||
import ru.dbotthepony.kommons.util.getValue
|
||||
import ru.dbotthepony.mc.otm.block.entity.decorative.GrillBlockEntity
|
||||
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
||||
import ru.dbotthepony.mc.otm.container.slotted.ContainerSlot
|
||||
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
|
||||
import ru.dbotthepony.mc.otm.core.immutableList
|
||||
import ru.dbotthepony.mc.otm.menu.ChemicalFuelMenuSlot
|
||||
import ru.dbotthepony.mc.otm.menu.MatteryMenu
|
||||
@ -26,17 +28,9 @@ class GrillMenu(
|
||||
inventory: Inventory,
|
||||
tile: GrillBlockEntity? = null
|
||||
) : MatteryMenu(MMenus.GRILL, containerId, inventory, tile) {
|
||||
val fuelSlot = makeSlots(tile?.fuelSlot ?: object : MatteryContainer(1) {
|
||||
override fun getMaxStackSize(): Int {
|
||||
return 4
|
||||
}
|
||||
}, ::ChemicalFuelMenuSlot)
|
||||
val fuelSlot = makeSlots(tile?.fuelSlot ?: SlottedContainer.simple(1, GrillBlockEntity.FUEL_SLOT_PROVIDER), ::ChemicalFuelMenuSlot)
|
||||
|
||||
val inputSlots = makeSlots<Container, MatteryMenuSlot>(tile?.inputSlots ?: object : MatteryContainer(GrillBlockEntity.SLOTS) {
|
||||
override fun getMaxStackSize(): Int {
|
||||
return 1
|
||||
}
|
||||
}) { c, i ->
|
||||
val inputSlots = makeSlots<Container, MatteryMenuSlot>(tile?.inputSlots ?: SlottedContainer.simple(GrillBlockEntity.SLOTS, ContainerSlot.Simple(maxStackSize = 1))) { c, i ->
|
||||
object : MatteryMenuSlot(c, i) {
|
||||
override fun onTake(p_150645_: Player, p_150646_: ItemStack) {
|
||||
super.onTake(p_150645_, p_150646_)
|
||||
|
Loading…
Reference in New Issue
Block a user