From a6982807858dc9e4412a7e44e1861912c9a905b2 Mon Sep 17 00:00:00 2001 From: ferriarnus <61201275+ferriarnus@users.noreply.github.com> Date: Sat, 24 Feb 2024 21:30:45 +0100 Subject: [PATCH] it works --- .../common/io/fluid/MachineFluidHandler.java | 29 ++++++++++--------- .../common/io/fluid/MachineFluidTank.java | 23 ++------------- .../common/io/fluid/MachineTankLayout.java | 8 ----- .../machines/common/io/fluid/TankAccess.java | 6 ++-- .../machines/common/item/FluidTankItem.java | 16 +++++++--- 5 files changed, 33 insertions(+), 49 deletions(-) diff --git a/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidHandler.java b/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidHandler.java index 9fd57f0e90..6ce65c0dee 100644 --- a/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidHandler.java +++ b/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidHandler.java @@ -12,7 +12,9 @@ import org.jetbrains.annotations.Nullable; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.function.IntConsumer; /** @@ -24,7 +26,8 @@ public class MachineFluidHandler implements IFluidHandler, INBTSerializable tanks; + private Map tanks = new HashMap<>(); + private List stacks; // Not sure if we need this but might be useful to update recipe/task if tank is filled. private IntConsumer changeListener = i -> {}; @@ -32,7 +35,7 @@ public class MachineFluidHandler implements IFluidHandler, INBTSerializable getTanks()) { + throw new IndexOutOfBoundsException("No tank found for index " + tank + " in range" + getTanks() + "."); } - return machineFluidTank; + return tanks.computeIfAbsent(tank, i -> new MachineFluidTank(i, this)); } @Override @@ -65,11 +66,11 @@ public int getTanks() { @Override public FluidStack getFluidInTank(int tank) { - return tanks.get(tank).getFluid(); + return stacks.get(tank); } public void setFluidInTank(int tank, FluidStack fluid) { - tanks.get(tank).setFluid(fluid); + stacks.set(tank, fluid); } @Override @@ -112,6 +113,7 @@ public int fill(int tank, FluidStack resource, IFluidHandler.FluidAction action) } if (fluid.isEmpty()) { fluid = new FluidStack(resource, Math.min(capacity, resource.getAmount())); + setFluidInTank(tank, fluid); onContentsChanged(tank); return fluid.getAmount(); } @@ -142,7 +144,7 @@ public int fill(FluidStack resource, FluidAction action) { FluidStack resourceLeft = resource.copy(); int totalFilled = 0; - for (int index = 0; index < tanks.size(); index++) { + for (int index = 0; index < getTanks(); index++) { if (!layout.canInsert(index)) continue; @@ -191,7 +193,7 @@ public FluidStack drain(FluidStack resource, FluidAction action) { @Override public FluidStack drain(int maxDrain, FluidAction action) { - for (int index = 0; index < tanks.size(); index++) { + for (int index = 0; index < getTanks(); index++) { if (drain(index, maxDrain, FluidAction.SIMULATE) != FluidStack.EMPTY) { FluidStack drained = drain(index, maxDrain, action); if (!drained.isEmpty()) { @@ -213,7 +215,7 @@ public CompoundTag serializeNBT() { for (int i = 0; i < getTanks(); i++) { CompoundTag tankTag = new CompoundTag(); tankTag.putInt(TANK_INDEX, i); - tanks.get(i).save(tankTag); + stacks.get(i).writeToNBT(tankTag); nbtTagList.add(tankTag); } CompoundTag nbt = new CompoundTag(); @@ -223,12 +225,11 @@ public CompoundTag serializeNBT() { @Override public void deserializeNBT(CompoundTag nbt) { - tanks = layout.createTanks(this); ListTag tagList = nbt.getList(TANKS, Tag.TAG_COMPOUND); for (int i = 0; i < tagList.size(); i++) { CompoundTag tankTag = tagList.getCompound(i); int index = tankTag.getInt(TANK_INDEX); - tanks.set(index, MachineFluidTank.from(tankTag, index, this)); + stacks.set(index, FluidStack.loadFluidStackFromNBT(tankTag)); } } diff --git a/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidTank.java b/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidTank.java index 26fa24228b..69a3d9108b 100644 --- a/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidTank.java +++ b/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidTank.java @@ -10,21 +10,12 @@ public class MachineFluidTank implements IFluidTank { private final int index; private final MachineFluidHandler handler; - @NotNull - private FluidStack fluid = FluidStack.EMPTY; public MachineFluidTank(int index, MachineFluidHandler handler) { this.index = index; this.handler = handler; } - public static MachineFluidTank from(CompoundTag tag, int index, MachineFluidHandler handler) { - FluidStack stack = FluidStack.loadFluidStackFromNBT(tag); - MachineFluidTank machineFluidTank = new MachineFluidTank(index, handler); - machineFluidTank.setFluid(stack); - return machineFluidTank; - } - @Override public int getCapacity() { return handler.getTankCapacity(index); @@ -32,16 +23,16 @@ public int getCapacity() { @Override public @NotNull FluidStack getFluid() { - return fluid; + return handler.getFluidInTank(index); } public void setFluid(FluidStack fluid) { - this.fluid = fluid; + handler.setFluidInTank(index, fluid); } @Override public int getFluidAmount() { - return fluid.getAmount(); + return getFluid().getAmount(); } @Override @@ -68,12 +59,4 @@ public FluidStack drain(FluidStack resource, IFluidHandler.FluidAction action) { public FluidStack drain(int maxDrain, IFluidHandler.FluidAction action) { return handler.drain(index, maxDrain, action); } - - protected void onContentsChanged() {} - - public CompoundTag save(CompoundTag compoundTag) { - getFluid().writeToNBT(compoundTag); - return compoundTag; - } - } diff --git a/src/machines/java/com/enderio/machines/common/io/fluid/MachineTankLayout.java b/src/machines/java/com/enderio/machines/common/io/fluid/MachineTankLayout.java index a3165094cf..84599c24fb 100644 --- a/src/machines/java/com/enderio/machines/common/io/fluid/MachineTankLayout.java +++ b/src/machines/java/com/enderio/machines/common/io/fluid/MachineTankLayout.java @@ -44,14 +44,6 @@ public boolean isFluidValid(int slot, FluidStack stack) { return tanks.get(slot).filter().test(stack); } - public List createTanks(MachineFluidHandler handler) { - List tankList = new ArrayList<>(); - tanks.forEach(config -> { - tankList.add(new MachineFluidTank(tanks.indexOf(config), handler)); - }); - return tankList; - } - public static class Builder { private final ArrayList tanks = new ArrayList<>(); diff --git a/src/machines/java/com/enderio/machines/common/io/fluid/TankAccess.java b/src/machines/java/com/enderio/machines/common/io/fluid/TankAccess.java index 66621904e1..1190d43374 100644 --- a/src/machines/java/com/enderio/machines/common/io/fluid/TankAccess.java +++ b/src/machines/java/com/enderio/machines/common/io/fluid/TankAccess.java @@ -65,7 +65,7 @@ public boolean isEmpty(MachineFluidHandler handler) { } public int fill(MachineFluidHandler handler, FluidStack stack, IFluidHandler.FluidAction action) { - return handler.fill(stack, action); + return handler.fill(index, stack, action); } public int fill(IFluidTankUser machine, FluidStack stack, IFluidHandler.FluidAction action) { @@ -73,7 +73,7 @@ public int fill(IFluidTankUser machine, FluidStack stack, IFluidHandler.FluidAct } public FluidStack drain(MachineFluidHandler handler, FluidStack resource, IFluidHandler.FluidAction action) { - return handler.drain(resource, action); + return handler.drain(index, resource, action); } public FluidStack drain(IFluidTankUser machine, FluidStack resource, IFluidHandler.FluidAction action) { @@ -81,7 +81,7 @@ public FluidStack drain(IFluidTankUser machine, FluidStack resource, IFluidHandl } public FluidStack drain(MachineFluidHandler handler, int maxDrain, IFluidHandler.FluidAction action) { - return handler.drain(maxDrain, action); + return handler.drain(index, maxDrain, action); } public FluidStack drain(IFluidTankUser machine, int maxDrain, IFluidHandler.FluidAction action) { diff --git a/src/machines/java/com/enderio/machines/common/item/FluidTankItem.java b/src/machines/java/com/enderio/machines/common/item/FluidTankItem.java index 3ffa4ccf93..58800ae639 100644 --- a/src/machines/java/com/enderio/machines/common/item/FluidTankItem.java +++ b/src/machines/java/com/enderio/machines/common/item/FluidTankItem.java @@ -8,6 +8,8 @@ import com.enderio.machines.common.block.MachineBlock; import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer; import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.ListTag; +import net.minecraft.nbt.Tag; import net.minecraft.network.chat.Component; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.BlockItem; @@ -90,7 +92,9 @@ public FluidStack getFluid() { Optional tagCompoundOptional = Optional.ofNullable(container.getTag()); return tagCompoundOptional .map(tagCompound -> tagCompound.getCompound(BLOCK_ENTITY_TAG)) - .map(blockEntityTag -> blockEntityTag.getCompound(MachineNBTKeys.FLUID)) + .map(blockEntityTag -> blockEntityTag.getCompound(MachineNBTKeys.FLUIDS)) + .map(fluidTag -> fluidTag.getList("Tanks", Tag.TAG_COMPOUND)) + .map(tank -> tank.getCompound(0)) .map(FluidStack::loadFluidStackFromNBT) .orElse(FluidStack.EMPTY); } @@ -102,7 +106,11 @@ protected void setFluid(FluidStack fluid) { CompoundTag fluidTag = new CompoundTag(); fluid.writeToNBT(fluidTag);//rewrites the old value - blockEntityTag.put(MachineNBTKeys.FLUID, fluidTag); + ListTag listTag = new ListTag(); + listTag.add(0, fluidTag); + CompoundTag tanks = new CompoundTag(); + tanks.put("Tanks", listTag); + blockEntityTag.put(MachineNBTKeys.FLUIDS, tanks); } @Override @@ -110,8 +118,8 @@ protected void setContainerToEmpty() { CompoundTag tagCompound = container.getTag(); if (tagCompound != null) { CompoundTag blockEntityTag = tagCompound.getCompound(BLOCK_ENTITY_TAG); - if (blockEntityTag.contains(MachineNBTKeys.FLUID)) { - blockEntityTag.remove(MachineNBTKeys.FLUID); + if (blockEntityTag.contains("Tanks")) { + blockEntityTag.remove("Tanks"); } } }