75 lines
2.3 KiB
Kotlin
75 lines
2.3 KiB
Kotlin
package net.shadowfacts.extrahoppers.block.wood
|
|
|
|
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
|
|
import net.minecraft.util.Identifier
|
|
import net.minecraft.util.PacketByteBuf
|
|
import net.shadowfacts.extrahoppers.init.EHBlocks
|
|
|
|
class WoodHopperContainer(syncId: Int, playerInv: PlayerInventory, val hopperInv: Inventory): Container(null, syncId) {
|
|
|
|
companion object {
|
|
val ID = Identifier("extrahoppers", "wood_hopper")
|
|
|
|
fun create(syncId: Int, identifier: Identifier, player: PlayerEntity, buf: PacketByteBuf): WoodHopperContainer {
|
|
val pos = buf.readBlockPos()
|
|
val blockEntity = EHBlocks.WOOD_HOPPER.getBlockEntity(player.world, pos)!!
|
|
return WoodHopperContainer(syncId, player.inventory, blockEntity)
|
|
}
|
|
}
|
|
|
|
init {
|
|
addSlot(Slot(hopperInv, 0, 80, 20))
|
|
|
|
// 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))
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
}
|