From 708bb1e529802799f448373cd47f6eb232c7a418 Mon Sep 17 00:00:00 2001 From: ferriarnus <61201275+ferriarnus@users.noreply.github.com> Date: Thu, 22 Feb 2024 23:28:25 +0100 Subject: [PATCH 1/5] start --- .../common/attachment/IFluidTankUser.java | 33 +++++++ .../common/blockentity/DrainBlockEntity.java | 42 ++++++-- .../blockentity/FluidTankBlockEntity.java | 62 ++++++++---- .../blockentity/SoulBinderBlockEntity.java | 51 +++++++--- .../blockentity/SoulEngineBlockEntity.java | 62 +++++++++--- .../blockentity/XPObeliskBlockEntity.java | 55 ++++++++++- .../blockentity/XPVacuumBlockEntity.java | 58 +++++++++-- .../blockentity/base/MachineBlockEntity.java | 96 ++----------------- .../common/init/MachineBlockEntities.java | 45 ++++----- .../io/fluid/IFluidItemInteractive.java | 5 +- .../machines/common/io/fluid/TankAccess.java | 22 ++--- 11 files changed, 340 insertions(+), 191 deletions(-) create mode 100644 src/machines/java/com/enderio/machines/common/attachment/IFluidTankUser.java diff --git a/src/machines/java/com/enderio/machines/common/attachment/IFluidTankUser.java b/src/machines/java/com/enderio/machines/common/attachment/IFluidTankUser.java new file mode 100644 index 0000000000..42b160053f --- /dev/null +++ b/src/machines/java/com/enderio/machines/common/attachment/IFluidTankUser.java @@ -0,0 +1,33 @@ +package com.enderio.machines.common.attachment; + +import com.enderio.machines.common.MachineNBTKeys; +import com.enderio.machines.common.blockentity.base.MachineBlockEntity; +import com.enderio.machines.common.io.fluid.MachineFluidHandler; +import com.enderio.machines.common.io.fluid.MachineTankLayout; +import net.minecraft.core.Direction; +import net.minecraft.nbt.CompoundTag; +import net.neoforged.neoforge.capabilities.ICapabilityProvider; +import net.neoforged.neoforge.fluids.capability.IFluidHandler; + +public interface IFluidTankUser { + + MachineTankLayout getTankLayout(); + MachineFluidHandler getFluidHandler(); + MachineFluidHandler createFluidTank(); + + default void saveTank(CompoundTag pTag) { + pTag.put(MachineNBTKeys.FLUIDS, getFluidHandler().serializeNBT()); + } + + default void loadTank(CompoundTag pTag) { + getFluidHandler().deserializeNBT(pTag.getCompound(MachineNBTKeys.FLUIDS)); + } + + ICapabilityProvider FLUID_HANDLER_PROVIDER = + (be, side) ->{ + if (be instanceof IFluidTankUser user) { + return user.getFluidHandler() != null ? user.getFluidHandler().getForSide(side) : null; + } + return null; + }; +} diff --git a/src/machines/java/com/enderio/machines/common/blockentity/DrainBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/DrainBlockEntity.java index a25fbefb96..a308c88991 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/DrainBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/DrainBlockEntity.java @@ -5,17 +5,17 @@ import com.enderio.api.io.IIOConfig; import com.enderio.api.io.IOMode; import com.enderio.api.io.energy.EnergyIOMode; -import com.enderio.core.common.network.slot.BooleanNetworkDataSlot; import com.enderio.core.common.network.slot.CodecNetworkDataSlot; import com.enderio.core.common.network.slot.FluidStackNetworkDataSlot; -import com.enderio.core.common.network.slot.IntegerNetworkDataSlot; import com.enderio.machines.common.attachment.ActionRange; +import com.enderio.machines.common.attachment.IFluidTankUser; import com.enderio.machines.common.attachment.IRangedActor; import com.enderio.machines.common.blockentity.base.PoweredMachineBlockEntity; import com.enderio.machines.common.config.MachinesConfig; import com.enderio.machines.common.init.MachineAttachments; import com.enderio.machines.common.init.MachineBlockEntities; import com.enderio.machines.common.io.FixedIOConfig; +import com.enderio.machines.common.io.TransferUtil; import com.enderio.machines.common.io.fluid.MachineFluidHandler; import com.enderio.machines.common.io.fluid.MachineFluidTank; import com.enderio.machines.common.io.fluid.MachineTankLayout; @@ -24,17 +24,18 @@ import com.enderio.machines.common.menu.DrainMenu; import net.minecraft.client.multiplayer.ClientLevel; import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; -import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.material.Fluid; import net.minecraft.world.level.material.FluidState; import net.minecraft.world.level.material.Fluids; +import net.neoforged.neoforge.capabilities.Capabilities; import net.neoforged.neoforge.fluids.FluidStack; import net.neoforged.neoforge.fluids.FluidType; import net.neoforged.neoforge.fluids.capability.IFluidHandler; @@ -43,10 +44,11 @@ import java.util.ArrayList; import java.util.List; -public class DrainBlockEntity extends PoweredMachineBlockEntity implements IRangedActor { +public class DrainBlockEntity extends PoweredMachineBlockEntity implements IRangedActor, IFluidTankUser { public static final String CONSUMED = "Consumed"; private static final QuadraticScalable ENERGY_CAPACITY = new QuadraticScalable(CapacitorModifier.ENERGY_CAPACITY, MachinesConfig.COMMON.ENERGY.DRAIN_CAPACITY); private static final QuadraticScalable ENERGY_USAGE = new QuadraticScalable(CapacitorModifier.ENERGY_USE, MachinesConfig.COMMON.ENERGY.DRAIN_USAGE); + private final MachineFluidHandler fluidHandler; private static final TankAccess TANK = new TankAccess(); private static final int CAPACITY = 3 * FluidType.BUCKET_VOLUME; private static final int ENERGY_PER_BUCKET = 1_500; @@ -60,6 +62,8 @@ public class DrainBlockEntity extends PoweredMachineBlockEntity implements IRang public DrainBlockEntity(BlockPos worldPosition, BlockState blockState) { super(EnergyIOMode.Input, ENERGY_CAPACITY, ENERGY_USAGE, MachineBlockEntities.DRAIN.get(), worldPosition, blockState); + fluidHandler = createFluidTank(); + addDataSlot(new FluidStackNetworkDataSlot(() -> TANK.getFluid(this), fluid -> TANK.setFluid(this, fluid))); // TODO: rubbish way of having a default. use an interface instead? @@ -112,8 +116,13 @@ public MachineFluidTank getFluidTank() { } @Override - protected @Nullable MachineFluidHandler createFluidHandler(MachineTankLayout layout) { - return new MachineFluidHandler(getIOConfig(), layout) { + public MachineFluidHandler getFluidHandler() { + return fluidHandler; + } + + @Override + public MachineFluidHandler createFluidTank() { + return new MachineFluidHandler(getIOConfig(), getTankLayout()) { @Override protected void onContentsChanged(int slot) { super.onContentsChanged(slot); @@ -235,11 +244,32 @@ public AbstractContainerMenu createMenu(int containerId, Inventory playerInvento public void saveAdditional(CompoundTag pTag) { super.saveAdditional(pTag); pTag.putInt(CONSUMED, consumed); + saveTank(pTag); } @Override public void load(CompoundTag pTag) { super.load(pTag); consumed = pTag.getInt(CONSUMED); + loadTank(pTag); + } + + /** + * Move fluids to and fro via the given side. + */ + private void moveFluids(Direction side) { + IFluidHandler selfHandler = getSelfCapability(Capabilities.FluidHandler.BLOCK, side); + IFluidHandler otherHandler = getNeighbouringCapability(Capabilities.FluidHandler.BLOCK, side); + if (selfHandler == null || otherHandler == null) { + return; + } + + TransferUtil.distributeFluids(getIOConfig().getMode(side), selfHandler, otherHandler); + } + + @Override + public void moveResource(Direction direction) { + super.moveResource(direction); + moveFluids(direction); } } diff --git a/src/machines/java/com/enderio/machines/common/blockentity/FluidTankBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/FluidTankBlockEntity.java index bc13eac213..477e3ebfb7 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/FluidTankBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/FluidTankBlockEntity.java @@ -3,9 +3,11 @@ import com.enderio.base.common.tag.EIOTags; import com.enderio.base.common.util.ExperienceUtil; import com.enderio.core.common.network.slot.FluidStackNetworkDataSlot; +import com.enderio.machines.common.attachment.IFluidTankUser; import com.enderio.machines.common.blockentity.base.MachineBlockEntity; import com.enderio.machines.common.init.MachineBlockEntities; import com.enderio.machines.common.init.MachineRecipes; +import com.enderio.machines.common.io.TransferUtil; import com.enderio.machines.common.io.fluid.IFluidItemInteractive; import com.enderio.machines.common.io.fluid.MachineFluidHandler; import com.enderio.machines.common.io.fluid.MachineFluidTank; @@ -16,6 +18,7 @@ import com.enderio.machines.common.menu.FluidTankMenu; import com.enderio.machines.common.recipe.TankRecipe; import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; @@ -34,6 +37,7 @@ import net.neoforged.neoforge.capabilities.Capabilities; import net.neoforged.neoforge.fluids.FluidStack; import net.neoforged.neoforge.fluids.FluidType; +import net.neoforged.neoforge.fluids.FluidUtil; import net.neoforged.neoforge.fluids.capability.IFluidHandler; import net.neoforged.neoforge.fluids.capability.IFluidHandlerItem; import org.jetbrains.annotations.Nullable; @@ -43,9 +47,7 @@ // TODO: Rewrite this with tasks? // Could implement a task for each thing it currently has in the If's -public abstract class FluidTankBlockEntity extends MachineBlockEntity implements IFluidItemInteractive { - - private static final TankAccess TANK = new TankAccess(); +public abstract class FluidTankBlockEntity extends MachineBlockEntity implements IFluidItemInteractive, IFluidTankUser { public static class Standard extends FluidTankBlockEntity { public static final int CAPACITY = 16 * FluidType.BUCKET_VOLUME; @@ -75,6 +77,8 @@ public Enhanced(BlockPos worldPosition, BlockState blockState) { } private final TankRecipe.Container container; + private final MachineFluidHandler fluidHandler; + private static final TankAccess TANK = new TankAccess(); // TODO: Swap from optional to nullable? private Optional> currentRecipe = Optional.empty(); @@ -86,6 +90,7 @@ public Enhanced(BlockPos worldPosition, BlockState blockState) { public FluidTankBlockEntity(BlockEntityType type, BlockPos worldPosition, BlockState blockState) { super(type, worldPosition, blockState); + fluidHandler = createFluidTank(); // Sync fluid for model addDataSlot(new FluidStackNetworkDataSlot(() -> TANK.getFluid(this), f -> TANK.setFluid(this, f))); @@ -181,14 +186,18 @@ protected void onInventoryContentsChanged(int slot) { // endregion - // region Fluid Storage + @Override + public MachineFluidHandler getFluidHandler() { + return this.fluidHandler; + } @Override - protected @Nullable MachineFluidHandler createFluidHandler(MachineTankLayout layout) { - return new MachineFluidHandler(getIOConfig(), layout) { + public MachineFluidHandler createFluidTank() { + return new MachineFluidHandler(getIOConfig(), getTankLayout()) { @Override protected void onContentsChanged(int slot) { onTankContentsChanged(); + setChanged(); super.onContentsChanged(slot); updateMachineState(MachineState.EMPTY_TANK, TANK.getFluidAmount(this) <= 0); } @@ -199,8 +208,6 @@ public MachineFluidTank getFluidTank() { return TANK.getTank(this); } - // endregion - //TODO: enable fluid tanks to receive stackable fluid containers private void fillInternal() { ItemStack inputItem = FLUID_FILL_INPUT.getItemStack(this); @@ -216,11 +223,11 @@ private void fillInternal() { } } } else { - IFluidHandlerItem fluidHandler = inputItem.getCapability(Capabilities.FluidHandler.ITEM); - if (fluidHandler != null && outputItem.isEmpty()) { - int filled = moveFluids(fluidHandler, getFluidHandlerNN(), TANK.getCapacity(this)); + IFluidHandlerItem fluidHandlerItem = inputItem.getCapability(Capabilities.FluidHandler.ITEM); + if (fluidHandlerItem != null && outputItem.isEmpty()) { + int filled = FluidUtil.tryFluidTransfer(getFluidHandler(), fluidHandlerItem, TANK.getFluidAmount(this), true).getAmount(); if (filled > 0) { - FLUID_FILL_OUTPUT.setStackInSlot(this, fluidHandler.getContainer()); + FLUID_FILL_OUTPUT.setStackInSlot(this, fluidHandlerItem.getContainer()); FLUID_FILL_INPUT.setStackInSlot(this, ItemStack.EMPTY); } } @@ -228,6 +235,8 @@ private void fillInternal() { } } + // endregion + @Override public InteractionResult onBlockEntityUsed(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { ItemStack stack = player.getItemInHand(hand); @@ -258,11 +267,11 @@ private void drainInternal() { } } } else { - IFluidHandlerItem fluidHandler = inputItem.getCapability(Capabilities.FluidHandler.ITEM); - if (fluidHandler != null && outputItem.isEmpty()) { - int filled = moveFluids(getFluidHandlerNN(), fluidHandler, TANK.getFluidAmount(this)); + IFluidHandlerItem fluidHandlerItem = inputItem.getCapability(Capabilities.FluidHandler.ITEM); + if (fluidHandlerItem != null && outputItem.isEmpty()) { + int filled = FluidUtil.tryFluidTransfer(fluidHandlerItem, getFluidHandler(), TANK.getFluidAmount(this), true).getAmount(); if (filled > 0) { - FLUID_DRAIN_OUTPUT.setStackInSlot(this, fluidHandler.getContainer()); + FLUID_DRAIN_OUTPUT.setStackInSlot(this, fluidHandlerItem.getContainer()); FLUID_DRAIN_INPUT.setStackInSlot(this, ItemStack.EMPTY); } } @@ -352,12 +361,33 @@ public int getLightEmission() { @Override public void saveAdditional(CompoundTag pTag) { super.saveAdditional(pTag); + saveTank(pTag); } @Override public void load(CompoundTag pTag) { super.load(pTag); + loadTank(pTag); } // endregion + + /** + * Move fluids to and fro via the given side. + */ + private void moveFluids(Direction side) { + IFluidHandler selfHandler = getSelfCapability(Capabilities.FluidHandler.BLOCK, side); + IFluidHandler otherHandler = getNeighbouringCapability(Capabilities.FluidHandler.BLOCK, side); + if (selfHandler == null || otherHandler == null) { + return; + } + + TransferUtil.distributeFluids(getIOConfig().getMode(side), selfHandler, otherHandler); + } + + @Override + public void moveResource(Direction direction) { + super.moveResource(direction); + moveFluids(direction); + } } diff --git a/src/machines/java/com/enderio/machines/common/blockentity/SoulBinderBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/SoulBinderBlockEntity.java index a68b584fe7..cbc0a4d4b6 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/SoulBinderBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/SoulBinderBlockEntity.java @@ -9,12 +9,14 @@ import com.enderio.base.common.tag.EIOTags; import com.enderio.base.common.util.ExperienceUtil; import com.enderio.core.common.network.slot.IntegerNetworkDataSlot; +import com.enderio.machines.common.attachment.IFluidTankUser; import com.enderio.machines.common.blockentity.base.PoweredMachineBlockEntity; import com.enderio.machines.common.blockentity.task.PoweredCraftingMachineTask; import com.enderio.machines.common.blockentity.task.host.CraftingMachineTaskHost; import com.enderio.machines.common.config.MachinesConfig; import com.enderio.machines.common.init.MachineBlockEntities; import com.enderio.machines.common.init.MachineRecipes; +import com.enderio.machines.common.io.TransferUtil; import com.enderio.machines.common.io.fluid.MachineFluidHandler; import com.enderio.machines.common.io.fluid.MachineFluidTank; import com.enderio.machines.common.io.fluid.MachineTankLayout; @@ -26,6 +28,7 @@ import com.enderio.machines.common.recipe.RecipeCaches; import com.enderio.machines.common.recipe.SoulBindingRecipe; import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; @@ -33,19 +36,19 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.crafting.RecipeHolder; import net.minecraft.world.level.Level; -import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.material.Fluids; +import net.neoforged.fml.LogicalSide; +import net.neoforged.neoforge.capabilities.Capabilities; import net.neoforged.neoforge.fluids.FluidStack; import net.neoforged.neoforge.fluids.capability.IFluidHandler; -import net.neoforged.fml.LogicalSide; import org.jetbrains.annotations.Nullable; import java.util.List; import static com.enderio.base.common.util.ExperienceUtil.EXP_TO_FLUID; -public class SoulBinderBlockEntity extends PoweredMachineBlockEntity { +public class SoulBinderBlockEntity extends PoweredMachineBlockEntity implements IFluidTankUser { public static final QuadraticScalable CAPACITY = new QuadraticScalable(CapacitorModifier.ENERGY_CAPACITY, MachinesConfig.COMMON.ENERGY.SOUL_BINDER_CAPACITY); public static final QuadraticScalable USAGE = new QuadraticScalable(CapacitorModifier.ENERGY_USE, MachinesConfig.COMMON.ENERGY.SOUL_BINDER_USAGE); @@ -54,6 +57,7 @@ public class SoulBinderBlockEntity extends PoweredMachineBlockEntity { public static final SingleSlotAccess INPUT_OTHER = new SingleSlotAccess(); public static final MultiSlotAccess OUTPUT = new MultiSlotAccess(); private final SoulBindingRecipe.Container fakeContainer = new SoulBindingRecipe.Container(getInventoryNN(), () -> Integer.MAX_VALUE); + private final MachineFluidHandler fluidHandler; private static final TankAccess TANK = new TankAccess(); @Nullable private RecipeHolder recipe; @UseOnly(LogicalSide.CLIENT) private int clientExp = 0; @@ -62,6 +66,7 @@ public class SoulBinderBlockEntity extends PoweredMachineBlockEntity { public SoulBinderBlockEntity(BlockPos worldPosition, BlockState blockState) { super(EnergyIOMode.Input, CAPACITY, USAGE, MachineBlockEntities.SOUL_BINDER.get(), worldPosition, blockState); + fluidHandler = createFluidTank(); // Sync fluid amount to client. addDataSlot(new IntegerNetworkDataSlot(() -> TANK.getFluidAmount(this), i -> TANK.setFluid(this, new FluidStack(EIOFluids.XP_JUICE.getSource(), i)) @@ -112,11 +117,6 @@ public MachineInventoryLayout getInventoryLayout() { .build(); } - @Override - public @Nullable MachineTankLayout getTankLayout() { - return MachineTankLayout.builder().tank(TANK, 10000, f -> f.getFluid().is(EIOTags.Fluids.EXPERIENCE)).build(); - } - private boolean isValidInput(int index, ItemStack stack) { return RecipeCaches.SOUL_BINDING.hasRecipe(List.of(stack)); } @@ -136,10 +136,14 @@ public int getClientExp() { } // region Fluid Storage + @Override + public @Nullable MachineTankLayout getTankLayout() { + return MachineTankLayout.builder().tank(TANK, 10000, f -> f.getFluid().is(EIOTags.Fluids.EXPERIENCE)).build(); + } @Override - protected @Nullable MachineFluidHandler createFluidHandler(MachineTankLayout layout) { - return new MachineFluidHandler(getIOConfig(), layout) { + public MachineFluidHandler createFluidTank() { + return new MachineFluidHandler(getIOConfig(), getTankLayout()) { @Override protected void onContentsChanged(int slot) { craftingTaskHost.newTaskAvailable(); @@ -166,6 +170,11 @@ public int fill(FluidStack resource, FluidAction action) { }; } + @Override + public MachineFluidHandler getFluidHandler() { + return fluidHandler; + } + public MachineFluidTank getFluidTank() { return TANK.getTank(this); } @@ -192,7 +201,7 @@ protected void consumeInputs(SoulBindingRecipe recipe) { INPUT_SOUL.getItemStack(getInventory()).shrink(1); INPUT_OTHER.getItemStack(getInventory()).shrink(1); - MachineFluidHandler handler = getFluidHandlerNN(); + MachineFluidHandler handler = getFluidHandler(); int leftover = ExperienceUtil.getLevelFromFluidWithLeftover(TANK.getFluidAmount(handler), 0, recipe.getExpCost()).experience(); TANK.drain(handler, TANK.getFluidAmount(handler) - leftover * EXP_TO_FLUID, IFluidHandler.FluidAction.EXECUTE); } @@ -208,14 +217,34 @@ protected void consumeInputs(SoulBindingRecipe recipe) { public void saveAdditional(CompoundTag pTag) { super.saveAdditional(pTag); craftingTaskHost.save(pTag); + saveTank(pTag); } @Override public void load(CompoundTag pTag) { super.load(pTag); craftingTaskHost.load(pTag); + loadTank(pTag); } // endregion + /** + * Move fluids to and fro via the given side. + */ + private void moveFluids(Direction side) { + IFluidHandler selfHandler = getSelfCapability(Capabilities.FluidHandler.BLOCK, side); + IFluidHandler otherHandler = getNeighbouringCapability(Capabilities.FluidHandler.BLOCK, side); + if (selfHandler == null || otherHandler == null) { + return; + } + + TransferUtil.distributeFluids(getIOConfig().getMode(side), selfHandler, otherHandler); + } + + @Override + public void moveResource(Direction direction) { + super.moveResource(direction); + moveFluids(direction); + } } diff --git a/src/machines/java/com/enderio/machines/common/blockentity/SoulEngineBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/SoulEngineBlockEntity.java index 9a7c72dab8..b3124f84e6 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/SoulEngineBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/SoulEngineBlockEntity.java @@ -9,9 +9,11 @@ import com.enderio.core.common.network.slot.FluidStackNetworkDataSlot; import com.enderio.core.common.network.slot.ResourceLocationNetworkDataSlot; import com.enderio.machines.common.MachineNBTKeys; +import com.enderio.machines.common.attachment.IFluidTankUser; import com.enderio.machines.common.blockentity.base.PoweredMachineBlockEntity; import com.enderio.machines.common.config.MachinesConfig; import com.enderio.machines.common.init.MachineBlockEntities; +import com.enderio.machines.common.io.TransferUtil; import com.enderio.machines.common.io.energy.MachineEnergyStorage; import com.enderio.machines.common.io.fluid.MachineFluidHandler; import com.enderio.machines.common.io.fluid.MachineFluidTank; @@ -21,6 +23,7 @@ import com.enderio.machines.common.menu.SoulEngineMenu; import com.enderio.machines.common.souldata.EngineSoul; import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; import net.minecraft.core.Holder; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.core.registries.Registries; @@ -32,16 +35,16 @@ import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.level.Level; -import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.material.Fluid; import net.minecraft.world.level.material.Fluids; -import net.neoforged.neoforge.client.event.RecipesUpdatedEvent; import net.neoforged.bus.api.SubscribeEvent; +import net.neoforged.fml.common.Mod; +import net.neoforged.neoforge.capabilities.Capabilities; +import net.neoforged.neoforge.client.event.RecipesUpdatedEvent; import net.neoforged.neoforge.fluids.FluidStack; import net.neoforged.neoforge.fluids.FluidType; import net.neoforged.neoforge.fluids.capability.IFluidHandler; -import net.neoforged.fml.common.Mod; import org.jetbrains.annotations.Nullable; import java.util.Optional; @@ -51,7 +54,7 @@ import static com.enderio.machines.common.blockentity.PoweredSpawnerBlockEntity.NO_MOB; @Mod.EventBusSubscriber -public class SoulEngineBlockEntity extends PoweredMachineBlockEntity { +public class SoulEngineBlockEntity extends PoweredMachineBlockEntity implements IFluidTankUser { private static final QuadraticScalable CAPACITY = new QuadraticScalable(CapacitorModifier.ENERGY_CAPACITY, MachinesConfig.COMMON.ENERGY.SOUL_ENGINE_CAPACITY); public static final LinearScalable BURN_SPEED = new LinearScalable(CapacitorModifier.FIXED, MachinesConfig.COMMON.ENERGY.SOUL_ENGINE_BURN_SPEED); @@ -61,6 +64,7 @@ public class SoulEngineBlockEntity extends PoweredMachineBlockEntity { private static final String BURNED_TICKS = "BurnedTicks"; private StoredEntityData entityData = StoredEntityData.empty(); public static final int FLUID_CAPACITY = 2 * FluidType.BUCKET_VOLUME; + private final MachineFluidHandler fluidHandler; private static final TankAccess TANK = new TankAccess(); @Nullable private EngineSoul.SoulData soulData; @@ -70,8 +74,11 @@ public class SoulEngineBlockEntity extends PoweredMachineBlockEntity { public SoulEngineBlockEntity(BlockPos worldPosition, BlockState blockState) { super(EnergyIOMode.Output, CAPACITY, FixedScalable.ZERO, MachineBlockEntities.SOUL_ENGINE.get(), worldPosition, blockState); + fluidHandler = createFluidTank(); + addDataSlot(new ResourceLocationNetworkDataSlot(() -> this.getEntityType().orElse(NO_MOB),this::setEntityType)); addDataSlot(new FluidStackNetworkDataSlot(() -> TANK.getFluid(this), f -> TANK.setFluid(this, f))); + } @Override @@ -81,11 +88,6 @@ public MachineInventoryLayout getInventoryLayout() { .build(); } - @Override - public @Nullable MachineTankLayout getTankLayout() { - return MachineTankLayout.builder().tank(TANK, FLUID_CAPACITY, isFluidValid()).build(); - } - @Override public void serverTick() { if (reloadCache != reload && entityData != StoredEntityData.empty() && entityData.getEntityType().isPresent()) { @@ -137,8 +139,12 @@ public float getGenerationRate() { } @Override - protected @Nullable MachineFluidHandler createFluidHandler(MachineTankLayout layout) { - return new MachineFluidHandler(getIOConfig(), layout) { + public MachineTankLayout getTankLayout() { + return MachineTankLayout.builder().tank(TANK, FLUID_CAPACITY, isFluidValid()).build(); + } + + public MachineFluidHandler createFluidTank() { + return new MachineFluidHandler(getIOConfig(), getTankLayout()) { @Override protected void onContentsChanged(int slot) { super.onContentsChanged(slot); @@ -164,6 +170,15 @@ public int fill(FluidStack resource, FluidAction action) { }; } + public MachineFluidTank getFluidTank() { + return TANK.getTank(this); + } + + @Override + public MachineFluidHandler getFluidHandler() { + return fluidHandler; + } + private Predicate isFluidValid() { return fluidStack -> { if (soulData == null) { @@ -192,10 +207,6 @@ private Predicate isFluidValid() { } - public MachineFluidTank getFluidTank() { - return TANK.getTank(this); - } - @Nullable @Override public AbstractContainerMenu createMenu(int containerId, Inventory playerInventory, Player player) { @@ -218,6 +229,7 @@ public void saveAdditional(CompoundTag pTag) { super.saveAdditional(pTag); pTag.putInt(BURNED_TICKS, burnedTicks); pTag.put(MachineNBTKeys.ENTITY_STORAGE, entityData.serializeNBT()); + saveTank(pTag); } @Override @@ -229,6 +241,7 @@ public void load(CompoundTag pTag) { updateMachineState(MachineState.NO_POWER, false); updateMachineState(MachineState.FULL_POWER, (getEnergyStorage().getEnergyStored() >= getEnergyStorage().getMaxEnergyStored()) && isCapacitorInstalled()); + loadTank(pTag); } @Override @@ -244,4 +257,23 @@ public void setLevel(Level level) { static void onReload(RecipesUpdatedEvent event) { reload = !reload; } + + /** + * Move fluids to and fro via the given side. + */ + private void moveFluids(Direction side) { + IFluidHandler selfHandler = getSelfCapability(Capabilities.FluidHandler.BLOCK, side); + IFluidHandler otherHandler = getNeighbouringCapability(Capabilities.FluidHandler.BLOCK, side); + if (selfHandler == null || otherHandler == null) { + return; + } + + TransferUtil.distributeFluids(getIOConfig().getMode(side), selfHandler, otherHandler); + } + + @Override + public void moveResource(Direction direction) { + super.moveResource(direction); + moveFluids(direction); + } } diff --git a/src/machines/java/com/enderio/machines/common/blockentity/XPObeliskBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/XPObeliskBlockEntity.java index dea1e2002e..c0e5a2dff3 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/XPObeliskBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/XPObeliskBlockEntity.java @@ -4,31 +4,37 @@ import com.enderio.base.common.tag.EIOTags; import com.enderio.base.common.util.ExperienceUtil; import com.enderio.core.common.network.slot.IntegerNetworkDataSlot; +import com.enderio.machines.common.attachment.IFluidTankUser; import com.enderio.machines.common.blockentity.base.MachineBlockEntity; import com.enderio.machines.common.init.MachineBlockEntities; +import com.enderio.machines.common.io.TransferUtil; import com.enderio.machines.common.io.fluid.MachineFluidHandler; import com.enderio.machines.common.io.fluid.MachineFluidTank; import com.enderio.machines.common.io.fluid.MachineTankLayout; import com.enderio.machines.common.io.fluid.TankAccess; import com.enderio.machines.common.menu.XPObeliskMenu; import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; +import net.minecraft.nbt.CompoundTag; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; -import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.material.Fluids; +import net.neoforged.neoforge.capabilities.Capabilities; import net.neoforged.neoforge.fluids.FluidStack; import net.neoforged.neoforge.fluids.capability.IFluidHandler; import org.jetbrains.annotations.Nullable; -public class XPObeliskBlockEntity extends MachineBlockEntity { +public class XPObeliskBlockEntity extends MachineBlockEntity implements IFluidTankUser { IntegerNetworkDataSlot xpTankDataSlot; + private final MachineFluidHandler fluidHandler; private static final TankAccess TANK = new TankAccess(); public XPObeliskBlockEntity(BlockPos worldPosition, BlockState blockState) { super(MachineBlockEntities.XP_OBELISK.get(), worldPosition, blockState); + fluidHandler = createFluidTank(); this.xpTankDataSlot = new IntegerNetworkDataSlot(() -> TANK.getFluidAmount(this), amount -> TANK.setFluid(this, new FluidStack(EIOFluids.XP_JUICE.getSource(), amount))); @@ -42,13 +48,13 @@ public AbstractContainerMenu createMenu(int containerId, Inventory playerInvento } @Override - public @Nullable MachineTankLayout getTankLayout() { + public MachineTankLayout getTankLayout() { return new MachineTankLayout.Builder().tank(TANK, Integer.MAX_VALUE, fluidStack -> fluidStack.getFluid().is(EIOTags.Fluids.EXPERIENCE)).build(); } @Override - protected @Nullable MachineFluidHandler createFluidHandler(MachineTankLayout layout) { - return new MachineFluidHandler(getIOConfig(), layout) { + public MachineFluidHandler createFluidTank() { + return new MachineFluidHandler(getIOConfig(), getTankLayout()) { @Override protected void onContentsChanged(int slot) { super.onContentsChanged(slot); @@ -77,6 +83,11 @@ public MachineFluidTank getFluidTank() { return TANK.getTank(this); } + @Override + public MachineFluidHandler getFluidHandler() { + return fluidHandler; + } + public void addLevelToPlayer(int levelDiff, Player player) { int requestedLevel = player.experienceLevel + levelDiff; requestedLevel = Math.max(requestedLevel, 0); @@ -113,4 +124,38 @@ public void awardXP(long exp, Player player) { } } + // region Serialization + + @Override + public void saveAdditional(CompoundTag pTag) { + super.saveAdditional(pTag); + saveTank(pTag); + } + + @Override + public void load(CompoundTag pTag) { + super.load(pTag); + loadTank(pTag); + } + + // endregion + + /** + * Move fluids to and fro via the given side. + */ + private void moveFluids(Direction side) { + IFluidHandler selfHandler = getSelfCapability(Capabilities.FluidHandler.BLOCK, side); + IFluidHandler otherHandler = getNeighbouringCapability(Capabilities.FluidHandler.BLOCK, side); + if (selfHandler == null || otherHandler == null) { + return; + } + + TransferUtil.distributeFluids(getIOConfig().getMode(side), selfHandler, otherHandler); + } + + @Override + public void moveResource(Direction direction) { + super.moveResource(direction); + moveFluids(direction); + } } diff --git a/src/machines/java/com/enderio/machines/common/blockentity/XPVacuumBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/XPVacuumBlockEntity.java index 243f68085b..cca90ac59a 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/XPVacuumBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/XPVacuumBlockEntity.java @@ -2,32 +2,38 @@ import com.enderio.base.common.init.EIOFluids; import com.enderio.core.common.network.slot.IntegerNetworkDataSlot; +import com.enderio.machines.common.attachment.IFluidTankUser; import com.enderio.machines.common.blockentity.base.VacuumMachineBlockEntity; import com.enderio.machines.common.config.MachinesConfig; import com.enderio.machines.common.init.MachineBlockEntities; +import com.enderio.machines.common.io.TransferUtil; import com.enderio.machines.common.io.fluid.MachineFluidHandler; import com.enderio.machines.common.io.fluid.MachineFluidTank; import com.enderio.machines.common.io.fluid.MachineTankLayout; import com.enderio.machines.common.io.fluid.TankAccess; import com.enderio.machines.common.menu.XPVacuumMenu; import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; +import net.minecraft.nbt.CompoundTag; import net.minecraft.world.entity.ExperienceOrb; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; -import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockState; +import net.neoforged.neoforge.capabilities.Capabilities; import net.neoforged.neoforge.fluids.FluidStack; +import net.neoforged.neoforge.fluids.capability.IFluidHandler; import net.neoforged.neoforge.fluids.capability.IFluidHandler.FluidAction; -import org.jetbrains.annotations.Nullable; import static com.enderio.base.common.util.ExperienceUtil.EXP_TO_FLUID; -public class XPVacuumBlockEntity extends VacuumMachineBlockEntity { +public class XPVacuumBlockEntity extends VacuumMachineBlockEntity implements IFluidTankUser { + private final MachineFluidHandler fluidHandler; private static final TankAccess TANK = new TankAccess(); public XPVacuumBlockEntity(BlockPos pWorldPosition, BlockState pBlockState) { super(MachineBlockEntities.XP_VACUUM.get(), pWorldPosition, pBlockState, ExperienceOrb.class); + fluidHandler = createFluidTank(); // Sync fluid level. addDataSlot(new IntegerNetworkDataSlot(() -> TANK.getFluidAmount(this), i -> TANK.setFluid(this, new FluidStack(EIOFluids.XP_JUICE.getSource(), i)) @@ -58,13 +64,13 @@ public void handleEntity(ExperienceOrb xpe) { // region Fluid Storage @Override - public @Nullable MachineTankLayout getTankLayout() { + public MachineTankLayout getTankLayout() { return new MachineTankLayout.Builder().tank(TANK, Integer.MAX_VALUE).build(); } @Override - protected @Nullable MachineFluidHandler createFluidHandler(MachineTankLayout layout) { - return new MachineFluidHandler(getIOConfig(), layout) { + public MachineFluidHandler createFluidTank() { + return new MachineFluidHandler(getIOConfig(), getTankLayout()) { @Override protected void onContentsChanged(int slot) { super.onContentsChanged(slot); @@ -77,5 +83,45 @@ public MachineFluidTank getFluidTank() { return TANK.getTank(this); } + @Override + public MachineFluidHandler getFluidHandler() { + return fluidHandler; + } + // endregion + + // region Serialization + + @Override + public void saveAdditional(CompoundTag pTag) { + super.saveAdditional(pTag); + saveTank(pTag); + } + + @Override + public void load(CompoundTag pTag) { + super.load(pTag); + loadTank(pTag); + } + + // endregion + + /** + * Move fluids to and fro via the given side. + */ + private void moveFluids(Direction side) { + IFluidHandler selfHandler = getSelfCapability(Capabilities.FluidHandler.BLOCK, side); + IFluidHandler otherHandler = getNeighbouringCapability(Capabilities.FluidHandler.BLOCK, side); + if (selfHandler == null || otherHandler == null) { + return; + } + + TransferUtil.distributeFluids(getIOConfig().getMode(side), selfHandler, otherHandler); + } + + @Override + public void moveResource(Direction direction) { + super.moveResource(direction); + moveFluids(direction); + } } diff --git a/src/machines/java/com/enderio/machines/common/blockentity/base/MachineBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/base/MachineBlockEntity.java index 9cadb15487..875be55dfe 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/base/MachineBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/base/MachineBlockEntity.java @@ -16,11 +16,8 @@ import com.enderio.machines.common.init.MachineAttachments; import com.enderio.machines.common.io.IOConfig; import com.enderio.machines.common.io.TransferUtil; -import com.enderio.machines.common.io.fluid.MachineFluidHandler; -import com.enderio.machines.common.io.fluid.MachineTankLayout; import com.enderio.machines.common.io.item.MachineInventory; import com.enderio.machines.common.io.item.MachineInventoryLayout; -import net.minecraft.client.multiplayer.ClientLevel; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; @@ -42,9 +39,6 @@ import net.neoforged.neoforge.capabilities.ICapabilityProvider; import net.neoforged.neoforge.client.model.data.ModelData; import net.neoforged.neoforge.client.model.data.ModelProperty; -import net.neoforged.neoforge.fluids.FluidStack; -import net.neoforged.neoforge.fluids.FluidUtil; -import net.neoforged.neoforge.fluids.capability.IFluidHandler; import net.neoforged.neoforge.items.IItemHandler; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -62,9 +56,6 @@ public abstract class MachineBlockEntity extends EnderBlockEntity implements Men public static final ICapabilityProvider ITEM_HANDLER_PROVIDER = (be, side) -> be.inventory != null ? be.inventory.getForSide(side) : null; - public static final ICapabilityProvider FLUID_HANDLER_PROVIDER = - (be, side) -> be.fluidHandler != null ? be.fluidHandler.getForSide(side) : null; - // region IO Configuration private final IIOConfig ioConfig; @@ -75,14 +66,11 @@ public abstract class MachineBlockEntity extends EnderBlockEntity implements Men // endregion - // region Items and Fluids + // region Items @Nullable private final MachineInventory inventory; - @Nullable - private final MachineFluidHandler fluidHandler; - // endregion // region Common Dataslots @@ -108,14 +96,6 @@ public MachineBlockEntity(BlockEntityType type, BlockPos worldPosition, Block inventory = null; } - // Create fluid storage - MachineTankLayout tankLayout = getTankLayout(); - if (tankLayout != null) { - fluidHandler = createFluidHandler(tankLayout); - } else { - fluidHandler = null; - } - if (supportsRedstoneControl()) { redstoneControlDataSlot = addDataSlot(new EnumNetworkDataSlot<>(RedstoneControl.class, this::getRedstoneControl, this::internalSetRedstoneControl)); @@ -314,43 +294,6 @@ protected void onInventoryContentsChanged(int slot) { } // endregion - // region Fluid Storage - - @Nullable - public MachineTankLayout getTankLayout() { - return null; - } - - @Nullable - public final MachineFluidHandler getFluidHandler() { - return fluidHandler; - } - - /** - * Only call this if you're sure your machine has an tank. - */ - protected final MachineFluidHandler getFluidHandlerNN() { - return Objects.requireNonNull(fluidHandler); - } - - @Nullable - protected MachineFluidHandler createFluidHandler(MachineTankLayout layout) { - return new MachineFluidHandler(getIOConfig(), layout) { - @Override - protected void onContentsChanged(int slot) { - onTankContentsChanged(slot); - setChanged(); - } - }; - } - - /** - * @apiNote Must call this on custom MachineFluid handlers! - */ - protected void onTankContentsChanged(int slot) {} - - // endregion - // region Block Entity ticking @Override @@ -395,12 +338,15 @@ private void forceResources() { for (Direction direction : Direction.values()) { if (ioConfig.getMode(direction).canForce()) { // TODO: Maybe some kind of resource distributor so that items are transmitted evenly around? rather than taking the order of Direction.values() - moveItems(direction); - moveFluids(direction); + moveResource(direction); } } } + public void moveResource(Direction direction) { + moveItems(direction); + } + /** * Move items to and fro via the given side. */ @@ -414,28 +360,6 @@ private void moveItems(Direction side) { TransferUtil.distributeItems(ioConfig.getMode(side), selfHandler, otherHandler); } - /** - * Move fluids to and fro via the given side. - */ - private void moveFluids(Direction side) { - IFluidHandler selfHandler = getSelfCapability(Capabilities.FluidHandler.BLOCK, side); - IFluidHandler otherHandler = getNeighbouringCapability(Capabilities.FluidHandler.BLOCK, side); - if (selfHandler == null || otherHandler == null) { - return; - } - - TransferUtil.distributeFluids(ioConfig.getMode(side), selfHandler, otherHandler); - } - - /** - * Move fluids from one handler to the other. - */ - @Deprecated(forRemoval = true) - protected int moveFluids(IFluidHandler from, IFluidHandler to, int maxDrain) { - FluidStack stack = FluidUtil.tryFluidTransfer(to, from, maxDrain, true); - return stack.getAmount(); - } - // endregion // region Serialization @@ -450,10 +374,6 @@ public void saveAdditional(CompoundTag pTag) { if (this.inventory != null) { pTag.put(MachineNBTKeys.ITEMS, inventory.serializeNBT()); } - - if (this.fluidHandler != null) { - pTag.put(MachineNBTKeys.FLUIDS, fluidHandler.serializeNBT()); - } } @Override @@ -465,10 +385,6 @@ public void load(CompoundTag pTag) { inventory.deserializeNBT(pTag.getCompound(MachineNBTKeys.ITEMS)); } - if (this.fluidHandler != null) { - fluidHandler.deserializeNBT(pTag.getCompound(MachineNBTKeys.FLUIDS)); - } - // For rendering io overlays after placed by an nbt filled block item if (this.level != null) { onIOConfigChanged(); diff --git a/src/machines/java/com/enderio/machines/common/init/MachineBlockEntities.java b/src/machines/java/com/enderio/machines/common/init/MachineBlockEntities.java index 7fbb42d817..dcf5dd3846 100644 --- a/src/machines/java/com/enderio/machines/common/init/MachineBlockEntities.java +++ b/src/machines/java/com/enderio/machines/common/init/MachineBlockEntities.java @@ -5,26 +5,8 @@ import com.enderio.machines.client.rendering.blockentity.CapacitorBankBER; import com.enderio.machines.client.rendering.blockentity.FluidTankBER; import com.enderio.machines.client.rendering.blockentity.XPObeliskBER; -import com.enderio.machines.common.blockentity.AlloySmelterBlockEntity; -import com.enderio.machines.common.blockentity.CrafterBlockEntity; -import com.enderio.machines.common.blockentity.CreativePowerBlockEntity; -import com.enderio.machines.common.blockentity.DrainBlockEntity; -import com.enderio.machines.common.blockentity.EnchanterBlockEntity; -import com.enderio.machines.common.blockentity.FluidTankBlockEntity; -import com.enderio.machines.common.blockentity.ImpulseHopperBlockEntity; -import com.enderio.machines.common.blockentity.PaintingMachineBlockEntity; -import com.enderio.machines.common.blockentity.PoweredSpawnerBlockEntity; -import com.enderio.machines.common.blockentity.PrimitiveAlloySmelterBlockEntity; -import com.enderio.machines.common.blockentity.SagMillBlockEntity; -import com.enderio.machines.common.blockentity.SlicerBlockEntity; -import com.enderio.machines.common.blockentity.SoulBinderBlockEntity; -import com.enderio.machines.common.blockentity.SoulEngineBlockEntity; -import com.enderio.machines.common.blockentity.StirlingGeneratorBlockEntity; -import com.enderio.machines.common.blockentity.TravelAnchorBlockEntity; -import com.enderio.machines.common.blockentity.VacuumChestBlockEntity; -import com.enderio.machines.common.blockentity.WiredChargerBlockEntity; -import com.enderio.machines.common.blockentity.XPObeliskBlockEntity; -import com.enderio.machines.common.blockentity.XPVacuumBlockEntity; +import com.enderio.machines.common.attachment.IFluidTankUser; +import com.enderio.machines.common.blockentity.*; import com.enderio.machines.common.blockentity.base.MachineBlockEntity; import com.enderio.machines.common.blockentity.base.PoweredMachineBlockEntity; import com.enderio.machines.common.blockentity.capacitorbank.CapacitorBankBlockEntity; @@ -52,12 +34,15 @@ public class MachineBlockEntities { public static final RegiliteBlockEntity FLUID_TANK = register("fluid_tank", FluidTankBlockEntity.Standard::new, MachineBlocks.FLUID_TANK) .setRenderer(() -> FluidTankBER::new) - .apply(MachineBlockEntities::machineBlockEntityCapabilities); + .apply(MachineBlockEntities::machineBlockEntityCapabilities) + .addCapability(Capabilities.FluidHandler.BLOCK, IFluidTankUser.FLUID_HANDLER_PROVIDER); + public static final RegiliteBlockEntity PRESSURIZED_FLUID_TANK = register("pressurized_fluid_tank", FluidTankBlockEntity.Enhanced::new, MachineBlocks.PRESSURIZED_FLUID_TANK) .setRenderer(() -> FluidTankBER::new) - .apply(MachineBlockEntities::machineBlockEntityCapabilities); + .apply(MachineBlockEntities::machineBlockEntityCapabilities) + .addCapability(Capabilities.FluidHandler.BLOCK, IFluidTankUser.FLUID_HANDLER_PROVIDER); public static final RegiliteBlockEntity ENCHANTER = register("enchanter", EnchanterBlockEntity::new, MachineBlocks.ENCHANTER); @@ -96,7 +81,8 @@ public class MachineBlockEntities { public static final RegiliteBlockEntity XP_VACUUM = register("xp_vacuum", XPVacuumBlockEntity::new, MachineBlocks.XP_VACUUM) - .apply(MachineBlockEntities::machineBlockEntityCapabilities); + .apply(MachineBlockEntities::machineBlockEntityCapabilities) + .addCapability(Capabilities.FluidHandler.BLOCK, IFluidTankUser.FLUID_HANDLER_PROVIDER); public static final RegiliteBlockEntity TRAVEL_ANCHOR = register("travel_anchor", TravelAnchorBlockEntity::new, MachineBlocks.TRAVEL_ANCHOR) @@ -108,11 +94,13 @@ public class MachineBlockEntities { public static final RegiliteBlockEntity DRAIN = register("drain", DrainBlockEntity::new, MachineBlocks.DRAIN) - .apply(MachineBlockEntities::poweredMachineBlockEntityCapabilities); + .apply(MachineBlockEntities::poweredMachineBlockEntityCapabilities) + .addCapability(Capabilities.FluidHandler.BLOCK, IFluidTankUser.FLUID_HANDLER_PROVIDER); public static final RegiliteBlockEntity SOUL_BINDER = register("soul_binder", SoulBinderBlockEntity::new, MachineBlocks.SOUL_BINDER) - .apply(MachineBlockEntities::poweredMachineBlockEntityCapabilities); + .apply(MachineBlockEntities::poweredMachineBlockEntityCapabilities) + .addCapability(Capabilities.FluidHandler.BLOCK, IFluidTankUser.FLUID_HANDLER_PROVIDER); public static final RegiliteBlockEntity WIRED_CHARGER = register("wired_charger", WiredChargerBlockEntity::new, MachineBlocks.WIRED_CHARGER) @@ -156,12 +144,14 @@ public class MachineBlockEntities { public static final RegiliteBlockEntity SOUL_ENGINE = register("soul_engine", SoulEngineBlockEntity::new, MachineBlocks.SOUL_ENGINE) - .apply(MachineBlockEntities::poweredMachineBlockEntityCapabilities); + .apply(MachineBlockEntities::poweredMachineBlockEntityCapabilities) + .addCapability(Capabilities.FluidHandler.BLOCK, IFluidTankUser.FLUID_HANDLER_PROVIDER); public static final RegiliteBlockEntity XP_OBELISK = register("xp_obelisk", XPObeliskBlockEntity::new, MachineBlocks.XP_OBELISK) .setRenderer(() -> XPObeliskBER::new) - .apply(MachineBlockEntities::machineBlockEntityCapabilities); + .apply(MachineBlockEntities::machineBlockEntityCapabilities) + .addCapability(Capabilities.FluidHandler.BLOCK, IFluidTankUser.FLUID_HANDLER_PROVIDER); @SafeVarargs private static RegiliteBlockEntity register(String name, BlockEntityType.BlockEntitySupplier beFactory, @@ -173,7 +163,6 @@ private static RegiliteBlockEntity register(String na private static void machineBlockEntityCapabilities(RegiliteBlockEntity blockEntity) { blockEntity.addCapability(EIOCapabilities.SideConfig.BLOCK, MachineBlockEntity.SIDE_CONFIG_PROVIDER); blockEntity.addCapability(Capabilities.ItemHandler.BLOCK, MachineBlockEntity.ITEM_HANDLER_PROVIDER); - blockEntity.addCapability(Capabilities.FluidHandler.BLOCK, MachineBlockEntity.FLUID_HANDLER_PROVIDER); } private static void poweredMachineBlockEntityCapabilities(RegiliteBlockEntity blockEntity) { diff --git a/src/machines/java/com/enderio/machines/common/io/fluid/IFluidItemInteractive.java b/src/machines/java/com/enderio/machines/common/io/fluid/IFluidItemInteractive.java index b0bb1e5d34..358e52766d 100644 --- a/src/machines/java/com/enderio/machines/common/io/fluid/IFluidItemInteractive.java +++ b/src/machines/java/com/enderio/machines/common/io/fluid/IFluidItemInteractive.java @@ -1,10 +1,9 @@ package com.enderio.machines.common.io.fluid; -import com.enderio.machines.common.blockentity.base.MachineBlockEntity; +import com.enderio.machines.common.attachment.IFluidTankUser; import net.minecraft.world.InteractionHand; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; -import net.neoforged.neoforge.capabilities.Capabilities; import net.neoforged.neoforge.fluids.FluidStack; import net.neoforged.neoforge.fluids.FluidUtil; import net.neoforged.neoforge.fluids.capability.IFluidHandler; @@ -19,7 +18,7 @@ public interface IFluidItemInteractive { // Requires direct tank access which is undesirable. MachineFluidHandler would be better to for multi-tank block. - default boolean handleFluidItemInteraction(Player player, InteractionHand hand, ItemStack itemStack, MachineBlockEntity machine, TankAccess tankAccess) { + default boolean handleFluidItemInteraction(Player player, InteractionHand hand, ItemStack itemStack, IFluidTankUser machine, TankAccess tankAccess) { ItemStack copyStack = itemStack.copyWithCount(1); Optional fluidHandlerItem = FluidUtil.getFluidHandler(copyStack); if (fluidHandlerItem.isPresent()) { diff --git a/src/machines/java/com/enderio/machines/common/io/fluid/TankAccess.java b/src/machines/java/com/enderio/machines/common/io/fluid/TankAccess.java index 2f8f90a038..66621904e1 100644 --- a/src/machines/java/com/enderio/machines/common/io/fluid/TankAccess.java +++ b/src/machines/java/com/enderio/machines/common/io/fluid/TankAccess.java @@ -1,6 +1,6 @@ package com.enderio.machines.common.io.fluid; -import com.enderio.machines.common.blockentity.base.MachineBlockEntity; +import com.enderio.machines.common.attachment.IFluidTankUser; import net.neoforged.neoforge.fluids.FluidStack; import net.neoforged.neoforge.fluids.capability.IFluidHandler; @@ -8,7 +8,7 @@ public class TankAccess { private int index = Integer.MIN_VALUE; - public MachineFluidTank getTank(MachineBlockEntity machine) { + public MachineFluidTank getTank(IFluidTankUser machine) { return getTank(machine.getFluidHandler()); } @@ -16,7 +16,7 @@ public MachineFluidTank getTank(MachineFluidHandler fluidHandler) { return fluidHandler.getTank(index); } - public int getCapacity(MachineBlockEntity machine) { + public int getCapacity(IFluidTankUser machine) { return getCapacity(machine.getFluidHandler()); } @@ -24,7 +24,7 @@ public int getCapacity(MachineFluidHandler fluidHandler) { return fluidHandler.getTankCapacity(index); } - public FluidStack getFluid(MachineBlockEntity machine) { + public FluidStack getFluid(IFluidTankUser machine) { return getFluid(machine.getFluidHandler()); } @@ -32,7 +32,7 @@ public FluidStack getFluid(MachineFluidHandler handler) { return handler.getFluidInTank(index); } - public int getFluidAmount(MachineBlockEntity machine) { + public int getFluidAmount(IFluidTankUser machine) { return getFluid(machine).getAmount(); } @@ -40,7 +40,7 @@ public int getFluidAmount(MachineFluidHandler handler) { return getFluid(handler).getAmount(); } - public void setFluid(MachineBlockEntity machine, FluidStack fluid) { + public void setFluid(IFluidTankUser machine, FluidStack fluid) { setFluid(machine.getFluidHandler(), fluid); } @@ -48,7 +48,7 @@ public void setFluid(MachineFluidHandler handler, FluidStack fluid) { handler.setFluidInTank(index, fluid); } - public boolean isFluidValid(MachineBlockEntity machine, FluidStack fluid) { + public boolean isFluidValid(IFluidTankUser machine, FluidStack fluid) { return isFluidValid(machine.getFluidHandler(), fluid); } @@ -56,7 +56,7 @@ public boolean isFluidValid(MachineFluidHandler handler, FluidStack fluid) { return handler.isFluidValid(index, fluid); } - public boolean isEmpty(MachineBlockEntity machine) { + public boolean isEmpty(IFluidTankUser machine) { return isEmpty(machine.getFluidHandler()); } @@ -68,7 +68,7 @@ public int fill(MachineFluidHandler handler, FluidStack stack, IFluidHandler.Flu return handler.fill(stack, action); } - public int fill(MachineBlockEntity machine, FluidStack stack, IFluidHandler.FluidAction action) { + public int fill(IFluidTankUser machine, FluidStack stack, IFluidHandler.FluidAction action) { return fill(machine.getFluidHandler(), stack, action); } @@ -76,7 +76,7 @@ public FluidStack drain(MachineFluidHandler handler, FluidStack resource, IFluid return handler.drain(resource, action); } - public FluidStack drain(MachineBlockEntity machine, FluidStack resource, IFluidHandler.FluidAction action) { + public FluidStack drain(IFluidTankUser machine, FluidStack resource, IFluidHandler.FluidAction action) { return drain(machine.getFluidHandler(), resource, action); } @@ -84,7 +84,7 @@ public FluidStack drain(MachineFluidHandler handler, int maxDrain, IFluidHandler return handler.drain(maxDrain, action); } - public FluidStack drain(MachineBlockEntity machine, int maxDrain, IFluidHandler.FluidAction action) { + public FluidStack drain(IFluidTankUser machine, int maxDrain, IFluidHandler.FluidAction action) { return drain(machine.getFluidHandler(), maxDrain, action); } From cb153aca04dbaa612639c330113c63f78709eac4 Mon Sep 17 00:00:00 2001 From: ferriarnus <61201275+ferriarnus@users.noreply.github.com> Date: Fri, 23 Feb 2024 00:35:31 +0100 Subject: [PATCH 2/5] fix transfer --- .../enderio/machines/common/attachment/IFluidTankUser.java | 2 +- .../machines/common/blockentity/FluidTankBlockEntity.java | 4 ++-- .../java/com/enderio/machines/common/io/TransferUtil.java | 7 +++++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/machines/java/com/enderio/machines/common/attachment/IFluidTankUser.java b/src/machines/java/com/enderio/machines/common/attachment/IFluidTankUser.java index 42b160053f..b87a16343f 100644 --- a/src/machines/java/com/enderio/machines/common/attachment/IFluidTankUser.java +++ b/src/machines/java/com/enderio/machines/common/attachment/IFluidTankUser.java @@ -26,7 +26,7 @@ default void loadTank(CompoundTag pTag) { ICapabilityProvider FLUID_HANDLER_PROVIDER = (be, side) ->{ if (be instanceof IFluidTankUser user) { - return user.getFluidHandler() != null ? user.getFluidHandler().getForSide(side) : null; + return user.getFluidHandler().getForSide(side); } return null; }; diff --git a/src/machines/java/com/enderio/machines/common/blockentity/FluidTankBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/FluidTankBlockEntity.java index 477e3ebfb7..7738d7d2da 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/FluidTankBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/FluidTankBlockEntity.java @@ -57,7 +57,7 @@ public Standard(BlockPos worldPosition, BlockState blockState) { } @Override - public @Nullable MachineTankLayout getTankLayout() { + public MachineTankLayout getTankLayout() { return new MachineTankLayout.Builder().tank(TANK, CAPACITY).build(); } } @@ -70,7 +70,7 @@ public Enhanced(BlockPos worldPosition, BlockState blockState) { } @Override - public @Nullable MachineTankLayout getTankLayout() { + public MachineTankLayout getTankLayout() { return new MachineTankLayout.Builder().tank(TANK, CAPACITY).build(); } diff --git a/src/machines/java/com/enderio/machines/common/io/TransferUtil.java b/src/machines/java/com/enderio/machines/common/io/TransferUtil.java index 9f4ff4424e..03f0e3cdee 100644 --- a/src/machines/java/com/enderio/machines/common/io/TransferUtil.java +++ b/src/machines/java/com/enderio/machines/common/io/TransferUtil.java @@ -61,11 +61,14 @@ public static void distributeFluids(boolean canPush, boolean canPull, IFluidHand // TODO: Do we want to imitate old behaviour where if we have no fluid, we pull by default? if (canPush) { - FluidUtil.tryFluidTransfer(selfItemHandler, otherItemHandler, maxDrain, true); + int filled = FluidUtil.tryFluidTransfer(otherItemHandler, selfItemHandler, maxDrain, true).getAmount(); + if (filled > 0) { + return; + } } if (canPull) { - FluidUtil.tryFluidTransfer(otherItemHandler, selfItemHandler, maxDrain, true); + FluidUtil.tryFluidTransfer(selfItemHandler, otherItemHandler, maxDrain, true); } } From 4ef24fbc7009f444e7d498e818672d7aa6e29ce1 Mon Sep 17 00:00:00 2001 From: ferriarnus <61201275+ferriarnus@users.noreply.github.com> Date: Sat, 24 Feb 2024 20:04:23 +0100 Subject: [PATCH 3/5] fluid tank changes --- .../common/attachment/IFluidTankUser.java | 2 +- .../common/blockentity/DrainBlockEntity.java | 4 +- .../blockentity/FluidTankBlockEntity.java | 4 +- .../blockentity/SoulBinderBlockEntity.java | 4 +- .../blockentity/SoulEngineBlockEntity.java | 4 +- .../blockentity/XPObeliskBlockEntity.java | 4 +- .../blockentity/XPVacuumBlockEntity.java | 4 +- .../common/io/fluid/MachineFluidHandler.java | 98 ++++++++++++++----- .../common/io/fluid/MachineFluidTank.java | 81 ++++----------- .../common/io/fluid/MachineTankLayout.java | 4 +- 10 files changed, 103 insertions(+), 106 deletions(-) diff --git a/src/machines/java/com/enderio/machines/common/attachment/IFluidTankUser.java b/src/machines/java/com/enderio/machines/common/attachment/IFluidTankUser.java index b87a16343f..3fb721bbe9 100644 --- a/src/machines/java/com/enderio/machines/common/attachment/IFluidTankUser.java +++ b/src/machines/java/com/enderio/machines/common/attachment/IFluidTankUser.java @@ -13,7 +13,7 @@ public interface IFluidTankUser { MachineTankLayout getTankLayout(); MachineFluidHandler getFluidHandler(); - MachineFluidHandler createFluidTank(); + MachineFluidHandler createFluidHandler(); default void saveTank(CompoundTag pTag) { pTag.put(MachineNBTKeys.FLUIDS, getFluidHandler().serializeNBT()); diff --git a/src/machines/java/com/enderio/machines/common/blockentity/DrainBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/DrainBlockEntity.java index a308c88991..2d39467069 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/DrainBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/DrainBlockEntity.java @@ -62,7 +62,7 @@ public class DrainBlockEntity extends PoweredMachineBlockEntity implements IRang public DrainBlockEntity(BlockPos worldPosition, BlockState blockState) { super(EnergyIOMode.Input, ENERGY_CAPACITY, ENERGY_USAGE, MachineBlockEntities.DRAIN.get(), worldPosition, blockState); - fluidHandler = createFluidTank(); + fluidHandler = createFluidHandler(); addDataSlot(new FluidStackNetworkDataSlot(() -> TANK.getFluid(this), fluid -> TANK.setFluid(this, fluid))); @@ -121,7 +121,7 @@ public MachineFluidHandler getFluidHandler() { } @Override - public MachineFluidHandler createFluidTank() { + public MachineFluidHandler createFluidHandler() { return new MachineFluidHandler(getIOConfig(), getTankLayout()) { @Override protected void onContentsChanged(int slot) { diff --git a/src/machines/java/com/enderio/machines/common/blockentity/FluidTankBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/FluidTankBlockEntity.java index 7738d7d2da..0efae3fa80 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/FluidTankBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/FluidTankBlockEntity.java @@ -90,7 +90,7 @@ public MachineTankLayout getTankLayout() { public FluidTankBlockEntity(BlockEntityType type, BlockPos worldPosition, BlockState blockState) { super(type, worldPosition, blockState); - fluidHandler = createFluidTank(); + fluidHandler = createFluidHandler(); // Sync fluid for model addDataSlot(new FluidStackNetworkDataSlot(() -> TANK.getFluid(this), f -> TANK.setFluid(this, f))); @@ -192,7 +192,7 @@ public MachineFluidHandler getFluidHandler() { } @Override - public MachineFluidHandler createFluidTank() { + public MachineFluidHandler createFluidHandler() { return new MachineFluidHandler(getIOConfig(), getTankLayout()) { @Override protected void onContentsChanged(int slot) { diff --git a/src/machines/java/com/enderio/machines/common/blockentity/SoulBinderBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/SoulBinderBlockEntity.java index cbc0a4d4b6..28207c9159 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/SoulBinderBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/SoulBinderBlockEntity.java @@ -66,7 +66,7 @@ public class SoulBinderBlockEntity extends PoweredMachineBlockEntity implements public SoulBinderBlockEntity(BlockPos worldPosition, BlockState blockState) { super(EnergyIOMode.Input, CAPACITY, USAGE, MachineBlockEntities.SOUL_BINDER.get(), worldPosition, blockState); - fluidHandler = createFluidTank(); + fluidHandler = createFluidHandler(); // Sync fluid amount to client. addDataSlot(new IntegerNetworkDataSlot(() -> TANK.getFluidAmount(this), i -> TANK.setFluid(this, new FluidStack(EIOFluids.XP_JUICE.getSource(), i)) @@ -142,7 +142,7 @@ public int getClientExp() { } @Override - public MachineFluidHandler createFluidTank() { + public MachineFluidHandler createFluidHandler() { return new MachineFluidHandler(getIOConfig(), getTankLayout()) { @Override protected void onContentsChanged(int slot) { diff --git a/src/machines/java/com/enderio/machines/common/blockentity/SoulEngineBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/SoulEngineBlockEntity.java index b3124f84e6..9691e9600c 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/SoulEngineBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/SoulEngineBlockEntity.java @@ -74,7 +74,7 @@ public class SoulEngineBlockEntity extends PoweredMachineBlockEntity implements public SoulEngineBlockEntity(BlockPos worldPosition, BlockState blockState) { super(EnergyIOMode.Output, CAPACITY, FixedScalable.ZERO, MachineBlockEntities.SOUL_ENGINE.get(), worldPosition, blockState); - fluidHandler = createFluidTank(); + fluidHandler = createFluidHandler(); addDataSlot(new ResourceLocationNetworkDataSlot(() -> this.getEntityType().orElse(NO_MOB),this::setEntityType)); addDataSlot(new FluidStackNetworkDataSlot(() -> TANK.getFluid(this), f -> TANK.setFluid(this, f))); @@ -143,7 +143,7 @@ public MachineTankLayout getTankLayout() { return MachineTankLayout.builder().tank(TANK, FLUID_CAPACITY, isFluidValid()).build(); } - public MachineFluidHandler createFluidTank() { + public MachineFluidHandler createFluidHandler() { return new MachineFluidHandler(getIOConfig(), getTankLayout()) { @Override protected void onContentsChanged(int slot) { diff --git a/src/machines/java/com/enderio/machines/common/blockentity/XPObeliskBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/XPObeliskBlockEntity.java index c0e5a2dff3..ea14646edb 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/XPObeliskBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/XPObeliskBlockEntity.java @@ -34,7 +34,7 @@ public class XPObeliskBlockEntity extends MachineBlockEntity implements IFluidTa public XPObeliskBlockEntity(BlockPos worldPosition, BlockState blockState) { super(MachineBlockEntities.XP_OBELISK.get(), worldPosition, blockState); - fluidHandler = createFluidTank(); + fluidHandler = createFluidHandler(); this.xpTankDataSlot = new IntegerNetworkDataSlot(() -> TANK.getFluidAmount(this), amount -> TANK.setFluid(this, new FluidStack(EIOFluids.XP_JUICE.getSource(), amount))); @@ -53,7 +53,7 @@ public MachineTankLayout getTankLayout() { } @Override - public MachineFluidHandler createFluidTank() { + public MachineFluidHandler createFluidHandler() { return new MachineFluidHandler(getIOConfig(), getTankLayout()) { @Override protected void onContentsChanged(int slot) { diff --git a/src/machines/java/com/enderio/machines/common/blockentity/XPVacuumBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/XPVacuumBlockEntity.java index cca90ac59a..a7899ac67a 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/XPVacuumBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/XPVacuumBlockEntity.java @@ -33,7 +33,7 @@ public class XPVacuumBlockEntity extends VacuumMachineBlockEntity private static final TankAccess TANK = new TankAccess(); public XPVacuumBlockEntity(BlockPos pWorldPosition, BlockState pBlockState) { super(MachineBlockEntities.XP_VACUUM.get(), pWorldPosition, pBlockState, ExperienceOrb.class); - fluidHandler = createFluidTank(); + fluidHandler = createFluidHandler(); // Sync fluid level. addDataSlot(new IntegerNetworkDataSlot(() -> TANK.getFluidAmount(this), i -> TANK.setFluid(this, new FluidStack(EIOFluids.XP_JUICE.getSource(), i)) @@ -69,7 +69,7 @@ public MachineTankLayout getTankLayout() { } @Override - public MachineFluidHandler createFluidTank() { + public MachineFluidHandler createFluidHandler() { return new MachineFluidHandler(getIOConfig(), getTankLayout()) { @Override protected void onContentsChanged(int slot) { diff --git a/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidHandler.java b/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidHandler.java index c629b8b190..9fd57f0e90 100644 --- a/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidHandler.java +++ b/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidHandler.java @@ -11,6 +11,7 @@ import net.neoforged.neoforge.fluids.capability.IFluidHandler; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; import java.util.List; import java.util.function.IntConsumer; @@ -21,8 +22,6 @@ public class MachineFluidHandler implements IFluidHandler, INBTSerializable tanks; @@ -33,7 +32,7 @@ public class MachineFluidHandler implements IFluidHandler, INBTSerializable 0) + onContentsChanged(tank); + return filled; + } + @Override public int fill(FluidStack resource, FluidAction action) { // Don't waste any time. @@ -107,7 +147,7 @@ public int fill(FluidStack resource, FluidAction action) { continue; // Attempt to fill the tank - int filled = tanks.get(index).fill(resourceLeft, action); + int filled = fill(index, resourceLeft, action); resourceLeft.shrink(filled); totalFilled += filled; @@ -124,30 +164,36 @@ public int fill(FluidStack resource, FluidAction action) { return totalFilled; } - @Override - public FluidStack drain(FluidStack resource, FluidAction action) { - for (int index = 0; index < tanks.size(); index++) { - if (!layout.canExtract(index)) - continue; - - if (tanks.get(index).drain(resource, FluidAction.SIMULATE) != FluidStack.EMPTY) { - FluidStack drained = tanks.get(index).drain(resource, action); - if (!drained.isEmpty()) { - onContentsChanged(index); - changeListener.accept(index); - } - return drained; - } + public FluidStack drain(int tank, int maxDrain, IFluidHandler.FluidAction action) { + FluidStack fluid = getFluidInTank(tank); + int drained = maxDrain; + if (fluid.getAmount() < drained) { + drained = fluid.getAmount(); } + FluidStack stack = new FluidStack(fluid, drained); + if (action.execute() && drained > 0) { + fluid.shrink(drained); + onContentsChanged(tank); + } + return stack; + } - return FluidStack.EMPTY; + public FluidStack drain(int tank, FluidStack resource, IFluidHandler.FluidAction action) { + if (resource.isEmpty() || !isFluidValid(tank, resource)) + return FluidStack.EMPTY; + return drain(tank, resource.getAmount(), action); + } + + @Override + public FluidStack drain(FluidStack resource, FluidAction action) { + return drain(resource.getAmount(), action); } @Override public FluidStack drain(int maxDrain, FluidAction action) { for (int index = 0; index < tanks.size(); index++) { - if (tanks.get(index).drain(maxDrain, FluidAction.SIMULATE) != FluidStack.EMPTY) { - FluidStack drained = tanks.get(index).drain(maxDrain, action); + if (drain(index, maxDrain, FluidAction.SIMULATE) != FluidStack.EMPTY) { + FluidStack drained = drain(index, maxDrain, action); if (!drained.isEmpty()) { onContentsChanged(index); changeListener.accept(index); @@ -164,7 +210,7 @@ protected void onContentsChanged(int slot) {} @Override public CompoundTag serializeNBT() { ListTag nbtTagList = new ListTag(); - for (int i = 0; i < tanks.size(); i++) { + for (int i = 0; i < getTanks(); i++) { CompoundTag tankTag = new CompoundTag(); tankTag.putInt(TANK_INDEX, i); tanks.get(i).save(tankTag); @@ -172,19 +218,17 @@ public CompoundTag serializeNBT() { } CompoundTag nbt = new CompoundTag(); nbt.put(TANKS, nbtTagList); - nbt.putInt(TANK_LIST_SIZE, tanks.size()); return nbt; } @Override public void deserializeNBT(CompoundTag nbt) { - int size = nbt.contains(TANK_LIST_SIZE, Tag.TAG_INT) ? nbt.getInt(TANK_LIST_SIZE) : tanks.size(); - tanks = NonNullList.withSize(size, MachineFluidTank.EMPTY); + tanks = layout.createTanks(this); ListTag tagList = nbt.getList(TANKS, Tag.TAG_COMPOUND); for (int i = 0; i < tagList.size(); i++) { CompoundTag tankTag = tagList.getCompound(i); int index = tankTag.getInt(TANK_INDEX); - tanks.set(index, MachineFluidTank.from(tankTag)); + tanks.set(index, MachineFluidTank.from(tankTag, index, this)); } } diff --git a/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidTank.java b/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidTank.java index 00452311de..26fa24228b 100644 --- a/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidTank.java +++ b/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidTank.java @@ -1,37 +1,33 @@ package com.enderio.machines.common.io.fluid; import net.minecraft.nbt.CompoundTag; +import net.neoforged.neoforge.capabilities.Capabilities; import net.neoforged.neoforge.fluids.FluidStack; import net.neoforged.neoforge.fluids.IFluidTank; import net.neoforged.neoforge.fluids.capability.IFluidHandler; import org.jetbrains.annotations.NotNull; public class MachineFluidTank implements IFluidTank { + private final int index; + private final MachineFluidHandler handler; + @NotNull + private FluidStack fluid = FluidStack.EMPTY; - public static final String Capacity = "Capacity"; - - private final int capacity; - @NotNull private FluidStack fluid = FluidStack.EMPTY; - public static final MachineFluidTank EMPTY = new MachineFluidTank(0); - - public MachineFluidTank(int capacity) { - this.capacity = capacity; - } - - public MachineFluidTank(FluidStack stack, int capacity) { - this(capacity); - this.fluid = stack.copy(); + public MachineFluidTank(int index, MachineFluidHandler handler) { + this.index = index; + this.handler = handler; } - public static MachineFluidTank from(CompoundTag tag) { + public static MachineFluidTank from(CompoundTag tag, int index, MachineFluidHandler handler) { FluidStack stack = FluidStack.loadFluidStackFromNBT(tag); - int capacity = tag.getInt(Capacity); - return new MachineFluidTank(stack, capacity); + MachineFluidTank machineFluidTank = new MachineFluidTank(index, handler); + machineFluidTank.setFluid(stack); + return machineFluidTank; } @Override public int getCapacity() { - return capacity; + return handler.getTankCapacity(index); } @Override @@ -50,7 +46,7 @@ public int getFluidAmount() { @Override public boolean isFluidValid(FluidStack stack) { - return true; + return handler.isFluidValid(index, stack); } public boolean isEmpty() { @@ -58,68 +54,25 @@ public boolean isEmpty() { } public int fill(FluidStack resource, IFluidHandler.FluidAction action) { - if (resource.isEmpty()) - return 0; - - if (action.simulate()) { - if (fluid.isEmpty()) { - return Math.min(capacity, resource.getAmount()); - } - if (!fluid.isFluidEqual(resource)) { - return 0; - } - return Math.min(capacity - fluid.getAmount(), resource.getAmount()); - } - if (fluid.isEmpty()) { - fluid = new FluidStack(resource, Math.min(capacity, resource.getAmount())); - onContentsChanged(); - return fluid.getAmount(); - } - if (!fluid.isFluidEqual(resource)) { - return 0; - } - int filled = capacity - fluid.getAmount(); - - if (resource.getAmount() < filled) { - fluid.grow(resource.getAmount()); - filled = resource.getAmount(); - } else { - fluid.setAmount(capacity); - } - if (filled > 0) - onContentsChanged(); - return filled; + return handler.fill(index, resource, action); } @NotNull @Override public FluidStack drain(FluidStack resource, IFluidHandler.FluidAction action) { - if (resource.isEmpty() || !resource.isFluidEqual(fluid)) - return FluidStack.EMPTY; - - return drain(resource.getAmount(), action); + return handler.drain(index, resource, action); } @NotNull @Override public FluidStack drain(int maxDrain, IFluidHandler.FluidAction action) { - int drained = maxDrain; - if (fluid.getAmount() < drained) { - drained = fluid.getAmount(); - } - FluidStack stack = new FluidStack(fluid, drained); - if (action.execute() && drained > 0) { - fluid.shrink(drained); - onContentsChanged(); - } - return stack; + return handler.drain(index, maxDrain, action); } protected void onContentsChanged() {} public CompoundTag save(CompoundTag compoundTag) { getFluid().writeToNBT(compoundTag); - compoundTag.putInt(Capacity, getCapacity()); return compoundTag; } diff --git a/src/machines/java/com/enderio/machines/common/io/fluid/MachineTankLayout.java b/src/machines/java/com/enderio/machines/common/io/fluid/MachineTankLayout.java index 86007a00dd..a3165094cf 100644 --- a/src/machines/java/com/enderio/machines/common/io/fluid/MachineTankLayout.java +++ b/src/machines/java/com/enderio/machines/common/io/fluid/MachineTankLayout.java @@ -44,10 +44,10 @@ public boolean isFluidValid(int slot, FluidStack stack) { return tanks.get(slot).filter().test(stack); } - public List createTanks() { + public List createTanks(MachineFluidHandler handler) { List tankList = new ArrayList<>(); tanks.forEach(config -> { - tankList.add(new MachineFluidTank(config.capacity())); + tankList.add(new MachineFluidTank(tanks.indexOf(config), handler)); }); return tankList; } From a6982807858dc9e4412a7e44e1861912c9a905b2 Mon Sep 17 00:00:00 2001 From: ferriarnus <61201275+ferriarnus@users.noreply.github.com> Date: Sat, 24 Feb 2024 21:30:45 +0100 Subject: [PATCH 4/5] it works --- .../common/io/fluid/MachineFluidHandler.java | 29 ++++++++++--------- .../common/io/fluid/MachineFluidTank.java | 23 ++------------- .../common/io/fluid/MachineTankLayout.java | 8 ----- .../machines/common/io/fluid/TankAccess.java | 6 ++-- .../machines/common/item/FluidTankItem.java | 16 +++++++--- 5 files changed, 33 insertions(+), 49 deletions(-) diff --git a/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidHandler.java b/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidHandler.java index 9fd57f0e90..6ce65c0dee 100644 --- a/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidHandler.java +++ b/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidHandler.java @@ -12,7 +12,9 @@ import org.jetbrains.annotations.Nullable; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.function.IntConsumer; /** @@ -24,7 +26,8 @@ public class MachineFluidHandler implements IFluidHandler, INBTSerializable tanks; + private Map tanks = new HashMap<>(); + private List stacks; // Not sure if we need this but might be useful to update recipe/task if tank is filled. private IntConsumer changeListener = i -> {}; @@ -32,7 +35,7 @@ public class MachineFluidHandler implements IFluidHandler, INBTSerializable getTanks()) { + throw new IndexOutOfBoundsException("No tank found for index " + tank + " in range" + getTanks() + "."); } - return machineFluidTank; + return tanks.computeIfAbsent(tank, i -> new MachineFluidTank(i, this)); } @Override @@ -65,11 +66,11 @@ public int getTanks() { @Override public FluidStack getFluidInTank(int tank) { - return tanks.get(tank).getFluid(); + return stacks.get(tank); } public void setFluidInTank(int tank, FluidStack fluid) { - tanks.get(tank).setFluid(fluid); + stacks.set(tank, fluid); } @Override @@ -112,6 +113,7 @@ public int fill(int tank, FluidStack resource, IFluidHandler.FluidAction action) } if (fluid.isEmpty()) { fluid = new FluidStack(resource, Math.min(capacity, resource.getAmount())); + setFluidInTank(tank, fluid); onContentsChanged(tank); return fluid.getAmount(); } @@ -142,7 +144,7 @@ public int fill(FluidStack resource, FluidAction action) { FluidStack resourceLeft = resource.copy(); int totalFilled = 0; - for (int index = 0; index < tanks.size(); index++) { + for (int index = 0; index < getTanks(); index++) { if (!layout.canInsert(index)) continue; @@ -191,7 +193,7 @@ public FluidStack drain(FluidStack resource, FluidAction action) { @Override public FluidStack drain(int maxDrain, FluidAction action) { - for (int index = 0; index < tanks.size(); index++) { + for (int index = 0; index < getTanks(); index++) { if (drain(index, maxDrain, FluidAction.SIMULATE) != FluidStack.EMPTY) { FluidStack drained = drain(index, maxDrain, action); if (!drained.isEmpty()) { @@ -213,7 +215,7 @@ public CompoundTag serializeNBT() { for (int i = 0; i < getTanks(); i++) { CompoundTag tankTag = new CompoundTag(); tankTag.putInt(TANK_INDEX, i); - tanks.get(i).save(tankTag); + stacks.get(i).writeToNBT(tankTag); nbtTagList.add(tankTag); } CompoundTag nbt = new CompoundTag(); @@ -223,12 +225,11 @@ public CompoundTag serializeNBT() { @Override public void deserializeNBT(CompoundTag nbt) { - tanks = layout.createTanks(this); ListTag tagList = nbt.getList(TANKS, Tag.TAG_COMPOUND); for (int i = 0; i < tagList.size(); i++) { CompoundTag tankTag = tagList.getCompound(i); int index = tankTag.getInt(TANK_INDEX); - tanks.set(index, MachineFluidTank.from(tankTag, index, this)); + stacks.set(index, FluidStack.loadFluidStackFromNBT(tankTag)); } } diff --git a/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidTank.java b/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidTank.java index 26fa24228b..69a3d9108b 100644 --- a/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidTank.java +++ b/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidTank.java @@ -10,21 +10,12 @@ public class MachineFluidTank implements IFluidTank { private final int index; private final MachineFluidHandler handler; - @NotNull - private FluidStack fluid = FluidStack.EMPTY; public MachineFluidTank(int index, MachineFluidHandler handler) { this.index = index; this.handler = handler; } - public static MachineFluidTank from(CompoundTag tag, int index, MachineFluidHandler handler) { - FluidStack stack = FluidStack.loadFluidStackFromNBT(tag); - MachineFluidTank machineFluidTank = new MachineFluidTank(index, handler); - machineFluidTank.setFluid(stack); - return machineFluidTank; - } - @Override public int getCapacity() { return handler.getTankCapacity(index); @@ -32,16 +23,16 @@ public int getCapacity() { @Override public @NotNull FluidStack getFluid() { - return fluid; + return handler.getFluidInTank(index); } public void setFluid(FluidStack fluid) { - this.fluid = fluid; + handler.setFluidInTank(index, fluid); } @Override public int getFluidAmount() { - return fluid.getAmount(); + return getFluid().getAmount(); } @Override @@ -68,12 +59,4 @@ public FluidStack drain(FluidStack resource, IFluidHandler.FluidAction action) { public FluidStack drain(int maxDrain, IFluidHandler.FluidAction action) { return handler.drain(index, maxDrain, action); } - - protected void onContentsChanged() {} - - public CompoundTag save(CompoundTag compoundTag) { - getFluid().writeToNBT(compoundTag); - return compoundTag; - } - } diff --git a/src/machines/java/com/enderio/machines/common/io/fluid/MachineTankLayout.java b/src/machines/java/com/enderio/machines/common/io/fluid/MachineTankLayout.java index a3165094cf..84599c24fb 100644 --- a/src/machines/java/com/enderio/machines/common/io/fluid/MachineTankLayout.java +++ b/src/machines/java/com/enderio/machines/common/io/fluid/MachineTankLayout.java @@ -44,14 +44,6 @@ public boolean isFluidValid(int slot, FluidStack stack) { return tanks.get(slot).filter().test(stack); } - public List createTanks(MachineFluidHandler handler) { - List tankList = new ArrayList<>(); - tanks.forEach(config -> { - tankList.add(new MachineFluidTank(tanks.indexOf(config), handler)); - }); - return tankList; - } - public static class Builder { private final ArrayList tanks = new ArrayList<>(); diff --git a/src/machines/java/com/enderio/machines/common/io/fluid/TankAccess.java b/src/machines/java/com/enderio/machines/common/io/fluid/TankAccess.java index 66621904e1..1190d43374 100644 --- a/src/machines/java/com/enderio/machines/common/io/fluid/TankAccess.java +++ b/src/machines/java/com/enderio/machines/common/io/fluid/TankAccess.java @@ -65,7 +65,7 @@ public boolean isEmpty(MachineFluidHandler handler) { } public int fill(MachineFluidHandler handler, FluidStack stack, IFluidHandler.FluidAction action) { - return handler.fill(stack, action); + return handler.fill(index, stack, action); } public int fill(IFluidTankUser machine, FluidStack stack, IFluidHandler.FluidAction action) { @@ -73,7 +73,7 @@ public int fill(IFluidTankUser machine, FluidStack stack, IFluidHandler.FluidAct } public FluidStack drain(MachineFluidHandler handler, FluidStack resource, IFluidHandler.FluidAction action) { - return handler.drain(resource, action); + return handler.drain(index, resource, action); } public FluidStack drain(IFluidTankUser machine, FluidStack resource, IFluidHandler.FluidAction action) { @@ -81,7 +81,7 @@ public FluidStack drain(IFluidTankUser machine, FluidStack resource, IFluidHandl } public FluidStack drain(MachineFluidHandler handler, int maxDrain, IFluidHandler.FluidAction action) { - return handler.drain(maxDrain, action); + return handler.drain(index, maxDrain, action); } public FluidStack drain(IFluidTankUser machine, int maxDrain, IFluidHandler.FluidAction action) { diff --git a/src/machines/java/com/enderio/machines/common/item/FluidTankItem.java b/src/machines/java/com/enderio/machines/common/item/FluidTankItem.java index 3ffa4ccf93..58800ae639 100644 --- a/src/machines/java/com/enderio/machines/common/item/FluidTankItem.java +++ b/src/machines/java/com/enderio/machines/common/item/FluidTankItem.java @@ -8,6 +8,8 @@ import com.enderio.machines.common.block.MachineBlock; import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer; import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.ListTag; +import net.minecraft.nbt.Tag; import net.minecraft.network.chat.Component; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.BlockItem; @@ -90,7 +92,9 @@ public FluidStack getFluid() { Optional tagCompoundOptional = Optional.ofNullable(container.getTag()); return tagCompoundOptional .map(tagCompound -> tagCompound.getCompound(BLOCK_ENTITY_TAG)) - .map(blockEntityTag -> blockEntityTag.getCompound(MachineNBTKeys.FLUID)) + .map(blockEntityTag -> blockEntityTag.getCompound(MachineNBTKeys.FLUIDS)) + .map(fluidTag -> fluidTag.getList("Tanks", Tag.TAG_COMPOUND)) + .map(tank -> tank.getCompound(0)) .map(FluidStack::loadFluidStackFromNBT) .orElse(FluidStack.EMPTY); } @@ -102,7 +106,11 @@ protected void setFluid(FluidStack fluid) { CompoundTag fluidTag = new CompoundTag(); fluid.writeToNBT(fluidTag);//rewrites the old value - blockEntityTag.put(MachineNBTKeys.FLUID, fluidTag); + ListTag listTag = new ListTag(); + listTag.add(0, fluidTag); + CompoundTag tanks = new CompoundTag(); + tanks.put("Tanks", listTag); + blockEntityTag.put(MachineNBTKeys.FLUIDS, tanks); } @Override @@ -110,8 +118,8 @@ protected void setContainerToEmpty() { CompoundTag tagCompound = container.getTag(); if (tagCompound != null) { CompoundTag blockEntityTag = tagCompound.getCompound(BLOCK_ENTITY_TAG); - if (blockEntityTag.contains(MachineNBTKeys.FLUID)) { - blockEntityTag.remove(MachineNBTKeys.FLUID); + if (blockEntityTag.contains("Tanks")) { + blockEntityTag.remove("Tanks"); } } } From bfe1fcdc5332da563b1f450895b5b5f0e22e780f Mon Sep 17 00:00:00 2001 From: ferriarnus <61201275+ferriarnus@users.noreply.github.com> Date: Wed, 28 Feb 2024 18:40:45 +0100 Subject: [PATCH 5/5] use nbt keys and put move back --- .../java/com/enderio/core/CoreNBTKeys.java | 1 + .../common/blockentity/DrainBlockEntity.java | 22 ------------------ .../blockentity/FluidTankBlockEntity.java | 21 ----------------- .../blockentity/SoulBinderBlockEntity.java | 22 ------------------ .../blockentity/SoulEngineBlockEntity.java | 22 ------------------ .../blockentity/XPObeliskBlockEntity.java | 22 ------------------ .../blockentity/XPVacuumBlockEntity.java | 23 ------------------- .../blockentity/base/MachineBlockEntity.java | 23 ++++++++++++++----- .../common/io/fluid/MachineFluidHandler.java | 7 +++--- .../machines/common/item/FluidTankItem.java | 9 ++++---- 10 files changed, 26 insertions(+), 146 deletions(-) diff --git a/src/core/java/com/enderio/core/CoreNBTKeys.java b/src/core/java/com/enderio/core/CoreNBTKeys.java index ed34130843..de3f600e1b 100644 --- a/src/core/java/com/enderio/core/CoreNBTKeys.java +++ b/src/core/java/com/enderio/core/CoreNBTKeys.java @@ -11,6 +11,7 @@ public class CoreNBTKeys { public static final String ITEMS = "Items"; public static final String FLUID = "Fluid"; public static final String FLUIDS = "Fluids"; + public static final String TANKS = "Tanks"; public static final String ENERGY = "Energy"; public static final String BLOCK_ENTITY_TAG = BlockItem.BLOCK_ENTITY_TAG; diff --git a/src/machines/java/com/enderio/machines/common/blockentity/DrainBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/DrainBlockEntity.java index 2d39467069..70a7ed2355 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/DrainBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/DrainBlockEntity.java @@ -15,7 +15,6 @@ import com.enderio.machines.common.init.MachineAttachments; import com.enderio.machines.common.init.MachineBlockEntities; import com.enderio.machines.common.io.FixedIOConfig; -import com.enderio.machines.common.io.TransferUtil; import com.enderio.machines.common.io.fluid.MachineFluidHandler; import com.enderio.machines.common.io.fluid.MachineFluidTank; import com.enderio.machines.common.io.fluid.MachineTankLayout; @@ -24,7 +23,6 @@ import com.enderio.machines.common.menu.DrainMenu; import net.minecraft.client.multiplayer.ClientLevel; import net.minecraft.core.BlockPos; -import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; @@ -35,7 +33,6 @@ import net.minecraft.world.level.material.Fluid; import net.minecraft.world.level.material.FluidState; import net.minecraft.world.level.material.Fluids; -import net.neoforged.neoforge.capabilities.Capabilities; import net.neoforged.neoforge.fluids.FluidStack; import net.neoforged.neoforge.fluids.FluidType; import net.neoforged.neoforge.fluids.capability.IFluidHandler; @@ -253,23 +250,4 @@ public void load(CompoundTag pTag) { consumed = pTag.getInt(CONSUMED); loadTank(pTag); } - - /** - * Move fluids to and fro via the given side. - */ - private void moveFluids(Direction side) { - IFluidHandler selfHandler = getSelfCapability(Capabilities.FluidHandler.BLOCK, side); - IFluidHandler otherHandler = getNeighbouringCapability(Capabilities.FluidHandler.BLOCK, side); - if (selfHandler == null || otherHandler == null) { - return; - } - - TransferUtil.distributeFluids(getIOConfig().getMode(side), selfHandler, otherHandler); - } - - @Override - public void moveResource(Direction direction) { - super.moveResource(direction); - moveFluids(direction); - } } diff --git a/src/machines/java/com/enderio/machines/common/blockentity/FluidTankBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/FluidTankBlockEntity.java index 0efae3fa80..9516ad9eac 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/FluidTankBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/FluidTankBlockEntity.java @@ -7,7 +7,6 @@ import com.enderio.machines.common.blockentity.base.MachineBlockEntity; import com.enderio.machines.common.init.MachineBlockEntities; import com.enderio.machines.common.init.MachineRecipes; -import com.enderio.machines.common.io.TransferUtil; import com.enderio.machines.common.io.fluid.IFluidItemInteractive; import com.enderio.machines.common.io.fluid.MachineFluidHandler; import com.enderio.machines.common.io.fluid.MachineFluidTank; @@ -18,7 +17,6 @@ import com.enderio.machines.common.menu.FluidTankMenu; import com.enderio.machines.common.recipe.TankRecipe; import net.minecraft.core.BlockPos; -import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; @@ -371,23 +369,4 @@ public void load(CompoundTag pTag) { } // endregion - - /** - * Move fluids to and fro via the given side. - */ - private void moveFluids(Direction side) { - IFluidHandler selfHandler = getSelfCapability(Capabilities.FluidHandler.BLOCK, side); - IFluidHandler otherHandler = getNeighbouringCapability(Capabilities.FluidHandler.BLOCK, side); - if (selfHandler == null || otherHandler == null) { - return; - } - - TransferUtil.distributeFluids(getIOConfig().getMode(side), selfHandler, otherHandler); - } - - @Override - public void moveResource(Direction direction) { - super.moveResource(direction); - moveFluids(direction); - } } diff --git a/src/machines/java/com/enderio/machines/common/blockentity/SoulBinderBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/SoulBinderBlockEntity.java index 28207c9159..cab79b52fa 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/SoulBinderBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/SoulBinderBlockEntity.java @@ -16,7 +16,6 @@ import com.enderio.machines.common.config.MachinesConfig; import com.enderio.machines.common.init.MachineBlockEntities; import com.enderio.machines.common.init.MachineRecipes; -import com.enderio.machines.common.io.TransferUtil; import com.enderio.machines.common.io.fluid.MachineFluidHandler; import com.enderio.machines.common.io.fluid.MachineFluidTank; import com.enderio.machines.common.io.fluid.MachineTankLayout; @@ -28,7 +27,6 @@ import com.enderio.machines.common.recipe.RecipeCaches; import com.enderio.machines.common.recipe.SoulBindingRecipe; import net.minecraft.core.BlockPos; -import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; @@ -39,7 +37,6 @@ import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.material.Fluids; import net.neoforged.fml.LogicalSide; -import net.neoforged.neoforge.capabilities.Capabilities; import net.neoforged.neoforge.fluids.FluidStack; import net.neoforged.neoforge.fluids.capability.IFluidHandler; import org.jetbrains.annotations.Nullable; @@ -228,23 +225,4 @@ public void load(CompoundTag pTag) { } // endregion - - /** - * Move fluids to and fro via the given side. - */ - private void moveFluids(Direction side) { - IFluidHandler selfHandler = getSelfCapability(Capabilities.FluidHandler.BLOCK, side); - IFluidHandler otherHandler = getNeighbouringCapability(Capabilities.FluidHandler.BLOCK, side); - if (selfHandler == null || otherHandler == null) { - return; - } - - TransferUtil.distributeFluids(getIOConfig().getMode(side), selfHandler, otherHandler); - } - - @Override - public void moveResource(Direction direction) { - super.moveResource(direction); - moveFluids(direction); - } } diff --git a/src/machines/java/com/enderio/machines/common/blockentity/SoulEngineBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/SoulEngineBlockEntity.java index 9691e9600c..1e3804b9ac 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/SoulEngineBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/SoulEngineBlockEntity.java @@ -13,7 +13,6 @@ import com.enderio.machines.common.blockentity.base.PoweredMachineBlockEntity; import com.enderio.machines.common.config.MachinesConfig; import com.enderio.machines.common.init.MachineBlockEntities; -import com.enderio.machines.common.io.TransferUtil; import com.enderio.machines.common.io.energy.MachineEnergyStorage; import com.enderio.machines.common.io.fluid.MachineFluidHandler; import com.enderio.machines.common.io.fluid.MachineFluidTank; @@ -23,7 +22,6 @@ import com.enderio.machines.common.menu.SoulEngineMenu; import com.enderio.machines.common.souldata.EngineSoul; import net.minecraft.core.BlockPos; -import net.minecraft.core.Direction; import net.minecraft.core.Holder; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.core.registries.Registries; @@ -40,7 +38,6 @@ import net.minecraft.world.level.material.Fluids; import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.fml.common.Mod; -import net.neoforged.neoforge.capabilities.Capabilities; import net.neoforged.neoforge.client.event.RecipesUpdatedEvent; import net.neoforged.neoforge.fluids.FluidStack; import net.neoforged.neoforge.fluids.FluidType; @@ -257,23 +254,4 @@ public void setLevel(Level level) { static void onReload(RecipesUpdatedEvent event) { reload = !reload; } - - /** - * Move fluids to and fro via the given side. - */ - private void moveFluids(Direction side) { - IFluidHandler selfHandler = getSelfCapability(Capabilities.FluidHandler.BLOCK, side); - IFluidHandler otherHandler = getNeighbouringCapability(Capabilities.FluidHandler.BLOCK, side); - if (selfHandler == null || otherHandler == null) { - return; - } - - TransferUtil.distributeFluids(getIOConfig().getMode(side), selfHandler, otherHandler); - } - - @Override - public void moveResource(Direction direction) { - super.moveResource(direction); - moveFluids(direction); - } } diff --git a/src/machines/java/com/enderio/machines/common/blockentity/XPObeliskBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/XPObeliskBlockEntity.java index ea14646edb..26d5731829 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/XPObeliskBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/XPObeliskBlockEntity.java @@ -7,21 +7,18 @@ import com.enderio.machines.common.attachment.IFluidTankUser; import com.enderio.machines.common.blockentity.base.MachineBlockEntity; import com.enderio.machines.common.init.MachineBlockEntities; -import com.enderio.machines.common.io.TransferUtil; import com.enderio.machines.common.io.fluid.MachineFluidHandler; import com.enderio.machines.common.io.fluid.MachineFluidTank; import com.enderio.machines.common.io.fluid.MachineTankLayout; import com.enderio.machines.common.io.fluid.TankAccess; import com.enderio.machines.common.menu.XPObeliskMenu; import net.minecraft.core.BlockPos; -import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.material.Fluids; -import net.neoforged.neoforge.capabilities.Capabilities; import net.neoforged.neoforge.fluids.FluidStack; import net.neoforged.neoforge.fluids.capability.IFluidHandler; import org.jetbrains.annotations.Nullable; @@ -139,23 +136,4 @@ public void load(CompoundTag pTag) { } // endregion - - /** - * Move fluids to and fro via the given side. - */ - private void moveFluids(Direction side) { - IFluidHandler selfHandler = getSelfCapability(Capabilities.FluidHandler.BLOCK, side); - IFluidHandler otherHandler = getNeighbouringCapability(Capabilities.FluidHandler.BLOCK, side); - if (selfHandler == null || otherHandler == null) { - return; - } - - TransferUtil.distributeFluids(getIOConfig().getMode(side), selfHandler, otherHandler); - } - - @Override - public void moveResource(Direction direction) { - super.moveResource(direction); - moveFluids(direction); - } } diff --git a/src/machines/java/com/enderio/machines/common/blockentity/XPVacuumBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/XPVacuumBlockEntity.java index a7899ac67a..8f78cfa148 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/XPVacuumBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/XPVacuumBlockEntity.java @@ -6,23 +6,19 @@ import com.enderio.machines.common.blockentity.base.VacuumMachineBlockEntity; import com.enderio.machines.common.config.MachinesConfig; import com.enderio.machines.common.init.MachineBlockEntities; -import com.enderio.machines.common.io.TransferUtil; import com.enderio.machines.common.io.fluid.MachineFluidHandler; import com.enderio.machines.common.io.fluid.MachineFluidTank; import com.enderio.machines.common.io.fluid.MachineTankLayout; import com.enderio.machines.common.io.fluid.TankAccess; import com.enderio.machines.common.menu.XPVacuumMenu; import net.minecraft.core.BlockPos; -import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; import net.minecraft.world.entity.ExperienceOrb; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.level.block.state.BlockState; -import net.neoforged.neoforge.capabilities.Capabilities; import net.neoforged.neoforge.fluids.FluidStack; -import net.neoforged.neoforge.fluids.capability.IFluidHandler; import net.neoforged.neoforge.fluids.capability.IFluidHandler.FluidAction; import static com.enderio.base.common.util.ExperienceUtil.EXP_TO_FLUID; @@ -105,23 +101,4 @@ public void load(CompoundTag pTag) { } // endregion - - /** - * Move fluids to and fro via the given side. - */ - private void moveFluids(Direction side) { - IFluidHandler selfHandler = getSelfCapability(Capabilities.FluidHandler.BLOCK, side); - IFluidHandler otherHandler = getNeighbouringCapability(Capabilities.FluidHandler.BLOCK, side); - if (selfHandler == null || otherHandler == null) { - return; - } - - TransferUtil.distributeFluids(getIOConfig().getMode(side), selfHandler, otherHandler); - } - - @Override - public void moveResource(Direction direction) { - super.moveResource(direction); - moveFluids(direction); - } } diff --git a/src/machines/java/com/enderio/machines/common/blockentity/base/MachineBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/base/MachineBlockEntity.java index 875be55dfe..09402ca59a 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/base/MachineBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/base/MachineBlockEntity.java @@ -39,6 +39,7 @@ import net.neoforged.neoforge.capabilities.ICapabilityProvider; import net.neoforged.neoforge.client.model.data.ModelData; import net.neoforged.neoforge.client.model.data.ModelProperty; +import net.neoforged.neoforge.fluids.capability.IFluidHandler; import net.neoforged.neoforge.items.IItemHandler; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -338,17 +339,14 @@ private void forceResources() { for (Direction direction : Direction.values()) { if (ioConfig.getMode(direction).canForce()) { // TODO: Maybe some kind of resource distributor so that items are transmitted evenly around? rather than taking the order of Direction.values() - moveResource(direction); + moveItems(direction); + moveFluids(direction); } } } - public void moveResource(Direction direction) { - moveItems(direction); - } - /** - * Move items to and fro via the given side. + * Move items to and from via the given side. */ private void moveItems(Direction side) { IItemHandler selfHandler = getSelfCapability(Capabilities.ItemHandler.BLOCK, side); @@ -360,6 +358,19 @@ private void moveItems(Direction side) { TransferUtil.distributeItems(ioConfig.getMode(side), selfHandler, otherHandler); } + /** + * Move fluids to and from via the given side. + */ + private void moveFluids(Direction side) { + IFluidHandler selfHandler = getSelfCapability(Capabilities.FluidHandler.BLOCK, side); + IFluidHandler otherHandler = getNeighbouringCapability(Capabilities.FluidHandler.BLOCK, side); + if (selfHandler == null || otherHandler == null) { + return; + } + + TransferUtil.distributeFluids(getIOConfig().getMode(side), selfHandler, otherHandler); + } + // endregion // region Serialization diff --git a/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidHandler.java b/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidHandler.java index 6ce65c0dee..1ea899392b 100644 --- a/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidHandler.java +++ b/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidHandler.java @@ -1,6 +1,7 @@ package com.enderio.machines.common.io.fluid; import com.enderio.api.io.IIOConfig; +import com.enderio.core.CoreNBTKeys; import net.minecraft.core.Direction; import net.minecraft.core.NonNullList; import net.minecraft.nbt.CompoundTag; @@ -11,7 +12,6 @@ import net.neoforged.neoforge.fluids.capability.IFluidHandler; import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -23,7 +23,6 @@ public class MachineFluidHandler implements IFluidHandler, INBTSerializable { public static final String TANK_INDEX = "Index"; - public static final String TANKS = "Tanks"; private final IIOConfig config; private final MachineTankLayout layout; private Map tanks = new HashMap<>(); @@ -219,13 +218,13 @@ public CompoundTag serializeNBT() { nbtTagList.add(tankTag); } CompoundTag nbt = new CompoundTag(); - nbt.put(TANKS, nbtTagList); + nbt.put(CoreNBTKeys.TANKS, nbtTagList); return nbt; } @Override public void deserializeNBT(CompoundTag nbt) { - ListTag tagList = nbt.getList(TANKS, Tag.TAG_COMPOUND); + ListTag tagList = nbt.getList(CoreNBTKeys.TANKS, Tag.TAG_COMPOUND); for (int i = 0; i < tagList.size(); i++) { CompoundTag tankTag = tagList.getCompound(i); int index = tankTag.getInt(TANK_INDEX); diff --git a/src/machines/java/com/enderio/machines/common/item/FluidTankItem.java b/src/machines/java/com/enderio/machines/common/item/FluidTankItem.java index 58800ae639..56617379c3 100644 --- a/src/machines/java/com/enderio/machines/common/item/FluidTankItem.java +++ b/src/machines/java/com/enderio/machines/common/item/FluidTankItem.java @@ -1,6 +1,7 @@ package com.enderio.machines.common.item; import com.enderio.base.common.lang.EIOLang; +import com.enderio.core.CoreNBTKeys; import com.enderio.core.client.item.IAdvancedTooltipProvider; import com.enderio.core.common.util.TooltipUtil; import com.enderio.machines.client.rendering.item.FluidTankBEWLR; @@ -93,7 +94,7 @@ public FluidStack getFluid() { return tagCompoundOptional .map(tagCompound -> tagCompound.getCompound(BLOCK_ENTITY_TAG)) .map(blockEntityTag -> blockEntityTag.getCompound(MachineNBTKeys.FLUIDS)) - .map(fluidTag -> fluidTag.getList("Tanks", Tag.TAG_COMPOUND)) + .map(fluidTag -> fluidTag.getList(CoreNBTKeys.TANKS, Tag.TAG_COMPOUND)) .map(tank -> tank.getCompound(0)) .map(FluidStack::loadFluidStackFromNBT) .orElse(FluidStack.EMPTY); @@ -109,7 +110,7 @@ protected void setFluid(FluidStack fluid) { ListTag listTag = new ListTag(); listTag.add(0, fluidTag); CompoundTag tanks = new CompoundTag(); - tanks.put("Tanks", listTag); + tanks.put(CoreNBTKeys.TANKS, listTag); blockEntityTag.put(MachineNBTKeys.FLUIDS, tanks); } @@ -118,8 +119,8 @@ protected void setContainerToEmpty() { CompoundTag tagCompound = container.getTag(); if (tagCompound != null) { CompoundTag blockEntityTag = tagCompound.getCompound(BLOCK_ENTITY_TAG); - if (blockEntityTag.contains("Tanks")) { - blockEntityTag.remove("Tanks"); + if (blockEntityTag.contains(CoreNBTKeys.TANKS)) { + blockEntityTag.remove(CoreNBTKeys.TANKS); } } }