ExtraHoppers-forge/src/main/kotlin/net/shadowfacts/extrahoppers/block/wooden/TileEntityWoodenHopper.kt

173 lines
5.0 KiB
Kotlin
Raw Normal View History

2017-01-14 23:57:11 +00:00
package net.shadowfacts.extrahoppers.block.wooden
2017-01-15 15:02:15 +00:00
import net.minecraft.entity.item.EntityItem
2017-01-14 23:57:11 +00:00
import net.minecraft.inventory.IInventory
import net.minecraft.inventory.ISidedInventory
import net.minecraft.nbt.NBTTagCompound
import net.minecraft.util.EnumFacing
import net.minecraft.util.ITickable
2017-01-15 15:02:15 +00:00
import net.minecraft.util.math.AxisAlignedBB
2017-01-14 23:57:11 +00:00
import net.minecraftforge.common.capabilities.Capability
import net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY
import net.minecraftforge.items.ItemStackHandler
import net.shadowfacts.extrahoppers.block.base.TileEntityHopperBase
import net.shadowfacts.extrahoppers.util.insert
import net.shadowfacts.forgelin.extensions.get
/**
* @author shadowfacts
*/
2017-01-16 23:38:15 +00:00
class TileEntityWoodenHopper(inverted: Boolean): TileEntityHopperBase(inverted), ITickable {
2017-01-14 23:57:11 +00:00
companion object {
val COOLDOWN = 24
}
val inventory = ItemStackHandler(1)
2017-01-15 16:46:04 +00:00
var cooldown = COOLDOWN
2017-01-14 23:57:11 +00:00
2017-01-16 23:38:15 +00:00
constructor(): this(false)
2017-01-14 23:57:11 +00:00
override fun update() {
if (!world.isRemote) {
2017-01-15 17:19:44 +00:00
if (isPowered()) return
2017-01-14 23:57:11 +00:00
cooldown--
if (cooldown <= 0) {
val pulled = pull()
val pushed = push()
if (pulled || pushed) {
cooldown = COOLDOWN
markDirty()
}
}
}
}
private fun push(): Boolean {
if (!inventory[0].isEmpty) {
val facing = getHopperFacing()
val tile = world.getTileEntity(pos.offset(facing))
if (tile is ISidedInventory) {
val slots = tile.getSlotsForFace(facing.opposite)
for (i in slots) {
val remainder = tile.insert(inventory.extractItem(0, 1, true), i)
if (remainder.isEmpty) {
inventory.extractItem(0, 1, false)
2017-01-15 15:02:15 +00:00
return true
2017-01-14 23:57:11 +00:00
}
}
} else if (tile is IInventory) {
for (i in 0.until(tile.sizeInventory)) {
val remainder = tile.insert(inventory.extractItem(0, 1, true), i)
if (remainder.isEmpty) {
inventory.extractItem(0, 1, false)
2017-01-15 15:02:15 +00:00
return true
2017-01-14 23:57:11 +00:00
}
}
} else if (tile != null && tile.hasCapability(ITEM_HANDLER_CAPABILITY, facing.opposite)) {
val handler = tile.getCapability(ITEM_HANDLER_CAPABILITY, facing.opposite)!!
for (i in 0.until(handler.slots)) {
val remainder = handler.insertItem(i, inventory.extractItem(0, 1, true), false)
if (remainder.isEmpty) {
inventory.extractItem(0, 1, false)
2017-01-15 15:02:15 +00:00
return true
2017-01-14 23:57:11 +00:00
}
}
}
}
return false
}
private fun pull(): Boolean {
2017-01-15 16:46:04 +00:00
val yOffset = if (inverted) -1 else 0
val items = world.getEntitiesWithinAABB(EntityItem::class.java, AxisAlignedBB(pos.x.toDouble(), pos.y + 0.5 + yOffset, pos.z.toDouble(), pos.x + 1.0, pos.y + 1.5 + yOffset, pos.z + 1.0))
2017-01-15 15:02:15 +00:00
for (item in items) {
2017-06-17 22:47:56 +00:00
val result = inventory.insertItem(0, item.item, true)
if (result.count != item.item.count) {
inventory.insertItem(0, item.item, false)
2017-01-15 15:02:15 +00:00
if (result.isEmpty) {
item.setDead()
} else {
2017-06-17 22:47:56 +00:00
item.item = result
2017-01-15 15:02:15 +00:00
}
return true
}
}
2017-01-15 16:46:04 +00:00
val tile = world.getTileEntity(if (inverted) pos.down() else pos.up())
val side = if (inverted) EnumFacing.UP else EnumFacing.DOWN
2017-01-14 23:57:11 +00:00
if (tile is ISidedInventory) {
2017-01-15 16:46:04 +00:00
val slots = tile.getSlotsForFace(side)
2017-01-14 23:57:11 +00:00
for (i in slots) {
val current = tile[i]
if (!current.isEmpty) {
val copy = current.copy()
copy.count = 1
val remainder = inventory.insertItem(0, copy, false)
if (remainder.isEmpty) {
current.shrink(1)
2017-01-15 15:02:15 +00:00
return true
2017-01-14 23:57:11 +00:00
}
}
}
} else if (tile is IInventory) {
for (i in 0.until(tile.sizeInventory)) {
val current = tile[i]
if (!current.isEmpty) {
val copy = current.copy()
copy.count = 1
val remainder = inventory.insertItem(0, copy, false)
if (remainder.isEmpty) {
current.shrink(1)
2017-01-15 15:02:15 +00:00
return true
2017-01-14 23:57:11 +00:00
}
}
}
2017-01-15 16:46:04 +00:00
} else if (tile != null && tile.hasCapability(ITEM_HANDLER_CAPABILITY, side)) {
val handler = tile.getCapability(ITEM_HANDLER_CAPABILITY, side)!!
2017-01-14 23:57:11 +00:00
for (i in 0.until(handler.slots)) {
val remainder = inventory.insertItem(0, handler.extractItem(i, 1, true), false)
if (remainder.isEmpty) {
handler.extractItem(i, 1, false)
2017-01-15 15:02:15 +00:00
return true
2017-01-14 23:57:11 +00:00
}
}
}
return false
}
override fun writeToNBT(tag: NBTTagCompound): NBTTagCompound {
tag.setTag("inventory", inventory.serializeNBT())
tag.setInteger("cooldown", cooldown)
return super.writeToNBT(tag)
}
override fun readFromNBT(tag: NBTTagCompound) {
inventory.deserializeNBT(tag.getCompoundTag("inventory"))
cooldown = tag.getInteger("cooldown")
super.readFromNBT(tag)
}
override fun hasCapability(capability: Capability<*>, facing: EnumFacing?): Boolean {
if (capability == ITEM_HANDLER_CAPABILITY) {
2017-01-15 16:46:04 +00:00
return facing == (if (inverted) EnumFacing.DOWN else EnumFacing.UP) || facing == getHopperFacing()
2017-01-14 23:57:11 +00:00
}
return super.hasCapability(capability, facing)
}
override fun <T: Any?> getCapability(capability: Capability<T>, facing: EnumFacing?): T? {
if (capability == ITEM_HANDLER_CAPABILITY) {
2017-01-15 16:46:04 +00:00
if (facing == (if (inverted) EnumFacing.DOWN else EnumFacing.UP) || facing == getHopperFacing()) {
2017-01-14 23:57:11 +00:00
return inventory as T
}
}
return super.getCapability(capability, facing)
}
}