71 lines
1.8 KiB
Kotlin
71 lines
1.8 KiB
Kotlin
package net.shadowfacts.extrahoppers.block.base
|
|
|
|
import net.minecraft.container.Container
|
|
import net.minecraft.container.Slot
|
|
import net.minecraft.entity.player.PlayerEntity
|
|
import net.minecraft.entity.player.PlayerInventory
|
|
import net.minecraft.inventory.Inventory
|
|
import net.minecraft.item.ItemStack
|
|
|
|
/**
|
|
* @author shadowfacts
|
|
*/
|
|
abstract class BaseHopperContainer(
|
|
syncId: Int,
|
|
playerInv: PlayerInventory,
|
|
val hopperInv: Inventory
|
|
): Container(null, syncId) {
|
|
|
|
init {
|
|
addHopperSlots()
|
|
|
|
// player inv
|
|
for (y in 0 until 3) {
|
|
for (x in 0 until 9) {
|
|
addSlot(Slot(playerInv, x + y * 9 + 9, 8 + x * 18, 51 + y * 18))
|
|
}
|
|
}
|
|
// hotbar
|
|
for (x in 0 until 9) {
|
|
addSlot(Slot(playerInv, x, 8 + x * 18, 109))
|
|
}
|
|
}
|
|
|
|
abstract fun addHopperSlots()
|
|
|
|
override fun canUse(player: PlayerEntity): Boolean {
|
|
return true
|
|
}
|
|
|
|
override fun close(player: PlayerEntity) {
|
|
super.close(player)
|
|
|
|
hopperInv.onInvClose(player)
|
|
}
|
|
|
|
override fun transferSlot(player: PlayerEntity, slotIndex: Int): ItemStack {
|
|
var remaining = ItemStack.EMPTY
|
|
val slot = slots[slotIndex]
|
|
if (slot != null && slot.hasStack()) {
|
|
val slotStack = slot.stack
|
|
remaining = slotStack.copy()
|
|
if (slotIndex < hopperInv.invSize) {
|
|
if (!insertItem(slotStack, hopperInv.invSize, slots.size, true)) {
|
|
return ItemStack.EMPTY;
|
|
}
|
|
} else if (!insertItem(slotStack, 0, hopperInv.invSize, false)) {
|
|
return ItemStack.EMPTY;
|
|
}
|
|
|
|
if (slotStack.isEmpty) {
|
|
slot.stack = ItemStack.EMPTY;
|
|
} else {
|
|
slot.markDirty()
|
|
}
|
|
}
|
|
|
|
return remaining
|
|
}
|
|
|
|
}
|