-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
src/machines/java/com/enderio/machines/common/io/fluid/MachineTankLayout.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/machines/java/com/enderio/machines/common/io/fluid/TankAccess.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |