Skip to content

Commit

Permalink
Initial tank layout
Browse files Browse the repository at this point in the history
  • Loading branch information
dphaldes committed Nov 12, 2023
1 parent 0e4b5f1 commit 6cbe6b8
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<TankConfig> 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<TankConfig> tanks = new ArrayList<>();

public Builder tank(TankAccess access, int capacity, boolean canInsert, boolean canRemove, Predicate<FluidStack> 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<FluidStack> filter) {}

}
Original file line number Diff line number Diff line change
@@ -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");
}
}
}

0 comments on commit 6cbe6b8

Please sign in to comment.