From 6cbe6b8a7399ce1be2ba7cd2c413d0b94e716ee9 Mon Sep 17 00:00:00 2001 From: mystchonky Date: Sun, 12 Nov 2023 19:29:52 +0530 Subject: [PATCH 01/10] Initial tank layout --- .../common/io/fluid/MachineTankLayout.java | 63 +++++++++++++++++++ .../machines/common/io/fluid/TankAccess.java | 14 +++++ 2 files changed, 77 insertions(+) create mode 100644 src/machines/java/com/enderio/machines/common/io/fluid/MachineTankLayout.java create mode 100644 src/machines/java/com/enderio/machines/common/io/fluid/TankAccess.java 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 new file mode 100644 index 0000000000..741380ef21 --- /dev/null +++ b/src/machines/java/com/enderio/machines/common/io/fluid/MachineTankLayout.java @@ -0,0 +1,63 @@ +package com.enderio.machines.common.io.fluid; + +import net.minecraftforge.fluids.FluidStack; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Predicate; + +/** + * Describes the tank layout of a machine + */ +public class MachineTankLayout { + + /** + * Tank configurations + */ + private final List tanks; + + private MachineTankLayout(Builder builder) { + this.tanks = List.copyOf(builder.tanks); + } + + public static Builder builder() { + return new Builder(); + } + + public int getTankCount() { + return tanks.size(); + } + + public int getTankCapacity(int slot) { + return tanks.get(slot).capacity(); + } + + public boolean canInsert(int slot) { + return tanks.get(slot).insert(); + } + + public boolean canExtract(int slot) { + return tanks.get(slot).extract(); + } + + public boolean isFluidValid(int slot, FluidStack stack) { + return tanks.get(slot).filter().test(stack); + } + + public static class Builder { + private final ArrayList tanks = new ArrayList<>(); + + public Builder tank(TankAccess access, int capacity, boolean canInsert, boolean canRemove, Predicate filter) { + tanks.add(new TankConfig(capacity, canInsert, canRemove, filter)); + access.init(tanks.size() - 1); + return this; + } + + public MachineTankLayout build() { + return new MachineTankLayout(this); + } + } + + private record TankConfig(int capacity, boolean insert, boolean extract, Predicate filter) {} + +} 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 new file mode 100644 index 0000000000..86256c00d8 --- /dev/null +++ b/src/machines/java/com/enderio/machines/common/io/fluid/TankAccess.java @@ -0,0 +1,14 @@ +package com.enderio.machines.common.io.fluid; + +public class TankAccess { + + private int index = Integer.MIN_VALUE; + + void init(int i) { + if (index == Integer.MIN_VALUE) { + index = i; + } else if (index != i) { + throw new IllegalArgumentException("TankLayout changed dynamically from " + index + " to " + i + ", don't do that"); + } + } +} From 215d205906434f9030a67c73c0b8fe369eb326ac Mon Sep 17 00:00:00 2001 From: mystchonky Date: Mon, 13 Nov 2023 23:06:20 +0530 Subject: [PATCH 02/10] implement FluidHandlers --- .../common/io/fluid/MachineFluidHandler.java | 108 +++++++++-------- .../machines/common/io/fluid/MachineTank.java | 110 ++++++++++++++++++ .../common/io/fluid/MachineTankLayout.java | 12 +- 3 files changed, 181 insertions(+), 49 deletions(-) create mode 100644 src/machines/java/com/enderio/machines/common/io/fluid/MachineTank.java 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 01b9cc0f75..3d2e6945c8 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 @@ -13,27 +13,41 @@ import java.util.EnumMap; import java.util.List; +import java.util.function.IntConsumer; /** * MachineFluidStorage takes a list of fluid tanks and handles IO for them all. */ public class MachineFluidHandler implements IFluidHandler, IEnderCapabilityProvider { private final IIOConfig config; - - private final List tanks; + private final MachineTankLayout layout; + private final List tanks; private final EnumMap> sideCache = new EnumMap<>(Direction.class); private LazyOptional selfCache = LazyOptional.empty(); - public MachineFluidHandler(IIOConfig config, IFluidTank... tanks) { + // Not sure if we need this but might be useful to update recipe/task if tank is filled. + private IntConsumer changeListener = i -> {}; + + public MachineFluidHandler(IIOConfig config, MachineTankLayout layout) { this.config = config; - this.tanks = List.of(tanks); + this.layout = layout; + this.tanks = layout.createTanks(); + } + + public void addSlotChangedCallback(IntConsumer callback) { + changeListener = changeListener.andThen(callback); } public final IIOConfig getConfig() { return config; } + public MachineTankLayout getLayout() { + return layout; + } + + //Not a good idea to use this method. Tank Access should be the way to access tanks public final IFluidTank getTank(int tank) { return tanks.get(tank); } @@ -58,6 +72,49 @@ public boolean isFluidValid(int tank, FluidStack stack) { return tanks.get(tank).isFluidValid(stack); } + @Override + public Capability getCapabilityType() { + return ForgeCapabilities.FLUID_HANDLER; + } + + @Override + public LazyOptional getCapability(@Nullable Direction side) { + if (side == null) { + // Create own cache if its been invalidated or not created yet. + if (!selfCache.isPresent()) { + selfCache = LazyOptional.of(() -> this); + } + + return selfCache.cast(); + } + + if (!config.getMode(side).canConnect()) { + return LazyOptional.empty(); + } + + return sideCache.computeIfAbsent(side, dir -> LazyOptional.of(() -> new Sided(this, dir))).cast(); + } + + @Override + public void invalidateSide(@Nullable Direction side) { + if (side != null) { + if (sideCache.containsKey(side)) { + sideCache.get(side).invalidate(); + sideCache.remove(side); + } + } else { + selfCache.invalidate(); + } + } + + @Override + public void invalidateCaps() { + for (LazyOptional side : sideCache.values()) { + side.invalidate(); + } + selfCache.invalidate(); + } + @Override public int fill(FluidStack resource, FluidAction action) { // Don't waste any time. @@ -106,49 +163,6 @@ public FluidStack drain(int maxDrain, FluidAction action) { return FluidStack.EMPTY; } - @Override - public Capability getCapabilityType() { - return ForgeCapabilities.FLUID_HANDLER; - } - - @Override - public LazyOptional getCapability(@Nullable Direction side) { - if (side == null) { - // Create own cache if its been invalidated or not created yet. - if (!selfCache.isPresent()) { - selfCache = LazyOptional.of(() -> this); - } - - return selfCache.cast(); - } - - if (!config.getMode(side).canConnect()) { - return LazyOptional.empty(); - } - - return sideCache.computeIfAbsent(side, dir -> LazyOptional.of(() -> new Sided(this, dir))).cast(); - } - - @Override - public void invalidateSide(@Nullable Direction side) { - if (side != null) { - if (sideCache.containsKey(side)) { - sideCache.get(side).invalidate(); - sideCache.remove(side); - } - } else { - selfCache.invalidate(); - } - } - - @Override - public void invalidateCaps() { - for (LazyOptional side : sideCache.values()) { - side.invalidate(); - } - selfCache.invalidate(); - } - // Sided capability access private static class Sided implements IFluidHandler { diff --git a/src/machines/java/com/enderio/machines/common/io/fluid/MachineTank.java b/src/machines/java/com/enderio/machines/common/io/fluid/MachineTank.java new file mode 100644 index 0000000000..24cf522c25 --- /dev/null +++ b/src/machines/java/com/enderio/machines/common/io/fluid/MachineTank.java @@ -0,0 +1,110 @@ +package com.enderio.machines.common.io.fluid; + +import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.fluids.IFluidTank; +import net.minecraftforge.fluids.capability.IFluidHandler; +import org.jetbrains.annotations.NotNull; + +import java.util.function.Predicate; + +//Replace MachineFluidTank +public class MachineTank implements IFluidTank { + + private final int capacity; + private final Predicate validator; + + private boolean canInsert; + + private boolean canExtract; + @NotNull private FluidStack fluid = FluidStack.EMPTY; + + public MachineTank(int capacity, Predicate validator, boolean canInsert, boolean canExtract) { + this.capacity = capacity; + this.validator = validator; + this.canInsert = canInsert; + this.canExtract = canExtract; + } + + @Override + public int getCapacity() { + return capacity; + } + + @Override + public @NotNull FluidStack getFluid() { + return fluid; + } + + @Override + public int getFluidAmount() { + return fluid.getAmount(); + } + + @Override + public boolean isFluidValid(FluidStack stack) { + return validator.test(stack); + } + + public int fill(FluidStack resource, IFluidHandler.FluidAction action) { + if (!canInsert || resource.isEmpty() || !isFluidValid(resource)) { + 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; + } + + @NotNull + @Override + public FluidStack drain(FluidStack resource, IFluidHandler.FluidAction action) { + if (!canExtract || resource.isEmpty() || !resource.isFluidEqual(fluid)) { + return FluidStack.EMPTY; + } + return drain(resource.getAmount(), 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; + } + + protected void onContentsChanged() { + + } + +} 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 741380ef21..e30792693f 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,11 +44,19 @@ public boolean isFluidValid(int slot, FluidStack stack) { return tanks.get(slot).filter().test(stack); } + public List createTanks() { + List tankList = new ArrayList<>(); + tanks.forEach((config -> { + tankList.add(new MachineTank(config.capacity, config.filter, config.insert, config.extract)); + })); + return tankList; + } + public static class Builder { private final ArrayList tanks = new ArrayList<>(); - public Builder tank(TankAccess access, int capacity, boolean canInsert, boolean canRemove, Predicate filter) { - tanks.add(new TankConfig(capacity, canInsert, canRemove, filter)); + public Builder tank(TankAccess access, int capacity, boolean canInsert, boolean canExtract, Predicate filter) { + tanks.add(new TankConfig(capacity, canInsert, canExtract, filter)); access.init(tanks.size() - 1); return this; } From c9fbbd58e4f6eaaa923bec2c224fd1e5161a1d61 Mon Sep 17 00:00:00 2001 From: mystchonky Date: Tue, 14 Nov 2023 00:37:39 +0530 Subject: [PATCH 03/10] Finish Tank Access --- .../java/com/enderio/core/CoreNBTKeys.java | 1 + .../blockentity/base/MachineBlockEntity.java | 49 +++++++++++-------- .../common/io/fluid/MachineFluidHandler.java | 40 +++++++++++++-- .../machines/common/io/fluid/MachineTank.java | 35 +++++++++---- .../common/io/fluid/MachineTankLayout.java | 2 +- .../machines/common/io/fluid/TankAccess.java | 44 +++++++++++++++++ 6 files changed, 136 insertions(+), 35 deletions(-) diff --git a/src/core/java/com/enderio/core/CoreNBTKeys.java b/src/core/java/com/enderio/core/CoreNBTKeys.java index 2529f90c64..ed34130843 100644 --- a/src/core/java/com/enderio/core/CoreNBTKeys.java +++ b/src/core/java/com/enderio/core/CoreNBTKeys.java @@ -10,6 +10,7 @@ public class CoreNBTKeys { public static final String ITEM = "Item"; public static final String ITEMS = "Items"; public static final String FLUID = "Fluid"; + public static final String FLUIDS = "Fluids"; 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/base/MachineBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/base/MachineBlockEntity.java index 933c3e30de..c006b814be 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 @@ -18,6 +18,7 @@ import com.enderio.machines.common.block.MachineBlock; import com.enderio.machines.common.io.IOConfig; 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.core.BlockPos; @@ -47,7 +48,6 @@ import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.capability.IFluidHandler; -import net.minecraftforge.fluids.capability.templates.FluidTank; import net.minecraftforge.fml.LogicalSide; import net.minecraftforge.items.IItemHandler; import org.jetbrains.annotations.NotNull; @@ -95,9 +95,6 @@ public abstract class MachineBlockEntity extends EnderBlockEntity implements Men @Nullable private final MachineInventory inventory; - @Nullable - private final FluidTank fluidTank; - @Nullable private final MachineFluidHandler fluidHandler; @@ -133,9 +130,9 @@ public MachineBlockEntity(BlockEntityType type, BlockPos worldPosition, Block } // Create fluid storage - fluidTank = createFluidTank(); - if (fluidTank != null) { - fluidHandler = createFluidHandler(fluidTank); + MachineTankLayout tankLayout = getTankLayout(); + if (tankLayout != null) { + fluidHandler = createFluidHandler(tankLayout); if (fluidHandler != null) { addCapabilityProvider(fluidHandler); } @@ -381,30 +378,40 @@ protected void onInventoryContentsChanged(int slot) { } // endregion // region Fluid Storage - @Nullable - protected FluidTank createFluidTank() { + public MachineTankLayout getTankLayout() { return null; } @Nullable - public final FluidTank getFluidTank() { - return fluidTank; + public final MachineFluidHandler getFluidHandler() { + return fluidHandler; } /** - * Only call this if you're sure your machine has a fluid tank. + * Only call this if you're sure your machine has an tank. */ - protected final FluidTank getFluidTankNN() { - return Objects.requireNonNull(fluidTank); + protected final MachineFluidHandler getFluidHandlerNN() { + return Objects.requireNonNull(fluidHandler); } @Nullable - protected MachineFluidHandler createFluidHandler(FluidTank fluidTank) { - // We can have a default here, as if createFluidTank returns null, this is never called. - return new MachineFluidHandler(getIOConfig(), fluidTank); + protected MachineFluidHandler createFluidHandler(MachineTankLayout layout) { + return new MachineFluidHandler(getIOConfig(), layout) { + @Override + protected void onContentsChanged(int slot) { + super.onContentsChanged(slot); + onTankContentsChanged(slot); + setChanged(); + } + }; } + /** + * @apiNote Must call this on custom MachineFluid handlers! + */ + protected void onTankContentsChanged(int slot) {} + // endregion // region Block Entity ticking @@ -652,8 +659,8 @@ public void saveAdditional(CompoundTag pTag) { pTag.put(MachineNBTKeys.ITEMS, inventory.serializeNBT()); } - if (fluidTank != null) { - pTag.put(MachineNBTKeys.FLUID, fluidTank.writeToNBT(new CompoundTag())); + if (this.fluidHandler != null) { + pTag.put(MachineNBTKeys.FLUID, fluidHandler.serializeNBT()); } if (getMaxRange() > 0) { @@ -675,8 +682,8 @@ public void load(CompoundTag pTag) { inventory.deserializeNBT(pTag.getCompound(MachineNBTKeys.ITEMS)); } - if (fluidTank != null) { - fluidTank.readFromNBT(pTag.getCompound(MachineNBTKeys.FLUID)); + if (this.fluidHandler != null) { + fluidHandler.deserializeNBT(pTag.getCompound(MachineNBTKeys.FLUIDS)); } // For rendering io overlays after placed by an nbt filled block item 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 3d2e6945c8..ade914aca7 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 @@ -3,8 +3,13 @@ import com.enderio.api.capability.IEnderCapabilityProvider; import com.enderio.api.io.IIOConfig; import net.minecraft.core.Direction; +import net.minecraft.core.NonNullList; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.ListTag; +import net.minecraft.nbt.Tag; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.ForgeCapabilities; +import net.minecraftforge.common.util.INBTSerializable; import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.IFluidTank; @@ -18,10 +23,10 @@ /** * MachineFluidStorage takes a list of fluid tanks and handles IO for them all. */ -public class MachineFluidHandler implements IFluidHandler, IEnderCapabilityProvider { +public class MachineFluidHandler implements IFluidHandler, IEnderCapabilityProvider, INBTSerializable { private final IIOConfig config; private final MachineTankLayout layout; - private final List tanks; + private List tanks; private final EnumMap> sideCache = new EnumMap<>(Direction.class); private LazyOptional selfCache = LazyOptional.empty(); @@ -69,7 +74,7 @@ public int getTankCapacity(int tank) { @Override public boolean isFluidValid(int tank, FluidStack stack) { - return tanks.get(tank).isFluidValid(stack); + return layout.isFluidValid(tank, stack); } @Override @@ -163,6 +168,35 @@ public FluidStack drain(int maxDrain, FluidAction action) { return FluidStack.EMPTY; } + protected void onContentsChanged(int slot) {} + + @Override + public CompoundTag serializeNBT() { + ListTag nbtTagList = new ListTag(); + for (int i = 0; i < tanks.size(); i++) { + CompoundTag tankTag = new CompoundTag(); + tankTag.putInt("Index", i); + tanks.get(i).save(tankTag); + nbtTagList.add(tankTag); + } + CompoundTag nbt = new CompoundTag(); + nbt.put("Tanks", nbtTagList); + nbt.putInt("Size", tanks.size()); + return nbt; + } + + @Override + public void deserializeNBT(CompoundTag nbt) { + int size = nbt.contains("Size", Tag.TAG_INT) ? nbt.getInt("Size") : tanks.size(); + tanks = NonNullList.withSize(size, MachineTank.EMPTY); + 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("Index"); + tanks.set(index, MachineTank.from(tankTag)); + } + } + // Sided capability access private static class Sided implements IFluidHandler { diff --git a/src/machines/java/com/enderio/machines/common/io/fluid/MachineTank.java b/src/machines/java/com/enderio/machines/common/io/fluid/MachineTank.java index 24cf522c25..4d3bb9ff89 100644 --- a/src/machines/java/com/enderio/machines/common/io/fluid/MachineTank.java +++ b/src/machines/java/com/enderio/machines/common/io/fluid/MachineTank.java @@ -1,30 +1,39 @@ package com.enderio.machines.common.io.fluid; +import net.minecraft.nbt.CompoundTag; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.IFluidTank; import net.minecraftforge.fluids.capability.IFluidHandler; import org.jetbrains.annotations.NotNull; -import java.util.function.Predicate; - //Replace MachineFluidTank public class MachineTank implements IFluidTank { private final int capacity; - private final Predicate validator; - private boolean canInsert; - private boolean canExtract; @NotNull private FluidStack fluid = FluidStack.EMPTY; + public static final MachineTank EMPTY = new MachineTank(0, true, true); - public MachineTank(int capacity, Predicate validator, boolean canInsert, boolean canExtract) { + public MachineTank(int capacity, boolean canInsert, boolean canExtract) { this.capacity = capacity; - this.validator = validator; this.canInsert = canInsert; this.canExtract = canExtract; } + public MachineTank(FluidStack stack, int capacity, boolean canInsert, boolean canExtract) { + this(capacity, canInsert, canExtract); + this.fluid = stack.copy(); + } + + public static MachineTank from(CompoundTag tag) { + FluidStack stack = FluidStack.loadFluidStackFromNBT(tag); + int capacity = tag.getInt("Capacity"); + boolean canInsert = tag.getBoolean("CanInsert"); + boolean canExtract = tag.getBoolean("CanExtract"); + return new MachineTank(stack, capacity, canInsert, canExtract); + } + @Override public int getCapacity() { return capacity; @@ -42,11 +51,11 @@ public int getFluidAmount() { @Override public boolean isFluidValid(FluidStack stack) { - return validator.test(stack); + return true; } public int fill(FluidStack resource, IFluidHandler.FluidAction action) { - if (!canInsert || resource.isEmpty() || !isFluidValid(resource)) { + if (!canInsert || resource.isEmpty()) { return 0; } if (action.simulate()) { @@ -103,8 +112,14 @@ public FluidStack drain(int maxDrain, IFluidHandler.FluidAction action) { return stack; } - protected void onContentsChanged() { + protected void onContentsChanged() {} + public CompoundTag save(CompoundTag compoundTag) { + getFluid().writeToNBT(compoundTag); + compoundTag.putInt("Capacity", getCapacity()); + compoundTag.putBoolean("CanInsert", canInsert); + compoundTag.putBoolean("CanExtract", canExtract); + 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 e30792693f..a75ea7a759 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 @@ -47,7 +47,7 @@ public boolean isFluidValid(int slot, FluidStack stack) { public List createTanks() { List tankList = new ArrayList<>(); tanks.forEach((config -> { - tankList.add(new MachineTank(config.capacity, config.filter, config.insert, config.extract)); + tankList.add(new MachineTank(config.capacity, config.insert, config.extract)); })); return tankList; } 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 86256c00d8..74c4b02577 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,9 +1,53 @@ package com.enderio.machines.common.io.fluid; +import com.enderio.machines.common.blockentity.base.MachineBlockEntity; +import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.fluids.capability.IFluidHandler; + public class TankAccess { private int index = Integer.MIN_VALUE; + public FluidStack getFluid(MachineBlockEntity blockEntity) { + return getFluid(blockEntity.getFluidHandler()); + } + + public FluidStack getFluid(MachineFluidHandler handler) { + return handler.getFluidInTank(index); + } + + public int fill(MachineFluidHandler handler, FluidStack stack, IFluidHandler.FluidAction action) { + return handler.fill(stack, action); + } + + public int fill(MachineBlockEntity machine, FluidStack stack, IFluidHandler.FluidAction action) { + return fill(machine.getFluidHandler(), stack, action); + } + + public FluidStack drain(MachineFluidHandler handler, FluidStack resource, IFluidHandler.FluidAction action) { + return handler.drain(resource, action); + } + + public FluidStack drain(MachineBlockEntity machine, FluidStack resource, IFluidHandler.FluidAction action) { + return drain(machine.getFluidHandler(), resource, action); + } + + public FluidStack drain(MachineFluidHandler handler, int maxDrain, IFluidHandler.FluidAction action) { + return handler.drain(maxDrain, action); + } + + public FluidStack drain(MachineBlockEntity machine, int maxDrain, IFluidHandler.FluidAction action) { + return drain(machine.getFluidHandler(), maxDrain, action); + } + + public boolean isSlot(int slot) { + return this.index == slot; + } + + public int getIndex() { + return index; + } + void init(int i) { if (index == Integer.MIN_VALUE) { index = i; From 51298242439f18ea5710094ecab1761742fe82e4 Mon Sep 17 00:00:00 2001 From: mystchonky Date: Sat, 18 Nov 2023 18:49:39 +0530 Subject: [PATCH 04/10] move all the machines over to new tank system --- .../gui/widget/ExperienceCraftingWidget.java | 6 +- .../client/gui/widget/ExperienceWidget.java | 6 +- .../gui/widget/FluidStackStaticWidget.java | 10 +-- .../client/gui/widget/FluidStackWidget.java | 10 +-- .../rendering/blockentity/FluidTankBER.java | 6 +- .../common/blockentity/DrainBlockEntity.java | 30 ++++--- .../blockentity/FluidTankBlockEntity.java | 82 ++++++++++--------- .../blockentity/SoulBinderBlockEntity.java | 39 +++++---- .../blockentity/SoulEngineBlockEntity.java | 36 +++++--- .../blockentity/XPObeliskBlockEntity.java | 39 ++++++--- .../blockentity/XPVacuumBlockEntity.java | 33 +++++--- .../blockentity/base/MachineBlockEntity.java | 1 - .../common/io/fluid/MachineFluidHandler.java | 11 ++- .../machines/common/io/fluid/MachineTank.java | 6 +- .../common/io/fluid/MachineTankLayout.java | 7 ++ .../machines/common/io/fluid/TankAccess.java | 36 +++++++- .../machines/common/recipe/TankRecipe.java | 8 +- 17 files changed, 235 insertions(+), 131 deletions(-) diff --git a/src/machines/java/com/enderio/machines/client/gui/widget/ExperienceCraftingWidget.java b/src/machines/java/com/enderio/machines/client/gui/widget/ExperienceCraftingWidget.java index ad96e5d98a..29f07216d6 100644 --- a/src/machines/java/com/enderio/machines/client/gui/widget/ExperienceCraftingWidget.java +++ b/src/machines/java/com/enderio/machines/client/gui/widget/ExperienceCraftingWidget.java @@ -2,6 +2,7 @@ import com.enderio.base.common.util.ExperienceUtil; import com.enderio.core.client.gui.widgets.EIOWidget; +import com.enderio.machines.common.io.fluid.MachineTank; import com.mojang.blaze3d.systems.RenderSystem; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiGraphics; @@ -10,17 +11,16 @@ import net.minecraft.client.gui.screens.Screen; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; -import net.minecraftforge.fluids.capability.templates.FluidTank; import java.util.function.Supplier; public class ExperienceCraftingWidget extends EIOWidget { private static final ResourceLocation GUI_ICONS_LOCATION = new ResourceLocation("textures/gui/icons.png"); private final Screen displayOn; - private final Supplier getFluid; + private final Supplier getFluid; private final Supplier maxXP; - public ExperienceCraftingWidget(Screen displayOn, Supplier getFluid, Supplier maxXP, int pX, int pY, int pWidth, int pHeight) { + public ExperienceCraftingWidget(Screen displayOn, Supplier getFluid, Supplier maxXP, int pX, int pY, int pWidth, int pHeight) { super(pX, pY, pWidth, pHeight); this.displayOn = displayOn; this.getFluid = getFluid; diff --git a/src/machines/java/com/enderio/machines/client/gui/widget/ExperienceWidget.java b/src/machines/java/com/enderio/machines/client/gui/widget/ExperienceWidget.java index 26c57f0a73..ee2cd9c463 100644 --- a/src/machines/java/com/enderio/machines/client/gui/widget/ExperienceWidget.java +++ b/src/machines/java/com/enderio/machines/client/gui/widget/ExperienceWidget.java @@ -3,22 +3,22 @@ import com.enderio.api.misc.Vector2i; import com.enderio.base.common.util.ExperienceUtil; import com.enderio.core.client.gui.widgets.EIOWidget; +import com.enderio.machines.common.io.fluid.MachineTank; import com.mojang.blaze3d.systems.RenderSystem; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.narration.NarrationElementOutput; import net.minecraft.client.gui.screens.Screen; import net.minecraft.resources.ResourceLocation; -import net.minecraftforge.fluids.capability.templates.FluidTank; import java.util.function.Supplier; public class ExperienceWidget extends EIOWidget { private static final ResourceLocation GUI_ICONS_LOCATION = new ResourceLocation("textures/gui/icons.png"); private final Screen displayOn; - private final Supplier getFluid; + private final Supplier getFluid; - public ExperienceWidget(Screen displayOn, Supplier getFluid, int pX, int pY, int pWidth, int pHeight) { + public ExperienceWidget(Screen displayOn, Supplier getFluid, int pX, int pY, int pWidth, int pHeight) { super(pX, pY, pWidth, pHeight); this.displayOn = displayOn; this.getFluid = getFluid; diff --git a/src/machines/java/com/enderio/machines/client/gui/widget/FluidStackStaticWidget.java b/src/machines/java/com/enderio/machines/client/gui/widget/FluidStackStaticWidget.java index 7d90542f1b..c5fd2df9ee 100644 --- a/src/machines/java/com/enderio/machines/client/gui/widget/FluidStackStaticWidget.java +++ b/src/machines/java/com/enderio/machines/client/gui/widget/FluidStackStaticWidget.java @@ -1,6 +1,7 @@ package com.enderio.machines.client.gui.widget; import com.enderio.core.client.gui.widgets.EIOWidget; +import com.enderio.machines.common.io.fluid.MachineTank; import com.mojang.blaze3d.systems.RenderSystem; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiGraphics; @@ -14,7 +15,6 @@ import net.minecraft.util.FastColor; import net.minecraftforge.client.extensions.common.IClientFluidTypeExtensions; import net.minecraftforge.fluids.FluidStack; -import net.minecraftforge.fluids.capability.templates.FluidTank; import java.util.Arrays; import java.util.function.Supplier; @@ -22,9 +22,9 @@ public class FluidStackStaticWidget extends EIOWidget { private final Screen displayOn; - private final Supplier getFluid; + private final Supplier getFluid; - public FluidStackStaticWidget(Screen displayOn, Supplier getFluid, int pX, int pY, int pWidth, int pHeight) { + public FluidStackStaticWidget(Screen displayOn, Supplier getFluid, int pX, int pY, int pWidth, int pHeight) { super(pX, pY, pWidth, pHeight); this.displayOn = displayOn; this.getFluid = getFluid; @@ -35,8 +35,8 @@ public void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float Minecraft minecraft = Minecraft.getInstance(); RenderSystem.defaultBlendFunc(); RenderSystem.enableDepthTest(); - FluidTank fluidTank = getFluid.get(); - if (!fluidTank.isEmpty()) { + MachineTank fluidTank = getFluid.get(); + if (!fluidTank.getFluid().isEmpty()) { FluidStack fluidStack = fluidTank.getFluid(); IClientFluidTypeExtensions props = IClientFluidTypeExtensions.of(fluidStack.getFluid()); ResourceLocation still = props.getStillTexture(fluidStack); diff --git a/src/machines/java/com/enderio/machines/client/gui/widget/FluidStackWidget.java b/src/machines/java/com/enderio/machines/client/gui/widget/FluidStackWidget.java index 34812df87a..7e585700da 100644 --- a/src/machines/java/com/enderio/machines/client/gui/widget/FluidStackWidget.java +++ b/src/machines/java/com/enderio/machines/client/gui/widget/FluidStackWidget.java @@ -1,6 +1,7 @@ package com.enderio.machines.client.gui.widget; import com.enderio.core.client.gui.widgets.EIOWidget; +import com.enderio.machines.common.io.fluid.MachineTank; import com.mojang.blaze3d.systems.RenderSystem; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiGraphics; @@ -14,7 +15,6 @@ import net.minecraft.util.FastColor; import net.minecraftforge.client.extensions.common.IClientFluidTypeExtensions; import net.minecraftforge.fluids.FluidStack; -import net.minecraftforge.fluids.capability.templates.FluidTank; import java.util.Arrays; import java.util.function.Supplier; @@ -22,9 +22,9 @@ public class FluidStackWidget extends EIOWidget { private final Screen displayOn; - private final Supplier getFluid; + private final Supplier getFluid; - public FluidStackWidget(Screen displayOn, Supplier getFluid, int pX, int pY, int pWidth, int pHeight) { + public FluidStackWidget(Screen displayOn, Supplier getFluid, int pX, int pY, int pWidth, int pHeight) { super(pX, pY, pWidth, pHeight); this.displayOn = displayOn; this.getFluid = getFluid; @@ -35,8 +35,8 @@ public void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float Minecraft minecraft = Minecraft.getInstance(); RenderSystem.defaultBlendFunc(); RenderSystem.enableDepthTest(); - FluidTank fluidTank = getFluid.get(); - if (!fluidTank.isEmpty()) { + MachineTank fluidTank = getFluid.get(); + if (!fluidTank.getFluid().isEmpty()) { FluidStack fluidStack = fluidTank.getFluid(); IClientFluidTypeExtensions props = IClientFluidTypeExtensions.of(fluidStack.getFluid()); ResourceLocation still = props.getStillTexture(fluidStack); diff --git a/src/machines/java/com/enderio/machines/client/rendering/blockentity/FluidTankBER.java b/src/machines/java/com/enderio/machines/client/rendering/blockentity/FluidTankBER.java index 564f6253a1..cdfea5ff7c 100644 --- a/src/machines/java/com/enderio/machines/client/rendering/blockentity/FluidTankBER.java +++ b/src/machines/java/com/enderio/machines/client/rendering/blockentity/FluidTankBER.java @@ -2,6 +2,7 @@ import com.enderio.core.client.RenderUtil; import com.enderio.machines.common.blockentity.FluidTankBlockEntity; +import com.enderio.machines.common.io.fluid.MachineTank; import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.blaze3d.vertex.VertexConsumer; import net.minecraft.client.Minecraft; @@ -16,7 +17,6 @@ import net.minecraft.world.level.material.Fluid; import net.minecraftforge.client.extensions.common.IClientFluidTypeExtensions; import net.minecraftforge.fluids.FluidStack; -import net.minecraftforge.fluids.capability.templates.FluidTank; import org.joml.Matrix3f; import org.joml.Matrix4f; @@ -29,10 +29,10 @@ public FluidTankBER(BlockEntityRendererProvider.Context context) { public void render(FluidTankBlockEntity blockEntity, float partialTick, PoseStack poseStack, MultiBufferSource bufferSource, int packedLight, int packedOverlay) { - FluidTank tank = blockEntity.getFluidTank(); + MachineTank tank = blockEntity.getFluidTank(); // Don't waste time if there's no fluid. - if (!tank.isEmpty()) { + if (!tank.getFluid().isEmpty()) { FluidStack fluidStack = tank.getFluid(); // Use entity translucent cull sheet so fluid renders in Fabulous 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 c48146d558..05426bc807 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/DrainBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/DrainBlockEntity.java @@ -11,7 +11,9 @@ import com.enderio.machines.common.blockentity.base.PoweredMachineBlockEntity; import com.enderio.machines.common.config.MachinesConfig; import com.enderio.machines.common.io.FixedIOConfig; -import com.enderio.machines.common.io.fluid.MachineFluidTank; +import com.enderio.machines.common.io.fluid.MachineTank; +import com.enderio.machines.common.io.fluid.MachineTankLayout; +import com.enderio.machines.common.io.fluid.TankAccess; import com.enderio.machines.common.io.item.MachineInventoryLayout; import com.enderio.machines.common.menu.DrainMenu; import net.minecraft.core.BlockPos; @@ -29,7 +31,6 @@ import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.FluidType; import net.minecraftforge.fluids.capability.IFluidHandler; -import net.minecraftforge.fluids.capability.templates.FluidTank; import org.jetbrains.annotations.Nullable; import java.util.ArrayList; @@ -39,6 +40,7 @@ public class DrainBlockEntity extends PoweredMachineBlockEntity { 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 static final TankAccess TANK = new TankAccess(); private static final int CAPACITY = 3 * FluidType.BUCKET_VOLUME; private static final int ENERGY_PER_BUCKET = 1_500; private List positions; @@ -50,7 +52,7 @@ public class DrainBlockEntity extends PoweredMachineBlockEntity { public DrainBlockEntity(BlockEntityType type, BlockPos worldPosition, BlockState blockState) { super(EnergyIOMode.Input, ENERGY_CAPACITY, ENERGY_USAGE, type, worldPosition, blockState); - addDataSlot(new FluidStackNetworkDataSlot(getFluidTankNN()::getFluid, getFluidTankNN()::setFluid)); + addDataSlot(new FluidStackNetworkDataSlot(() -> TANK.getFluid(this), fluid -> TANK.setFluid(this, fluid))); this.range = 5; @@ -73,6 +75,15 @@ public void updateServerCallback() { .build(); } + @Override + public @Nullable MachineTankLayout getTankLayout() { + return MachineTankLayout.builder().tank(TANK, CAPACITY, f -> type.isSame(f.getFluid())).build(); + } + + public MachineTank getFluidTank() { + return getFluidHandler().getTank(TANK.getIndex()); + } + @Override public void serverTick() { if (isActive()) { @@ -97,7 +108,7 @@ protected boolean isActive() { return false; } type = fluidState.getType(); - return getFluidTankNN().fill(new FluidStack(type, FluidType.BUCKET_VOLUME), IFluidHandler.FluidAction.SIMULATE) == FluidType.BUCKET_VOLUME; + return TANK.fill(this, new FluidStack(type, FluidType.BUCKET_VOLUME), IFluidHandler.FluidAction.SIMULATE) == FluidType.BUCKET_VOLUME; } public void drainFluids() { @@ -126,17 +137,17 @@ public void drainFluids() { //Not a valid fluid FluidState fluidState = level.getFluidState(pos); - if (fluidState.isEmpty() || !fluidState.isSource() || !getFluidTankNN().isFluidValid(new FluidStack(fluidState.getType(),1))) { + if (fluidState.isEmpty() || !fluidState.isSource() || !TANK.isFluidValid(this, new FluidStack(fluidState.getType(), 1))) { currentIndex++; continue; } //Fluid found, try to consume it fluidFound = true; - if (getFluidTankNN().fill(new FluidStack(fluidState.getType(), FluidType.BUCKET_VOLUME), IFluidHandler.FluidAction.SIMULATE) == FluidType.BUCKET_VOLUME) { + if (TANK.fill(this, new FluidStack(fluidState.getType(), FluidType.BUCKET_VOLUME), IFluidHandler.FluidAction.SIMULATE) == FluidType.BUCKET_VOLUME) { if (consumed >= ENERGY_PER_BUCKET) { level.setBlock(pos, Blocks.AIR.defaultBlockState(), Block.UPDATE_ALL); - getFluidTankNN().fill(new FluidStack(fluidState.getType(), FluidType.BUCKET_VOLUME), IFluidHandler.FluidAction.EXECUTE); + TANK.fill(this, new FluidStack(fluidState.getType(), FluidType.BUCKET_VOLUME), IFluidHandler.FluidAction.EXECUTE); consumed -= ENERGY_PER_BUCKET; currentIndex++; } else { @@ -182,11 +193,6 @@ private void updateLocations() { } } - @Override - protected @Nullable FluidTank createFluidTank() { - return new MachineFluidTank(CAPACITY, f-> type.isSame(f.getFluid()),this); - } - @Nullable @Override public AbstractContainerMenu createMenu(int containerId, Inventory playerInventory, Player player) { 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 faf6147cf0..2422e6fdab 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/FluidTankBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/FluidTankBlockEntity.java @@ -5,15 +5,16 @@ import com.enderio.core.common.network.slot.FluidStackNetworkDataSlot; import com.enderio.machines.common.blockentity.base.MachineBlockEntity; import com.enderio.machines.common.init.MachineRecipes; -import com.enderio.machines.common.io.fluid.MachineFluidTank; +import com.enderio.machines.common.io.fluid.MachineFluidHandler; +import com.enderio.machines.common.io.fluid.MachineTank; +import com.enderio.machines.common.io.fluid.MachineTankLayout; +import com.enderio.machines.common.io.fluid.TankAccess; import com.enderio.machines.common.io.item.MachineInventoryLayout; import com.enderio.machines.common.io.item.SingleSlotAccess; import com.enderio.machines.common.menu.FluidTankMenu; import com.enderio.machines.common.recipe.TankRecipe; import net.minecraft.core.BlockPos; import net.minecraft.nbt.CompoundTag; -import net.minecraft.world.InteractionHand; -import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; @@ -21,16 +22,13 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; import net.minecraft.world.item.enchantment.Enchantments; -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.phys.BlockHitResult; import net.minecraftforge.common.capabilities.ForgeCapabilities; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.FluidType; import net.minecraftforge.fluids.capability.IFluidHandler; import net.minecraftforge.fluids.capability.IFluidHandlerItem; -import net.minecraftforge.fluids.capability.templates.FluidTank; import org.jetbrains.annotations.Nullable; import java.util.List; @@ -40,6 +38,8 @@ // Could implement a task for each thing it currently has in the If's public abstract class FluidTankBlockEntity extends MachineBlockEntity { + private static final TankAccess TANK = new TankAccess(); + public static class Standard extends FluidTankBlockEntity { public static final int CAPACITY = 16 * FluidType.BUCKET_VOLUME; @@ -48,8 +48,8 @@ public Standard(BlockEntityType type, BlockPos worldPosition, BlockState bloc } @Override - protected @Nullable FluidTank createFluidTank() { - return createFluidTank(CAPACITY); + public @Nullable MachineTankLayout getTankLayout() { + return new MachineTankLayout.Builder().tank(TANK, CAPACITY).build(); } } @@ -61,9 +61,10 @@ public Enhanced(BlockEntityType type, BlockPos worldPosition, BlockState bloc } @Override - protected @Nullable FluidTank createFluidTank() { - return createFluidTank(CAPACITY); + public @Nullable MachineTankLayout getTankLayout() { + return new MachineTankLayout.Builder().tank(TANK, CAPACITY).build(); } + } private final TankRecipe.Container container; @@ -78,10 +79,10 @@ public FluidTankBlockEntity(BlockEntityType type, BlockPos worldPosition, Blo super(type, worldPosition, blockState); // Sync fluid for model - addDataSlot(new FluidStackNetworkDataSlot(getFluidTankNN()::getFluid, getFluidTankNN()::setFluid)); + addDataSlot(new FluidStackNetworkDataSlot(() -> TANK.getFluid(this), f -> TANK.setFluid(this, f))); // Wrap container for fluid recipes - container = new TankRecipe.Container(getInventoryNN(), getFluidTankNN()); + container = new TankRecipe.Container(getInventoryNN(), getFluidHandler().getTank(TANK.getIndex())); } @Override @@ -126,8 +127,7 @@ public boolean acceptItemDrain(ItemStack item) { } // Mending - FluidTank fluidTank = getFluidTankNN(); - FluidStack fluid = fluidTank.getFluid(); + FluidStack fluid = TANK.getFluid(this); if (item.isDamageableItem() && !fluid.isEmpty() && fluid.getFluid().is(EIOTags.Fluids.EXPERIENCE)) { if (item.getEnchantmentLevel(Enchantments.MENDING) > 0) { @@ -176,29 +176,33 @@ protected void onInventoryContentsChanged(int slot) { // region Fluid Storage - protected FluidTank createFluidTank(int capacity) { - return new MachineFluidTank(capacity, this) { + @Override + protected @Nullable MachineFluidHandler createFluidHandler(MachineTankLayout layout) { + return new MachineFluidHandler(getIOConfig(), getTankLayout()) { @Override - protected void onContentsChanged() { + protected void onContentsChanged(int slot) { onTankContentsChanged(); - super.onContentsChanged(); + super.onContentsChanged(slot); } }; } + public MachineTank getFluidTank() { + return getFluidHandler().getTank(TANK.getIndex()); + } + // endregion //TODO: enable fluid tanks to receive stackable fluid containers private void fillInternal() { ItemStack inputItem = FLUID_FILL_INPUT.getItemStack(this); ItemStack outputItem = FLUID_FILL_OUTPUT.getItemStack(this); - FluidTank fluidTank = getFluidTankNN(); if (!inputItem.isEmpty()) { if (inputItem.getItem() instanceof BucketItem filledBucket) { if (outputItem.isEmpty() || (outputItem.getItem() == Items.BUCKET && outputItem.getCount() < outputItem.getMaxStackSize())) { - int filled = fluidTank.fill(new FluidStack(filledBucket.getFluid(), FluidType.BUCKET_VOLUME), IFluidHandler.FluidAction.SIMULATE); + int filled = TANK.fill(this, new FluidStack(filledBucket.getFluid(), FluidType.BUCKET_VOLUME), IFluidHandler.FluidAction.SIMULATE); if (filled == FluidType.BUCKET_VOLUME) { - fluidTank.fill(new FluidStack(filledBucket.getFluid(), FluidType.BUCKET_VOLUME), IFluidHandler.FluidAction.EXECUTE); + TANK.fill(this, new FluidStack(filledBucket.getFluid(), FluidType.BUCKET_VOLUME), IFluidHandler.FluidAction.EXECUTE); inputItem.shrink(1); FLUID_FILL_OUTPUT.insertItem(this, Items.BUCKET.getDefaultInstance(), false); } @@ -208,7 +212,7 @@ private void fillInternal() { if (fluidHandlerCap.isPresent() && outputItem.isEmpty()) { IFluidHandlerItem itemFluid = fluidHandlerCap.get(); - int filled = moveFluids(itemFluid, fluidTank, fluidTank.getCapacity()); + int filled = moveFluids(itemFluid, getFluidHandler(), TANK.getCapacity(this)); if (filled > 0) { FLUID_FILL_OUTPUT.setStackInSlot(this, itemFluid.getContainer()); FLUID_FILL_INPUT.setStackInSlot(this, ItemStack.EMPTY); @@ -218,25 +222,25 @@ private void fillInternal() { } } - @Override - public InteractionResult onBlockEntityUsed(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { - // TODO: Not a fan of the MachineFluidTank having actiions like this. - // I want to review the tank in its entirety after alpha release. - return ((MachineFluidTank) getFluidTankNN()).onClickedWithPotentialFluidItem(player, hand); - } + // TODO: FIX THIS + // @Override + // public InteractionResult onBlockEntityUsed(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { + // // TODO: Not a fan of the MachineFluidTank having actiions like this. + // // I want to review the tank in its entirety after alpha release. + // return ((MachineFluidTank) getFluidTankNN()).onClickedWithPotentialFluidItem(player, hand); + // } //TODO: enable fluid tanks to receive stackable fluid containers private void drainInternal() { ItemStack inputItem = FLUID_DRAIN_INPUT.getItemStack(this); ItemStack outputItem = FLUID_DRAIN_OUTPUT.getItemStack(this); - FluidTank fluidTank = getFluidTankNN(); if (!inputItem.isEmpty()) { if (inputItem.getItem() == Items.BUCKET) { - if (!fluidTank.isEmpty()) { - FluidStack stack = fluidTank.drain(FluidType.BUCKET_VOLUME, IFluidHandler.FluidAction.SIMULATE); + if (!TANK.getFluid(this).isEmpty()) { + FluidStack stack = TANK.drain(this, FluidType.BUCKET_VOLUME, IFluidHandler.FluidAction.SIMULATE); if (stack.getAmount() == FluidType.BUCKET_VOLUME && (outputItem.isEmpty() || (outputItem.getItem() == stack.getFluid().getBucket() && outputItem.getCount() < outputItem.getMaxStackSize()))) { - fluidTank.drain(FluidType.BUCKET_VOLUME, IFluidHandler.FluidAction.EXECUTE); + TANK.drain(this, FluidType.BUCKET_VOLUME, IFluidHandler.FluidAction.EXECUTE); inputItem.shrink(1); if (outputItem.isEmpty()) { FLUID_DRAIN_OUTPUT.setStackInSlot(this, stack.getFluid().getBucket().getDefaultInstance()); @@ -249,7 +253,7 @@ private void drainInternal() { Optional fluidHandlerCap = inputItem.getCapability(ForgeCapabilities.FLUID_HANDLER_ITEM).resolve(); if (fluidHandlerCap.isPresent() && outputItem.isEmpty()) { IFluidHandlerItem itemFluid = fluidHandlerCap.get(); - int filled = moveFluids(fluidTank, itemFluid, fluidTank.getFluidAmount()); + int filled = moveFluids(getFluidHandler(), itemFluid, TANK.getFluidAmount(this)); if (filled > 0) { FLUID_DRAIN_OUTPUT.setStackInSlot(this, itemFluid.getContainer()); FLUID_DRAIN_INPUT.setStackInSlot(this, ItemStack.EMPTY); @@ -261,7 +265,6 @@ private void drainInternal() { private void tryTankRecipe() { currentRecipe.ifPresent(recipe -> { - FluidTank fluidTank = getFluidTankNN(); if (recipe.isEmptying()) { ItemStack outputStack = FLUID_FILL_OUTPUT.getItemStack(this); @@ -269,7 +272,7 @@ private void tryTankRecipe() { if (outputStack.isEmpty() || (outputStack.is(recipe.getOutput()) && outputStack.getCount() < outputStack.getMaxStackSize())) { FLUID_FILL_INPUT.getItemStack(this).shrink(1); - fluidTank.fill(recipe.getFluid(), IFluidHandler.FluidAction.EXECUTE); + TANK.fill(this, recipe.getFluid(), IFluidHandler.FluidAction.EXECUTE); if (outputStack.isEmpty()) { FLUID_FILL_OUTPUT.setStackInSlot(this, new ItemStack(recipe.getOutput(), 1)); @@ -283,7 +286,7 @@ private void tryTankRecipe() { if (outputStack.isEmpty() || (outputStack.is(recipe.getOutput()) && outputStack.getCount() < outputStack.getMaxStackSize())) { FLUID_DRAIN_INPUT.getItemStack(this).shrink(1); - fluidTank.drain(recipe.getFluid(), IFluidHandler.FluidAction.EXECUTE); + TANK.drain(this, recipe.getFluid(), IFluidHandler.FluidAction.EXECUTE); if (outputStack.isEmpty()) { FLUID_DRAIN_OUTPUT.setStackInSlot(this, new ItemStack(recipe.getOutput(), 1)); @@ -296,8 +299,7 @@ private void tryTankRecipe() { } private void tryMendTool() { - FluidTank fluidTank = getFluidTankNN(); - FluidStack fluid = fluidTank.getFluid(); + FluidStack fluid = TANK.getFluid(this); if (!fluid.isEmpty() && fluid.getFluid().is(EIOTags.Fluids.EXPERIENCE)) { ItemStack tool = FLUID_DRAIN_INPUT.getItemStack(this); @@ -309,7 +311,7 @@ private void tryMendTool() { int xpAmount = (int) Math.floor(damage / tool.getXpRepairRatio()); int fluidAmount = xpAmount * ExperienceUtil.EXP_TO_FLUID; - FluidStack drainedXp = fluidTank.drain(fluidAmount, IFluidHandler.FluidAction.EXECUTE); + FluidStack drainedXp = TANK.drain(this, fluidAmount, IFluidHandler.FluidAction.EXECUTE); int repairAmount = (int) Math.floor(drainedXp.getAmount() * tool.getXpRepairRatio() / ExperienceUtil.EXP_TO_FLUID); repairedTool.setDamageValue(Math.max(0, damage - repairAmount)); @@ -335,7 +337,7 @@ public AbstractContainerMenu createMenu(int pContainerId, Inventory pInventory, @Override public int getLightEmission() { - return getFluidTank().getFluid().getFluid().getFluidType().getLightLevel(); + return TANK.getFluid(this).getFluid().getFluidType().getLightLevel(); } // region Serialization 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 ce3a9924cb..894983777f 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/SoulBinderBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/SoulBinderBlockEntity.java @@ -14,6 +14,10 @@ import com.enderio.machines.common.blockentity.task.host.CraftingMachineTaskHost; import com.enderio.machines.common.config.MachinesConfig; import com.enderio.machines.common.init.MachineRecipes; +import com.enderio.machines.common.io.fluid.MachineFluidHandler; +import com.enderio.machines.common.io.fluid.MachineTank; +import com.enderio.machines.common.io.fluid.MachineTankLayout; +import com.enderio.machines.common.io.fluid.TankAccess; import com.enderio.machines.common.io.item.MachineInventoryLayout; import com.enderio.machines.common.io.item.MultiSlotAccess; import com.enderio.machines.common.io.item.SingleSlotAccess; @@ -32,7 +36,6 @@ import net.minecraft.world.level.material.Fluids; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.capability.IFluidHandler; -import net.minecraftforge.fluids.capability.templates.FluidTank; import net.minecraftforge.fml.LogicalSide; import org.jetbrains.annotations.Nullable; @@ -49,6 +52,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 static final TankAccess TANK = new TankAccess(); @Nullable private SoulBindingRecipe recipe; @UseOnly(LogicalSide.CLIENT) @@ -60,14 +64,12 @@ public SoulBinderBlockEntity(BlockEntityType type, BlockPos worldPosition, Bl super(EnergyIOMode.Input, CAPACITY, USAGE, type, worldPosition, blockState); // Sync fluid amount to client. - addDataSlot(new IntegerNetworkDataSlot( - () -> getFluidTankNN().getFluidInTank(0).getAmount(), - i -> getFluidTankNN().setFluid(new FluidStack(EIOFluids.XP_JUICE.get(), i)) + addDataSlot(new IntegerNetworkDataSlot(() -> TANK.getFluidAmount(this), i -> TANK.setFluid(this, new FluidStack(EIOFluids.XP_JUICE.get(), i)) )); // Create the crafting task host - craftingTaskHost = new CraftingMachineTaskHost<>(this, this::hasEnergy, - MachineRecipes.SOUL_BINDING.type().get(), new SoulBindingRecipe.Container(getInventoryNN(), getFluidTankNN()::getFluidAmount), this::createTask); + craftingTaskHost = new CraftingMachineTaskHost<>(this, this::hasEnergy, MachineRecipes.SOUL_BINDING.type().get(), + new SoulBindingRecipe.Container(getInventoryNN(), () -> TANK.getFluidAmount(this)), this::createTask); // Sync crafting container needed xp addDataSlot(new IntegerNetworkDataSlot(() -> recipe == null ? 0: recipe.getExpCost(), i -> clientExp = i)); @@ -110,6 +112,11 @@ 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)); } @@ -131,10 +138,10 @@ public int getClientExp() { // region Fluid Storage @Override - protected @Nullable FluidTank createFluidTank() { - return new FluidTank(10000, f -> f.getFluid().is(EIOTags.Fluids.EXPERIENCE)) { + protected @Nullable MachineFluidHandler createFluidHandler(MachineTankLayout layout) { + return new MachineFluidHandler(getIOConfig(), layout) { @Override - protected void onContentsChanged() { + protected void onContentsChanged(int slot) { craftingTaskHost.newTaskAvailable(); recipe = level.getRecipeManager().getRecipeFor(MachineRecipes.SOUL_BINDING.type().get(), fakeContainer, level).orElse(null); setChanged(); @@ -143,8 +150,8 @@ protected void onContentsChanged() { @Override public int fill(FluidStack resource, FluidAction action) { // Convert into XP Juice - if (this.isFluidValid(resource)) { - var currentFluid = this.getFluid().getFluid(); + if (TANK.isFluidValid(this, resource)) { + var currentFluid = TANK.getFluid(this).getFluid(); if (currentFluid == Fluids.EMPTY || resource.getFluid().isSame(currentFluid)) { return super.fill(resource, action); } else { @@ -158,6 +165,10 @@ public int fill(FluidStack resource, FluidAction action) { }; } + public MachineTank getFluidTank() { + return getFluidHandler().getTank(TANK.getIndex()); + } + // endregion // region Crafting Task @@ -179,9 +190,9 @@ protected void consumeInputs(SoulBindingRecipe recipe) { INPUT_SOUL.getItemStack(getInventory()).shrink(1); INPUT_OTHER.getItemStack(getInventory()).shrink(1); - var fluidTank = getFluidTankNN(); - int leftover = ExperienceUtil.getLevelFromFluidWithLeftover(fluidTank.getFluidAmount(), 0, recipe.getExpCost()).y(); - fluidTank.drain(fluidTank.getFluidAmount() - leftover * EXP_TO_FLUID, IFluidHandler.FluidAction.EXECUTE); + MachineFluidHandler handler = getFluidHandlerNN(); + int leftover = ExperienceUtil.getLevelFromFluidWithLeftover(TANK.getFluidAmount(handler), 0, recipe.getExpCost()).y(); + TANK.drain(handler, TANK.getFluidAmount(handler) - leftover * EXP_TO_FLUID, IFluidHandler.FluidAction.EXECUTE); } }; 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 c4e6b8791c..e381a66377 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/SoulEngineBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/SoulEngineBlockEntity.java @@ -11,7 +11,10 @@ import com.enderio.machines.common.MachineNBTKeys; import com.enderio.machines.common.blockentity.base.PoweredMachineBlockEntity; import com.enderio.machines.common.config.MachinesConfig; -import com.enderio.machines.common.io.fluid.MachineFluidTank; +import com.enderio.machines.common.io.fluid.MachineFluidHandler; +import com.enderio.machines.common.io.fluid.MachineTank; +import com.enderio.machines.common.io.fluid.MachineTankLayout; +import com.enderio.machines.common.io.fluid.TankAccess; import com.enderio.machines.common.io.item.MachineInventoryLayout; import com.enderio.machines.common.menu.SoulEngineMenu; import com.enderio.machines.common.souldata.EngineSoul; @@ -33,7 +36,6 @@ import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.FluidType; import net.minecraftforge.fluids.capability.IFluidHandler; -import net.minecraftforge.fluids.capability.templates.FluidTank; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.registries.ForgeRegistries; import org.jetbrains.annotations.Nullable; @@ -54,6 +56,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 static final TankAccess TANK = new TankAccess(); @Nullable private EngineSoul.SoulData soulData; private int burnedTicks = 0; @@ -64,7 +67,7 @@ public SoulEngineBlockEntity(BlockEntityType type, BlockPos worldPosition, BlockState blockState) { super(EnergyIOMode.Output, CAPACITY, FixedScalable.ZERO, type, worldPosition, blockState); addDataSlot(new ResourceLocationNetworkDataSlot(() -> this.getEntityType().orElse(NO_MOB),this::setEntityType)); - addDataSlot(new FluidStackNetworkDataSlot(getFluidTankNN()::getFluid, getFluidTankNN()::setFluid)); + addDataSlot(new FluidStackNetworkDataSlot(() -> TANK.getFluid(this), f -> TANK.setFluid(this, f))); } @Override @@ -74,6 +77,11 @@ 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()) { @@ -98,14 +106,14 @@ public void setEntityType(ResourceLocation entityType) { @Override protected boolean isActive() { - return canAct() && getFluidTankNN().getFluidAmount() > 0; + return canAct() && TANK.getFluidAmount(this) > 0; } public void producePower() { if (burnedTicks >= soulData.tickpermb()) { int energy = (int) (soulData.powerpermb() * getGenerationRate()); - if (!getFluidTankNN().isEmpty() && getEnergyStorage().addEnergy(energy, true) == energy) { - getFluidTankNN().drain(1, IFluidHandler.FluidAction.EXECUTE); + if (!TANK.getFluid(this).isEmpty() && getEnergyStorage().addEnergy(energy, true) == energy) { + TANK.drain(this, 1, IFluidHandler.FluidAction.EXECUTE); getEnergyStorage().addEnergy(energy); burnedTicks -= soulData.tickpermb(); } @@ -124,19 +132,19 @@ public float getGenerationRate() { } @Override - protected @Nullable FluidTank createFluidTank() { - return new MachineFluidTank(FLUID_CAPACITY, isFluidValid(), this) { + protected @Nullable MachineFluidHandler createFluidHandler(MachineTankLayout layout) { + return new MachineFluidHandler(getIOConfig(), getTankLayout()) { @Override - protected void onContentsChanged() { - super.onContentsChanged(); + protected void onContentsChanged(int slot) { + super.onContentsChanged(slot); setChanged(); } @Override public int fill(FluidStack resource, FluidAction action) { // Convert into tagged fluid - if (this.isFluidValid(resource)) { - var currentFluid = this.getFluid().getFluid(); + if (TANK.isFluidValid(this, resource)) { + var currentFluid = TANK.getFluid(this).getFluid(); if (currentFluid == Fluids.EMPTY || resource.getFluid().isSame(currentFluid)) { return super.fill(resource, action); } else { @@ -173,6 +181,10 @@ private Predicate isFluidValid() { } + public MachineTank getFluidTank() { + return getFluidHandler().getTank(TANK.getIndex()); + } + @Nullable @Override public AbstractContainerMenu createMenu(int containerId, Inventory playerInventory, Player player) { 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 b470f412ae..d3a4791717 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/XPObeliskBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/XPObeliskBlockEntity.java @@ -5,6 +5,10 @@ import com.enderio.base.common.util.ExperienceUtil; import com.enderio.core.common.network.slot.IntegerNetworkDataSlot; import com.enderio.machines.common.blockentity.base.MachineBlockEntity; +import com.enderio.machines.common.io.fluid.MachineFluidHandler; +import com.enderio.machines.common.io.fluid.MachineTank; +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.world.entity.player.Inventory; @@ -15,18 +19,18 @@ import net.minecraft.world.level.material.Fluids; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.capability.IFluidHandler; -import net.minecraftforge.fluids.capability.templates.FluidTank; import org.jetbrains.annotations.Nullable; public class XPObeliskBlockEntity extends MachineBlockEntity { IntegerNetworkDataSlot xpTankDataSlot; + private static final TankAccess TANK = new TankAccess(); public XPObeliskBlockEntity(BlockEntityType type, BlockPos worldPosition, BlockState blockState) { super(type, worldPosition, blockState); - this.xpTankDataSlot = new IntegerNetworkDataSlot(() -> getFluidTankNN().getFluidAmount(), - amount -> getFluidTankNN().setFluid(new FluidStack(EIOFluids.XP_JUICE.getSource(), amount))); + this.xpTankDataSlot = new IntegerNetworkDataSlot(() -> TANK.getFluidAmount(this), + amount -> TANK.setFluid(this, new FluidStack(EIOFluids.XP_JUICE.getSource(), amount))); addDataSlot(xpTankDataSlot); } @@ -37,19 +41,24 @@ public AbstractContainerMenu createMenu(int containerId, Inventory playerInvento } @Override - protected @Nullable FluidTank createFluidTank() { - return new FluidTank(Integer.MAX_VALUE, fluidStack -> fluidStack.getFluid().is(EIOTags.Fluids.EXPERIENCE)) { + public @Nullable 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(), getTankLayout()) { @Override - protected void onContentsChanged() { - super.onContentsChanged(); + protected void onContentsChanged(int slot) { + super.onContentsChanged(slot); setChanged(); } @Override public int fill(FluidStack resource, FluidAction action) { // Convert into XP Juice - if (this.isFluidValid(resource)) { - var currentFluid = this.getFluid().getFluid(); + if (TANK.isFluidValid(this, resource)) { + var currentFluid = TANK.getFluid(this).getFluid(); if (currentFluid == Fluids.EMPTY || resource.getFluid().isSame(currentFluid)) { return super.fill(resource, action); } else { @@ -63,12 +72,16 @@ public int fill(FluidStack resource, FluidAction action) { }; } + public MachineTank getFluidTank() { + return getFluidHandler().getTank(TANK.getIndex()); + } + public void addLevelToPlayer(int levelDiff, Player player) { int requestedLevel = player.experienceLevel + levelDiff; requestedLevel = Math.max(requestedLevel, 0); int playerXP = ExperienceUtil.getPlayerTotalXp(player); int requestedXP = ExperienceUtil.getExpFromLevel(requestedLevel) - playerXP; - int storedXP = getFluidTankNN().getFluidAmount() / ExperienceUtil.EXP_TO_FLUID; + int storedXP = TANK.getFluidAmount(this) / ExperienceUtil.EXP_TO_FLUID; int awardXP = levelDiff > 0 ? Math.min(storedXP, requestedXP) : requestedXP; awardXP(awardXP, player); @@ -77,7 +90,7 @@ public void addLevelToPlayer(int levelDiff, Player player) { public void addAllLevelToPlayer(boolean give, Player player) { int awardXP = 0; if (give) { - awardXP = getFluidTankNN().getFluidAmount() / ExperienceUtil.EXP_TO_FLUID; + awardXP = TANK.getFluidAmount(this) / ExperienceUtil.EXP_TO_FLUID; } else { awardXP = -ExperienceUtil.getPlayerTotalXp(player); } @@ -92,9 +105,9 @@ public void awardXP(int exp, Player player) { private void updateTankContents(int amount) { if (amount > 0) { - getFluidTankNN().fill(new FluidStack(EIOFluids.XP_JUICE.getSource(), amount), IFluidHandler.FluidAction.EXECUTE); + TANK.fill(this, new FluidStack(EIOFluids.XP_JUICE.getSource(), amount), IFluidHandler.FluidAction.EXECUTE); } else { - getFluidTankNN().drain(new FluidStack(EIOFluids.XP_JUICE.getSource(), -amount), IFluidHandler.FluidAction.EXECUTE); + TANK.drain(this, new FluidStack(EIOFluids.XP_JUICE.getSource(), -amount), IFluidHandler.FluidAction.EXECUTE); } } 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 12dff05718..5b00aef5c4 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/XPVacuumBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/XPVacuumBlockEntity.java @@ -4,6 +4,10 @@ import com.enderio.core.common.network.slot.IntegerNetworkDataSlot; import com.enderio.machines.common.blockentity.base.VacuumMachineBlockEntity; import com.enderio.machines.common.config.MachinesConfig; +import com.enderio.machines.common.io.fluid.MachineFluidHandler; +import com.enderio.machines.common.io.fluid.MachineTank; +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.world.entity.ExperienceOrb; @@ -14,19 +18,18 @@ import net.minecraft.world.level.block.state.BlockState; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.capability.IFluidHandler.FluidAction; -import net.minecraftforge.fluids.capability.templates.FluidTank; import org.jetbrains.annotations.Nullable; import static com.enderio.base.common.util.ExperienceUtil.EXP_TO_FLUID; public class XPVacuumBlockEntity extends VacuumMachineBlockEntity { + + private static final TankAccess TANK = new TankAccess(); public XPVacuumBlockEntity(BlockEntityType pType, BlockPos pWorldPosition, BlockState pBlockState) { super(pType, pWorldPosition, pBlockState, ExperienceOrb.class); // Sync fluid level. - addDataSlot(new IntegerNetworkDataSlot( - () -> getFluidTankNN().getFluidInTank(0).getAmount(), - i -> getFluidTankNN().setFluid(new FluidStack(EIOFluids.XP_JUICE.getSource(), i)) + addDataSlot(new IntegerNetworkDataSlot(() -> TANK.getFluidAmount(this), i -> TANK.setFluid(this, new FluidStack(EIOFluids.XP_JUICE.getSource(), i)) )); } @@ -40,9 +43,10 @@ public AbstractContainerMenu createMenu(int containerId, Inventory inventory, Pl return new XPVacuumMenu(this, inventory, containerId); } + @Override public void handleEntity(ExperienceOrb xpe) { - int filled = getFluidTankNN().fill(new FluidStack(EIOFluids.XP_JUICE.getSource(), xpe.getValue() * EXP_TO_FLUID), FluidAction.EXECUTE); + int filled = TANK.fill(this, new FluidStack(EIOFluids.XP_JUICE.getSource(), xpe.getValue() * EXP_TO_FLUID), FluidAction.EXECUTE); if (filled == xpe.getValue() * EXP_TO_FLUID) { xpe.discard(); } else { @@ -53,15 +57,24 @@ public void handleEntity(ExperienceOrb xpe) { // region Fluid Storage @Override - protected @Nullable FluidTank createFluidTank() { - return new FluidTank(Integer.MAX_VALUE) { + public @Nullable MachineTankLayout getTankLayout() { + return new MachineTankLayout.Builder().tank(TANK, Integer.MAX_VALUE).build(); + } + + @Override + protected @Nullable MachineFluidHandler createFluidHandler(MachineTankLayout layout) { + return new MachineFluidHandler(getIOConfig(), getTankLayout()) { @Override - protected void onContentsChanged() { - super.onContentsChanged(); + protected void onContentsChanged(int slot) { + super.onContentsChanged(slot); setChanged(); } }; } + public MachineTank getFluidTank() { + return getFluidHandler().getTank(TANK.getIndex()); + } + // endregion -} \ No newline at end of file +} 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 c006b814be..8b62f441ff 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 @@ -400,7 +400,6 @@ protected MachineFluidHandler createFluidHandler(MachineTankLayout layout) { return new MachineFluidHandler(getIOConfig(), layout) { @Override protected void onContentsChanged(int slot) { - super.onContentsChanged(slot); onTankContentsChanged(slot); setChanged(); } 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 ade914aca7..fb016a1724 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 @@ -53,13 +53,14 @@ public MachineTankLayout getLayout() { } //Not a good idea to use this method. Tank Access should be the way to access tanks - public final IFluidTank getTank(int tank) { + @Deprecated + public final MachineTank getTank(int tank) { return tanks.get(tank); } @Override public int getTanks() { - return tanks.size(); + return layout.getTankCount(); } @Override @@ -67,9 +68,13 @@ public FluidStack getFluidInTank(int tank) { return tanks.get(tank).getFluid(); } + public void setFluidInTank(int tank, FluidStack fluid) { + tanks.get(tank).setFluid(fluid); + } + @Override public int getTankCapacity(int tank) { - return tanks.get(tank).getCapacity(); + return layout.getTankCapacity(tank); } @Override diff --git a/src/machines/java/com/enderio/machines/common/io/fluid/MachineTank.java b/src/machines/java/com/enderio/machines/common/io/fluid/MachineTank.java index 4d3bb9ff89..cb98a44c0c 100644 --- a/src/machines/java/com/enderio/machines/common/io/fluid/MachineTank.java +++ b/src/machines/java/com/enderio/machines/common/io/fluid/MachineTank.java @@ -44,6 +44,10 @@ public int getCapacity() { return fluid; } + public void setFluid(FluidStack fluid) { + this.fluid = fluid; + } + @Override public int getFluidAmount() { return fluid.getAmount(); @@ -55,7 +59,7 @@ public boolean isFluidValid(FluidStack stack) { } public int fill(FluidStack resource, IFluidHandler.FluidAction action) { - if (!canInsert || resource.isEmpty()) { + if (!canInsert || resource.isEmpty()) { // TODO: Should canInsert/canExtract be moved to handler? return 0; } if (action.simulate()) { 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 a75ea7a759..2288e6ceff 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 @@ -55,6 +55,13 @@ public List createTanks() { public static class Builder { private final ArrayList tanks = new ArrayList<>(); + public Builder tank(TankAccess access, int capacity) { + return tank(access, capacity, t -> true); + } + + public Builder tank(TankAccess access, int capacity, Predicate filter) { + return tank(access, capacity, true, true, filter); + } public Builder tank(TankAccess access, int capacity, boolean canInsert, boolean canExtract, Predicate filter) { tanks.add(new TankConfig(capacity, canInsert, canExtract, filter)); access.init(tanks.size() - 1); 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 74c4b02577..f082c75649 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 @@ -8,14 +8,46 @@ public class TankAccess { private int index = Integer.MIN_VALUE; - public FluidStack getFluid(MachineBlockEntity blockEntity) { - return getFluid(blockEntity.getFluidHandler()); + public int getCapacity(MachineBlockEntity machine) { + return getCapacity(machine.getFluidHandler()); + } + + public int getCapacity(MachineFluidHandler fluidHandler) { + return fluidHandler.getTankCapacity(index); + } + + public FluidStack getFluid(MachineBlockEntity machine) { + return getFluid(machine.getFluidHandler()); } public FluidStack getFluid(MachineFluidHandler handler) { return handler.getFluidInTank(index); } + public int getFluidAmount(MachineBlockEntity machine) { + return getFluid(machine).getAmount(); + } + + public int getFluidAmount(MachineFluidHandler handler) { + return getFluid(handler).getAmount(); + } + + public void setFluid(MachineBlockEntity machine, FluidStack fluid) { + setFluid(machine.getFluidHandler(), fluid); + } + + public void setFluid(MachineFluidHandler handler, FluidStack fluid) { + handler.setFluidInTank(index, fluid); + } + + public boolean isFluidValid(MachineBlockEntity machine, FluidStack fluid) { + return isFluidValid(machine.getFluidHandler(), fluid); + } + + public boolean isFluidValid(MachineFluidHandler handler, FluidStack fluid) { + return handler.isFluidValid(index, fluid); + } + public int fill(MachineFluidHandler handler, FluidStack stack, IFluidHandler.FluidAction action) { return handler.fill(stack, action); } diff --git a/src/machines/java/com/enderio/machines/common/recipe/TankRecipe.java b/src/machines/java/com/enderio/machines/common/recipe/TankRecipe.java index 2cf1729eef..5d07a49211 100644 --- a/src/machines/java/com/enderio/machines/common/recipe/TankRecipe.java +++ b/src/machines/java/com/enderio/machines/common/recipe/TankRecipe.java @@ -4,6 +4,7 @@ import com.enderio.core.common.recipes.EnderRecipe; import com.enderio.machines.common.blockentity.FluidTankBlockEntity; import com.enderio.machines.common.init.MachineRecipes; +import com.enderio.machines.common.io.fluid.MachineTank; import com.google.gson.JsonObject; import net.minecraft.ResourceLocationException; import net.minecraft.core.RegistryAccess; @@ -17,7 +18,6 @@ import net.minecraft.world.level.Level; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.capability.IFluidHandler; -import net.minecraftforge.fluids.capability.templates.FluidTank; import net.minecraftforge.items.IItemHandlerModifiable; import net.minecraftforge.items.wrapper.RecipeWrapper; import net.minecraftforge.registries.ForgeRegistries; @@ -101,14 +101,14 @@ public RecipeType getType() { public static class Container extends RecipeWrapper { - private final FluidTank fluidTank; + private final MachineTank fluidTank; - public Container(IItemHandlerModifiable inv, FluidTank fluidTank) { + public Container(IItemHandlerModifiable inv, MachineTank fluidTank) { super(inv); this.fluidTank = fluidTank; } - public FluidTank getFluidTank() { + public MachineTank getFluidTank() { return fluidTank; } } From 37c4ea52ead2fb05066be49616910a7f483d1386 Mon Sep 17 00:00:00 2001 From: mystchonky Date: Sat, 18 Nov 2023 21:03:59 +0530 Subject: [PATCH 05/10] trigger updates for MachineFluidHandler --- .../common/io/fluid/MachineFluidHandler.java | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 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 fb016a1724..6ea92a781d 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,6 @@ import net.minecraftforge.common.util.INBTSerializable; import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.fluids.FluidStack; -import net.minecraftforge.fluids.IFluidTank; import net.minecraftforge.fluids.capability.IFluidHandler; import org.jetbrains.annotations.Nullable; @@ -136,26 +135,34 @@ public int fill(FluidStack resource, FluidAction action) { FluidStack resourceLeft = resource.copy(); int totalFilled = 0; - for (IFluidTank tank : tanks) { + for (int index = 0; index < tanks.size(); index++) { // Attempt to fill the tank - int filled = tank.fill(resourceLeft, action); + int filled = tanks.get(index).fill(resourceLeft, action); resourceLeft.shrink(filled); totalFilled += filled; + if (filled > 0) { + onContentsChanged(index); + } + // If we used up all the resource, stop trying. if (resourceLeft.isEmpty()) { break; } - } + } return totalFilled; } @Override public FluidStack drain(FluidStack resource, FluidAction action) { - for (IFluidTank tank : tanks) { - if (tank.drain(resource, FluidAction.SIMULATE) != FluidStack.EMPTY) { - return tank.drain(resource, action); + for (int index = 0; index < tanks.size(); index++) { + if (tanks.get(index).drain(resource, FluidAction.SIMULATE) != FluidStack.EMPTY) { + FluidStack drained = tanks.get(index).drain(resource, action); + if (!drained.isEmpty()) { + onContentsChanged(index); + } + return drained; } } @@ -164,9 +171,13 @@ public FluidStack drain(FluidStack resource, FluidAction action) { @Override public FluidStack drain(int maxDrain, FluidAction action) { - for (IFluidTank tank : tanks) { - if (tank.drain(maxDrain, FluidAction.SIMULATE) != FluidStack.EMPTY) { - return tank.drain(maxDrain, 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 (!drained.isEmpty()) { + onContentsChanged(index); + } + return drained; } } From 64e52626d55c6f862f7e58b93c63b0356c1dc054 Mon Sep 17 00:00:00 2001 From: mystchonky Date: Tue, 5 Dec 2023 18:08:03 +0530 Subject: [PATCH 06/10] change serialization keys and simply tank impl --- .../common/io/fluid/MachineFluidHandler.java | 36 ++++++++++--------- .../machines/common/io/fluid/MachineTank.java | 32 +++++++---------- .../common/io/fluid/MachineTankLayout.java | 6 ++-- 3 files changed, 36 insertions(+), 38 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 6ea92a781d..c0b56b81a4 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 @@ -23,6 +23,11 @@ * MachineFluidStorage takes a list of fluid tanks and handles IO for them all. */ public class MachineFluidHandler implements IFluidHandler, IEnderCapabilityProvider, INBTSerializable { + + public static final String TANK_INDEX = "Index"; + public static final String TANKS = "Tanks"; + public static final String TANK_LIST_SIZE = "Size"; + private final IIOConfig config; private final MachineTankLayout layout; private List tanks; @@ -136,6 +141,9 @@ public int fill(FluidStack resource, FluidAction action) { int totalFilled = 0; for (int index = 0; index < tanks.size(); index++) { + if (!layout.canInsert(index)) + continue; + // Attempt to fill the tank int filled = tanks.get(index).fill(resourceLeft, action); resourceLeft.shrink(filled); @@ -157,10 +165,14 @@ public int fill(FluidStack resource, FluidAction action) { @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; } @@ -176,6 +188,7 @@ public FluidStack drain(int maxDrain, FluidAction action) { FluidStack drained = tanks.get(index).drain(maxDrain, action); if (!drained.isEmpty()) { onContentsChanged(index); + changeListener.accept(index); } return drained; } @@ -191,39 +204,30 @@ public CompoundTag serializeNBT() { ListTag nbtTagList = new ListTag(); for (int i = 0; i < tanks.size(); i++) { CompoundTag tankTag = new CompoundTag(); - tankTag.putInt("Index", i); + tankTag.putInt(TANK_INDEX, i); tanks.get(i).save(tankTag); nbtTagList.add(tankTag); } CompoundTag nbt = new CompoundTag(); - nbt.put("Tanks", nbtTagList); - nbt.putInt("Size", tanks.size()); + nbt.put(TANKS, nbtTagList); + nbt.putInt(TANK_LIST_SIZE, tanks.size()); return nbt; } @Override public void deserializeNBT(CompoundTag nbt) { - int size = nbt.contains("Size", Tag.TAG_INT) ? nbt.getInt("Size") : tanks.size(); + int size = nbt.contains(TANK_LIST_SIZE, Tag.TAG_INT) ? nbt.getInt(TANK_LIST_SIZE) : tanks.size(); tanks = NonNullList.withSize(size, MachineTank.EMPTY); - ListTag tagList = nbt.getList("Tanks", Tag.TAG_COMPOUND); + 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("Index"); + int index = tankTag.getInt(TANK_INDEX); tanks.set(index, MachineTank.from(tankTag)); } } // Sided capability access - private static class Sided implements IFluidHandler { - - private final MachineFluidHandler master; - private final Direction direction; - - Sided(MachineFluidHandler master, Direction direction) { - this.master = master; - this.direction = direction; - } - + private record Sided(MachineFluidHandler master, Direction direction) implements IFluidHandler { @Override public int getTanks() { return master.getTanks(); diff --git a/src/machines/java/com/enderio/machines/common/io/fluid/MachineTank.java b/src/machines/java/com/enderio/machines/common/io/fluid/MachineTank.java index cb98a44c0c..ebd6348b69 100644 --- a/src/machines/java/com/enderio/machines/common/io/fluid/MachineTank.java +++ b/src/machines/java/com/enderio/machines/common/io/fluid/MachineTank.java @@ -9,29 +9,25 @@ //Replace MachineFluidTank public class MachineTank implements IFluidTank { + public static final String Capacity = "Capacity"; + private final int capacity; - private boolean canInsert; - private boolean canExtract; @NotNull private FluidStack fluid = FluidStack.EMPTY; - public static final MachineTank EMPTY = new MachineTank(0, true, true); + public static final MachineTank EMPTY = new MachineTank(0); - public MachineTank(int capacity, boolean canInsert, boolean canExtract) { + public MachineTank(int capacity) { this.capacity = capacity; - this.canInsert = canInsert; - this.canExtract = canExtract; } - public MachineTank(FluidStack stack, int capacity, boolean canInsert, boolean canExtract) { - this(capacity, canInsert, canExtract); + public MachineTank(FluidStack stack, int capacity) { + this(capacity); this.fluid = stack.copy(); } public static MachineTank from(CompoundTag tag) { FluidStack stack = FluidStack.loadFluidStackFromNBT(tag); - int capacity = tag.getInt("Capacity"); - boolean canInsert = tag.getBoolean("CanInsert"); - boolean canExtract = tag.getBoolean("CanExtract"); - return new MachineTank(stack, capacity, canInsert, canExtract); + int capacity = tag.getInt(Capacity); + return new MachineTank(stack, capacity); } @Override @@ -59,9 +55,9 @@ public boolean isFluidValid(FluidStack stack) { } public int fill(FluidStack resource, IFluidHandler.FluidAction action) { - if (!canInsert || resource.isEmpty()) { // TODO: Should canInsert/canExtract be moved to handler? + if (resource.isEmpty()) return 0; - } + if (action.simulate()) { if (fluid.isEmpty()) { return Math.min(capacity, resource.getAmount()); @@ -95,9 +91,9 @@ public int fill(FluidStack resource, IFluidHandler.FluidAction action) { @NotNull @Override public FluidStack drain(FluidStack resource, IFluidHandler.FluidAction action) { - if (!canExtract || resource.isEmpty() || !resource.isFluidEqual(fluid)) { + if (resource.isEmpty() || !resource.isFluidEqual(fluid)) return FluidStack.EMPTY; - } + return drain(resource.getAmount(), action); } @@ -120,9 +116,7 @@ protected void onContentsChanged() {} public CompoundTag save(CompoundTag compoundTag) { getFluid().writeToNBT(compoundTag); - compoundTag.putInt("Capacity", getCapacity()); - compoundTag.putBoolean("CanInsert", canInsert); - compoundTag.putBoolean("CanExtract", canExtract); + 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 2288e6ceff..d7d814f557 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 @@ -46,9 +46,9 @@ public boolean isFluidValid(int slot, FluidStack stack) { public List createTanks() { List tankList = new ArrayList<>(); - tanks.forEach((config -> { - tankList.add(new MachineTank(config.capacity, config.insert, config.extract)); - })); + tanks.forEach(config -> { + tankList.add(new MachineTank(config.capacity())); + }); return tankList; } From 0b89252e77373c0a568c1e24896fd61651d36b35 Mon Sep 17 00:00:00 2001 From: mystchonky Date: Tue, 5 Dec 2023 18:27:03 +0530 Subject: [PATCH 07/10] fix serialization --- .../machines/common/blockentity/base/MachineBlockEntity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 8b62f441ff..15b87ec6d7 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 @@ -659,7 +659,7 @@ public void saveAdditional(CompoundTag pTag) { } if (this.fluidHandler != null) { - pTag.put(MachineNBTKeys.FLUID, fluidHandler.serializeNBT()); + pTag.put(MachineNBTKeys.FLUIDS, fluidHandler.serializeNBT()); } if (getMaxRange() > 0) { From f41cf1c6e6279916ade44421779860ac6a57b2f8 Mon Sep 17 00:00:00 2001 From: mystchonky Date: Tue, 5 Dec 2023 20:44:42 +0530 Subject: [PATCH 08/10] reimpl interaction with Items with IFluidHandlerItem --- .../blockentity/FluidTankBlockEntity.java | 23 +++-- .../io/fluid/IFluidItemInteractive.java | 89 +++++++++++++++++++ .../machines/common/io/fluid/MachineTank.java | 4 + .../machines/common/io/fluid/TankAccess.java | 8 ++ 4 files changed, 116 insertions(+), 8 deletions(-) create mode 100644 src/machines/java/com/enderio/machines/common/io/fluid/IFluidItemInteractive.java 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 2422e6fdab..de36770154 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/FluidTankBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/FluidTankBlockEntity.java @@ -5,6 +5,7 @@ import com.enderio.core.common.network.slot.FluidStackNetworkDataSlot; import com.enderio.machines.common.blockentity.base.MachineBlockEntity; import com.enderio.machines.common.init.MachineRecipes; +import com.enderio.machines.common.io.fluid.IFluidItemInteractive; import com.enderio.machines.common.io.fluid.MachineFluidHandler; import com.enderio.machines.common.io.fluid.MachineTank; import com.enderio.machines.common.io.fluid.MachineTankLayout; @@ -15,6 +16,8 @@ import com.enderio.machines.common.recipe.TankRecipe; import net.minecraft.core.BlockPos; import net.minecraft.nbt.CompoundTag; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; @@ -22,8 +25,10 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; import net.minecraft.world.item.enchantment.Enchantments; +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.phys.BlockHitResult; import net.minecraftforge.common.capabilities.ForgeCapabilities; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.FluidType; @@ -36,7 +41,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 { +public abstract class FluidTankBlockEntity extends MachineBlockEntity implements IFluidItemInteractive { private static final TankAccess TANK = new TankAccess(); @@ -222,13 +227,15 @@ private void fillInternal() { } } - // TODO: FIX THIS - // @Override - // public InteractionResult onBlockEntityUsed(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { - // // TODO: Not a fan of the MachineFluidTank having actiions like this. - // // I want to review the tank in its entirety after alpha release. - // return ((MachineFluidTank) getFluidTankNN()).onClickedWithPotentialFluidItem(player, hand); - // } + @Override + public InteractionResult onBlockEntityUsed(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { + ItemStack stack = player.getItemInHand(hand); + if (!stack.isEmpty() && handleTankInteraction(player, hand, stack, this, TANK)) { + player.getInventory().setChanged(); + return InteractionResult.CONSUME; + } + return super.onBlockEntityUsed(state, level, pos, player, hand, hit); + } //TODO: enable fluid tanks to receive stackable fluid containers private void drainInternal() { 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 new file mode 100644 index 0000000000..727349751f --- /dev/null +++ b/src/machines/java/com/enderio/machines/common/io/fluid/IFluidItemInteractive.java @@ -0,0 +1,89 @@ +package com.enderio.machines.common.io.fluid; + +import com.enderio.machines.common.blockentity.base.MachineBlockEntity; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.ItemStack; +import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.fluids.FluidUtil; +import net.minecraftforge.fluids.capability.IFluidHandler; +import net.minecraftforge.fluids.capability.IFluidHandlerItem; + +import java.util.Optional; + +/** + * An interface that block entities may implement in order to interact with items support IFluidHandlerItem + * Adapted from Mekanism's FluidUtils. + */ +public interface IFluidItemInteractive { + + // Requires direct tank access which is undesirable. MachineFluidHandler would be better to for multi-tank block. + default boolean handleTankInteraction(Player player, InteractionHand hand, ItemStack itemStack, MachineBlockEntity machine, TankAccess tankAccess) { + ItemStack copyStack = itemStack.copyWithCount(1); + Optional fluidHandlerItem = FluidUtil.getFluidHandler(copyStack).resolve(); + if (fluidHandlerItem.isPresent()) { + IFluidHandlerItem handler = fluidHandlerItem.get(); + FluidStack fluidInItem; + if (tankAccess.isEmpty(machine)) { + //If we don't have a fluid stored try draining in general + fluidInItem = handler.drain(Integer.MAX_VALUE, IFluidHandler.FluidAction.SIMULATE); + } else { + //Otherwise, try draining the same type of fluid we have stored + // We do this to better support multiple tanks in case the fluid we have stored we could pull out of a block's + // second tank but just asking to drain a specific amount + fluidInItem = handler.drain(new FluidStack(tankAccess.getFluid(machine), Integer.MAX_VALUE), IFluidHandler.FluidAction.SIMULATE); + } + if (fluidInItem.isEmpty()) { + if (!tankAccess.isEmpty(machine)) { + int filled = handler.fill(tankAccess.getFluid(machine), + player.isCreative() ? IFluidHandler.FluidAction.SIMULATE : IFluidHandler.FluidAction.EXECUTE); + ItemStack container = handler.getContainer(); + if (filled > 0) { + if (itemStack.getCount() == 1) { + player.setItemInHand(hand, container); + } else if (itemStack.getCount() > 1 && player.getInventory().add(container)) { + itemStack.shrink(1); + } else { + player.drop(container, false, true); + itemStack.shrink(1); + } + tankAccess.drain(machine, filled, IFluidHandler.FluidAction.EXECUTE); + return true; + } + } + } else { + int filledAmount = tankAccess.fill(machine, fluidInItem, IFluidHandler.FluidAction.SIMULATE); + if (filledAmount > 0) { + boolean filled = false; + FluidStack fluidToFill = handler.drain(new FluidStack(fluidInItem, filledAmount), + player.isCreative() ? IFluidHandler.FluidAction.SIMULATE : IFluidHandler.FluidAction.EXECUTE); + if (!fluidToFill.isEmpty()) { + ItemStack container = handler.getContainer(); + if (player.isCreative()) { + filled = true; + } else if (!container.isEmpty()) { + if (itemStack.getCount() == 1) { + player.setItemInHand(hand, container); + filled = true; + } else if (player.getInventory().add(container)) { + itemStack.shrink(1); + filled = true; + } + } else { + itemStack.shrink(1); + if (itemStack.isEmpty()) { + player.setItemInHand(hand, ItemStack.EMPTY); + } + filled = true; + } + if (filled) { + tankAccess.fill(machine, fluidToFill, IFluidHandler.FluidAction.EXECUTE); + return true; + } + } + } + } + } + return false; + } +} diff --git a/src/machines/java/com/enderio/machines/common/io/fluid/MachineTank.java b/src/machines/java/com/enderio/machines/common/io/fluid/MachineTank.java index ebd6348b69..a550059310 100644 --- a/src/machines/java/com/enderio/machines/common/io/fluid/MachineTank.java +++ b/src/machines/java/com/enderio/machines/common/io/fluid/MachineTank.java @@ -54,6 +54,10 @@ public boolean isFluidValid(FluidStack stack) { return true; } + public boolean isEmpty() { + return getFluid().isEmpty(); + } + public int fill(FluidStack resource, IFluidHandler.FluidAction action) { if (resource.isEmpty()) return 0; 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 f082c75649..07b07443f0 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 @@ -48,6 +48,14 @@ public boolean isFluidValid(MachineFluidHandler handler, FluidStack fluid) { return handler.isFluidValid(index, fluid); } + public boolean isEmpty(MachineBlockEntity machine) { + return isEmpty(machine.getFluidHandler()); + } + + public boolean isEmpty(MachineFluidHandler handler) { + return getFluid(handler).isEmpty(); + } + public int fill(MachineFluidHandler handler, FluidStack stack, IFluidHandler.FluidAction action) { return handler.fill(stack, action); } From e726d0b98f319cb3456edd966593910ddaedf08b Mon Sep 17 00:00:00 2001 From: mystchonky Date: Tue, 5 Dec 2023 20:46:29 +0530 Subject: [PATCH 09/10] delete old MachineFluidTank impl and renames --- .../gui/widget/ExperienceCraftingWidget.java | 6 +- .../client/gui/widget/ExperienceWidget.java | 6 +- .../gui/widget/FluidStackStaticWidget.java | 8 +- .../client/gui/widget/FluidStackWidget.java | 8 +- .../rendering/blockentity/FluidTankBER.java | 4 +- .../common/blockentity/DrainBlockEntity.java | 4 +- .../blockentity/FluidTankBlockEntity.java | 6 +- .../blockentity/SoulBinderBlockEntity.java | 4 +- .../blockentity/SoulEngineBlockEntity.java | 4 +- .../blockentity/XPObeliskBlockEntity.java | 4 +- .../blockentity/XPVacuumBlockEntity.java | 4 +- .../io/fluid/IFluidItemInteractive.java | 2 +- .../common/io/fluid/MachineFluidHandler.java | 8 +- .../common/io/fluid/MachineFluidTank.java | 298 +++++------------- .../machines/common/io/fluid/MachineTank.java | 127 -------- .../common/io/fluid/MachineTankLayout.java | 6 +- .../machines/common/recipe/TankRecipe.java | 8 +- 17 files changed, 117 insertions(+), 390 deletions(-) delete mode 100644 src/machines/java/com/enderio/machines/common/io/fluid/MachineTank.java diff --git a/src/machines/java/com/enderio/machines/client/gui/widget/ExperienceCraftingWidget.java b/src/machines/java/com/enderio/machines/client/gui/widget/ExperienceCraftingWidget.java index 29f07216d6..cefe7ebf0c 100644 --- a/src/machines/java/com/enderio/machines/client/gui/widget/ExperienceCraftingWidget.java +++ b/src/machines/java/com/enderio/machines/client/gui/widget/ExperienceCraftingWidget.java @@ -2,7 +2,7 @@ import com.enderio.base.common.util.ExperienceUtil; import com.enderio.core.client.gui.widgets.EIOWidget; -import com.enderio.machines.common.io.fluid.MachineTank; +import com.enderio.machines.common.io.fluid.MachineFluidTank; import com.mojang.blaze3d.systems.RenderSystem; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiGraphics; @@ -17,10 +17,10 @@ public class ExperienceCraftingWidget extends EIOWidget { private static final ResourceLocation GUI_ICONS_LOCATION = new ResourceLocation("textures/gui/icons.png"); private final Screen displayOn; - private final Supplier getFluid; + private final Supplier getFluid; private final Supplier maxXP; - public ExperienceCraftingWidget(Screen displayOn, Supplier getFluid, Supplier maxXP, int pX, int pY, int pWidth, int pHeight) { + public ExperienceCraftingWidget(Screen displayOn, Supplier getFluid, Supplier maxXP, int pX, int pY, int pWidth, int pHeight) { super(pX, pY, pWidth, pHeight); this.displayOn = displayOn; this.getFluid = getFluid; diff --git a/src/machines/java/com/enderio/machines/client/gui/widget/ExperienceWidget.java b/src/machines/java/com/enderio/machines/client/gui/widget/ExperienceWidget.java index ee2cd9c463..e2bf5b029a 100644 --- a/src/machines/java/com/enderio/machines/client/gui/widget/ExperienceWidget.java +++ b/src/machines/java/com/enderio/machines/client/gui/widget/ExperienceWidget.java @@ -3,7 +3,7 @@ import com.enderio.api.misc.Vector2i; import com.enderio.base.common.util.ExperienceUtil; import com.enderio.core.client.gui.widgets.EIOWidget; -import com.enderio.machines.common.io.fluid.MachineTank; +import com.enderio.machines.common.io.fluid.MachineFluidTank; import com.mojang.blaze3d.systems.RenderSystem; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiGraphics; @@ -16,9 +16,9 @@ public class ExperienceWidget extends EIOWidget { private static final ResourceLocation GUI_ICONS_LOCATION = new ResourceLocation("textures/gui/icons.png"); private final Screen displayOn; - private final Supplier getFluid; + private final Supplier getFluid; - public ExperienceWidget(Screen displayOn, Supplier getFluid, int pX, int pY, int pWidth, int pHeight) { + public ExperienceWidget(Screen displayOn, Supplier getFluid, int pX, int pY, int pWidth, int pHeight) { super(pX, pY, pWidth, pHeight); this.displayOn = displayOn; this.getFluid = getFluid; diff --git a/src/machines/java/com/enderio/machines/client/gui/widget/FluidStackStaticWidget.java b/src/machines/java/com/enderio/machines/client/gui/widget/FluidStackStaticWidget.java index c5fd2df9ee..4cdc978d42 100644 --- a/src/machines/java/com/enderio/machines/client/gui/widget/FluidStackStaticWidget.java +++ b/src/machines/java/com/enderio/machines/client/gui/widget/FluidStackStaticWidget.java @@ -1,7 +1,7 @@ package com.enderio.machines.client.gui.widget; import com.enderio.core.client.gui.widgets.EIOWidget; -import com.enderio.machines.common.io.fluid.MachineTank; +import com.enderio.machines.common.io.fluid.MachineFluidTank; import com.mojang.blaze3d.systems.RenderSystem; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiGraphics; @@ -22,9 +22,9 @@ public class FluidStackStaticWidget extends EIOWidget { private final Screen displayOn; - private final Supplier getFluid; + private final Supplier getFluid; - public FluidStackStaticWidget(Screen displayOn, Supplier getFluid, int pX, int pY, int pWidth, int pHeight) { + public FluidStackStaticWidget(Screen displayOn, Supplier getFluid, int pX, int pY, int pWidth, int pHeight) { super(pX, pY, pWidth, pHeight); this.displayOn = displayOn; this.getFluid = getFluid; @@ -35,7 +35,7 @@ public void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float Minecraft minecraft = Minecraft.getInstance(); RenderSystem.defaultBlendFunc(); RenderSystem.enableDepthTest(); - MachineTank fluidTank = getFluid.get(); + MachineFluidTank fluidTank = getFluid.get(); if (!fluidTank.getFluid().isEmpty()) { FluidStack fluidStack = fluidTank.getFluid(); IClientFluidTypeExtensions props = IClientFluidTypeExtensions.of(fluidStack.getFluid()); diff --git a/src/machines/java/com/enderio/machines/client/gui/widget/FluidStackWidget.java b/src/machines/java/com/enderio/machines/client/gui/widget/FluidStackWidget.java index 7e585700da..1e75a7a5a4 100644 --- a/src/machines/java/com/enderio/machines/client/gui/widget/FluidStackWidget.java +++ b/src/machines/java/com/enderio/machines/client/gui/widget/FluidStackWidget.java @@ -1,7 +1,7 @@ package com.enderio.machines.client.gui.widget; import com.enderio.core.client.gui.widgets.EIOWidget; -import com.enderio.machines.common.io.fluid.MachineTank; +import com.enderio.machines.common.io.fluid.MachineFluidTank; import com.mojang.blaze3d.systems.RenderSystem; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiGraphics; @@ -22,9 +22,9 @@ public class FluidStackWidget extends EIOWidget { private final Screen displayOn; - private final Supplier getFluid; + private final Supplier getFluid; - public FluidStackWidget(Screen displayOn, Supplier getFluid, int pX, int pY, int pWidth, int pHeight) { + public FluidStackWidget(Screen displayOn, Supplier getFluid, int pX, int pY, int pWidth, int pHeight) { super(pX, pY, pWidth, pHeight); this.displayOn = displayOn; this.getFluid = getFluid; @@ -35,7 +35,7 @@ public void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float Minecraft minecraft = Minecraft.getInstance(); RenderSystem.defaultBlendFunc(); RenderSystem.enableDepthTest(); - MachineTank fluidTank = getFluid.get(); + MachineFluidTank fluidTank = getFluid.get(); if (!fluidTank.getFluid().isEmpty()) { FluidStack fluidStack = fluidTank.getFluid(); IClientFluidTypeExtensions props = IClientFluidTypeExtensions.of(fluidStack.getFluid()); diff --git a/src/machines/java/com/enderio/machines/client/rendering/blockentity/FluidTankBER.java b/src/machines/java/com/enderio/machines/client/rendering/blockentity/FluidTankBER.java index cdfea5ff7c..44b841692f 100644 --- a/src/machines/java/com/enderio/machines/client/rendering/blockentity/FluidTankBER.java +++ b/src/machines/java/com/enderio/machines/client/rendering/blockentity/FluidTankBER.java @@ -2,7 +2,7 @@ import com.enderio.core.client.RenderUtil; import com.enderio.machines.common.blockentity.FluidTankBlockEntity; -import com.enderio.machines.common.io.fluid.MachineTank; +import com.enderio.machines.common.io.fluid.MachineFluidTank; import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.blaze3d.vertex.VertexConsumer; import net.minecraft.client.Minecraft; @@ -29,7 +29,7 @@ public FluidTankBER(BlockEntityRendererProvider.Context context) { public void render(FluidTankBlockEntity blockEntity, float partialTick, PoseStack poseStack, MultiBufferSource bufferSource, int packedLight, int packedOverlay) { - MachineTank tank = blockEntity.getFluidTank(); + MachineFluidTank tank = blockEntity.getFluidTank(); // Don't waste time if there's no fluid. if (!tank.getFluid().isEmpty()) { 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 05426bc807..afb0c0c792 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/DrainBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/DrainBlockEntity.java @@ -11,7 +11,7 @@ import com.enderio.machines.common.blockentity.base.PoweredMachineBlockEntity; import com.enderio.machines.common.config.MachinesConfig; import com.enderio.machines.common.io.FixedIOConfig; -import com.enderio.machines.common.io.fluid.MachineTank; +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.io.item.MachineInventoryLayout; @@ -80,7 +80,7 @@ public void updateServerCallback() { return MachineTankLayout.builder().tank(TANK, CAPACITY, f -> type.isSame(f.getFluid())).build(); } - public MachineTank getFluidTank() { + public MachineFluidTank getFluidTank() { return getFluidHandler().getTank(TANK.getIndex()); } 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 de36770154..91ab68e3a7 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,7 @@ import com.enderio.machines.common.init.MachineRecipes; import com.enderio.machines.common.io.fluid.IFluidItemInteractive; import com.enderio.machines.common.io.fluid.MachineFluidHandler; -import com.enderio.machines.common.io.fluid.MachineTank; +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.io.item.MachineInventoryLayout; @@ -192,7 +192,7 @@ protected void onContentsChanged(int slot) { }; } - public MachineTank getFluidTank() { + public MachineFluidTank getFluidTank() { return getFluidHandler().getTank(TANK.getIndex()); } @@ -230,7 +230,7 @@ private void fillInternal() { @Override public InteractionResult onBlockEntityUsed(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { ItemStack stack = player.getItemInHand(hand); - if (!stack.isEmpty() && handleTankInteraction(player, hand, stack, this, TANK)) { + if (!stack.isEmpty() && handleFluidItemInteraction(player, hand, stack, this, TANK)) { player.getInventory().setChanged(); return InteractionResult.CONSUME; } 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 894983777f..0686c930c4 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/SoulBinderBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/SoulBinderBlockEntity.java @@ -15,7 +15,7 @@ import com.enderio.machines.common.config.MachinesConfig; import com.enderio.machines.common.init.MachineRecipes; import com.enderio.machines.common.io.fluid.MachineFluidHandler; -import com.enderio.machines.common.io.fluid.MachineTank; +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.io.item.MachineInventoryLayout; @@ -165,7 +165,7 @@ public int fill(FluidStack resource, FluidAction action) { }; } - public MachineTank getFluidTank() { + public MachineFluidTank getFluidTank() { return getFluidHandler().getTank(TANK.getIndex()); } 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 e381a66377..760ada097c 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/SoulEngineBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/SoulEngineBlockEntity.java @@ -12,7 +12,7 @@ import com.enderio.machines.common.blockentity.base.PoweredMachineBlockEntity; import com.enderio.machines.common.config.MachinesConfig; import com.enderio.machines.common.io.fluid.MachineFluidHandler; -import com.enderio.machines.common.io.fluid.MachineTank; +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.io.item.MachineInventoryLayout; @@ -181,7 +181,7 @@ private Predicate isFluidValid() { } - public MachineTank getFluidTank() { + public MachineFluidTank getFluidTank() { return getFluidHandler().getTank(TANK.getIndex()); } 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 d3a4791717..2ef0d2311c 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/XPObeliskBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/XPObeliskBlockEntity.java @@ -6,7 +6,7 @@ import com.enderio.core.common.network.slot.IntegerNetworkDataSlot; import com.enderio.machines.common.blockentity.base.MachineBlockEntity; import com.enderio.machines.common.io.fluid.MachineFluidHandler; -import com.enderio.machines.common.io.fluid.MachineTank; +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; @@ -72,7 +72,7 @@ public int fill(FluidStack resource, FluidAction action) { }; } - public MachineTank getFluidTank() { + public MachineFluidTank getFluidTank() { return getFluidHandler().getTank(TANK.getIndex()); } 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 5b00aef5c4..60a1d05bd0 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/XPVacuumBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/XPVacuumBlockEntity.java @@ -5,7 +5,7 @@ import com.enderio.machines.common.blockentity.base.VacuumMachineBlockEntity; import com.enderio.machines.common.config.MachinesConfig; import com.enderio.machines.common.io.fluid.MachineFluidHandler; -import com.enderio.machines.common.io.fluid.MachineTank; +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; @@ -72,7 +72,7 @@ protected void onContentsChanged(int slot) { }; } - public MachineTank getFluidTank() { + public MachineFluidTank getFluidTank() { return getFluidHandler().getTank(TANK.getIndex()); } 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 727349751f..41ba6fcb75 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 @@ -18,7 +18,7 @@ public interface IFluidItemInteractive { // Requires direct tank access which is undesirable. MachineFluidHandler would be better to for multi-tank block. - default boolean handleTankInteraction(Player player, InteractionHand hand, ItemStack itemStack, MachineBlockEntity machine, TankAccess tankAccess) { + default boolean handleFluidItemInteraction(Player player, InteractionHand hand, ItemStack itemStack, MachineBlockEntity machine, TankAccess tankAccess) { ItemStack copyStack = itemStack.copyWithCount(1); Optional fluidHandlerItem = FluidUtil.getFluidHandler(copyStack).resolve(); if (fluidHandlerItem.isPresent()) { 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 c0b56b81a4..2347ac0a8e 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 @@ -30,7 +30,7 @@ public class MachineFluidHandler implements IFluidHandler, IEnderCapabilityProvi private final IIOConfig config; private final MachineTankLayout layout; - private List tanks; + private List tanks; private final EnumMap> sideCache = new EnumMap<>(Direction.class); private LazyOptional selfCache = LazyOptional.empty(); @@ -58,7 +58,7 @@ public MachineTankLayout getLayout() { //Not a good idea to use this method. Tank Access should be the way to access tanks @Deprecated - public final MachineTank getTank(int tank) { + public final MachineFluidTank getTank(int tank) { return tanks.get(tank); } @@ -217,12 +217,12 @@ public CompoundTag serializeNBT() { @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, MachineTank.EMPTY); + tanks = NonNullList.withSize(size, MachineFluidTank.EMPTY); 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, MachineTank.from(tankTag)); + tanks.set(index, MachineFluidTank.from(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 2eb7516443..787d6a14fe 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,272 +1,126 @@ package com.enderio.machines.common.io.fluid; -import com.enderio.EnderIO; -import net.minecraft.world.InteractionHand; -import net.minecraft.world.InteractionResult; -import net.minecraft.world.entity.EquipmentSlot; -import net.minecraft.world.entity.player.Player; -import net.minecraft.world.item.BucketItem; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.Items; -import net.minecraft.world.level.block.entity.BlockEntity; -import net.minecraftforge.common.capabilities.ForgeCapabilities; +import net.minecraft.nbt.CompoundTag; import net.minecraftforge.fluids.FluidStack; -import net.minecraftforge.fluids.FluidType; +import net.minecraftforge.fluids.IFluidTank; import net.minecraftforge.fluids.capability.IFluidHandler; -import net.minecraftforge.fluids.capability.IFluidHandlerItem; -import net.minecraftforge.fluids.capability.templates.FluidTank; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import java.util.Optional; -import java.util.function.Predicate; +public class MachineFluidTank implements IFluidTank { -public class MachineFluidTank extends FluidTank { - public boolean allowInput = true; - public boolean allowOutput = true; + public static final String Capacity = "Capacity"; - @Nullable - private final BlockEntity parent; + private final int capacity; + @NotNull private FluidStack fluid = FluidStack.EMPTY; + public static final MachineFluidTank EMPTY = new MachineFluidTank(0); - public MachineFluidTank(int capacity, BlockEntity parent) { - super(capacity, (FluidStack stack) -> true); - this.parent = parent; + public MachineFluidTank(int capacity) { + this.capacity = capacity; } - public MachineFluidTank(BlockEntity parent) { - super(8 * FluidType.BUCKET_VOLUME, (FluidStack stack) -> true); - this.parent = parent; + public MachineFluidTank(FluidStack stack, int capacity) { + this(capacity); + this.fluid = stack.copy(); } - /** - * @param capacity The capacity of the tank - * @param validator The filter, a lambda expression that should return whether a fluid is allowed or not. - * @param parent The container to be updated in case that the amount or type of fluid changes. - * (Usually the block entity that "contains" the tank) - */ - public MachineFluidTank(int capacity, Predicate validator, BlockEntity parent) { - super(capacity, validator); - this.parent = parent; + public static MachineFluidTank from(CompoundTag tag) { + FluidStack stack = FluidStack.loadFluidStackFromNBT(tag); + int capacity = tag.getInt(Capacity); + return new MachineFluidTank(stack, capacity); } - /** - * Transfers fluid from a fluid handler to this tank. - * - * @param from Fluid handler to transfer from - * @param desiredAmount The max amount to transfer if possible. - * @param force Force transfer even if 'allowInput' is false or validator prohibits fluid to enter. It respects the fluid types though - * @return The amount that was actually transferred. - */ - public int transferFrom(IFluidHandler from, int desiredAmount, boolean force) { - FluidStack incomingStack = from.drain(desiredAmount, FluidAction.SIMULATE); - int transferredAmount = fill(incomingStack, FluidAction.EXECUTE, force); - from.drain(transferredAmount, FluidAction.EXECUTE); - return transferredAmount; + @Override + public int getCapacity() { + return capacity; } - /** - * Transfers fluid from a fluid handler to this tank. - * - * @param to The handler to transfer into. - * @param desiredAmount The max amount to transfer if possible. - * @param force Force transfer even if 'allowOutput' is false. - * @return The amount that was actually transferred. - */ - public int transferTo(IFluidHandler to, int desiredAmount, boolean force) { - FluidStack outStack = fluid.copy(); - int transferAmount = 0; - if (!outStack.isEmpty()) { - outStack.setAmount(drain(desiredAmount, FluidAction.SIMULATE, force));//caps at the tank's capacity to transfer out of and at the desired amount - transferAmount = to.fill(outStack, FluidAction.EXECUTE); - } - drain(transferAmount, FluidAction.EXECUTE);//should always succeed to transfer what we have simulated - return transferAmount; + @Override + public @NotNull FluidStack getFluid() { + return fluid; } - /** - * Works like the FluidTank class of forge with some additions described below. - * - * @param source The source fluid stack. Tries to increase tank amount with entire source amount. - * WARNING! This method does not drain the source stack according to the forge FluidTank implementation. - * For that use case, use the method "transferFrom" - * @param action Execute makes the transfer happen, simulate checks what would happen if executed. - * @param force Force your way around the allow input flag and the validator (filter). Usually used to - * fill a machine output tank from within the machine. - * @return The amount that could be filled in case of simulation or was filled in case of execution. - *

- * Credit: the Forge team, since the logic is their work. - */ - public int fill(FluidStack source, FluidAction action, boolean force) { - if (source.isEmpty()) { - return 0; - } + public void setFluid(FluidStack fluid) { + this.fluid = fluid; + } - if (!fluid.isEmpty() && !fluid.isFluidEqual(source)) { - return 0; - } + @Override + public int getFluidAmount() { + return fluid.getAmount(); + } - if (!allowInput && !force) { - return 0;//Continue filling if either allow input or force input - } + @Override + public boolean isFluidValid(FluidStack stack) { + return true; + } + + public boolean isEmpty() { + return getFluid().isEmpty(); + } - if (!isFluidValid(source) && !force) { + public int fill(FluidStack resource, IFluidHandler.FluidAction action) { + if (resource.isEmpty()) return 0; - } else if (action.simulate()) { - return Math.min(capacity - fluid.getAmount(), source.getAmount()); - } else { - if (fluid.isEmpty()) { - fluid = new FluidStack(source, Math.min(capacity, source.getAmount())); - onContentsChanged();//case source is empty is checked in beginning. - return fluid.getAmount(); - } else { - int availableSpace = capacity - fluid.getAmount(); - int sourceAmount = source.getAmount(); - int transferAmount = Math.min(availableSpace, sourceAmount); - fluid.grow(transferAmount); - //according to the parent implementation the source shall not be drained - if (transferAmount > 0) { - onContentsChanged(); - } - return transferAmount; + 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()); } - } - - //Same as parent behavior with the addition of this class capabilities, see method 'fill' above for details. - public int fill(int desiredAmount, FluidAction action, boolean force) { if (fluid.isEmpty()) { - EnderIO.LOGGER.error("No fluid in tank, can't contain an amount of unspecified fluid other than 0"); - return 0; + fluid = new FluidStack(resource, Math.min(capacity, resource.getAmount())); + onContentsChanged(); + return fluid.getAmount(); } - return fill(new FluidStack(fluid.getFluid(), desiredAmount), action, force); - } - - //Same as parent behavior with the addition of this class capabilities, see method 'fill' above for details. - @Override - public int fill(FluidStack source, FluidAction action) { - return fill(source, action, false); - } - - /** - * Drains fluid out of the tank. - * - * @param maxDrain The maximal amount that you wish to draw in mb. In case of using a bucket it would be FluidType.BUCKET_VOLUME (1000)mb. - * @param action Execute if the tank amount should be changed, simulate if not. - * @param force Force your way around the allow output flag. Usually used to drain a machine "input" tank from within the machine. - * @return The amount that was drained in case of action is execute and how much could be drawn in case of - */ - public int drain(int maxDrain, FluidAction action, boolean force) { - if (maxDrain <= 0) { + if (!fluid.isFluidEqual(resource)) { return 0; } + int filled = capacity - fluid.getAmount(); - if (!allowOutput && !force) { - return 0; // Continue draining only if either allow drain or force + if (resource.getAmount() < filled) { + fluid.grow(resource.getAmount()); + filled = resource.getAmount(); + } else { + fluid.setAmount(capacity); } - - int transferAmount = Math.min(maxDrain, fluid.getAmount()); - if (action == FluidAction.EXECUTE && transferAmount > 0) { - fluid.shrink(transferAmount); + if (filled > 0) onContentsChanged(); - } - - return transferAmount; + return filled; } - //Same as parent behavior with the addition of this class capabilities, see method 'drain' above for details. + @NotNull @Override - public @NotNull FluidStack drain(FluidStack resource, FluidAction action) { - if (resource.isEmpty() || !resource.isFluidEqual(fluid)) { + public FluidStack drain(FluidStack resource, IFluidHandler.FluidAction action) { + if (resource.isEmpty() || !resource.isFluidEqual(fluid)) return FluidStack.EMPTY; - } return drain(resource.getAmount(), action); } - //Same as parent behavior with the addition of this class capabilities, see method 'drain' above for details + @NotNull @Override - public @NotNull FluidStack drain(int maxDrain, FluidAction action) { - // Get this before the drain, otherwise it will forget what we had stored. - var storedFluid = fluid.getFluid(); - int amount = drain(maxDrain, action, false); - if (amount == 0) { - return FluidStack.EMPTY; + public FluidStack drain(int maxDrain, IFluidHandler.FluidAction action) { + int drained = maxDrain; + if (fluid.getAmount() < drained) { + drained = fluid.getAmount(); } - - return new FluidStack(storedFluid, amount); - } - - /** - * Checks whether the item in the players hand can deposit or take up fluids from this tank, and if so does the transfer - * - * @param player The player. - * @param hand The interaction hand. - * @return Consumed if interacted, success otherwise. - */ - public InteractionResult onClickedWithPotentialFluidItem(Player player, InteractionHand hand) { - if (!player.level().isClientSide() && player.hasItemInSlot(EquipmentSlot.MAINHAND) && hand == InteractionHand.MAIN_HAND) { - ItemStack heldStack = player.getMainHandItem(); - Optional heldItemFH = heldStack.getCapability(ForgeCapabilities.FLUID_HANDLER_ITEM).resolve(); - if (heldStack.getItem() == Items.BUCKET && allowOutput) { - //Clicked with empty bucket and tank allowed to output fluids - if (getFluidAmount() >= FluidType.BUCKET_VOLUME) { - FluidStack outStack = drain(FluidType.BUCKET_VOLUME, IFluidHandler.FluidAction.EXECUTE); - ItemStack filledBucket = new ItemStack(outStack.getFluid().getBucket(), 1); - if (heldStack.getCount() == 1) { - player.setItemInHand(hand, filledBucket); - } else { - heldStack.shrink(1); - if (!player.addItem(filledBucket)) {//has an empty slot in inventory - player.drop(filledBucket, true); - } - } - return InteractionResult.CONSUME; - } - } else if (heldStack.getItem() instanceof BucketItem filledBucket && allowInput) { - //Clicked with bucket of something and tank allowed to input said thing - FluidStack bucketContent = new FluidStack(filledBucket.getFluid(), FluidType.BUCKET_VOLUME); - if (fill(bucketContent, IFluidHandler.FluidAction.SIMULATE) == FluidType.BUCKET_VOLUME) {//can fit entire bucket in tank? - fill(bucketContent, IFluidHandler.FluidAction.EXECUTE); - player.setItemInHand(InteractionHand.MAIN_HAND, new ItemStack(Items.BUCKET, 1)); - return InteractionResult.CONSUME; - } - } else if (heldItemFH.isPresent()) {//Player holding item capable of holding fluids that isn't a bucket - IFluidHandlerItem itemFluid = heldItemFH.get(); - boolean hasTransferred = false; - - if (allowOutput) {//clicked with tank that can take my fluid, extracting fluid - hasTransferred = transferTo(itemFluid, Integer.MAX_VALUE, false) > 0; - } - if (allowInput && !hasTransferred) {//clicked with tank containing fluids, push fluid into tank - hasTransferred = transferFrom(itemFluid, Integer.MAX_VALUE, false) > 0;//op-operator in edge case - //where interacted item has more than one fluid and I have - } - - return hasTransferred ? InteractionResult.CONSUME : InteractionResult.PASS; - } + FluidStack stack = new FluidStack(fluid, drained); + if (action.execute() && drained > 0) { + fluid.shrink(drained); + onContentsChanged(); } - return InteractionResult.PASS;//Leave vanilla behaviour. + return stack; } - @Override - public void setFluid(FluidStack stack) { - setFluid(stack, true); - } + protected void onContentsChanged() {} - public void setFluid(FluidStack stack, boolean force) { - if (force || (allowInput && isFluidValid(stack))) { - this.fluid = stack; - onContentsChanged(); - } + public CompoundTag save(CompoundTag compoundTag) { + getFluid().writeToNBT(compoundTag); + compoundTag.putInt(Capacity, getCapacity()); + return compoundTag; } - //updates the entity if one is marked - @Override - protected void onContentsChanged() { - super.onContentsChanged(); - if (parent != null) { - parent.setChanged(); - } - } } diff --git a/src/machines/java/com/enderio/machines/common/io/fluid/MachineTank.java b/src/machines/java/com/enderio/machines/common/io/fluid/MachineTank.java deleted file mode 100644 index a550059310..0000000000 --- a/src/machines/java/com/enderio/machines/common/io/fluid/MachineTank.java +++ /dev/null @@ -1,127 +0,0 @@ -package com.enderio.machines.common.io.fluid; - -import net.minecraft.nbt.CompoundTag; -import net.minecraftforge.fluids.FluidStack; -import net.minecraftforge.fluids.IFluidTank; -import net.minecraftforge.fluids.capability.IFluidHandler; -import org.jetbrains.annotations.NotNull; - -//Replace MachineFluidTank -public class MachineTank implements IFluidTank { - - public static final String Capacity = "Capacity"; - - private final int capacity; - @NotNull private FluidStack fluid = FluidStack.EMPTY; - public static final MachineTank EMPTY = new MachineTank(0); - - public MachineTank(int capacity) { - this.capacity = capacity; - } - - public MachineTank(FluidStack stack, int capacity) { - this(capacity); - this.fluid = stack.copy(); - } - - public static MachineTank from(CompoundTag tag) { - FluidStack stack = FluidStack.loadFluidStackFromNBT(tag); - int capacity = tag.getInt(Capacity); - return new MachineTank(stack, capacity); - } - - @Override - public int getCapacity() { - return capacity; - } - - @Override - public @NotNull FluidStack getFluid() { - return fluid; - } - - public void setFluid(FluidStack fluid) { - this.fluid = fluid; - } - - @Override - public int getFluidAmount() { - return fluid.getAmount(); - } - - @Override - public boolean isFluidValid(FluidStack stack) { - return true; - } - - public boolean isEmpty() { - return getFluid().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; - } - - @NotNull - @Override - public FluidStack drain(FluidStack resource, IFluidHandler.FluidAction action) { - if (resource.isEmpty() || !resource.isFluidEqual(fluid)) - return FluidStack.EMPTY; - - return drain(resource.getAmount(), 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; - } - - 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 d7d814f557..67832b5416 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() { - List tankList = new ArrayList<>(); + public List createTanks() { + List tankList = new ArrayList<>(); tanks.forEach(config -> { - tankList.add(new MachineTank(config.capacity())); + tankList.add(new MachineFluidTank(config.capacity())); }); return tankList; } diff --git a/src/machines/java/com/enderio/machines/common/recipe/TankRecipe.java b/src/machines/java/com/enderio/machines/common/recipe/TankRecipe.java index 5d07a49211..3f609ef83a 100644 --- a/src/machines/java/com/enderio/machines/common/recipe/TankRecipe.java +++ b/src/machines/java/com/enderio/machines/common/recipe/TankRecipe.java @@ -4,7 +4,7 @@ import com.enderio.core.common.recipes.EnderRecipe; import com.enderio.machines.common.blockentity.FluidTankBlockEntity; import com.enderio.machines.common.init.MachineRecipes; -import com.enderio.machines.common.io.fluid.MachineTank; +import com.enderio.machines.common.io.fluid.MachineFluidTank; import com.google.gson.JsonObject; import net.minecraft.ResourceLocationException; import net.minecraft.core.RegistryAccess; @@ -101,14 +101,14 @@ public RecipeType getType() { public static class Container extends RecipeWrapper { - private final MachineTank fluidTank; + private final MachineFluidTank fluidTank; - public Container(IItemHandlerModifiable inv, MachineTank fluidTank) { + public Container(IItemHandlerModifiable inv, MachineFluidTank fluidTank) { super(inv); this.fluidTank = fluidTank; } - public MachineTank getFluidTank() { + public MachineFluidTank getFluidTank() { return fluidTank; } } From 93e3f286175ec192a0196cd505054f3ebdd805b5 Mon Sep 17 00:00:00 2001 From: mystchonky Date: Fri, 22 Dec 2023 13:09:30 +0530 Subject: [PATCH 10/10] remove direct references of Tank index --- .../common/blockentity/DrainBlockEntity.java | 2 +- .../common/blockentity/FluidTankBlockEntity.java | 4 ++-- .../common/blockentity/SoulBinderBlockEntity.java | 2 +- .../common/blockentity/SoulEngineBlockEntity.java | 2 +- .../common/blockentity/XPObeliskBlockEntity.java | 2 +- .../common/blockentity/XPVacuumBlockEntity.java | 2 +- .../machines/common/io/fluid/TankAccess.java | 14 +++++++++----- 7 files changed, 16 insertions(+), 12 deletions(-) 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 e58c501825..526f074d48 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/DrainBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/DrainBlockEntity.java @@ -82,7 +82,7 @@ public void updateServerCallback() { } public MachineFluidTank getFluidTank() { - return getFluidHandler().getTank(TANK.getIndex()); + return TANK.getTank(this); } @Override 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 3f965774a1..0338f6fc74 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/FluidTankBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/FluidTankBlockEntity.java @@ -87,7 +87,7 @@ public FluidTankBlockEntity(BlockEntityType type, BlockPos worldPosition, Blo addDataSlot(new FluidStackNetworkDataSlot(() -> TANK.getFluid(this), f -> TANK.setFluid(this, f))); // Wrap container for fluid recipes - container = new TankRecipe.Container(getInventoryNN(), getFluidHandler().getTank(TANK.getIndex())); + container = new TankRecipe.Container(getInventoryNN(), TANK.getTank(this)); } @Override @@ -194,7 +194,7 @@ protected void onContentsChanged(int slot) { } public MachineFluidTank getFluidTank() { - return getFluidHandler().getTank(TANK.getIndex()); + return TANK.getTank(this); } // endregion 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 80e21329cf..b0cbaf0688 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/SoulBinderBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/SoulBinderBlockEntity.java @@ -165,7 +165,7 @@ public int fill(FluidStack resource, FluidAction action) { } public MachineFluidTank getFluidTank() { - return getFluidHandler().getTank(TANK.getIndex()); + return TANK.getTank(this); } // endregion 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 3efee1d272..c5548c2aaf 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/SoulEngineBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/SoulEngineBlockEntity.java @@ -187,7 +187,7 @@ private Predicate isFluidValid() { } public MachineFluidTank getFluidTank() { - return getFluidHandler().getTank(TANK.getIndex()); + return TANK.getTank(this); } @Nullable 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 9d95950da0..8f46c1a85e 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/XPObeliskBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/XPObeliskBlockEntity.java @@ -73,7 +73,7 @@ public int fill(FluidStack resource, FluidAction action) { } public MachineFluidTank getFluidTank() { - return getFluidHandler().getTank(TANK.getIndex()); + return TANK.getTank(this); } public void addLevelToPlayer(int levelDiff, Player player) { 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 fded6855bb..2995b7ba73 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/XPVacuumBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/XPVacuumBlockEntity.java @@ -73,7 +73,7 @@ protected void onContentsChanged(int slot) { } public MachineFluidTank getFluidTank() { - return getFluidHandler().getTank(TANK.getIndex()); + return TANK.getTank(this); } // endregion 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 07b07443f0..e1afd3ffd1 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 @@ -8,6 +8,14 @@ public class TankAccess { private int index = Integer.MIN_VALUE; + public MachineFluidTank getTank(MachineBlockEntity machine) { + return getTank(machine.getFluidHandler()); + } + + public MachineFluidTank getTank(MachineFluidHandler fluidHandler) { + return fluidHandler.getTank(index); + } + public int getCapacity(MachineBlockEntity machine) { return getCapacity(machine.getFluidHandler()); } @@ -80,14 +88,10 @@ public FluidStack drain(MachineBlockEntity machine, int maxDrain, IFluidHandler. return drain(machine.getFluidHandler(), maxDrain, action); } - public boolean isSlot(int slot) { + public boolean isTank(int slot) { return this.index == slot; } - public int getIndex() { - return index; - } - void init(int i) { if (index == Integer.MIN_VALUE) { index = i;