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.ExperienceStorage
|
||||||
import ru.dbotthepony.mc.otm.block.entity.MatteryBlockEntity
|
import ru.dbotthepony.mc.otm.block.entity.MatteryBlockEntity
|
||||||
import ru.dbotthepony.mc.otm.config.MachinesConfig
|
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.TextComponent
|
||||||
import ru.dbotthepony.mc.otm.core.get
|
import ru.dbotthepony.mc.otm.core.get
|
||||||
import ru.dbotthepony.mc.otm.core.isNotEmpty
|
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
|
import ru.dbotthepony.mc.otm.registry.game.MBlockEntities
|
||||||
|
|
||||||
class GrillBlockEntity(blockPos: BlockPos, blockState: BlockState) : MatteryBlockEntity(MBlockEntities.GRILL, blockPos, blockState), MenuProvider, IBlockWithCustomName {
|
class GrillBlockEntity(blockPos: BlockPos, blockState: BlockState) : MatteryBlockEntity(MBlockEntities.GRILL, blockPos, blockState), MenuProvider, IBlockWithCustomName {
|
||||||
val fuelSlot = object : MatteryContainer(this@GrillBlockEntity::markDirtyFast, 1) {
|
val fuelSlot = SlottedContainer.simple(1, FUEL_SLOT_PROVIDER, ::markDirtyFast)
|
||||||
override fun getMaxStackSize(): Int {
|
|
||||||
return 4
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val inputSlots = object : MatteryContainer(this@GrillBlockEntity::markDirtyFast, SLOTS) {
|
private inner class InputSlot(container: SlottedContainer, slot: Int) : ContainerSlot(container, slot) {
|
||||||
override fun getMaxStackSize(): Int {
|
override val maxStackSize: Int
|
||||||
return 1
|
get() = 1
|
||||||
}
|
|
||||||
|
|
||||||
private fun clearSlot(slot: Int) {
|
|
||||||
|
private fun clearSlot() {
|
||||||
inputProgress[slot] = 0
|
inputProgress[slot] = 0
|
||||||
outputs[slot] = null
|
outputs[slot] = null
|
||||||
activeSlots.rem(slot)
|
activeSlots.rem(slot)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun setChanged(slot: Int, new: ItemStack, old: ItemStack) {
|
override fun notifyChanged(old: ItemStack) {
|
||||||
super.setChanged(slot, new, old)
|
super.notifyChanged(old)
|
||||||
|
|
||||||
if (new.isEmpty || new.count > 1) {
|
if (item.isEmpty || item.count > 1) {
|
||||||
clearSlot(slot)
|
clearSlot()
|
||||||
} else {
|
} else {
|
||||||
val level = level
|
val level = level
|
||||||
|
|
||||||
if (level == null) {
|
if (level == null) {
|
||||||
clearSlot(slot)
|
clearSlot()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val input = SingleRecipeInput(new)
|
val input = SingleRecipeInput(item)
|
||||||
val result = level.recipeManager
|
val result = level.recipeManager
|
||||||
.byType(RecipeType.SMOKING)
|
.byType(RecipeType.SMOKING)
|
||||||
.firstOrNull { it.value.matches(input, level) }
|
.firstOrNull { it.value.matches(input, level) }
|
||||||
|
|
||||||
if (result == null) {
|
if (result == null) {
|
||||||
clearSlot(slot)
|
clearSlot()
|
||||||
} else {
|
} else {
|
||||||
if (outputs[slot] != result.value)
|
if (outputs[slot] != result.value)
|
||||||
inputProgress[slot] = 0
|
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 var customDisplayName: Component? = null
|
||||||
|
|
||||||
override fun createMenu(p_39954_: Int, p_39955_: Inventory, p_39956_: Player): AbstractContainerMenu {
|
override fun createMenu(containerID: Int, inventory: Inventory, player: Player): AbstractContainerMenu {
|
||||||
return GrillMenu(p_39954_, p_39955_, this)
|
return GrillMenu(containerID, inventory, this)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getDisplayName(): Component {
|
override fun getDisplayName(): Component {
|
||||||
@ -205,5 +205,6 @@ class GrillBlockEntity(blockPos: BlockPos, blockState: BlockState) : MatteryBloc
|
|||||||
const val SLOTS = 6
|
const val SLOTS = 6
|
||||||
|
|
||||||
private val progressCodec = Codec.INT.minRange(0)
|
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.
|
* 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> {
|
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 {
|
fun containerSlot(slot: Int): IContainerSlot {
|
||||||
return IContainerSlot.Simple(slot, this)
|
return IContainerSlot.Simple(slot, this)
|
||||||
}
|
}
|
||||||
|
@ -392,6 +392,17 @@ class SlottedContainer(
|
|||||||
.build()
|
.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 {
|
fun filtered(size: Int): SlottedContainer {
|
||||||
return Builder().add(size, ::FilteredContainerSlot).build()
|
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.kommons.util.getValue
|
||||||
import ru.dbotthepony.mc.otm.block.entity.decorative.GrillBlockEntity
|
import ru.dbotthepony.mc.otm.block.entity.decorative.GrillBlockEntity
|
||||||
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
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.core.immutableList
|
||||||
import ru.dbotthepony.mc.otm.menu.ChemicalFuelMenuSlot
|
import ru.dbotthepony.mc.otm.menu.ChemicalFuelMenuSlot
|
||||||
import ru.dbotthepony.mc.otm.menu.MatteryMenu
|
import ru.dbotthepony.mc.otm.menu.MatteryMenu
|
||||||
@ -26,17 +28,9 @@ class GrillMenu(
|
|||||||
inventory: Inventory,
|
inventory: Inventory,
|
||||||
tile: GrillBlockEntity? = null
|
tile: GrillBlockEntity? = null
|
||||||
) : MatteryMenu(MMenus.GRILL, containerId, inventory, tile) {
|
) : MatteryMenu(MMenus.GRILL, containerId, inventory, tile) {
|
||||||
val fuelSlot = makeSlots(tile?.fuelSlot ?: object : MatteryContainer(1) {
|
val fuelSlot = makeSlots(tile?.fuelSlot ?: SlottedContainer.simple(1, GrillBlockEntity.FUEL_SLOT_PROVIDER), ::ChemicalFuelMenuSlot)
|
||||||
override fun getMaxStackSize(): Int {
|
|
||||||
return 4
|
|
||||||
}
|
|
||||||
}, ::ChemicalFuelMenuSlot)
|
|
||||||
|
|
||||||
val inputSlots = makeSlots<Container, MatteryMenuSlot>(tile?.inputSlots ?: object : MatteryContainer(GrillBlockEntity.SLOTS) {
|
val inputSlots = makeSlots<Container, MatteryMenuSlot>(tile?.inputSlots ?: SlottedContainer.simple(GrillBlockEntity.SLOTS, ContainerSlot.Simple(maxStackSize = 1))) { c, i ->
|
||||||
override fun getMaxStackSize(): Int {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
}) { c, i ->
|
|
||||||
object : MatteryMenuSlot(c, i) {
|
object : MatteryMenuSlot(c, i) {
|
||||||
override fun onTake(p_150645_: Player, p_150646_: ItemStack) {
|
override fun onTake(p_150645_: Player, p_150646_: ItemStack) {
|
||||||
super.onTake(p_150645_, p_150646_)
|
super.onTake(p_150645_, p_150646_)
|
||||||
|
Loading…
Reference in New Issue
Block a user