-
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
3 changed files
with
181 additions
and
49 deletions.
There are no files selected for viewing
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
110 changes: 110 additions & 0 deletions
110
src/machines/java/com/enderio/machines/common/io/fluid/MachineTank.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,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<FluidStack> validator; | ||
|
||
private boolean canInsert; | ||
|
||
private boolean canExtract; | ||
@NotNull private FluidStack fluid = FluidStack.EMPTY; | ||
|
||
public MachineTank(int capacity, Predicate<FluidStack> 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() { | ||
|
||
} | ||
|
||
} |
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