Make getMaxStackSize also accept itemstack

This commit is contained in:
DBotThePony 2023-08-02 13:50:53 +07:00
parent 716ccae94a
commit 0878bd9a7e
Signed by: DBot
GPG Key ID: DCC23B5715498507
4 changed files with 10 additions and 20 deletions

View File

@ -71,7 +71,7 @@ class MatterBottlerBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) :
//private var work_behavior = true //private var work_behavior = true
val container: MatteryContainer = object : MatteryContainer(this::setChangedLight, 6) { val container: MatteryContainer = object : MatteryContainer(this::setChangedLight, 6) {
override fun getMaxStackSize(slot: Int): Int { override fun getMaxStackSize(slot: Int, itemStack: ItemStack): Int {
return 1 return 1
} }

View File

@ -20,16 +20,16 @@ class ContainerHandler @JvmOverloads internal constructor(
if (localStack.isEmpty) { if (localStack.isEmpty) {
if (!simulate) { if (!simulate) {
container.setItem(slot, stack.copyWithCount(stack.count.coerceAtMost(stack.maxStackSize).coerceAtMost(container.getMaxStackSize(slot)))) container.setItem(slot, stack.copyWithCount(stack.count.coerceAtMost(container.getMaxStackSize(slot, stack))))
} }
if (stack.count <= container.getMaxStackSize(slot).coerceAtMost(stack.maxStackSize)) { if (stack.count <= container.getMaxStackSize(slot, stack)) {
return ItemStack.EMPTY return ItemStack.EMPTY
} else { } else {
return stack.copyWithCount(container.getMaxStackSize(slot)) return stack.copyWithCount(container.getMaxStackSize(slot, stack))
} }
} else if (localStack.isStackable && localStack.maxStackSize.coerceAtMost(container.getMaxStackSize(slot)) > localStack.count && ItemStack.isSameItemSameTags(localStack, stack)) { } else if (localStack.isStackable && container.getMaxStackSize(slot, localStack) > localStack.count && ItemStack.isSameItemSameTags(localStack, stack)) {
val newCount = localStack.maxStackSize.coerceAtMost(localStack.count + stack.count).coerceAtMost(container.getMaxStackSize(slot)) val newCount = container.getMaxStackSize(slot, localStack).coerceAtMost(localStack.count + stack.count)
val diff = newCount - localStack.count val diff = newCount - localStack.count
if (diff != 0) { if (diff != 0) {

View File

@ -215,17 +215,7 @@ open class MatteryContainer(protected val watcher: Runnable, private val size: I
return ContainerHandler(this, filter) return ContainerHandler(this, filter)
} }
open fun getMaxStackSize(slot: Int) = maxStackSize open fun getMaxStackSize(slot: Int, itemStack: ItemStack) = maxStackSize.coerceAtMost(itemStack.maxStackSize)
open fun getMaxStackSizeWithItem(slot: Int): Int {
val item = this[slot]
if (!item.isEmpty) {
return getMaxStackSize(slot).coerceAtMost(item.maxStackSize)
}
return getMaxStackSize(slot)
}
/** /**
* @return Leftover [ItemStack] * @return Leftover [ItemStack]
@ -244,7 +234,7 @@ open class MatteryContainer(protected val watcher: Runnable, private val size: I
for (slot in range) { for (slot in range) {
if (ItemStack.isSameItemSameTags(slots[slot], copy)) { if (ItemStack.isSameItemSameTags(slots[slot], copy)) {
val slotStack = slots[slot] val slotStack = slots[slot]
val slotLimit = getMaxStackSize(slot).coerceAtMost(slotStack.maxStackSize) val slotLimit = getMaxStackSize(slot, slotStack)
if (slotStack.count < slotLimit) { if (slotStack.count < slotLimit) {
val newCount = (slotStack.count + copy.count).coerceAtMost(slotLimit) val newCount = (slotStack.count + copy.count).coerceAtMost(slotLimit)
@ -274,7 +264,7 @@ open class MatteryContainer(protected val watcher: Runnable, private val size: I
// двигаем в пустые слоты // двигаем в пустые слоты
for (slot in range) { for (slot in range) {
if (slots[slot].isEmpty) { if (slots[slot].isEmpty) {
val diff = copy.count.coerceAtMost(getMaxStackSize(slot).coerceAtMost(copy.maxStackSize)) val diff = copy.count.coerceAtMost(getMaxStackSize(slot, copy))
if (!simulate) { if (!simulate) {
val copyToPut = copy.copy() val copyToPut = copy.copy()

View File

@ -71,7 +71,7 @@ class MatterDustItem : Item(Properties().stacksTo(64)), IMatterItem {
// можем ли мы стакнуть материю из второго слота в первый? // можем ли мы стакнуть материю из второго слота в первый?
if (!stack2.isEmpty && isFull(stack2)) { if (!stack2.isEmpty && isFull(stack2)) {
if (ItemStack.isSameItemSameTags(stack, stack2) && container.getMaxStackSizeWithItem(mainSlot) >= stack.count + 1) { if (ItemStack.isSameItemSameTags(stack, stack2) && container.getMaxStackSize(mainSlot, stack) >= stack.count + 1) {
stack.count++ stack.count++
stack2.count-- stack2.count--