Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```kotlin
- private fun Inventory.stackIsNotEmptyAndCanAddMore(toStack: ItemStack, stackToAdd: ItemStack): Boolean {
- return !toStack.isEmpty &&
- areItemsEqual(toStack, stackToAdd)
- && toStack.isStackable
- && toStack.count < toStack.maxCount
- && toStack.count < this.invMaxStackAmount
- }
- private fun areItemsEqual(stack1: ItemStack, stack2: ItemStack): Boolean {
- return stack1.item === stack2.item && ItemStack.areTagsEqual(stack1, stack2)
- }
- private fun Inventory.availableSlots(direction: Direction): Iterable<Int> {
- return if (this is SidedInventory) getInvAvailableSlots(direction).toList() else (0 until invSize)
- }
- private fun Inventory.canInsert(slot: Int, stack: ItemStack, direction: Direction): Boolean {
- return if (this is SidedInventory) canInsertInvStack(slot, stack, direction) else isValidInvStack(slot, stack)
- }
- private fun Inventory.distributeToAvailableSlots(stack: ItemStack, acceptEmptySlots: Boolean, direction: Direction): ItemStack {
- val maxStackSize = invMaxStackAmount
- var stackCountLeftToDistribute = stack.count
- for (slot in availableSlots(direction)) {
- if (!canInsert(slot, stack, direction)) continue
- val stackInSlot = getInvStack(slot)
- if ((acceptEmptySlots && stackInSlot.isEmpty) || stackIsNotEmptyAndCanAddMore(stackInSlot, stack)) {
- val amountThatCanFitInSlot = maxStackSize - stackInSlot.count
- if (amountThatCanFitInSlot >= 0) {
- setInvStack(slot, ItemStack(stack.item,
- min(maxStackSize, stackInSlot.count + stackCountLeftToDistribute)
- ))
- stackCountLeftToDistribute -= amountThatCanFitInSlot
- }
- }
- if (stackCountLeftToDistribute <= 0) return ItemStack.EMPTY
- }
- return stack.copy(count = stackCountLeftToDistribute)
- }
- /**
- * Returns the remaining stack
- */
- fun Inventory.insert(stack: ItemStack, direction: Direction = Direction.UP): ItemStack {
- val remainingAfterNonEmptySlots = distributeToAvailableSlots(stack, acceptEmptySlots = false,direction = direction)
- return distributeToAvailableSlots(remainingAfterNonEmptySlots, acceptEmptySlots = true,direction = direction)
- }
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement