pick 97420a31d The big reformat of 2020
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
|
||||
package appeng.fluids.util;
|
||||
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import net.minecraft.nbt.CompoundNBT;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
|
||||
@@ -11,373 +12,300 @@ import appeng.api.storage.data.IAEFluidStack;
|
||||
import appeng.core.AELog;
|
||||
import appeng.util.Platform;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
public class AEFluidInventory implements IAEFluidTank {
|
||||
private final IAEFluidStack[] fluids;
|
||||
private final IAEFluidInventory handler;
|
||||
private final int capacity;
|
||||
|
||||
public AEFluidInventory(final IAEFluidInventory handler, final int slots, final int capacity) {
|
||||
this.fluids = new IAEFluidStack[slots];
|
||||
this.handler = handler;
|
||||
this.capacity = capacity;
|
||||
}
|
||||
|
||||
public class AEFluidInventory implements IAEFluidTank
|
||||
{
|
||||
private final IAEFluidStack[] fluids;
|
||||
private final IAEFluidInventory handler;
|
||||
private final int capacity;
|
||||
public AEFluidInventory(final IAEFluidInventory handler, final int slots) {
|
||||
this(handler, slots, Integer.MAX_VALUE);
|
||||
}
|
||||
|
||||
public AEFluidInventory( final IAEFluidInventory handler, final int slots, final int capacity )
|
||||
{
|
||||
this.fluids = new IAEFluidStack[slots];
|
||||
this.handler = handler;
|
||||
this.capacity = capacity;
|
||||
}
|
||||
@Override
|
||||
public void setFluidInSlot(final int slot, final IAEFluidStack fluid) {
|
||||
if (slot >= 0 && slot < this.getSlots()) {
|
||||
if (Objects.equals(this.fluids[slot], fluid)) {
|
||||
if (fluid != null && fluid.getStackSize() != this.fluids[slot].getStackSize()) {
|
||||
this.fluids[slot].setStackSize(Math.min(fluid.getStackSize(), this.capacity));
|
||||
this.onContentChanged(slot);
|
||||
}
|
||||
} else {
|
||||
if (fluid == null) {
|
||||
this.fluids[slot] = null;
|
||||
} else {
|
||||
this.fluids[slot] = fluid.copy();
|
||||
this.fluids[slot].setStackSize(Math.min(fluid.getStackSize(), this.capacity));
|
||||
}
|
||||
|
||||
public AEFluidInventory( final IAEFluidInventory handler, final int slots )
|
||||
{
|
||||
this( handler, slots, Integer.MAX_VALUE );
|
||||
}
|
||||
this.onContentChanged(slot);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFluidInSlot( final int slot, final IAEFluidStack fluid )
|
||||
{
|
||||
if( slot >= 0 && slot < this.getSlots() )
|
||||
{
|
||||
if( Objects.equals( this.fluids[slot], fluid ) )
|
||||
{
|
||||
if( fluid != null && fluid.getStackSize() != this.fluids[slot].getStackSize() )
|
||||
{
|
||||
this.fluids[slot].setStackSize( Math.min( fluid.getStackSize(), this.capacity ) );
|
||||
this.onContentChanged( slot );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if( fluid == null )
|
||||
{
|
||||
this.fluids[slot] = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.fluids[slot] = fluid.copy();
|
||||
this.fluids[slot].setStackSize( Math.min( fluid.getStackSize(), this.capacity ) );
|
||||
}
|
||||
private void onContentChanged(final int slot) {
|
||||
if (this.handler != null && Platform.isServer()) {
|
||||
this.handler.onFluidInventoryChanged(this, slot);
|
||||
}
|
||||
}
|
||||
|
||||
this.onContentChanged( slot );
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public IAEFluidStack getFluidInSlot(final int slot) {
|
||||
if (slot >= 0 && slot < this.getSlots()) {
|
||||
return this.fluids[slot];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void onContentChanged( final int slot )
|
||||
{
|
||||
if( this.handler != null && Platform.isServer() )
|
||||
{
|
||||
this.handler.onFluidInventoryChanged( this, slot );
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public int getSlots() {
|
||||
return this.fluids.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAEFluidStack getFluidInSlot( final int slot )
|
||||
{
|
||||
if( slot >= 0 && slot < this.getSlots() )
|
||||
{
|
||||
return this.fluids[slot];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public int getTanks() {
|
||||
return this.fluids.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSlots()
|
||||
{
|
||||
return this.fluids.length;
|
||||
}
|
||||
@Nonnull
|
||||
@Override
|
||||
public FluidStack getFluidInTank(int tank) {
|
||||
if (tank < 0 || tank >= fluids.length) {
|
||||
return FluidStack.EMPTY;
|
||||
}
|
||||
return fluids[tank] == null ? FluidStack.EMPTY : fluids[tank].getFluidStack();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTanks() {
|
||||
return this.fluids.length;
|
||||
}
|
||||
@Override
|
||||
public int getTankCapacity(int tank) {
|
||||
return Math.min(this.capacity, Integer.MAX_VALUE);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public FluidStack getFluidInTank(int tank) {
|
||||
if (tank < 0 || tank >= fluids.length) {
|
||||
return FluidStack.EMPTY;
|
||||
}
|
||||
return fluids[tank] == null ? FluidStack.EMPTY : fluids[tank].getFluidStack();
|
||||
}
|
||||
@Override
|
||||
public boolean isFluidValid(int tank, @Nonnull FluidStack stack) {
|
||||
return stack != FluidStack.EMPTY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTankCapacity(int tank) {
|
||||
return Math.min( this.capacity, Integer.MAX_VALUE );
|
||||
}
|
||||
public int fill(final int slot, final FluidStack resource, final boolean doFill) {
|
||||
if (resource.isEmpty() || resource.getAmount() <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFluidValid(int tank, @Nonnull FluidStack stack) {
|
||||
return stack != FluidStack.EMPTY;
|
||||
}
|
||||
final IAEFluidStack fluid = this.fluids[slot];
|
||||
|
||||
public int fill( final int slot, final FluidStack resource, final boolean doFill )
|
||||
{
|
||||
if( resource.isEmpty()|| resource.getAmount() <= 0 )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (fluid != null && !fluid.getFluidStack().equals(resource)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
final IAEFluidStack fluid = this.fluids[slot];
|
||||
int amountToStore = this.capacity;
|
||||
|
||||
if( fluid != null && !fluid.getFluidStack().equals( resource ) )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (fluid != null) {
|
||||
amountToStore -= fluid.getStackSize();
|
||||
}
|
||||
|
||||
int amountToStore = this.capacity;
|
||||
amountToStore = Math.min(amountToStore, resource.getAmount());
|
||||
|
||||
if( fluid != null )
|
||||
{
|
||||
amountToStore -= fluid.getStackSize();
|
||||
}
|
||||
if (doFill) {
|
||||
if (fluid == null) {
|
||||
this.setFluidInSlot(slot, AEFluidStack.fromFluidStack(resource));
|
||||
} else {
|
||||
fluid.setStackSize(fluid.getStackSize() + amountToStore);
|
||||
this.onContentChanged(slot);
|
||||
}
|
||||
}
|
||||
|
||||
amountToStore = Math.min( amountToStore, resource.getAmount() );
|
||||
return amountToStore;
|
||||
}
|
||||
|
||||
if( doFill )
|
||||
{
|
||||
if( fluid == null )
|
||||
{
|
||||
this.setFluidInSlot( slot, AEFluidStack.fromFluidStack( resource ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
fluid.setStackSize( fluid.getStackSize() + amountToStore );
|
||||
this.onContentChanged( slot );
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public int fill(FluidStack resource, FluidAction action) {
|
||||
if (resource == null || resource.isEmpty() || resource.getAmount() <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return amountToStore;
|
||||
}
|
||||
// Find a suitable slot
|
||||
int slot = indexOfFluid(resource);
|
||||
if (slot == -1) {
|
||||
slot = indexOfEmptySlot();
|
||||
if (slot == -1) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int fill(FluidStack resource, FluidAction action) {
|
||||
if( resource == null || resource.isEmpty() || resource.getAmount() <= 0 )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
final IAEFluidStack fluid = this.fluids[slot];
|
||||
|
||||
// Find a suitable slot
|
||||
int slot = indexOfFluid(resource);
|
||||
if (slot == -1) {
|
||||
slot = indexOfEmptySlot();
|
||||
if (slot == -1) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
int amountToStore = this.capacity;
|
||||
|
||||
final IAEFluidStack fluid = this.fluids[slot];
|
||||
if (fluid != null) {
|
||||
amountToStore -= fluid.getStackSize();
|
||||
}
|
||||
|
||||
int amountToStore = this.capacity;
|
||||
amountToStore = Math.min(amountToStore, resource.getAmount());
|
||||
|
||||
if( fluid != null )
|
||||
{
|
||||
amountToStore -= fluid.getStackSize();
|
||||
}
|
||||
if (action == FluidAction.EXECUTE) {
|
||||
if (fluid == null) {
|
||||
this.setFluidInSlot(slot, AEFluidStack.fromFluidStack(resource));
|
||||
} else {
|
||||
fluid.setStackSize(fluid.getStackSize() + amountToStore);
|
||||
this.onContentChanged(slot);
|
||||
}
|
||||
}
|
||||
|
||||
amountToStore = Math.min( amountToStore, resource.getAmount() );
|
||||
return amountToStore;
|
||||
}
|
||||
|
||||
if( action == FluidAction.EXECUTE )
|
||||
{
|
||||
if( fluid == null )
|
||||
{
|
||||
this.setFluidInSlot( slot, AEFluidStack.fromFluidStack( resource ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
fluid.setStackSize( fluid.getStackSize() + amountToStore );
|
||||
this.onContentChanged( slot );
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public FluidStack drain(final FluidStack fluid, final FluidAction action) {
|
||||
if (fluid == null || fluid.getAmount() <= 0) {
|
||||
return FluidStack.EMPTY;
|
||||
}
|
||||
|
||||
return amountToStore;
|
||||
}
|
||||
final FluidStack resource = fluid.copy();
|
||||
|
||||
@Override
|
||||
public FluidStack drain( final FluidStack fluid, final FluidAction action )
|
||||
{
|
||||
if( fluid == null || fluid.getAmount() <= 0 )
|
||||
{
|
||||
return FluidStack.EMPTY;
|
||||
}
|
||||
FluidStack totalDrained = FluidStack.EMPTY;
|
||||
for (int slot = 0; slot < this.getSlots(); ++slot) {
|
||||
FluidStack drain = this.drain(slot, resource, action == FluidAction.EXECUTE);
|
||||
if (drain != null) {
|
||||
if (totalDrained.isEmpty()) {
|
||||
totalDrained = drain;
|
||||
} else {
|
||||
totalDrained.setAmount(totalDrained.getAmount() + drain.getAmount());
|
||||
}
|
||||
|
||||
final FluidStack resource = fluid.copy();
|
||||
resource.setAmount(resource.getAmount() - drain.getAmount());
|
||||
if (resource.getAmount() <= 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return totalDrained;
|
||||
}
|
||||
|
||||
FluidStack totalDrained = FluidStack.EMPTY;
|
||||
for( int slot = 0; slot < this.getSlots(); ++slot )
|
||||
{
|
||||
FluidStack drain = this.drain( slot, resource, action == FluidAction.EXECUTE );
|
||||
if( drain != null )
|
||||
{
|
||||
if( totalDrained.isEmpty() )
|
||||
{
|
||||
totalDrained = drain;
|
||||
}
|
||||
else
|
||||
{
|
||||
totalDrained.setAmount(totalDrained.getAmount() + drain.getAmount());
|
||||
}
|
||||
@Override
|
||||
public FluidStack drain(final int maxDrain, final FluidAction action) {
|
||||
if (maxDrain == 0) {
|
||||
return FluidStack.EMPTY;
|
||||
}
|
||||
|
||||
resource.setAmount(resource.getAmount() - drain.getAmount());
|
||||
if( resource.getAmount() <= 0 )
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return totalDrained;
|
||||
}
|
||||
FluidStack totalDrained = FluidStack.EMPTY;
|
||||
int toDrain = maxDrain;
|
||||
|
||||
@Override
|
||||
public FluidStack drain( final int maxDrain, final FluidAction action )
|
||||
{
|
||||
if( maxDrain == 0 )
|
||||
{
|
||||
return FluidStack.EMPTY;
|
||||
}
|
||||
for (int slot = 0; slot < this.getSlots(); ++slot) {
|
||||
if (totalDrained.isEmpty()) {
|
||||
totalDrained = this.drain(slot, toDrain, action == FluidAction.EXECUTE);
|
||||
if (totalDrained.isEmpty()) {
|
||||
toDrain -= totalDrained.getAmount();
|
||||
}
|
||||
} else {
|
||||
FluidStack copy = totalDrained.copy();
|
||||
copy.setAmount(toDrain);
|
||||
FluidStack drain = this.drain(slot, copy, action == FluidAction.EXECUTE);
|
||||
if (drain != null) {
|
||||
totalDrained.setAmount(totalDrained.getAmount() + drain.getAmount());
|
||||
toDrain -= drain.getAmount();
|
||||
}
|
||||
}
|
||||
|
||||
FluidStack totalDrained = FluidStack.EMPTY;
|
||||
int toDrain = maxDrain;
|
||||
if (toDrain <= 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return totalDrained;
|
||||
}
|
||||
|
||||
for( int slot = 0; slot < this.getSlots(); ++slot )
|
||||
{
|
||||
if( totalDrained.isEmpty() )
|
||||
{
|
||||
totalDrained = this.drain( slot, toDrain, action == FluidAction.EXECUTE );
|
||||
if( totalDrained.isEmpty() )
|
||||
{
|
||||
toDrain -= totalDrained.getAmount();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
FluidStack copy = totalDrained.copy();
|
||||
copy.setAmount(toDrain);
|
||||
FluidStack drain = this.drain( slot, copy, action == FluidAction.EXECUTE );
|
||||
if( drain != null )
|
||||
{
|
||||
totalDrained.setAmount(totalDrained.getAmount() + drain.getAmount());
|
||||
toDrain -= drain.getAmount();
|
||||
}
|
||||
}
|
||||
private int indexOfFluid(FluidStack resource) {
|
||||
for (int slot = 0; slot < fluids.length; slot++) {
|
||||
if (fluids[slot] != null && fluids[slot].getFluidStack().isFluidEqual(resource)) {
|
||||
return slot;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
if( toDrain <= 0 )
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
return totalDrained;
|
||||
}
|
||||
private int indexOfEmptySlot() {
|
||||
for (int slot = 0; slot < fluids.length; slot++) {
|
||||
if (fluids[slot] == null) {
|
||||
return slot;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private int indexOfFluid(FluidStack resource) {
|
||||
for (int slot = 0; slot < fluids.length; slot++) {
|
||||
if (fluids[slot] != null && fluids[slot].getFluidStack().isFluidEqual(resource)) {
|
||||
return slot;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
public FluidStack drain(final int slot, final FluidStack resource, final boolean doDrain) {
|
||||
final IAEFluidStack fluid = this.fluids[slot];
|
||||
if (resource.isEmpty() || fluid == null || !fluid.getFluidStack().equals(resource)) {
|
||||
return null;
|
||||
}
|
||||
return this.drain(slot, resource.getAmount(), doDrain);
|
||||
}
|
||||
|
||||
private int indexOfEmptySlot() {
|
||||
for (int slot = 0; slot < fluids.length; slot++) {
|
||||
if (fluids[slot] == null) {
|
||||
return slot;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
public FluidStack drain(final int slot, final int maxDrain, boolean doDrain) {
|
||||
final IAEFluidStack fluid = this.fluids[slot];
|
||||
if (fluid == null || maxDrain <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public FluidStack drain( final int slot, final FluidStack resource, final boolean doDrain )
|
||||
{
|
||||
final IAEFluidStack fluid = this.fluids[slot];
|
||||
if( resource.isEmpty() || fluid == null || !fluid.getFluidStack().equals( resource ) )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return this.drain( slot, resource.getAmount(), doDrain );
|
||||
}
|
||||
int drained = maxDrain;
|
||||
if (fluid.getStackSize() < drained) {
|
||||
drained = (int) fluid.getStackSize();
|
||||
}
|
||||
|
||||
public FluidStack drain( final int slot, final int maxDrain, boolean doDrain )
|
||||
{
|
||||
final IAEFluidStack fluid = this.fluids[slot];
|
||||
if( fluid == null || maxDrain <= 0 )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
FluidStack stack = new FluidStack(fluid.getFluid(), drained);
|
||||
if (doDrain) {
|
||||
fluid.setStackSize(fluid.getStackSize() - drained);
|
||||
if (fluid.getStackSize() <= 0) {
|
||||
this.fluids[slot] = null;
|
||||
}
|
||||
this.onContentChanged(slot);
|
||||
}
|
||||
return stack;
|
||||
}
|
||||
|
||||
int drained = maxDrain;
|
||||
if( fluid.getStackSize() < drained )
|
||||
{
|
||||
drained = (int) fluid.getStackSize();
|
||||
}
|
||||
public void writeToNBT(final CompoundNBT data, final String name) {
|
||||
final CompoundNBT c = new CompoundNBT();
|
||||
this.writeToNBT(c);
|
||||
data.put(name, c);
|
||||
}
|
||||
|
||||
FluidStack stack = new FluidStack( fluid.getFluid(), drained );
|
||||
if( doDrain )
|
||||
{
|
||||
fluid.setStackSize( fluid.getStackSize() - drained );
|
||||
if( fluid.getStackSize() <= 0 )
|
||||
{
|
||||
this.fluids[slot] = null;
|
||||
}
|
||||
this.onContentChanged( slot );
|
||||
}
|
||||
return stack;
|
||||
}
|
||||
private void writeToNBT(final CompoundNBT target) {
|
||||
for (int x = 0; x < this.fluids.length; x++) {
|
||||
try {
|
||||
final CompoundNBT c = new CompoundNBT();
|
||||
|
||||
public void writeToNBT( final CompoundNBT data, final String name )
|
||||
{
|
||||
final CompoundNBT c = new CompoundNBT();
|
||||
this.writeToNBT( c );
|
||||
data.put( name, c );
|
||||
}
|
||||
if (this.fluids[x] != null) {
|
||||
this.fluids[x].writeToNBT(c);
|
||||
}
|
||||
|
||||
private void writeToNBT( final CompoundNBT target )
|
||||
{
|
||||
for( int x = 0; x < this.fluids.length; x++ )
|
||||
{
|
||||
try
|
||||
{
|
||||
final CompoundNBT c = new CompoundNBT();
|
||||
target.put("#" + x, c);
|
||||
} catch (final Exception ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if( this.fluids[x] != null )
|
||||
{
|
||||
this.fluids[x].writeToNBT( c );
|
||||
}
|
||||
public void readFromNBT(final CompoundNBT data, final String name) {
|
||||
final CompoundNBT c = data.getCompound(name);
|
||||
if (!c.isEmpty()) {
|
||||
this.readFromNBT(c);
|
||||
}
|
||||
}
|
||||
|
||||
target.put( "#" + x, c );
|
||||
}
|
||||
catch( final Exception ignored )
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
private void readFromNBT(final CompoundNBT target) {
|
||||
for (int x = 0; x < this.fluids.length; x++) {
|
||||
try {
|
||||
final CompoundNBT c = target.getCompound("#" + x);
|
||||
|
||||
public void readFromNBT( final CompoundNBT data, final String name )
|
||||
{
|
||||
final CompoundNBT c = data.getCompound( name );
|
||||
if( !c.isEmpty() )
|
||||
{
|
||||
this.readFromNBT( c );
|
||||
}
|
||||
}
|
||||
|
||||
private void readFromNBT( final CompoundNBT target )
|
||||
{
|
||||
for( int x = 0; x < this.fluids.length; x++ )
|
||||
{
|
||||
try
|
||||
{
|
||||
final CompoundNBT c = target.getCompound( "#" + x );
|
||||
|
||||
if( !c.isEmpty() )
|
||||
{
|
||||
this.fluids[x] = AEFluidStack.fromNBT( c );
|
||||
}
|
||||
}
|
||||
catch( final Exception e )
|
||||
{
|
||||
AELog.debug( e );
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!c.isEmpty()) {
|
||||
this.fluids[x] = AEFluidStack.fromNBT(c);
|
||||
}
|
||||
} catch (final Exception e) {
|
||||
AELog.debug(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user