Skip to content

Commit

Permalink
it works
Browse files Browse the repository at this point in the history
  • Loading branch information
ferriarnus committed Feb 24, 2024
1 parent 4ef24fb commit a698280
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -24,15 +26,16 @@ public class MachineFluidHandler implements IFluidHandler, INBTSerializable<Comp
public static final String TANKS = "Tanks";
private final IIOConfig config;
private final MachineTankLayout layout;
private List<MachineFluidTank> tanks;
private Map<Integer, MachineFluidTank> tanks = new HashMap<>();
private List<FluidStack> stacks;

// 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.layout = layout;
this.tanks = layout.createTanks(this);
this.stacks = NonNullList.withSize(getTanks(), FluidStack.EMPTY);
}

public void addSlotChangedCallback(IntConsumer callback) {
Expand All @@ -50,12 +53,10 @@ public MachineTankLayout getLayout() {
//Not a good idea to use this method. Tank Access should be the way to access tanks
@Deprecated
public final MachineFluidTank getTank(int tank) {
MachineFluidTank machineFluidTank = tanks.get(tank);
if (machineFluidTank == null) {
throw new IllegalArgumentException("No tank found for index " + tank + ".");

if (tank > 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
Expand All @@ -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
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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()) {
Expand All @@ -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();
Expand All @@ -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));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,29 @@
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);
}

@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
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,6 @@ public boolean isFluidValid(int slot, FluidStack stack) {
return tanks.get(slot).filter().test(stack);
}

public List<MachineFluidTank> createTanks(MachineFluidHandler handler) {
List<MachineFluidTank> tankList = new ArrayList<>();
tanks.forEach(config -> {
tankList.add(new MachineFluidTank(tanks.indexOf(config), handler));
});
return tankList;
}

public static class Builder {
private final ArrayList<TankConfig> tanks = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,23 @@ 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) {
return fill(machine.getFluidHandler(), stack, action);
}

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) {
return drain(machine.getFluidHandler(), resource, action);
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -90,7 +92,9 @@ public FluidStack getFluid() {
Optional<CompoundTag> 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);
}
Expand All @@ -102,16 +106,20 @@ 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
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");
}
}
}
Expand Down

0 comments on commit a698280

Please sign in to comment.