Фиксим гигаприколы с IFluidHandler
This commit is contained in:
parent
56f3395c47
commit
c3568a7fc1
@ -29,7 +29,7 @@ import ru.dbotthepony.mc.otm.registry.MBlockEntities
|
|||||||
|
|
||||||
class FluidTankBlockEntity(blockPos: BlockPos, blockState: BlockState) : MatteryDeviceBlockEntity(MBlockEntities.FLUID_TANK, blockPos, blockState) {
|
class FluidTankBlockEntity(blockPos: BlockPos, blockState: BlockState) : MatteryDeviceBlockEntity(MBlockEntities.FLUID_TANK, blockPos, blockState) {
|
||||||
val fluid = BlockMatteryFluidHandler(::onChanged, ItemsConfig::FLUID_TANK_CAPACITY)
|
val fluid = BlockMatteryFluidHandler(::onChanged, ItemsConfig::FLUID_TANK_CAPACITY)
|
||||||
var synchronizedFluid by synchronizer.Field(FluidStack.EMPTY!!, FluidStackValueCodec)
|
var synchronizedFluid by synchronizer.Field(FluidStack.EMPTY, FluidStackValueCodec)
|
||||||
private set
|
private set
|
||||||
|
|
||||||
val fillInput = MatteryContainer(::setChangedLight, 1).also(::addDroppableContainer)
|
val fillInput = MatteryContainer(::setChangedLight, 1).also(::addDroppableContainer)
|
||||||
@ -42,25 +42,15 @@ class FluidTankBlockEntity(blockPos: BlockPos, blockState: BlockState) : Mattery
|
|||||||
fillInput.handler(object : HandlerFilter {
|
fillInput.handler(object : HandlerFilter {
|
||||||
override fun canInsert(slot: Int, stack: ItemStack): Boolean {
|
override fun canInsert(slot: Int, stack: ItemStack): Boolean {
|
||||||
if (fluid.isEmpty) {
|
if (fluid.isEmpty) {
|
||||||
stack.getCapability(ForgeCapabilities.FLUID_HANDLER).ifPresentK {
|
|
||||||
if (it.tanks > 0) return true
|
|
||||||
}
|
|
||||||
|
|
||||||
stack.getCapability(ForgeCapabilities.FLUID_HANDLER_ITEM).ifPresentK {
|
stack.getCapability(ForgeCapabilities.FLUID_HANDLER_ITEM).ifPresentK {
|
||||||
if (it.tanks > 0) return true
|
return it.tanks > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
stack.getCapability(ForgeCapabilities.FLUID_HANDLER).ifPresentK {
|
|
||||||
if (it.fill(fluid[0], IFluidHandler.FluidAction.SIMULATE) > 0)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
stack.getCapability(ForgeCapabilities.FLUID_HANDLER_ITEM).ifPresentK {
|
stack.getCapability(ForgeCapabilities.FLUID_HANDLER_ITEM).ifPresentK {
|
||||||
if (it.fill(fluid[0], IFluidHandler.FluidAction.SIMULATE) > 0)
|
return it.fill(fluid[0], IFluidHandler.FluidAction.SIMULATE) > 0
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
@ -91,8 +81,7 @@ class FluidTankBlockEntity(blockPos: BlockPos, blockState: BlockState) : Mattery
|
|||||||
val item = drainInput[0]
|
val item = drainInput[0]
|
||||||
|
|
||||||
if (item.isNotEmpty) {
|
if (item.isNotEmpty) {
|
||||||
val target = if (item.count == 1) item else item.copyWithCount(1)
|
val cap = (if (item.count == 1) item else item.copyWithCount(1)).getCapability(ForgeCapabilities.FLUID_HANDLER_ITEM).orNull()
|
||||||
val cap = target.getCapability(ForgeCapabilities.FLUID_HANDLER_ITEM).orNull() ?: target.getCapability(ForgeCapabilities.FLUID_HANDLER).orNull()
|
|
||||||
|
|
||||||
if (cap == null) {
|
if (cap == null) {
|
||||||
if (output.consumeItem(item, simulate = false)) {
|
if (output.consumeItem(item, simulate = false)) {
|
||||||
@ -107,9 +96,9 @@ class FluidTankBlockEntity(blockPos: BlockPos, blockState: BlockState) : Mattery
|
|||||||
val moved0 = moveFluid(source = cap, destination = fluid)
|
val moved0 = moveFluid(source = cap, destination = fluid)
|
||||||
|
|
||||||
if (moved0.isNotEmpty) {
|
if (moved0.isNotEmpty) {
|
||||||
drainInput.setChanged(0)
|
drainInput[0] = cap.container
|
||||||
|
|
||||||
if (output.consumeItem(item, simulate = false)) {
|
if (output.consumeItem(drainInput[0], simulate = false)) {
|
||||||
drainInput.setChanged(0)
|
drainInput.setChanged(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -117,21 +106,20 @@ class FluidTankBlockEntity(blockPos: BlockPos, blockState: BlockState) : Mattery
|
|||||||
val moved0 = moveFluid(source = cap, destination = fluid, actuallyFill = false)
|
val moved0 = moveFluid(source = cap, destination = fluid, actuallyFill = false)
|
||||||
|
|
||||||
if (moved0.isNotEmpty) {
|
if (moved0.isNotEmpty) {
|
||||||
if (output.consumeItem(target, simulate = true)) {
|
if (output.consumeItem(cap.container, simulate = true)) {
|
||||||
val target1 = item.copyWithCount(1)
|
val cap1 = item.copyWithCount(1).getCapability(ForgeCapabilities.FLUID_HANDLER_ITEM).orNull() ?: throw ConcurrentModificationException()
|
||||||
val cap1 = target1.getCapability(ForgeCapabilities.FLUID_HANDLER_ITEM).orNull() ?: target1.getCapability(ForgeCapabilities.FLUID_HANDLER).orNull() ?: throw ConcurrentModificationException()
|
|
||||||
|
|
||||||
val moved1 = moveFluid(source = cap1, destination = fluid)
|
val moved1 = moveFluid(source = cap1, destination = fluid)
|
||||||
|
|
||||||
if (moved1 != moved0 || moved1.amount != moved0.amount) {
|
if (moved1 != moved0 || moved1.amount != moved0.amount) {
|
||||||
LOGGER.error("Error moving fluids in Fluid tank at $blockPos: moved $moved0 during simulation from $target, moved $moved1 from $target1 during execution. This is likely a bug in OTM or other mod!")
|
LOGGER.error("Error moving fluids in Fluid tank at $blockPos: moved $moved0 during simulation from ${cap.container}, moved $moved1 from ${cap1.container} during execution. This is likely a bug in OTM or other mod!")
|
||||||
} else {
|
} else {
|
||||||
item.count--
|
item.count--
|
||||||
drainInput.setChanged(0)
|
drainInput.setChanged(0)
|
||||||
|
|
||||||
if (!output.consumeItem(target1, simulate = false)) {
|
if (!output.consumeItem(cap1.container, simulate = false)) {
|
||||||
LOGGER.error("Unable to insert $target1 into output slot of Fluid tank at $blockPos, popping item in world instead to avoid item loss! This is likely a bug in OTM or other mod!")
|
LOGGER.error("Unable to insert ${cap1.container} into output slot of Fluid tank at $blockPos, popping item in world instead to avoid item loss! This is likely a bug in OTM or other mod!")
|
||||||
(level as? ServerLevel)?.addFreshEntity(ItemEntity(level!!, blockPos.x.toDouble(), blockPos.y.toDouble(), blockPos.z.toDouble(), target1))
|
(level as? ServerLevel)?.addFreshEntity(ItemEntity(level!!, blockPos.x.toDouble(), blockPos.y.toDouble(), blockPos.z.toDouble(), cap1.container))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -145,8 +133,7 @@ class FluidTankBlockEntity(blockPos: BlockPos, blockState: BlockState) : Mattery
|
|||||||
val item = fillInput[0]
|
val item = fillInput[0]
|
||||||
|
|
||||||
if (item.isNotEmpty) {
|
if (item.isNotEmpty) {
|
||||||
val target = if (item.count == 1) item else item.copyWithCount(1)
|
val cap = (if (item.count == 1) item else item.copyWithCount(1)).getCapability(ForgeCapabilities.FLUID_HANDLER_ITEM).orNull()
|
||||||
val cap = target.getCapability(ForgeCapabilities.FLUID_HANDLER_ITEM).orNull() ?: target.getCapability(ForgeCapabilities.FLUID_HANDLER).orNull()
|
|
||||||
|
|
||||||
if (cap == null) {
|
if (cap == null) {
|
||||||
if (output.consumeItem(item, simulate = false)) {
|
if (output.consumeItem(item, simulate = false)) {
|
||||||
@ -161,9 +148,9 @@ class FluidTankBlockEntity(blockPos: BlockPos, blockState: BlockState) : Mattery
|
|||||||
val moved0 = moveFluid(source = fluid, destination = cap)
|
val moved0 = moveFluid(source = fluid, destination = cap)
|
||||||
|
|
||||||
if (moved0.isNotEmpty) {
|
if (moved0.isNotEmpty) {
|
||||||
fillInput.setChanged(0)
|
fillInput[0] = cap.container
|
||||||
|
|
||||||
if (output.consumeItem(item, simulate = false)) {
|
if (output.consumeItem(fillInput[0], simulate = false)) {
|
||||||
fillInput.setChanged(0)
|
fillInput.setChanged(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -171,21 +158,20 @@ class FluidTankBlockEntity(blockPos: BlockPos, blockState: BlockState) : Mattery
|
|||||||
val moved0 = moveFluid(source = fluid, destination = cap, actuallyDrain = false)
|
val moved0 = moveFluid(source = fluid, destination = cap, actuallyDrain = false)
|
||||||
|
|
||||||
if (moved0.isNotEmpty) {
|
if (moved0.isNotEmpty) {
|
||||||
if (output.consumeItem(target, simulate = true)) {
|
if (output.consumeItem(cap.container, simulate = true)) {
|
||||||
val target1 = item.copyWithCount(1)
|
val cap1 = item.copyWithCount(1).getCapability(ForgeCapabilities.FLUID_HANDLER_ITEM).orNull() ?: throw ConcurrentModificationException()
|
||||||
val cap1 = target1.getCapability(ForgeCapabilities.FLUID_HANDLER_ITEM).orNull() ?: target1.getCapability(ForgeCapabilities.FLUID_HANDLER).orNull() ?: throw ConcurrentModificationException()
|
|
||||||
|
|
||||||
val moved1 = moveFluid(source = fluid, destination = cap1)
|
val moved1 = moveFluid(source = fluid, destination = cap1)
|
||||||
|
|
||||||
if (moved1 != moved0 || moved1.amount != moved0.amount) {
|
if (moved1 != moved0 || moved1.amount != moved0.amount) {
|
||||||
LOGGER.error("Error moving fluids in Fluid tank at $blockPos: moved $moved0 during simulation from $target, moved $moved1 from $target1 during execution. This is likely a bug in OTM or other mod!")
|
LOGGER.error("Error moving fluids in Fluid tank at $blockPos: moved $moved0 during simulation from ${cap.container}, moved $moved1 from ${cap1.container} during execution. This is likely a bug in OTM or other mod!")
|
||||||
} else {
|
} else {
|
||||||
item.count--
|
item.count--
|
||||||
fillInput.setChanged(0)
|
fillInput.setChanged(0)
|
||||||
|
|
||||||
if (!output.consumeItem(target1, simulate = false)) {
|
if (!output.consumeItem(cap1.container, simulate = false)) {
|
||||||
LOGGER.error("Unable to insert $target1 into output slot of Fluid tank at $blockPos, popping item in world instead to avoid item loss! This is likely a bug in OTM or other mod!")
|
LOGGER.error("Unable to insert ${cap1.container} into output slot of Fluid tank at $blockPos, popping item in world instead to avoid item loss! This is likely a bug in OTM or other mod!")
|
||||||
(level as? ServerLevel)?.addFreshEntity(ItemEntity(level!!, blockPos.x.toDouble(), blockPos.y.toDouble(), blockPos.z.toDouble(), target1))
|
(level as? ServerLevel)?.addFreshEntity(ItemEntity(level!!, blockPos.x.toDouble(), blockPos.y.toDouble(), blockPos.z.toDouble(), cap1.container))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,7 +42,7 @@ abstract class AbstractMatteryFluidHandler : IFluidHandler {
|
|||||||
return isFluidValid(stack)
|
return isFluidValid(stack)
|
||||||
}
|
}
|
||||||
|
|
||||||
final override fun fill(resource: FluidStack, action: IFluidHandler.FluidAction): Int {
|
override fun fill(resource: FluidStack, action: IFluidHandler.FluidAction): Int {
|
||||||
if (resource.isEmpty || !isFluidValid(resource) || !direction.input) {
|
if (resource.isEmpty || !isFluidValid(resource) || !direction.input) {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
@ -63,7 +63,7 @@ abstract class AbstractMatteryFluidHandler : IFluidHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final override fun drain(resource: FluidStack, action: IFluidHandler.FluidAction): FluidStack {
|
override fun drain(resource: FluidStack, action: IFluidHandler.FluidAction): FluidStack {
|
||||||
if (resource.isEmpty || !direction.output) {
|
if (resource.isEmpty || !direction.output) {
|
||||||
return FluidStack.EMPTY
|
return FluidStack.EMPTY
|
||||||
}
|
}
|
||||||
@ -77,7 +77,7 @@ abstract class AbstractMatteryFluidHandler : IFluidHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final override fun drain(maxDrain: Int, action: IFluidHandler.FluidAction): FluidStack {
|
override fun drain(maxDrain: Int, action: IFluidHandler.FluidAction): FluidStack {
|
||||||
require(maxDrain >= 0) { "Invalid amount to drain: $maxDrain" }
|
require(maxDrain >= 0) { "Invalid amount to drain: $maxDrain" }
|
||||||
if (maxDrain == 0 || !direction.output) return FluidStack.EMPTY
|
if (maxDrain == 0 || !direction.output) return FluidStack.EMPTY
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@ import net.minecraftforge.common.capabilities.ForgeCapabilities
|
|||||||
import net.minecraftforge.common.capabilities.ICapabilityProvider
|
import net.minecraftforge.common.capabilities.ICapabilityProvider
|
||||||
import net.minecraftforge.common.util.LazyOptional
|
import net.minecraftforge.common.util.LazyOptional
|
||||||
import net.minecraftforge.fluids.FluidStack
|
import net.minecraftforge.fluids.FluidStack
|
||||||
|
import net.minecraftforge.fluids.capability.IFluidHandler
|
||||||
import net.minecraftforge.fluids.capability.IFluidHandlerItem
|
import net.minecraftforge.fluids.capability.IFluidHandlerItem
|
||||||
import ru.dbotthepony.mc.otm.core.nbt.set
|
import ru.dbotthepony.mc.otm.core.nbt.set
|
||||||
import ru.dbotthepony.mc.otm.core.tagNotNull
|
import ru.dbotthepony.mc.otm.core.tagNotNull
|
||||||
@ -38,4 +39,25 @@ open class ItemMatteryFluidHandler(val itemStack: ItemStack, private val _capaci
|
|||||||
|
|
||||||
return LazyOptional.empty()
|
return LazyOptional.empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun fill(resource: FluidStack, action: IFluidHandler.FluidAction): Int {
|
||||||
|
if (itemStack.count != 1)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
return super.fill(resource, action)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun drain(resource: FluidStack, action: IFluidHandler.FluidAction): FluidStack {
|
||||||
|
if (itemStack.count != 1)
|
||||||
|
return FluidStack.EMPTY
|
||||||
|
|
||||||
|
return super.drain(resource, action)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun drain(maxDrain: Int, action: IFluidHandler.FluidAction): FluidStack {
|
||||||
|
if (itemStack.count != 1)
|
||||||
|
return FluidStack.EMPTY
|
||||||
|
|
||||||
|
return super.drain(maxDrain, action)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -331,6 +331,9 @@ open class MatteryContainer(protected val watcher: Runnable, private val size: I
|
|||||||
* @return Whenever [stack] was modified
|
* @return Whenever [stack] was modified
|
||||||
*/
|
*/
|
||||||
fun consumeItem(stack: ItemStack, simulate: Boolean, onlyIntoExisting: Boolean = false, popTime: Int? = null): Boolean {
|
fun consumeItem(stack: ItemStack, simulate: Boolean, onlyIntoExisting: Boolean = false, popTime: Int? = null): Boolean {
|
||||||
|
if (stack.isEmpty)
|
||||||
|
return false
|
||||||
|
|
||||||
val result = addItem(stack, 0 until size, simulate, onlyIntoExisting = onlyIntoExisting, popTime = popTime)
|
val result = addItem(stack, 0 until size, simulate, onlyIntoExisting = onlyIntoExisting, popTime = popTime)
|
||||||
|
|
||||||
if (result.count != stack.count) {
|
if (result.count != stack.count) {
|
||||||
@ -411,7 +414,7 @@ open class MatteryContainer(protected val watcher: Runnable, private val size: I
|
|||||||
|
|
||||||
val old = slots[slot]
|
val old = slots[slot]
|
||||||
slots[slot] = stack
|
slots[slot] = stack
|
||||||
trackedSlots[slot] = stack.copy()
|
trackedSlots[slot] = if (stack.isEmpty) ItemStack.EMPTY else stack.copy()
|
||||||
setChanged(slot, stack, old)
|
setChanged(slot, stack, old)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,31 +25,26 @@ class FluidTankMenu(containerId: Int, inventory: Inventory, tile: FluidTankBlock
|
|||||||
|
|
||||||
val drainInput = object : MatterySlot(tile?.drainInput ?: SimpleContainer(1), 0) {
|
val drainInput = object : MatterySlot(tile?.drainInput ?: SimpleContainer(1), 0) {
|
||||||
override fun mayPlace(itemStack: ItemStack): Boolean {
|
override fun mayPlace(itemStack: ItemStack): Boolean {
|
||||||
return super.mayPlace(itemStack) && itemStack.getCapability(ForgeCapabilities.FLUID_HANDLER)
|
return super.mayPlace(itemStack) &&
|
||||||
|
itemStack
|
||||||
|
.getCapability(ForgeCapabilities.FLUID_HANDLER_ITEM)
|
||||||
.map { it.isNotEmpty }
|
.map { it.isNotEmpty }
|
||||||
.orElse(itemStack.getCapability(ForgeCapabilities.FLUID_HANDLER_ITEM)
|
.orElse(false)
|
||||||
.map { it.isNotEmpty }
|
|
||||||
.orElse(false))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val fillInput = object : MatterySlot(tile?.fillInput ?: SimpleContainer(1), 0) {
|
val fillInput = object : MatterySlot(tile?.fillInput ?: SimpleContainer(1), 0) {
|
||||||
override fun mayPlace(itemStack: ItemStack): Boolean {
|
override fun mayPlace(itemStack: ItemStack): Boolean {
|
||||||
return super.mayPlace(itemStack) && itemStack.getCapability(ForgeCapabilities.FLUID_HANDLER)
|
return super.mayPlace(itemStack) &&
|
||||||
|
(if (itemStack.count <= 1) itemStack else itemStack.copyWithCount(1))
|
||||||
|
.getCapability(ForgeCapabilities.FLUID_HANDLER_ITEM)
|
||||||
.map {
|
.map {
|
||||||
if (fluid.fluid.isEmpty)
|
if (fluid.fluid.isEmpty)
|
||||||
it.tanks > 0
|
it.tanks > 0
|
||||||
else
|
else
|
||||||
it.fill(fluid.fluid, IFluidHandler.FluidAction.SIMULATE) > 0
|
it.fill(fluid.fluid, IFluidHandler.FluidAction.SIMULATE) > 0
|
||||||
}
|
}
|
||||||
.orElse(itemStack.getCapability(ForgeCapabilities.FLUID_HANDLER_ITEM)
|
.orElse(false)
|
||||||
.map {
|
|
||||||
if (fluid.fluid.isEmpty)
|
|
||||||
it.tanks > 0
|
|
||||||
else
|
|
||||||
it.fill(fluid.fluid, IFluidHandler.FluidAction.SIMULATE) > 0
|
|
||||||
}
|
|
||||||
.orElse(false))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user