Add I/O slots to fluid hoppers

This commit is contained in:
Shadowfacts 2017-07-02 11:50:39 -04:00
parent 4a23f22b75
commit ce9bcd9556
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
8 changed files with 117 additions and 11 deletions

View File

@ -0,0 +1,35 @@
package net.shadowfacts.extrahoppers.block.fluid
import net.minecraft.entity.player.InventoryPlayer
import net.minecraft.inventory.Slot
import net.minecraft.util.math.BlockPos
import net.minecraftforge.items.SlotItemHandler
import net.shadowfacts.shadowmc.inventory.ContainerBase
/**
* @author shadowfacts
*/
class ContainerFluidHopper(val hopper: TileEntityFluidHopper, playerInv: InventoryPlayer, pos: BlockPos): ContainerBase(pos) {
init {
addSlotToContainer(SlotHopperIO(hopper, 0, 26, 35))
addSlotToContainer(SlotHopperIO(hopper, 1, 134, 35))
for (i in 0..2) {
for (j in 0..8) {
addSlotToContainer(Slot(playerInv, j + i * 9 + 9, 8 + j * 18, 84 + i * 18))
}
}
for (k in 0..8) {
this.addSlotToContainer(Slot(playerInv, k, 8 + k * 18, 142))
}
}
private class SlotHopperIO(val hopper: TileEntityFluidHopper, index: Int, x: Int, y: Int): SlotItemHandler(hopper.ioInventory, index, x, y) {
override fun onSlotChanged() {
hopper.markDirty()
}
}
}

View File

@ -13,7 +13,7 @@ import net.shadowfacts.shadowmc.ui.dsl.container
*/
object GUIFluidHopper {
private val BG = ResourceLocation("shadowmc", "textures/gui/blank.png")
private val BG = ResourceLocation(MOD_ID, "textures/gui/fluid_hopper.png")
fun create(hopper: TileEntityFluidHopper, container: Container): GuiContainer {
return container(container) {

View File

@ -1,6 +1,7 @@
package net.shadowfacts.extrahoppers.block.fluid
import net.minecraft.block.BlockLiquid
import net.minecraft.item.ItemStack
import net.minecraft.nbt.NBTTagCompound
import net.minecraft.util.EnumFacing
import net.minecraft.util.ITickable
@ -8,13 +9,21 @@ import net.minecraftforge.common.capabilities.Capability
import net.minecraftforge.fluids.Fluid.BUCKET_VOLUME
import net.minecraftforge.fluids.FluidStack
import net.minecraftforge.fluids.FluidTank
import net.minecraftforge.fluids.FluidUtil
import net.minecraftforge.fluids.capability.CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY
import net.minecraftforge.fluids.capability.CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY
import net.minecraftforge.fluids.capability.IFluidHandlerItem
import net.minecraftforge.fml.common.network.NetworkRegistry
import net.minecraftforge.items.ItemStackHandler
import net.shadowfacts.extrahoppers.EHConfig
import net.shadowfacts.extrahoppers.block.base.TileEntityHopperBase
import net.shadowfacts.extrahoppers.util.FluidUtils
import net.shadowfacts.extrahoppers.util.filter.Filter
import net.shadowfacts.extrahoppers.util.filter.FluidFilter
import net.shadowfacts.extrahoppers.util.getFluidHandler
import net.shadowfacts.extrahoppers.util.hasFluidHandler
import net.shadowfacts.forgelin.extensions.get
import net.shadowfacts.forgelin.extensions.set
import net.shadowfacts.shadowmc.ShadowMC
import net.shadowfacts.shadowmc.network.PacketRequestTEUpdate
import net.shadowfacts.shadowmc.network.PacketUpdateTE
@ -43,6 +52,21 @@ open class TileEntityFluidHopper(inverted: Boolean, advanced: Boolean): TileEnti
return fluid == null || fluidValiator(fluid)
}
}
val ioInventory = object: ItemStackHandler(2) {
override fun insertItem(slot: Int, stack: ItemStack, simulate: Boolean): ItemStack {
if (!stack.hasFluidHandler()) return stack
// val stackHandler = stack.getFluidHandler()
// when (slot) {
// 0 -> { // insert fluid
// if (stackHandler.drain(tank.fill(stackHandler.drain(tank.capacity - tank.fluidAmount, false), false), false) == null) return stack
// }
// 1 -> { // extract fluid
// if (tank.drain(stackHandler.fill(tank.drain(tank.fluidAmount, false), false), false) == null) return stack
// }
// }
return super.insertItem(slot, stack, simulate)
}
}
protected open val fluidValiator: (FluidStack) -> Boolean = { true }
@ -67,6 +91,9 @@ open class TileEntityFluidHopper(inverted: Boolean, advanced: Boolean): TileEnti
if (!checkRedstone()) return
extractFromItem()
insertIntoItem()
if (handlerCooldown <= 0) {
handleFluidHandlers()
}
@ -76,6 +103,28 @@ open class TileEntityFluidHopper(inverted: Boolean, advanced: Boolean): TileEnti
}
}
private fun extractFromItem() {
val stack = ioInventory[0]
if (!stack.isEmpty) {
val res = FluidUtil.tryEmptyContainer(stack, tank, tank.capacity - tank.fluidAmount, null, true)
if (res.isSuccess) {
ioInventory[0] = res.result
save()
}
}
}
private fun insertIntoItem() {
val stack = ioInventory[1]
if (!stack.isEmpty) {
val res = FluidUtil.tryFillContainer(stack, tank, tank.fluidAmount, null, true)
if (res.isSuccess) {
ioInventory[1] = res.result
save()
}
}
}
private fun handleFluidHandlers() {
val transferOut = transferOut()
val transferIn = transferIn()
@ -156,14 +205,16 @@ open class TileEntityFluidHopper(inverted: Boolean, advanced: Boolean): TileEnti
}
override fun writeToNBT(tag: NBTTagCompound): NBTTagCompound {
tank.writeToNBT(tag)
tag.setTag("tank", tank.writeToNBT(NBTTagCompound()))
tag.setTag("ioInventory", ioInventory.serializeNBT())
tag.setInteger("handlerCooldown", handlerCooldown)
tag.setInteger("worldCooldown", worldCooldown)
return super.writeToNBT(tag)
}
override fun readFromNBT(tag: NBTTagCompound) {
tank.readFromNBT(tag)
tank.readFromNBT(tag.getCompoundTag("tank"))
ioInventory.deserializeNBT(tag.getCompoundTag("ioInventory"))
handlerCooldown = tag.getInteger("handlerCooldown")
worldCooldown = tag.getInteger("worldCooldown")
super.readFromNBT(tag)

View File

@ -52,7 +52,7 @@ class ContainerInventoryHopper(val hopper: TileEntityInventoryHopper, playerInv:
}
override fun transferStackInSlot(player: EntityPlayer, index: Int): ItemStack {
if (index in hopper.inventory.slots..hopper.inventory.slots + 5) {
if (index in hopper.inventory.slots..hopper.inventory.slots + 5) { // if the slot is a filter slot
return ItemStack.EMPTY
}
return super.transferStackInSlot(player, index)

View File

@ -14,7 +14,7 @@ import net.shadowfacts.shadowmc.ui.dsl.container
*/
object GUIWoodenFluidHopper {
private val BG = ResourceLocation("shadowmc", "textures/gui/blank.png")
private val BG = ResourceLocation(MOD_ID, "textures/gui/fluid_hopper.png")
fun create(hopper: TileEntityFluidHopper, container: Container): GuiContainer {
return container(container) {

View File

@ -5,6 +5,7 @@ import net.minecraft.inventory.Container
import net.minecraft.util.math.BlockPos
import net.minecraft.world.World
import net.minecraftforge.fml.common.network.IGuiHandler
import net.shadowfacts.extrahoppers.block.fluid.ContainerFluidHopper
import net.shadowfacts.extrahoppers.block.inventory.ContainerInventoryHopper
import net.shadowfacts.extrahoppers.block.fluid.GUIFluidHopper
import net.shadowfacts.extrahoppers.block.fluid.TileEntityFluidHopper
@ -32,21 +33,23 @@ object GUIHandler: IGuiHandler {
override fun getClientGuiElement(ID: Int, player: EntityPlayer, world: World, x: Int, y: Int, z: Int): Any? {
val pos = BlockPos(x, y, z)
val tile = world.getTileEntity(pos)
return when (ID) {
INVERTED_HOPPER, ADVANCED_HOPPER -> GUIInventoryHopper(getServerGuiElement(ID, player, world, x, y, z)!!, world.getTileEntity(pos) as TileEntityInventoryHopper)
FLUID_HOPPER -> GUIFluidHopper.create(world.getTileEntity(pos) as TileEntityFluidHopper, getServerGuiElement(ID, player, world, x, y, z)!!)
INVERTED_HOPPER, ADVANCED_HOPPER -> GUIInventoryHopper(getServerGuiElement(ID, player, world, x, y, z)!!, tile as TileEntityInventoryHopper)
FLUID_HOPPER -> GUIFluidHopper.create(tile as TileEntityFluidHopper, getServerGuiElement(ID, player, world, x, y, z)!!)
WOODEN_FLUID_HOPPER -> GUIWoodenFluidHopper.create(tile as TileEntityFluidHopper, getServerGuiElement(ID, player, world, x, y, z)!!)
WOODEN_HOPPER -> GUIWoodenHopper(getServerGuiElement(ID, player, world, x, y, z)!!)
WOODEN_FLUID_HOPPER -> GUIWoodenFluidHopper.create(world.getTileEntity(pos) as TileEntityFluidHopper, getServerGuiElement(ID, player, world, x, y, z)!!)
else -> null
}
}
override fun getServerGuiElement(ID: Int, player: EntityPlayer, world: World, x: Int, y: Int, z: Int): Container? {
val pos = BlockPos(x, y, z)
val tile = world.getTileEntity(pos)
return when (ID) {
INVERTED_HOPPER, ADVANCED_HOPPER -> ContainerInventoryHopper(world.getTileEntity(pos) as TileEntityInventoryHopper, player.inventory, pos)
FLUID_HOPPER, WOODEN_FLUID_HOPPER -> ContainerPlayerInv(pos, player.inventory)
WOODEN_HOPPER -> ContainerWoodenHopper(world.getTileEntity(pos) as TileEntityWoodenHopper, player.inventory, pos)
INVERTED_HOPPER, ADVANCED_HOPPER -> ContainerInventoryHopper(tile as TileEntityInventoryHopper, player.inventory, pos)
FLUID_HOPPER, WOODEN_FLUID_HOPPER -> ContainerFluidHopper(tile as TileEntityFluidHopper, player.inventory, pos)
WOODEN_HOPPER -> ContainerWoodenHopper(tile as TileEntityWoodenHopper, player.inventory, pos)
else -> null
}
}

View File

@ -0,0 +1,17 @@
package net.shadowfacts.extrahoppers.util
import net.minecraft.item.ItemStack
import net.minecraftforge.fluids.capability.CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY
import net.minecraftforge.fluids.capability.CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY
import net.minecraftforge.fluids.capability.IFluidHandler
/**
* @author shadowfacts
*/
fun ItemStack.hasFluidHandler(): Boolean {
return hasCapability(FLUID_HANDLER_CAPABILITY, null) || hasCapability(FLUID_HANDLER_ITEM_CAPABILITY, null)
}
fun ItemStack.getFluidHandler(): IFluidHandler {
return getCapability(FLUID_HANDLER_CAPABILITY, null) ?: getCapability(FLUID_HANDLER_ITEM_CAPABILITY, null) ?: throw RuntimeException("Expected fluid handler or fluid handler item capability on $this")
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1017 B